Path: blob/main/build/azure-pipelines/upload-nlsmetadata.ts
3520 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import es from 'event-stream';6import Vinyl from 'vinyl';7import vfs from 'vinyl-fs';8import merge from 'gulp-merge-json';9import gzip from 'gulp-gzip';10import { ClientAssertionCredential } from '@azure/identity';11import path = require('path');12import { readFileSync } from 'fs';13const azure = require('gulp-azure-storage');1415const commit = process.env['BUILD_SOURCEVERSION'];16const credential = new ClientAssertionCredential(process.env['AZURE_TENANT_ID']!, process.env['AZURE_CLIENT_ID']!, () => Promise.resolve(process.env['AZURE_ID_TOKEN']!));1718interface NlsMetadata {19keys: { [module: string]: string };20messages: { [module: string]: string };21bundles: { [bundle: string]: string[] };22}2324function main(): Promise<void> {25return new Promise((c, e) => {26const combinedMetadataJson = es.merge(27// vscode: we are not using `out-build/nls.metadata.json` here because28// it includes metadata for translators for `keys`. but for our purpose29// we want only the `keys` and `messages` as `string`.30es.merge(31vfs.src('out-build/nls.keys.json', { base: 'out-build' }),32vfs.src('out-build/nls.messages.json', { base: 'out-build' }))33.pipe(merge({34fileName: 'vscode.json',35jsonSpace: '',36concatArrays: true,37edit: (parsedJson, file) => {38if (file.base === 'out-build') {39if (file.basename === 'nls.keys.json') {40return { keys: parsedJson };41} else {42return { messages: parsedJson };43}44}45}46})),4748// extensions49vfs.src('.build/extensions/**/nls.metadata.json', { base: '.build/extensions' }),50vfs.src('.build/extensions/**/nls.metadata.header.json', { base: '.build/extensions' }),51vfs.src('.build/extensions/**/package.nls.json', { base: '.build/extensions' })52).pipe(merge({53fileName: 'combined.nls.metadata.json',54jsonSpace: '',55concatArrays: true,56edit: (parsedJson, file) => {57if (file.basename === 'vscode.json') {58return { vscode: parsedJson };59}6061// Handle extensions and follow the same structure as the Core nls file.62switch (file.basename) {63case 'package.nls.json':64// put package.nls.json content in Core NlsMetadata format65// language packs use the key "package" to specify that66// translations are for the package.json file67parsedJson = {68messages: {69package: Object.values(parsedJson)70},71keys: {72package: Object.keys(parsedJson)73},74bundles: {75main: ['package']76}77};78break;7980case 'nls.metadata.header.json':81parsedJson = { header: parsedJson };82break;8384case 'nls.metadata.json': {85// put nls.metadata.json content in Core NlsMetadata format86const modules = Object.keys(parsedJson);8788const json: NlsMetadata = {89keys: {},90messages: {},91bundles: {92main: []93}94};95for (const module of modules) {96json.messages[module] = parsedJson[module].messages;97json.keys[module] = parsedJson[module].keys;98json.bundles.main.push(module);99}100parsedJson = json;101break;102}103}104105// Get extension id and use that as the key106const folderPath = path.join(file.base, file.relative.split('/')[0]);107const manifest = readFileSync(path.join(folderPath, 'package.json'), 'utf-8');108const manifestJson = JSON.parse(manifest);109const key = manifestJson.publisher + '.' + manifestJson.name;110return { [key]: parsedJson };111},112}));113114const nlsMessagesJs = vfs.src('out-build/nls.messages.js', { base: 'out-build' });115116es.merge(combinedMetadataJson, nlsMessagesJs)117.pipe(gzip({ append: false }))118.pipe(vfs.dest('./nlsMetadata'))119.pipe(es.through(function (data: Vinyl) {120console.log(`Uploading ${data.path}`);121// trigger artifact upload122console.log(`##vso[artifact.upload containerfolder=nlsmetadata;artifactname=${data.basename}]${data.path}`);123this.emit('data', data);124}))125.pipe(azure.upload({126account: process.env.AZURE_STORAGE_ACCOUNT,127credential,128container: '$web',129prefix: `nlsmetadata/${commit}/`,130contentSettings: {131contentEncoding: 'gzip',132cacheControl: 'max-age=31536000, public'133}134}))135.on('end', () => c())136.on('error', (err: any) => e(err));137});138}139140main().catch(err => {141console.error(err);142process.exit(1);143});144145146