|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as os from "os"; |
| 3 | +import * as path from "path"; |
| 4 | +import { assert } from "chai"; |
| 5 | +import { WindowsProjectService } from "../../lib/services/windows-project-service"; |
| 6 | +import { injector } from "../../lib/common/yok"; |
| 7 | +import * as stubs from "../stubs"; |
| 8 | + |
| 9 | +// Native sources on Windows: C# files under App_Resources/Windows and a plugin's |
| 10 | +// platforms/windows are staged into the platform project, where the template's SDK-style csproj |
| 11 | +// compiles them into the app (the counterpart of App_Resources/Android/src, App_Resources/iOS/src |
| 12 | +// and plugins' platforms/android|ios sources). |
| 13 | + |
| 14 | +const projectName = "TestApp"; |
| 15 | + |
| 16 | +// The subset of IFileSystem the Windows project service uses, backed by the real file system. |
| 17 | +const realFs = <any>{ |
| 18 | + exists: (p: string) => fs.existsSync(p), |
| 19 | + ensureDirectoryExists: (p: string) => fs.mkdirSync(p, { recursive: true }), |
| 20 | + copyFile: (from: string, to: string) => { |
| 21 | + fs.mkdirSync(path.dirname(to), { recursive: true }); |
| 22 | + fs.copyFileSync(from, to); |
| 23 | + }, |
| 24 | + writeFile: (p: string, content: string) => { |
| 25 | + fs.mkdirSync(path.dirname(p), { recursive: true }); |
| 26 | + fs.writeFileSync(p, content); |
| 27 | + }, |
| 28 | + writeJson: (p: string, value: any) => fs.writeFileSync(p, JSON.stringify(value)), |
| 29 | + deleteDirectory: (p: string) => fs.rmSync(p, { recursive: true, force: true }), |
| 30 | +}; |
| 31 | + |
| 32 | +function write(file: string, content = "") { |
| 33 | + fs.mkdirSync(path.dirname(file), { recursive: true }); |
| 34 | + fs.writeFileSync(file, content); |
| 35 | +} |
| 36 | + |
| 37 | +describe("WindowsProjectService native sources", () => { |
| 38 | + let root: string; |
| 39 | + let projectDir: string; |
| 40 | + let projectData: any; |
| 41 | + let service: WindowsProjectService; |
| 42 | + let appProjectDir: string; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + root = fs.mkdtempSync(path.join(os.tmpdir(), "ns-windows-project-")); |
| 46 | + projectDir = path.join(root, "app"); |
| 47 | + fs.mkdirSync(projectDir); |
| 48 | + projectData = { |
| 49 | + projectDir, |
| 50 | + projectName, |
| 51 | + projectId: "org.nativescript.testapp", |
| 52 | + projectIdentifiers: { windows: "org.nativescript.testapp" }, |
| 53 | + platformsDir: path.join(projectDir, "platforms"), |
| 54 | + nsConfig: {}, |
| 55 | + }; |
| 56 | + const projectDataService = <any>{ |
| 57 | + getRuntimePackage: () => ({ name: "@nativescript/windows", version: "0.1.0" }), |
| 58 | + }; |
| 59 | + service = new WindowsProjectService( |
| 60 | + realFs, |
| 61 | + projectDataService, |
| 62 | + <any>{}, |
| 63 | + new stubs.LoggerStub(), |
| 64 | + <any>{}, |
| 65 | + <any>{}, |
| 66 | + ); |
| 67 | + appProjectDir = path.join(projectData.platformsDir, "windows", projectName); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + fs.rmSync(root, { recursive: true, force: true }); |
| 72 | + }); |
| 73 | + |
| 74 | + function plugin(name: string, files: Record<string, string>) { |
| 75 | + const fullPath = path.join(projectDir, "node_modules", ...name.split("/")); |
| 76 | + for (const [rel, content] of Object.entries(files)) { |
| 77 | + write(path.join(fullPath, "platforms", "windows", rel), content); |
| 78 | + } |
| 79 | + return <any>{ name, fullPath, nativescript: {} }; |
| 80 | + } |
| 81 | + |
| 82 | + describe("preparePluginNativeCode", () => { |
| 83 | + it("stages C# sources into the app project, where the csproj compiles them", async () => { |
| 84 | + const p = plugin("test-plugin", { |
| 85 | + "src/Greeter.cs": "namespace TestPlugin { public class Greeter {} }", |
| 86 | + "src/index.js": "module.exports = {};", |
| 87 | + "lib/Native.dll": "dll", |
| 88 | + }); |
| 89 | + await service.preparePluginNativeCode(p, projectData); |
| 90 | + |
| 91 | + const stage = path.join(appProjectDir, "plugins", "test-plugin"); |
| 92 | + assert.isTrue(fs.existsSync(path.join(stage, "src", "Greeter.cs"))); |
| 93 | + assert.isTrue(fs.existsSync(path.join(stage, "lib", "Native.dll"))); |
| 94 | + assert.isFalse(fs.existsSync(path.join(stage, "src", "index.js"))); |
| 95 | + }); |
| 96 | + |
| 97 | + it("does not ship C# sources with the app", async () => { |
| 98 | + const p = plugin("test-plugin", { |
| 99 | + "src/Greeter.cs": "namespace TestPlugin { public class Greeter {} }", |
| 100 | + "lib/Native.dll": "dll", |
| 101 | + }); |
| 102 | + await service.preparePluginNativeCode(p, projectData); |
| 103 | + |
| 104 | + const stage = path.join(appProjectDir, "plugins", "test-plugin"); |
| 105 | + const props = fs.readFileSync(path.join(stage, "plugin.props"), "utf8"); |
| 106 | + const targets = fs.readFileSync(path.join(stage, "plugin.targets"), "utf8"); |
| 107 | + assert.include(props, "lib\\Native.dll"); |
| 108 | + assert.notInclude(props, "Greeter.cs"); |
| 109 | + assert.include(targets, "lib\\Native.dll"); |
| 110 | + assert.notInclude(targets, "Greeter.cs"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("stages scoped plugins under plugins/@scope/name", async () => { |
| 114 | + const p = plugin("@acme/native", { "src/Acme.cs": "namespace Acme { public class A {} }" }); |
| 115 | + await service.preparePluginNativeCode(p, projectData); |
| 116 | + assert.isTrue(fs.existsSync(path.join(appProjectDir, "plugins", "@acme", "native", "src", "Acme.cs"))); |
| 117 | + }); |
| 118 | + |
| 119 | + it("drops sources a newer plugin version no longer has", async () => { |
| 120 | + const p = plugin("test-plugin", { "src/Old.cs": "class Old {}", "src/Kept.cs": "class Kept {}" }); |
| 121 | + await service.preparePluginNativeCode(p, projectData); |
| 122 | + fs.rmSync(path.join(p.fullPath, "platforms", "windows", "src", "Old.cs")); |
| 123 | + await service.preparePluginNativeCode(p, projectData); |
| 124 | + |
| 125 | + const stage = path.join(appProjectDir, "plugins", "test-plugin", "src"); |
| 126 | + assert.isFalse(fs.existsSync(path.join(stage, "Old.cs"))); |
| 127 | + assert.isTrue(fs.existsSync(path.join(stage, "Kept.cs"))); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe("prepareAppResources", () => { |
| 132 | + it("mirrors App_Resources/Windows, C# sources included", () => { |
| 133 | + write(path.join(projectDir, "App_Resources", "Windows", "src", "Fixtures.cs"), "class Fixtures {}"); |
| 134 | + write(path.join(projectDir, "App_Resources", "Windows", "app.csproj"), "<Project />"); |
| 135 | + service.prepareAppResources(projectData); |
| 136 | + |
| 137 | + const dest = path.join(appProjectDir, "App_Resources", "Windows"); |
| 138 | + assert.isTrue(fs.existsSync(path.join(dest, "src", "Fixtures.cs"))); |
| 139 | + assert.isTrue(fs.existsSync(path.join(dest, "app.csproj"))); |
| 140 | + }); |
| 141 | + |
| 142 | + it("removes C# sources deleted from App_Resources/Windows", () => { |
| 143 | + const src = path.join(projectDir, "App_Resources", "Windows", "src"); |
| 144 | + write(path.join(src, "Gone.cs"), "class Gone {}"); |
| 145 | + write(path.join(src, "Stays.cs"), "class Stays {}"); |
| 146 | + service.prepareAppResources(projectData); |
| 147 | + fs.rmSync(path.join(src, "Gone.cs")); |
| 148 | + service.prepareAppResources(projectData); |
| 149 | + |
| 150 | + const dest = path.join(appProjectDir, "App_Resources", "Windows", "src"); |
| 151 | + assert.isFalse(fs.existsSync(path.join(dest, "Gone.cs"))); |
| 152 | + assert.isTrue(fs.existsSync(path.join(dest, "Stays.cs"))); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe("prepareProject", () => { |
| 157 | + it("removes the staged sources of uninstalled plugins", async () => { |
| 158 | + const kept = plugin("kept-plugin", { "src/Kept.cs": "class Kept {}" }); |
| 159 | + const scoped = plugin("@acme/kept", { "src/Acme.cs": "class Acme {}" }); |
| 160 | + write(path.join(appProjectDir, "plugins", "removed-plugin", "src", "Removed.cs"), "class Removed {}"); |
| 161 | + write(path.join(appProjectDir, "plugins", "@acme", "removed", "src", "Gone.cs"), "class Gone {}"); |
| 162 | + |
| 163 | + injector.register("pluginsService", { getAllInstalledPlugins: async () => [kept, scoped] }); |
| 164 | + await service.prepareProject(projectData, {}); |
| 165 | + |
| 166 | + const plugins = path.join(appProjectDir, "plugins"); |
| 167 | + assert.isTrue(fs.existsSync(path.join(plugins, "kept-plugin", "src", "Kept.cs"))); |
| 168 | + assert.isTrue(fs.existsSync(path.join(plugins, "@acme", "kept", "src", "Acme.cs"))); |
| 169 | + assert.isFalse(fs.existsSync(path.join(plugins, "removed-plugin"))); |
| 170 | + assert.isFalse(fs.existsSync(path.join(plugins, "@acme", "removed"))); |
| 171 | + const aggregate = fs.readFileSync(path.join(plugins, "Plugins.props"), "utf8"); |
| 172 | + assert.include(aggregate, "kept-plugin\\plugin.props"); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments