aboutsummaryrefslogtreecommitdiff
path: root/vcpkg/scripts/azure-pipelines/owners-db/file_script.ts
blob: abb79dc1e49f45c6ea50317f903940ba996825a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
import * as fs from "fs";
import * as path from "path";

const include_subpath = "/include/";

function getFiles(dirPath: string): string[] {
  const files = fs.readdirSync(dirPath);
  return files.filter((f) => !f.startsWith("."));
}

function genAllFileStrings(
  dirPath: string,
  files: string[],
  headersStream: fs.WriteStream,
  outputStream: fs.WriteStream
) {
  for (const file of files) {
    const components = file.split("_");
    const pkg = components[0] + ":" + components[2].replace(".list", "");
    const content = fs.readFileSync(path.join(dirPath, file), "utf8");
    const lines = content.split(/\r?\n/);
    for (const raw of lines) {
      if (!raw) continue;
      const line = raw.trim();
      if (line.length === 0) continue;
      if (line.endsWith("/")) continue;
      // Remove the leading triplet directory
      const idx = line.indexOf("/");
      const filepath = idx >= 0 ? line.substring(idx) : line;
      outputStream.write(pkg + ":" + filepath + "\n");
      if (filepath.startsWith(include_subpath)) {
        headersStream.write(pkg + ":" + filepath.substring(include_subpath.length) + "\n");
      }
    }
  }
}

function usage() {
  console.error("Usage: file_script.ts --info-dir <path-to-info-dir> [--out-dir <path>]");
}

function parseArgs(argv: string[]) {
  let infoDir: string | undefined;
  let outDir = "scripts/list_files";
  for (let i = 0; i < argv.length; i++) {
    const a = argv[i];
    if (a === "--info-dir") {
      i++;
      infoDir = argv[i];
    } else if (a === "--out-dir") {
      i++;
      outDir = argv[i];
    } else if (a.startsWith("--")) {
      console.error(`Unknown argument: ${a}`);
      usage();
      process.exit(2);
    } else {
      console.error(`Unexpected positional argument: ${a}`);
      usage();
      process.exit(2);
    }
  }
  if (!infoDir) {
    console.error("info-dir is required");
    usage();
    process.exit(2);
  }
  return { infoDir, outDir };
}

function main() {
  const { infoDir: dir, outDir } = parseArgs(process.argv.slice(2));
  try {
    fs.mkdirSync(outDir, { recursive: true });
  } catch {
    // ignore
  }

  const headersPath = path.join(outDir, "VCPKGHeadersDatabase.txt");
  const dbPath = path.join(outDir, "VCPKGDatabase.txt");
  const headers = fs.createWriteStream(headersPath, { encoding: "utf8" });
  const output = fs.createWriteStream(dbPath, { encoding: "utf8" });
  try {
    const files = getFiles(dir);
    genAllFileStrings(dir, files, headers, output);
  } finally {
    headers.end();
    output.end();
  }
}

main();