From 5b4795340602a97b16397328f71bc77e54f2bd6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moreno?= Date: Tue, 20 Jul 2021 10:00:31 +0200 Subject: [PATCH] support target --- resources/extension.vsixmanifest | 2 +- src/main.ts | 6 ++++++ src/package.ts | 23 ++++++++++++++++++-- src/test/package.test.ts | 37 ++++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/resources/extension.vsixmanifest b/resources/extension.vsixmanifest index a110f72d..8a2e669b 100644 --- a/resources/extension.vsixmanifest +++ b/resources/extension.vsixmanifest @@ -1,7 +1,7 @@ - + TargetPlatform="<%- target %>" <% } %> /> <%- displayName %> <%- description %> <%- tags %> diff --git a/src/main.ts b/src/main.ts index d58a3e60..a886f154 100644 --- a/src/main.ts +++ b/src/main.ts @@ -78,6 +78,7 @@ module.exports = function (argv: string[]): void { .command('package []') .description('Packages an extension') .option('-o, --out [path]', 'Output .vsix extension file to [path] location (defaults to -.vsix)') + .option('-t, --target ', 'Target architecture') .option('-m, --message ', 'Commit message used when calling `npm version`.') .option('--no-git-tag-version', 'Do not create a version commit and tag when calling `npm version`.') .option( @@ -100,6 +101,7 @@ module.exports = function (argv: string[]): void { version, { out, + target, message, gitTagVersion, githubBranch, @@ -116,6 +118,7 @@ module.exports = function (argv: string[]): void { packageCommand({ packagePath: out, version, + target, commitMessage: message, gitTagVersion, githubBranch, @@ -138,6 +141,7 @@ module.exports = function (argv: string[]): void { 'Personal Access Token (defaults to VSCE_PAT environment variable)', process.env['VSCE_PAT'] ) + .option('-t, --target ', 'Target architecture') .option('-m, --message ', 'Commit message used when calling `npm version`.') .option('--no-git-tag-version', 'Do not create a version commit and tag when calling `npm version`.') .option('--packagePath [path]', 'Publish the VSIX package located at the specified path.') @@ -160,6 +164,7 @@ module.exports = function (argv: string[]): void { version, { pat, + target, message, gitTagVersion, packagePath, @@ -176,6 +181,7 @@ module.exports = function (argv: string[]): void { publish({ pat, version, + target, commitMessage: message, gitTagVersion, packagePath, diff --git a/src/package.ts b/src/package.ts index 84963c18..3583a4d8 100644 --- a/src/package.ts +++ b/src/package.ts @@ -82,6 +82,7 @@ export interface IAsset { export interface IPackageOptions { readonly packagePath?: string; readonly version?: string; + readonly target?: string; readonly commitMessage?: string; readonly gitTagVersion?: boolean; readonly cwd?: string; @@ -279,8 +280,20 @@ export async function versionBump( } } +const Targets = new Set([ + 'win32-x64', + 'win32-ia32', + 'win32-arm64', + 'linux-x64', + 'linux-arm64', + 'linux-armhf', + 'darwin-x64', + 'darwin-arm64', + 'alpine-x64', +]); + export class ManifestProcessor extends BaseProcessor { - constructor(manifest: Manifest) { + constructor(manifest: Manifest, options: IPackageOptions = {}) { super(manifest); const flags = ['Public']; @@ -304,6 +317,11 @@ export class ManifestProcessor extends BaseProcessor { } const extensionKind = getExtensionKind(manifest); + const target = options.target; + + if (typeof target === 'string' && !Targets.has(target)) { + throw new Error(`'${target}' is not a valid VS Code target. Valid targets: ${[...Targets].join(', ')}`); + } this.vsix = { ...this.vsix, @@ -311,6 +329,7 @@ export class ManifestProcessor extends BaseProcessor { displayName: manifest.displayName || manifest.name, version: manifest.version, publisher: manifest.publisher, + target, engine: manifest.engines['vscode'], description: manifest.description || '', categories: (manifest.categories || []).join(','), @@ -1194,7 +1213,7 @@ export function processFiles(processors: IProcessor[], files: IFile[]): Promise< export function createDefaultProcessors(manifest: Manifest, options: IPackageOptions = {}): IProcessor[] { return [ - new ManifestProcessor(manifest), + new ManifestProcessor(manifest, options), new TagsProcessor(manifest), new ReadmeProcessor(manifest, options), new ChangelogProcessor(manifest, options), diff --git a/src/test/package.test.ts b/src/test/package.test.ts index 8f199b81..1793c4b0 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -55,7 +55,7 @@ type XMLManifest = { Metadata: { Description: { _: string }[]; DisplayName: string[]; - Identity: { $: { Id: string; Version: string; Publisher: string } }[]; + Identity: { $: { Id: string; Version: string; Publisher: string; TargetPlatform?: string } }[]; Tags: string[]; GalleryFlags: string[]; License: string[]; @@ -108,7 +108,7 @@ function assertMissingProperty(manifest: XMLManifest, name: string): void { assert.equal(property.length, 0, `Property '${name}' should not exist`); } -function createManifest(extra: Partial): Manifest { +function createManifest(extra: Partial = {}): Manifest { return { name: 'test', publisher: 'mocha', @@ -1547,6 +1547,39 @@ describe('toVsixManifest', () => { const extensionKindProps = properties.filter(p => p.$.Id === 'Microsoft.VisualStudio.Code.ExtensionKind'); assert.equal(extensionKindProps[0].$.Value, 'workspace'); }); + + it('should not have target platform by default', async () => { + const manifest = createManifest(); + const raw = await _toVsixManifest(manifest, []); + const dom = await parseXmlManifest(raw); + + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.Id, 'test'); + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.Version, '0.0.1'); + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.Publisher, 'mocha'); + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.TargetPlatform, undefined); + }); + + it('should set the right target platform by default', async () => { + const manifest = createManifest(); + const raw = await _toVsixManifest(manifest, [], { target: 'win32-x64' }); + const dom = await parseXmlManifest(raw); + + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.Id, 'test'); + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.Version, '0.0.1'); + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.Publisher, 'mocha'); + assert.strictEqual(dom.PackageManifest.Metadata[0].Identity[0].$.TargetPlatform, 'win32-x64'); + }); + + it('should throw when using an invalid target platform', async () => { + const manifest = createManifest(); + + try { + await _toVsixManifest(manifest, [], { target: 'linux-ia32' }); + throw new Error('oops'); + } catch (err) { + assert(/not a valid VS Code target/.test(err.message)); + } + }); }); describe('qna', () => {