From f4d50e207315db21a0e6ac13fb8117406fe89bee Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Mon, 28 May 2018 16:20:44 +0200 Subject: [PATCH] package: expose core localizations as assets --- src/manifest.ts | 20 +++++++++++++++++- src/package.ts | 44 +++++++++++++++++++++++++++++++++++++++- src/test/package.test.ts | 28 ++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/manifest.ts b/src/manifest.ts index 8c52b28..ffa680c 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -4,6 +4,24 @@ export interface Person { email?: string; } +export interface Translation { + id: string; + path: string; +} + +export interface Localization { + languageId: string; + languageName?: string; + languageNameLocalized?: string; + translations: Translation[]; + minimalTranslations?: { [key: string]: string }; +} + +export interface Contributions { + 'localizations'?: Localization[]; + [contributionType: string]: any; +} + export interface Manifest { // mandatory (npm) name: string; @@ -13,7 +31,7 @@ export interface Manifest { // vscode publisher: string; icon?: string; - contributes?: { [contributionType: string]: any; }; + contributes?: Contributions; activationEvents?: string[]; extensionDependencies?: string[]; galleryBanner?: { color?: string; theme?: string; }; diff --git a/src/package.ts b/src/package.ts index 18e7c15..119bbec 100644 --- a/src/package.ts +++ b/src/package.ts @@ -531,6 +531,47 @@ class IconProcessor extends BaseProcessor { } } +export class NLSProcessor extends BaseProcessor { + + private translations: { [path: string]: string } = Object.create(null); + + constructor(manifest: Manifest) { + super(manifest); + + if (!manifest.contributes || !manifest.contributes.localizations || manifest.contributes.localizations.length === 0) { + return; + } + + const localizations = manifest.contributes.localizations; + const translations: { [languageId: string]: string } = Object.create(null); + + // take last reference in the manifest for any given language + for (const localization of localizations) { + for (const translation of localization.translations) { + if (translation.id === 'vscode' && !!translation.path) { + translations[localization.languageId.toUpperCase()] = `extension/${util.normalize(translation.path)}`; + } + } + } + + // invert the map for later easier retrieval + for (const languageId of Object.keys(translations)) { + this.translations[translations[languageId]] = languageId; + } + } + + onFile(file: IFile): Promise { + const normalizedPath = util.normalize(file.path); + const language = this.translations[normalizedPath]; + + if (language) { + this.assets.push({ type: `Microsoft.VisualStudio.Code.Translation.${language}`, path: normalizedPath }); + } + + return Promise.resolve(file); + } +} + export function validateManifest(manifest: Manifest): Manifest { validatePublisher(manifest.publisher); validateExtensionName(manifest.name); @@ -700,7 +741,8 @@ export function createDefaultProcessors(manifest: Manifest, options: IPackageOpt new ReadmeProcessor(manifest, options), new ChangelogProcessor(manifest, options), new LicenseProcessor(manifest), - new IconProcessor(manifest) + new IconProcessor(manifest), + new NLSProcessor(manifest) ]; } diff --git a/src/test/package.test.ts b/src/test/package.test.ts index 1cdaee2..24b969e 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -918,7 +918,7 @@ describe('toVsixManifest', () => { contributes: { localizations: [{ languageId: 'de', - translations: [{ id: 'vscode' }, { id: 'vscode.go' }] + translations: [{ id: 'vscode', path: 'fake.json' }, { id: 'vscode.go', path: 'what.json' }] }] } }; @@ -935,6 +935,32 @@ describe('toVsixManifest', () => { }); }); + it('should expose localization contributions as assets', () => { + const manifest = { + name: 'test', + publisher: 'mocha', + version: '0.0.1', + engines: Object.create(null), + contributes: { + localizations: [{ + languageId: 'de', + translations: [{ id: 'vscode', path: 'fake.json' }, { id: 'vscode.go', path: 'what.json' }] + }] + } + }; + + const files = [ + { path: 'extension/fake.json', contents: new Buffer('') } + ]; + + return _toVsixManifest(manifest, files) + .then(parseXmlManifest) + .then(result => { + const assets = result.PackageManifest.Assets[0].Asset; + assert(assets.some(asset => asset.$.Type === 'Microsoft.VisualStudio.Code.Translation.DE' && asset.$.Path === 'extension/fake.json')); + }); + }); + it('should detect language extensions', () => { const manifest = { name: 'test',