2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-11-05 09:53:21 -05:00
|
|
|
import {
|
|
|
|
dirname,
|
|
|
|
fromFileUrl,
|
|
|
|
join,
|
2021-06-23 11:46:15 -04:00
|
|
|
resolve,
|
2021-06-06 12:32:06 -04:00
|
|
|
toFileUrl,
|
2021-06-25 20:02:31 -04:00
|
|
|
} from "../test_util/std/path/mod.ts";
|
2021-06-23 11:46:15 -04:00
|
|
|
export { dirname, fromFileUrl, join, resolve, toFileUrl };
|
2022-01-24 17:47:05 -05:00
|
|
|
export { existsSync, walk } from "../test_util/std/fs/mod.ts";
|
2022-05-18 16:00:11 -04:00
|
|
|
export { TextLineStream } from "../test_util/std/streams/delimiter.ts";
|
2021-06-25 20:02:31 -04:00
|
|
|
export { delay } from "../test_util/std/async/delay.ts";
|
2020-11-05 09:53:21 -05:00
|
|
|
|
|
|
|
export const ROOT_PATH = dirname(dirname(fromFileUrl(import.meta.url)));
|
|
|
|
|
2022-05-18 16:00:11 -04:00
|
|
|
async function getFilesFromGit(baseDir, args) {
|
|
|
|
const { status, stdout } = await Deno.spawn("git", {
|
|
|
|
stderr: "inherit",
|
|
|
|
args,
|
2020-11-05 09:53:21 -05:00
|
|
|
});
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = new TextDecoder().decode(stdout);
|
|
|
|
if (!status.success) {
|
2020-11-05 09:53:21 -05:00
|
|
|
throw new Error("gitLsFiles failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = output.split("\0").filter((line) => line.length > 0).map(
|
|
|
|
(filePath) => {
|
|
|
|
return Deno.realPathSync(join(baseDir, filePath));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:27:37 -05:00
|
|
|
function gitLsFiles(baseDir, patterns) {
|
2020-11-05 09:53:21 -05:00
|
|
|
baseDir = Deno.realPathSync(baseDir);
|
|
|
|
const cmd = [
|
|
|
|
"-C",
|
|
|
|
baseDir,
|
|
|
|
"ls-files",
|
|
|
|
"-z",
|
|
|
|
"--exclude-standard",
|
|
|
|
"--cached",
|
|
|
|
"--modified",
|
|
|
|
"--others",
|
|
|
|
"--",
|
|
|
|
...patterns,
|
|
|
|
];
|
|
|
|
return getFilesFromGit(baseDir, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** List all files staged for commit */
|
2020-11-14 14:27:37 -05:00
|
|
|
function gitStaged(baseDir, patterns) {
|
2020-11-05 09:53:21 -05:00
|
|
|
baseDir = Deno.realPathSync(baseDir);
|
|
|
|
const cmd = [
|
|
|
|
"-C",
|
|
|
|
baseDir,
|
|
|
|
"diff",
|
|
|
|
"--staged",
|
|
|
|
"--diff-filter=ACMR",
|
|
|
|
"--name-only",
|
|
|
|
"-z",
|
|
|
|
"--",
|
|
|
|
...patterns,
|
|
|
|
];
|
|
|
|
return getFilesFromGit(baseDir, cmd);
|
|
|
|
}
|
|
|
|
|
2021-04-28 10:08:51 -04:00
|
|
|
/**
|
2020-11-05 09:53:21 -05:00
|
|
|
* Recursively list all files in (a subdirectory of) a git worktree.
|
|
|
|
* * Optionally, glob patterns may be specified to e.g. only list files with a
|
|
|
|
* certain extension.
|
|
|
|
* * Untracked files are included, unless they're listed in .gitignore.
|
|
|
|
* * Directory names themselves are not listed (but the files inside are).
|
|
|
|
* * Submodules and their contents are ignored entirely.
|
|
|
|
* * This function fails if the query matches no files.
|
2021-04-28 10:08:51 -04:00
|
|
|
*
|
2020-11-05 09:53:21 -05:00
|
|
|
* If --staged argument was provided when program is run
|
|
|
|
* only staged sources will be returned.
|
|
|
|
*/
|
|
|
|
export async function getSources(baseDir, patterns) {
|
|
|
|
const stagedOnly = Deno.args.includes("--staged");
|
|
|
|
|
|
|
|
if (stagedOnly) {
|
|
|
|
return await gitStaged(baseDir, patterns);
|
|
|
|
} else {
|
|
|
|
return await gitLsFiles(baseDir, patterns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildMode() {
|
|
|
|
if (Deno.args.includes("--release")) {
|
|
|
|
return "release";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "debug";
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildPath() {
|
|
|
|
return join(ROOT_PATH, "target", buildMode());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPrebuiltToolPath(toolName) {
|
|
|
|
const PREBUILT_PATH = join(ROOT_PATH, "third_party", "prebuilt");
|
|
|
|
|
|
|
|
const platformDirName = {
|
|
|
|
"windows": "win",
|
|
|
|
"darwin": "mac",
|
|
|
|
"linux": "linux64",
|
|
|
|
}[Deno.build.os];
|
|
|
|
const executableSuffix = Deno.build.os === "windows" ? ".exe" : "";
|
|
|
|
return join(PREBUILT_PATH, platformDirName, toolName + executableSuffix);
|
|
|
|
}
|