Skip to content

Commit

Permalink
fix(windows): support for custom Package.appxmanifest
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Apr 10, 2021
1 parent 4929c48 commit 4d2516c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 49 deletions.
6 changes: 3 additions & 3 deletions test/windows-test-app/getBundleResources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("getBundleResources", () => {
[bundle]: "text",
});

const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
const { appName, bundleDirContent, bundleFileContent } = getBundleResources(
"app.json",
path.resolve("")
);
Expand All @@ -41,7 +41,7 @@ describe("getBundleResources", () => {
test("handles missing manifest", () => {
const warnSpy = jest.spyOn(global.console, "warn").mockImplementation();

const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
const { appName, bundleDirContent, bundleFileContent } = getBundleResources(
"",
""
);
Expand All @@ -63,7 +63,7 @@ describe("getBundleResources", () => {

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

const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
const { appName, bundleDirContent, bundleFileContent } = getBundleResources(
"app.json",
path.resolve("")
);
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
92 changes: 56 additions & 36 deletions windows/test-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const templateView = {

// Binary files in React Native Test App Windows project
const binaryExtensions = [".png", ".pfx"];
const textFileReadOptions = { encoding: "utf-8" };
const textFileWriteOptions = { encoding: "utf-8", mode: 0o644 };

/**
* Finds nearest relative path to a file or directory from current path.
Expand Down Expand Up @@ -58,7 +60,7 @@ function findUserProjects(projectDir, projects = []) {
findUserProjects(fullPath, projects);
}
} else if (fullPath.endsWith(".vcxproj")) {
const vcxproj = fs.readFileSync(fullPath, { encoding: "utf8" });
const vcxproj = fs.readFileSync(fullPath, textFileReadOptions);
const guidMatch = vcxproj.match(
/<ProjectGuid>({[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}})<\/ProjectGuid>/
);
Expand Down Expand Up @@ -130,6 +132,16 @@ function replaceContent(content, replacements) {
);
}

/**
* Rethrows specified error.
* @param {Error | null} error
*/
function rethrow(error) {
if (error) {
throw error;
}
}

/**
* Returns a solution entry for specified project.
* @param {{ path: string; name: string; guid: string; }} project
Expand Down Expand Up @@ -169,11 +181,11 @@ function copyAndReplace(srcPath, destPath, replacements = {}) {
return fs.writeFile(
destPath,
replaceContent(
fs.readFileSync(srcPath, { encoding: "utf8" }),
fs.readFileSync(srcPath, textFileReadOptions),
replacements
),
{
encoding: "utf8",
encoding: "utf-8",
mode: fs.statSync(srcPath).mode,
},
throwOnError
Expand All @@ -185,30 +197,49 @@ function copyAndReplace(srcPath, destPath, replacements = {}) {
* Reads manifest file and and resolves paths to bundle resources.
* @param {string | null} manifestFilePath Path to the closest manifest file.
* @param {string} projectFilesDestPath Resolved paths will be relative to this path.
* @return {[string, string, string]} Application name and paths to directories and files to include
* @return {{
* appName: string;
* appxManifest: string;
* bundleDirContent: string;
* bundleFileContent: string;
* }} Application name, and paths to directories and files to include.
*/
function getBundleResources(manifestFilePath, projectFilesDestPath) {
// Default value if manifest or 'name' field doesn't exist
// Default value if manifest or 'name' field don't exist.
const defaultName = "ReactTestApp";

// Default `Package.appxmanifest` path. The project will automatically use our
// fallback if there is no file at this path.
const defaultAppxManifest = "windows/Package.appxmanifest";

if (manifestFilePath) {
try {
const content = fs.readFileSync(manifestFilePath, { encoding: "utf8" });
const { name, resources } = JSON.parse(content);
const content = fs.readFileSync(manifestFilePath, textFileReadOptions);
const { name, resources, windows } = JSON.parse(content);
const [bundleDirContent, bundleFileContent] = parseResources(
resources,
path.dirname(manifestFilePath),
projectFilesDestPath
);
return [name || defaultName, bundleDirContent, bundleFileContent];
return {
appName: name || defaultName,
appxManifest: (windows && windows.appxManifest) || defaultAppxManifest,
bundleDirContent,
bundleFileContent,
};
} catch (e) {
console.warn(`Could not parse 'app.json':\n${e.message}`);
}
} else {
console.warn("Could not find 'app.json' file.");
}

return [defaultName, "", ""];
return {
appName: defaultName,
appxManifest: defaultAppxManifest,
bundleDirContent: "",
bundleFileContent: "",
};
}

/**
Expand All @@ -218,9 +249,7 @@ function getBundleResources(manifestFilePath, projectFilesDestPath) {
*/
function getPackageVersion(packagePath) {
const { version } = JSON.parse(
fs.readFileSync(path.join(packagePath, "package.json"), {
encoding: "utf8",
})
fs.readFileSync(path.join(packagePath, "package.json"), textFileReadOptions)
);
return version;
}
Expand Down Expand Up @@ -287,10 +316,12 @@ function generateSolution(destPath, { autolink, useNuGet }) {
fs.mkdirSync(destPath, { recursive: true });

const manifestFilePath = findNearest("app.json");
const [appName, bundleDirContent, bundleFileContent] = getBundleResources(
manifestFilePath,
projectFilesDestPath
);
const {
appName,
appxManifest,
bundleDirContent,
bundleFileContent,
} = getBundleResources(manifestFilePath, projectFilesDestPath);

const rnWindowsVersion = getPackageVersion(rnWindowsPath);

Expand All @@ -312,6 +343,9 @@ function generateSolution(destPath, { autolink, useNuGet }) {
)};`,
"\\$\\(BundleDirContentPaths\\)": bundleDirContent,
"\\$\\(BundleFileContentPaths\\)": bundleFileContent,
"\\$\\(ReactTestAppPackageManifest\\)": path.normalize(
path.relative(destPath, path.resolve(appxManifest))
),
};

const copyTasks = [
Expand Down Expand Up @@ -387,7 +421,7 @@ function generateSolution(destPath, { autolink, useNuGet }) {
const solutionTask = fs.writeFile(
path.join(destPath, `${appName}.sln`),
mustache
.render(fs.readFileSync(solutionTemplatePath, { encoding: "utf8" }), {
.render(fs.readFileSync(solutionTemplatePath, textFileReadOptions), {
...templateView,
useExperimentalNuget: useNuGet,
})
Expand All @@ -406,15 +440,8 @@ function generateSolution(destPath, { autolink, useNuGet }) {
/EndProject\r?\nGlobal/,
["EndProject", additionalProjectEntries, "Global"].join(os.EOL)
),
{
encoding: "utf8",
mode: 0o644,
},
(error) => {
if (error) {
throw error;
}
}
textFileWriteOptions,
rethrow
);
if (useNuGet) {
const nugetConfigPath =
Expand Down Expand Up @@ -445,18 +472,11 @@ function generateSolution(destPath, { autolink, useNuGet }) {
fs.writeFile(
path.join(destPath, "NuGet.Config"),
mustache.render(
fs.readFileSync(nugetConfigPath, { encoding: "utf8" }),
fs.readFileSync(nugetConfigPath, textFileReadOptions),
{}
),
{
encoding: "utf8",
mode: 0o644,
},
(error) => {
if (error) {
throw error;
}
}
textFileWriteOptions,
rethrow
);
}
}
Expand Down

0 comments on commit 4d2516c

Please sign in to comment.