add gulp tasks for localizing language service data

This commit is contained in:
Garrett Campbell 2024-11-12 14:24:26 -05:00
Родитель eac84a4014
Коммит 953a11ac33
3 изменённых файлов: 35 добавлений и 17 удалений

Просмотреть файл

@ -26,6 +26,11 @@ const jsonSchemaFilesPatterns = [
"*/*-schema.json"
];
// Patterns to find language services json files.
const languageServicesFilesPatterns = [
"assets/*.json"
];
const languages = [
{ id: "zh-tw", folderName: "cht", transifexId: "zh-hant" },
{ id: "zh-cn", folderName: "chs", transifexId: "zh-hans" },
@ -94,7 +99,7 @@ const traverseJson = (jsonTree, descriptionCallback, prefixPath) => {
// Traverses schema json files looking for "description" fields to localized.
// The path to the "description" field is used to create a localization key.
const processJsonSchemaFiles = () => {
const processJsonFiles = () => {
return es.through(function (file) {
let jsonTree = JSON.parse(file.contents.toString());
let localizationJsonContents = {};
@ -133,10 +138,13 @@ gulp.task("translations-export", (done) => {
// Scan schema files
let jsonSchemaStream = gulp.src(jsonSchemaFilesPatterns)
.pipe(processJsonSchemaFiles());
.pipe(processJsonFiles());
let jsonLanguageServicesStream = gulp.src(languageServicesFilesPatterns)
.pipe(processJsonFiles());
// Merge files from all source streams
es.merge(jsStream, jsonSchemaStream)
es.merge(jsStream, jsonSchemaStream, jsonLanguageServicesStream)
// Filter down to only the files we need
.pipe(filter(['**/*.nls.json', '**/*.nls.metadata.json']))
@ -214,7 +222,7 @@ const generatedSrcLocBundle = () => {
.pipe(gulp.dest('dist'));
};
const generateLocalizedJsonSchemaFiles = () => {
const generateLocalizedJsonFiles = (paths) => {
return es.through(function (file) {
let jsonTree = JSON.parse(file.contents.toString());
languages.map((language) => {
@ -237,7 +245,7 @@ const generateLocalizedJsonSchemaFiles = () => {
traverseJson(jsonTree, descriptionCallback, "");
let newContent = JSON.stringify(jsonTree, null, '\t');
this.queue(new vinyl({
path: path.join("schema", language.id, relativePath),
path: path.join(...paths, language.id, relativePath),
contents: Buffer.from(newContent, 'utf8')
}));
});
@ -249,18 +257,17 @@ const generateLocalizedJsonSchemaFiles = () => {
// Generate new version of the JSON schema file in dist/schema/<language_id>/<path>
const generateJsonSchemaLoc = () => {
return gulp.src(jsonSchemaFilesPatterns)
.pipe(generateLocalizedJsonSchemaFiles())
.pipe(generateLocalizedJsonFiles(["schema"]))
.pipe(gulp.dest('dist'));
};
gulp.task('translations-generate', gulp.series(generatedSrcLocBundle, generatedAdditionalLocFiles, generateJsonSchemaLoc));
const generateJsonLanguageServicesLoc = () => {
return gulp.src(languageServicesFilesPatterns)
.pipe(generateLocalizedJsonFiles(["languageServices"]))
.pipe(gulp.dest('dist'));
};
// TODO: Insert task that grabs the language services assets and drops them in a folder, maybe called 'data'.
const languageServices = () => {
return gulp.src(["assets/*.json"]).pipe(gulp.dest("assets"));
}
gulp.task('language-services', languageServices);
gulp.task('translations-generate', gulp.series(generatedSrcLocBundle, generatedAdditionalLocFiles, generateJsonSchemaLoc, generateJsonLanguageServicesLoc));
const allTypeScript = [
'src/**/*.ts',

Просмотреть файл

@ -3717,7 +3717,7 @@
"scripts": {
"compile": "yarn install && webpack --mode development",
"compile-watch": "yarn install && webpack --mode development --watch --progress",
"compile-production": "yarn install && yarn run translations-generate && yarn run language-services && webpack --env BUILD_VSCODE_NLS=true --mode production",
"compile-production": "yarn install && yarn run translations-generate && webpack --env BUILD_VSCODE_NLS=true --mode production",
"translations-export": "gulp translations-export",
"translations-generate": "gulp translations-generate",
"translations-import": "gulp translations-import",

Просмотреть файл

@ -2,6 +2,7 @@ import * as vscode from "vscode";
import * as path from "path";
import { fs } from "@cmt/pr";
import { thisExtensionPath } from "@cmt/util";
import * as util from "@cmt/util";
interface Commands {
[key: string]: Command;
@ -35,12 +36,22 @@ export class LanguageServiceData implements vscode.HoverProvider, vscode.Complet
private constructor() {
}
private async getFile(fileEnding: string, locale: string): Promise<string> {
let filePath: string = path.join(thisExtensionPath(), 'dist/languageServices', locale, 'assets', fileEnding);
const fileExists: boolean = await util.checkFileExists(filePath);
if (!fileExists) {
filePath = path.join(thisExtensionPath(), 'assets', fileEnding);
}
return fs.readFile(filePath);
}
private async load(): Promise<void> {
const test = thisExtensionPath();
console.log(test);
this.commands = JSON.parse(await fs.readFile(path.join(thisExtensionPath(), 'assets', 'commands.json')));
this.variables = JSON.parse(await fs.readFile(path.join(thisExtensionPath(), 'assets', 'variables.json')));
this.modules = JSON.parse(await fs.readFile(path.join(thisExtensionPath(), 'assets', 'modules.json')));
const locale: string = util.getLocaleId();
this.commands = JSON.parse(await this.getFile('commands.json', locale));
this.variables = JSON.parse(await this.getFile('variables.json', locale));
this.modules = JSON.parse(await this.getFile('modules.json', locale));
}
public static async create(): Promise<LanguageServiceData> {