Skip to content

Commit

Permalink
refactor(windows): move manifest writing to MSBuild target (#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed May 14, 2024
1 parent 6ccaf45 commit e94412b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 70 deletions.
31 changes: 4 additions & 27 deletions scripts/embed-manifest/cpp.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check
import * as nodefs from "node:fs";
import * as path from "node:path";
import { findFile, isMain } from "../helpers.js";
import { main } from "./main.mjs";
import { isMain } from "../helpers.js";
import { main, warn } from "./main.mjs";

const INDENT = " ";

Expand All @@ -23,13 +21,6 @@ function str(s, literal = "") {
return typeof s === "string" ? '"' + s + `"${literal}` : "std::nullopt";
}

/**
* @param {string} message
*/
export function warn(message) {
console.warn("app.json:", message);
}

/**
* @param {unknown[]} items
* @param {number} level
Expand Down Expand Up @@ -161,16 +152,8 @@ function components(components, level) {
* @param {string} checksum
* @returns {string}
*/
export function generate(json, checksum, fs = nodefs) {
const nodeModulesPath = findFile("node_modules", process.cwd(), fs);
if (!nodeModulesPath) {
console.error(
"Failed to find 'node_modules' — make sure you've installed npm dependencies"
);
return "";
}

const code = [
export function generate(json, checksum) {
return [
"// clang-format off",
'#include "Manifest.h"',
"",
Expand Down Expand Up @@ -200,12 +183,6 @@ export function generate(json, checksum, fs = nodefs) {
"}",
"",
].join("\n");

const dest = path.join(nodeModulesPath, ".generated", "Manifest.g.cpp");
fs.promises
.mkdir(path.dirname(dest), { recursive: true, mode: 0o755 })
.then(() => fs.promises.writeFile(dest, code));
return "app.json -> " + dest;
}

if (isMain(import.meta.url)) {
Expand Down
51 changes: 9 additions & 42 deletions test/embed-manifest/cpp.test.mjs
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
// @ts-check
import { equal, ok } from "node:assert/strict";
import * as fs from "node:fs";
import * as path from "node:path";
import { equal } from "node:assert/strict";
import { describe, it } from "node:test";
import { generate as generateActual } from "../../scripts/embed-manifest/cpp.mjs";
import * as fixtures from "./fixtures.mjs";

describe("embed manifest (C++)", () => {
/** @type {(resolve: (result: any) => void) => Partial<typeof fs.promises>} */
const fsMock = (resolve) => ({
mkdir: () => Promise.resolve(undefined),
writeFile: (_, data) => {
resolve(data.toString());
return Promise.resolve();
},
});

/** @type {(json: Record<string, unknown>, mockFs?: typeof fsMock) => Promise<string>} */
const generate = (json, mockFs = fsMock) => {
return new Promise((resolve) => {
generateActual(json, "0", {
...fs,
promises: {
...fs.promises,
...mockFs(resolve),
},
});
});
};
/** @type {(json: Record<string, unknown>) => string} */
const generate = (json) => generateActual(json, "0");

it("generates all properties", async () => {
it("generates all properties", () => {
equal(
await generate(fixtures.simple),
generate(fixtures.simple),
`// clang-format off
#include "Manifest.h"
Expand Down Expand Up @@ -78,9 +57,9 @@ std::string_view ReactApp::GetManifestChecksum()
);
});

it("handles missing properties", async () => {
it("handles missing properties", () => {
equal(
await generate(fixtures.minimum),
generate(fixtures.minimum),
`// clang-format off
#include "Manifest.h"
Expand Down Expand Up @@ -112,9 +91,9 @@ std::string_view ReactApp::GetManifestChecksum()
);
});

it("handles valid JSON data types", async () => {
it("handles valid JSON data types", () => {
equal(
await generate(fixtures.extended),
generate(fixtures.extended),
`// clang-format off
#include "Manifest.h"
Expand Down Expand Up @@ -269,16 +248,4 @@ std::string_view ReactApp::GetManifestChecksum()
`
);
});

it("writes the output under `/~/.node_modules/.generated`", async () => {
const expected = path.join("node_modules", ".generated", "Manifest.g.cpp");
const destination = await generate(fixtures.simple, (resolve) => ({
mkdir: () => Promise.resolve(undefined),
writeFile: (p) => {
resolve(p);
return Promise.resolve();
},
}));
ok(destination.endsWith(expected));
});
});
5 changes: 4 additions & 1 deletion windows/Shared/EmbedManifest.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<ReactAppManifestSource>$(MSBuildProjectDirectory)\..\..\Manifest.g.cpp</ReactAppManifestSource>
</PropertyGroup>
<Target Name="EmbedManifest" Inputs="$(ReactAppManifest)" Outputs="$(ReactAppManifestSource)" BeforeTargets="PrepareForBuild">
<Exec Command="node $(EmbedManifestScript)" WorkingDirectory="$(SolutionDir)" />
<Exec Command="node $(EmbedManifestScript)" ConsoleToMsBuild="true" WorkingDirectory="$(SolutionDir)">
<Output TaskParameter="ConsoleOutput" PropertyName="AppManifestSource" />
</Exec>
<WriteLinesToFile File="$(ReactAppManifestSource)" Lines="$(AppManifestSource)" Overwrite="true" />
</Target>
</Project>

0 comments on commit e94412b

Please sign in to comment.