All files / src/lib/compiler buildManifest.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 913x 3x 3x                                                       3x       4x   4x 6x 6x 6x 6x         6x             4x 4x 6x 6x 1x       5x     3x     3x                     7x 7x 7x                      
import Path from "path";
import { glob } from "lib/helpers/glob";
import { pathToPosix } from "shared/lib/helpers/path";
import type { CartType } from "shared/lib/resources/types";
import type { PluginFileAttribution } from "lib/compiler/enginePlugins";
 
export type BuildSourceOrigin =
  | { type: "engine" }
  | { type: "project" }
  | { type: "plugin"; pluginName: string; replacesDefault: boolean };
 
export type BuildSource = {
  sourcePath: string;
  objectPath: string;
  origin: BuildSourceOrigin;
};
 
type BuildArtifacts = {
  romPath: string;
  mapPath: string;
  noiPath: string;
};
 
export type BuildManifest = {
  buildRoot: string;
  artifacts: BuildArtifacts;
  sources: BuildSource[];
  cartType: CartType;
};
 
export const resolveBuildSources = async (
  buildRoot: string,
  attribution: PluginFileAttribution,
): Promise<BuildSource[]> => {
  const sourceFiles = await glob("src/**/*.@(c|s)", { cwd: buildRoot });
 
  const sources = sourceFiles.map((sourceFile) => {
    const sourcePath = pathToPosix(sourceFile);
    const sourceName = Path.basename(sourcePath, Path.extname(sourcePath));
    const plugin = attribution.ownedFiles[sourcePath];
    const origin: BuildSourceOrigin = plugin
      ? { type: "plugin", ...plugin }
      : sourcePath.startsWith("src/data/")
        ? { type: "project" }
        : { type: "engine" };
    return {
      sourcePath,
      objectPath: Path.join(buildRoot, "obj", `${sourceName}.o`),
      origin,
    };
  });
 
  const objectOwners = new Map<string, string>();
  for (const source of sources) {
    const existing = objectOwners.get(source.objectPath);
    if (existing) {
      throw new Error(
        `Build sources ${existing} and ${source.sourcePath} produce the same object ${source.objectPath}`,
      );
    }
    objectOwners.set(source.objectPath, source.sourcePath);
  }
 
  return sources;
};
 
export const createBuildManifest = ({
  buildRoot,
  romFilename,
  cartType,
  sources,
}: {
  buildRoot: string;
  romFilename: string;
  cartType: CartType;
  sources: BuildSource[];
}): BuildManifest => {
  const romStem = Path.parse(romFilename).name;
  const romRoot = Path.join(buildRoot, "build", "rom");
  return {
    buildRoot,
    cartType,
    sources,
    artifacts: {
      romPath: Path.join(romRoot, romFilename),
      mapPath: Path.join(romRoot, `${romStem}.map`),
      noiPath: Path.join(romRoot, `${romStem}.noi`),
    },
  };
};