Skip to content

Commit

Permalink
fix(windows): support for custom Package.appxmanifest (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Apr 13, 2021
1 parent 4929c48 commit 76286ee
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 106 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@types/mustache": "^4.0.0",
"@types/node": "^12.0.0",
"@types/prompts": "^2.0.0",
"@types/rimraf": "^3.0.0",
"eslint": "^7.10.0",
"eslint-plugin-jest": "^24.0.0",
"eslint-plugin-prettier": "^3.1.4",
Expand Down
1 change: 0 additions & 1 deletion scripts/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,6 @@ function isDestructive(packagePath, { files, oldFiles }) {
*/
function removeAllFiles(files, destination) {
/** @type {(p: string, cb: (error?: Error) => void) => void} */
// @ts-ignore
const rimraf = require("rimraf");

const rethrow = (/** @type {Error | undefined} */ error) => {
Expand Down
2 changes: 1 addition & 1 deletion test/configure/getAppName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
jest.mock("fs");

describe("getAppName()", () => {
const { mockFiles } = require("./mockFiles");
const { mockFiles } = require("../mockFiles");
const { getAppName } = require("../../scripts/configure");

const consoleSpy = jest.spyOn(global.console, "warn");
Expand Down
2 changes: 1 addition & 1 deletion test/configure/isDestructive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
jest.mock("fs");

describe("isDestructive()", () => {
const { mockFiles } = require("./mockFiles");
const { mockFiles } = require("../mockFiles");
const { isDestructive } = require("../../scripts/configure");

/**
Expand Down
2 changes: 1 addition & 1 deletion test/configure/removeAllFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jest.mock("fs");
const fs = require("fs");

describe("removeAllFiles()", () => {
const { mockFiles } = require("./mockFiles");
const { mockFiles } = require("../mockFiles");
const { removeAllFiles } = require("../../scripts/configure");

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/configure/updatePackageManifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
jest.mock("fs");

describe("updatePackageManifest()", () => {
const { mockFiles } = require("./mockFiles");
const { mockFiles } = require("../mockFiles");
const { updatePackageManifest } = require("../../scripts/configure");

afterEach(() => mockFiles());
Expand Down
2 changes: 1 addition & 1 deletion test/configure/writeAllFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fs = require("fs");
const path = require("path");

describe("writeAllFiles()", () => {
const { mockFiles } = require("./mockFiles");
const { mockFiles } = require("../mockFiles");
const { writeAllFiles } = require("../../scripts/configure");

afterEach(() => {
Expand Down
5 changes: 4 additions & 1 deletion test/configure/mockFiles.js → test/mockFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ function mockFiles(...files) {
// @ts-ignore `__setMockFiles`
fs.__setMockFiles(
Object.fromEntries(
files.map(([filename, content]) => [filename, JSON.stringify(content)])
files.map(([filename, content]) => [
filename,
typeof content === "string" ? content : JSON.stringify(content),
])
)
);
}
Expand Down
16 changes: 8 additions & 8 deletions test/windows-test-app/copyAndReplace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
jest.mock("fs");

describe("copyAndReplace", () => {
const { mockFiles } = require("../mockFiles");
const { copyAndReplace } = require("../../windows/test-app");

// @ts-ignore `__setMockFiles`
afterEach(() => require("fs").__setMockFiles({}));
afterEach(() => mockFiles());

test("replaces text files only", () => {
mockFiles(
["ReactTestApp_TemporaryKey.pfx", "binary"],
["ReactTestApp.png", "binary"],
["ReactTestApp.sln", "binary"]
);

const fs = require("fs");
// @ts-ignore `__setMockFiles`
fs.__setMockFiles({
"ReactTestApp_TemporaryKey.pfx": "binary",
"ReactTestApp.png": "binary",
"ReactTestApp.sln": "binary",
});

const replacements = { binary: "text" };

Expand Down
18 changes: 7 additions & 11 deletions test/windows-test-app/generateSolution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock("fs");

describe("generateSolution", () => {
const path = require("path");
const { mockFiles } = require("../mockFiles");
const { generateSolution } = require("../../windows/test-app");

const cwd = process.cwd();
Expand All @@ -24,8 +25,7 @@ describe("generateSolution", () => {
});

afterEach(() => {
// @ts-ignore `__setMockFiles`
require("fs").__setMockFiles({});
mockFiles();
process.chdir(cwd);
});

Expand All @@ -42,22 +42,18 @@ describe("generateSolution", () => {
});

test("exits if 'react-native-windows' folder cannot be found", () => {
// @ts-ignore `__setMockFiles`
require("fs").__setMockFiles({
[path.resolve("", "node_modules")]: "directory",
});
mockFiles([path.resolve("", "node_modules"), "directory"]);

expect(generateSolution("test", options)).toBe(
"Could not find 'react-native-windows'"
);
});

test("exits if 'react-native-test-app' folder cannot be found", () => {
// @ts-ignore `__setMockFiles`
require("fs").__setMockFiles({
[path.resolve("", "node_modules")]: "directory",
[path.resolve("", "node_modules", "react-native-windows")]: "directory",
});
mockFiles(
[path.resolve("", "node_modules"), "directory"],
[path.resolve("", "node_modules", "react-native-windows"), "directory"]
);

expect(generateSolution("test", options)).toBe(
"Could not find 'react-native-test-app'"
Expand Down
85 changes: 58 additions & 27 deletions test/windows-test-app/getBundleResources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,74 @@ jest.mock("fs");

describe("getBundleResources", () => {
const path = require("path");
const { mockFiles } = require("../mockFiles");
const { getBundleResources } = require("../../windows/test-app");

// @ts-ignore `__setMockFiles`
afterEach(() => require("fs").__setMockFiles({}));
afterEach(() => mockFiles());

test("returns app name and bundle resources", () => {
const assets = path.join("dist", "assets");
const bundle = path.join("dist", "main.bundle");
// @ts-ignore `__setMockFiles`
require("fs").__setMockFiles({
"app.json": JSON.stringify({
name: "Example",
resources: [assets, bundle],
}),
[assets]: "directory",
[bundle]: "text",
});

const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
"app.json",
path.resolve("")
mockFiles(
[
"app.json",
{
name: "Example",
resources: [assets, bundle],
},
],
[assets, "directory"],
[bundle, "text"]
);

const {
appName,
appxManifest,
bundleDirContent,
bundleFileContent,
} = getBundleResources("app.json", path.resolve(""));

expect(appName).toBe("Example");
expect(appxManifest).toBe("windows/Package.appxmanifest");
expect(bundleDirContent).toBe(`${assets}\\**\\*;`);
expect(bundleFileContent).toBe(`${bundle};`);
});

test("returns package manifest", () => {
mockFiles([
"app.json",
{
windows: {
appxManifest: "windows/Example/Package.appxmanifest",
},
},
]);

const {
appName,
appxManifest,
bundleDirContent,
bundleFileContent,
} = getBundleResources("app.json", path.resolve(""));

expect(appName).toBe("ReactTestApp");
expect(appxManifest).toBe("windows/Example/Package.appxmanifest");
expect(bundleDirContent).toBe("");
expect(bundleFileContent).toBe("");
});

test("handles missing manifest", () => {
const warnSpy = jest.spyOn(global.console, "warn").mockImplementation();

const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
"",
""
);
const {
appName,
appxManifest,
bundleDirContent,
bundleFileContent,
} = getBundleResources("", "");

expect(appName).toBe("ReactTestApp");
expect(appxManifest).toBe("windows/Package.appxmanifest");
expect(bundleDirContent).toBeFalsy();
expect(bundleFileContent).toBeFalsy();

Expand All @@ -56,19 +87,19 @@ describe("getBundleResources", () => {
});

test("handles invalid manifest", () => {
// @ts-ignore `__setMockFiles`
require("fs").__setMockFiles({
"app.json": "-",
});
mockFiles(["app.json", "-"]);

const warnSpy = jest.spyOn(global.console, "warn").mockImplementation();

const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
"app.json",
path.resolve("")
);
const {
appName,
appxManifest,
bundleDirContent,
bundleFileContent,
} = getBundleResources("app.json", path.resolve(""));

expect(appName).toBe("ReactTestApp");
expect(appxManifest).toBe("windows/Package.appxmanifest");
expect(bundleDirContent).toBeFalsy();
expect(bundleFileContent).toBeFalsy();

Expand Down
10 changes: 3 additions & 7 deletions test/windows-test-app/parseResources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
jest.mock("fs");

describe("parseResources", () => {
const { mockFiles } = require("../mockFiles");
const { parseResources } = require("../../windows/test-app");

// @ts-ignore `__setMockFiles`
afterEach(() => require("fs").__setMockFiles({}));
afterEach(() => mockFiles());

test("returns empty strings for no resources", () => {
expect(parseResources(undefined, "", "")).toEqual(["", ""]);
Expand All @@ -22,11 +22,7 @@ describe("parseResources", () => {
});

test("returns references to existing assets", () => {
// @ts-ignore `__setMockFiles`
require("fs").__setMockFiles({
"dist/assets": "directory",
"dist/main.jsbundle": "text",
});
mockFiles(["dist/assets", "directory"], ["dist/main.jsbundle", "text"]);

expect(
parseResources(
Expand Down
14 changes: 5 additions & 9 deletions windows/ReactTestApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "App.h"

#include "MainPage.h"

using winrt::ReactTestApp::implementation::App;
using winrt::Windows::ApplicationModel::SuspendingEventArgs;
using winrt::Windows::ApplicationModel::Activation::ApplicationExecutionState;
Expand Down Expand Up @@ -73,12 +71,10 @@ void App::OnLaunched(LaunchActivatedEventArgs const &e)
// Ensure the current window is active
Window::Current().Activate();
}
} else {
if (!e.PrelaunchActivated()) {
NavigateToFirstPage(rootFrame, e);
// Ensure the current window is active
Window::Current().Activate();
}
} else if (!e.PrelaunchActivated()) {
NavigateToFirstPage(rootFrame, e);
// Ensure the current window is active
Window::Current().Activate();
}
}

Expand Down Expand Up @@ -110,6 +106,6 @@ void App::NavigateToFirstPage(Frame &rootFrame, LaunchActivatedEventArgs const &
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(xaml_typename<ReactTestApp::MainPage>(), box_value(e.Arguments()));
rootFrame.Navigate(xaml_typename<MainPage>(), box_value(e.Arguments()));
}
}
5 changes: 4 additions & 1 deletion windows/ReactTestApp/ReactTestApp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@
</Page>
</ItemGroup>
<ItemGroup>
<AppxManifest Include="$(ReactTestAppDir)\Package.appxmanifest">
<AppxManifest Include="$(SolutionDir)$(ReactTestAppPackageManifest)" Condition="Exists('$(SolutionDir)$(ReactTestAppPackageManifest)')">
<SubType>Designer</SubType>
</AppxManifest>
<AppxManifest Include="$(ReactTestAppDir)\Package.appxmanifest" Condition="!Exists('$(SolutionDir)$(ReactTestAppPackageManifest)')">
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions windows/ReactTestApp/ReactTestApp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</Text>
</ItemGroup>
<ItemGroup>
<AppxManifest Include="$(SolutionDir)$(ReactTestAppPackageManifest)" />
<AppxManifest Include="$(ReactTestAppDir)\Package.appxmanifest" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 76286ee

Please sign in to comment.