UNPKG

coc-autark

Version:

Autark build-script completion for coc.nvim

228 lines (212 loc) • 6.51 kB
import fs from 'node:fs'; import path from 'node:path'; // Filesystem helpers for path completion. Autark path helpers resolve // arguments against several roots: // // S{...} project source root (directory of the root `Autark` script) // SS{...} source directory of the current script // C{...} project cache directory (default `autark-cache`) // CC{...} cache directory of the current script // %{...} path whose basename is taken, relative to the current script const ROOT_MARKER = 'Autark'; const DEFAULT_CACHE_DIR = 'autark-cache'; function isDirectory(target) { try { return fs.statSync(target).isDirectory(); } catch (_) { return false; } } function isFile(target) { try { return fs.statSync(target).isFile(); } catch (_) { return false; } } const rootCache = new Map(); /** * Walks up from `startDir` looking for the directory holding the root * `Autark` script. Returns null when no project root can be found. */ export function findProjectRoot(startDir) { if (!startDir) { return null; } const cached = rootCache.get(startDir); if (cached !== undefined) { return cached; } const found = []; let dir = path.resolve(startDir); let result = null; // Ascend to the nearest directory owning an Autark script. for (;;) { if (isFile(path.join(dir, ROOT_MARKER))) { result = dir; break; } const parent = path.dirname(dir); if (parent === dir) { break; } found.push(dir); dir = parent; } // Included scripts are also named `Autark`; the project root is the // topmost script of the ancestor chain. while (result) { const parent = path.dirname(result); if (parent === result || !isFile(path.join(parent, ROOT_MARKER))) { break; } found.push(result); result = parent; } for (const visited of found) { rootCache.set(visited, result); } rootCache.set(startDir, result); return result; } export function clearProjectRootCache() { rootCache.clear(); } /** * Resolves the project cache directory. `-H` builds use another directory, * so when the configured one is missing the first `autark-cache*` sibling is * used instead. */ export function resolveCacheRoot(projectRoot, cacheDir) { const name = cacheDir || DEFAULT_CACHE_DIR; const preferred = path.resolve(projectRoot, name); if (isDirectory(preferred) || name.includes('/')) { return preferred; } try { const candidates = fs.readdirSync(projectRoot).sort(); for (const candidate of candidates) { if (!candidate.startsWith(name)) { continue; } const full = path.join(projectRoot, candidate); if (isDirectory(full)) { return full; } } } catch (_) { // Unreadable project root: fall back to the configured path. } return preferred; } /** Cache directory of the build unit that owns `scriptDir`. */ export function unitCacheDir(projectRoot, cacheRoot, scriptDir) { const relative = path.relative(projectRoot, scriptDir); if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) { return cacheRoot; } return path.join(cacheRoot, relative); } /** Lists `dir` entries; returns an empty list when it cannot be read. */ export function listDir(dir) { let entries; try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch (_) { return []; } const result = []; for (const entry of entries) { let isDir = entry.isDirectory(); if (entry.isSymbolicLink()) { isDir = isDirectory(path.join(dir, entry.name)); } result.push({ name: entry.name, isDir }); } return result; } /** * Completes the path typed in `typed` against `baseDir`. * * Each result carries the visible name (`label`), the text that replaces the * whole typed path (`insertText`) and whether it is a directory. */ export function completePath(baseDir, typed) { const slash = typed.lastIndexOf('/'); const dirPart = slash < 0 ? '' : typed.slice(0, slash + 1); const namePart = slash < 0 ? typed : typed.slice(slash + 1); const absolute = path.resolve(baseDir, dirPart || '.'); const wanted = namePart.toLowerCase(); const results = []; for (const entry of listDir(absolute)) { if (!namePart.startsWith('.') && entry.name.startsWith('.')) { continue; } if (wanted && !entry.name.toLowerCase().startsWith(wanted)) { continue; } results.push({ label: entry.name + (entry.isDir ? '/' : ''), insertText: dirPart + entry.name + (entry.isDir ? '/' : ''), isDir: entry.isDir, }); } results.sort((a, b) => { if (a.isDir !== b.isDir) { return a.isDir ? -1 : 1; } return a.label.localeCompare(b.label); }); return results; } /** * Lists `*.env` and `*.deps` files of a build cache. * * Autark writes the variables produced by a node into `<unit cache>/<rule>.env` * and the names of the environment values it depended on into * `<unit cache>/<rule>.deps`. Both files reveal names that are only computed * during a build (check script results, lazily evaluated variables). * * External source trees staged in the cache are skipped, and the walk is * bounded so it stays cheap enough to run while typing. */ export function listCacheStateFiles(cacheRoot, limits) { const options = limits || {}; const maxDepth = options.maxDepth === undefined ? 3 : options.maxDepth; const maxFiles = options.maxFiles === undefined ? 300 : options.maxFiles; const files = []; const visit = (dir, depth) => { if (depth > maxDepth || files.length >= maxFiles) { return; } let entries; try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch (_) { return; } for (const entry of entries) { if (files.length >= maxFiles) { return; } const name = entry.name; if (entry.isDirectory()) { // `.autark` holds the check node state; other hidden dirs and staged // external projects are not part of this build unit. if (name.startsWith('.') && name !== '.autark') { continue; } if (name.startsWith('extern') || name === 'CMakeFiles' || name === 'node_modules') { continue; } visit(path.join(dir, name), depth + 1); continue; } if (name.endsWith('.env') || name.endsWith('.deps')) { files.push(path.join(dir, name)); } } }; visit(cacheRoot, 0); return files; }