Skip to content

Commit 2eb912a

Browse files
committed
implement yaml native bulk transpilation
1 parent 2dd8187 commit 2eb912a

File tree

1 file changed

+80
-24
lines changed

1 file changed

+80
-24
lines changed

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.ts

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ const getThreadsCount = () => {
4747
return 3; // Default (like the workerpool do)
4848
};
4949

50+
const splitFilesToChunks = (files: FileContent[], chunksCount: number): FileContent[][] => {
51+
let chunks: FileContent[][];
52+
if (files.length < chunksCount * chunksCount) {
53+
chunks = [files];
54+
} else {
55+
const baseSize = Math.floor(files.length / chunksCount);
56+
chunks = [];
57+
for (let i = 0; i < chunksCount; i++) {
58+
// For the last part, we take the remaining files so we don't lose the extra ones.
59+
const start = i * baseSize;
60+
const end = (i === chunksCount - 1) ? files.length : start + baseSize;
61+
chunks.push(files.slice(start, end));
62+
}
63+
}
64+
65+
return chunks;
66+
};
67+
5068
export type DataSchemaCompilerOptions = {
5169
compilerCache: CompilerCache;
5270
omitErrors?: boolean;
@@ -288,8 +306,27 @@ export class DataSchemaCompiler {
288306
}
289307

290308
if (transpilationNative) {
291-
const jsFiles = toCompile.filter(file => file.fileName.endsWith('.js') || file.convertedToJs);
292-
let JsFilesTasks = [];
309+
const jsFiles: FileContent[] = [];
310+
const yamlFiles: FileContent[] = [];
311+
const jinjaFiles: FileContent[] = [];
312+
313+
toCompile.forEach((file) => {
314+
if (file.fileName.endsWith('.js') || file.convertedToJs) {
315+
jsFiles.push(file);
316+
} else if (file.fileName.endsWith('.jinja') ||
317+
(file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml'))
318+
&& file.content.match(JINJA_SYNTAX)
319+
) {
320+
jinjaFiles.push(file);
321+
} else if (file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) {
322+
yamlFiles.push(file);
323+
}
324+
});
325+
326+
let jsFilesTasks: Promise<(FileContent | undefined)[]>[] = [];
327+
let yamlFilesTasks: Promise<(FileContent | undefined)[]>[] = [];
328+
// XXX: For now we don't have bulk transpilation for jinja files
329+
// let jinjaFilesTasks: Promise<(FileContent | undefined)[]>[] = [];
293330

294331
if (jsFiles.length > 0) {
295332
// Warming up swc compiler cache
@@ -300,26 +337,19 @@ export class DataSchemaCompiler {
300337

301338
await this.transpileJsFile(dummyFile, errorsReport, { cubeNames, cubeSymbols, transpilerNames, contextSymbols: CONTEXT_SYMBOLS, compilerId, stage });
302339

303-
let jsChunks;
304-
if (jsFiles.length < transpilationNativeThreadsCount * transpilationNativeThreadsCount) {
305-
jsChunks = [jsFiles];
306-
} else {
307-
const baseSize = Math.floor(jsFiles.length / transpilationNativeThreadsCount);
308-
jsChunks = [];
309-
for (let i = 0; i < transpilationNativeThreadsCount; i++) {
310-
// For the last part, we take the remaining files so we don't lose the extra ones.
311-
const start = i * baseSize;
312-
const end = (i === transpilationNativeThreadsCount - 1) ? jsFiles.length : start + baseSize;
313-
jsChunks.push(jsFiles.slice(start, end));
314-
}
315-
}
316-
JsFilesTasks = jsChunks.map(chunk => this.transpileJsFilesBulk(chunk, errorsReport, { transpilerNames, compilerId }));
340+
const jsChunks = splitFilesToChunks(jsFiles, transpilationNativeThreadsCount);
341+
jsFilesTasks = jsChunks.map(chunk => this.transpileJsFilesNativeBulk(chunk, errorsReport, { transpilerNames, compilerId }));
317342
}
318343

319-
const nonJsFilesTasks = toCompile.filter(file => !file.fileName.endsWith('.js') && !file.convertedToJs)
320-
.map(f => this.transpileFile(f, errorsReport, { cubeNames, cubeSymbols, transpilerNames, compilerId }));
344+
if (yamlFiles.length > 0) {
345+
const yamlChunks = splitFilesToChunks(yamlFiles, transpilationNativeThreadsCount);
346+
yamlFilesTasks = yamlChunks.map(chunk => this.transpileYamlFilesNativeBulk(chunk, errorsReport, { transpilerNames, compilerId }));
347+
}
348+
349+
const jinjaFilesTasks = jinjaFiles
350+
.map(f => this.transpileFile(f, errorsReport, { cubeNames, cubeSymbols, transpilerNames }));
321351

322-
results = (await Promise.all([...nonJsFilesTasks, ...JsFilesTasks])).flat();
352+
results = (await Promise.all([...jsFilesTasks, ...yamlFilesTasks, ...jinjaFilesTasks])).flat();
323353
} else if (transpilationWorkerThreads) {
324354
results = await Promise.all(toCompile.map(f => this.transpileFile(f, errorsReport, { cubeNames, cubeSymbols, transpilerNames })));
325355
} else {
@@ -521,11 +551,7 @@ export class DataSchemaCompiler {
521551
}
522552
}
523553

524-
/**
525-
* Right now it is used only for transpilation in native,
526-
* so no checks for transpilation type inside this method
527-
*/
528-
private async transpileJsFilesBulk(
554+
private async transpileJsFilesNativeBulk(
529555
files: FileContent[],
530556
errorsReport: ErrorReporter,
531557
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
@@ -563,6 +589,36 @@ export class DataSchemaCompiler {
563589
});
564590
}
565591

592+
private async transpileYamlFilesNativeBulk(
593+
files: FileContent[],
594+
errorsReport: ErrorReporter,
595+
{ compilerId }: TranspileOptions
596+
): Promise<(FileContent | undefined)[]> {
597+
const reqDataArr = files.map(file => ({
598+
fileName: file.fileName,
599+
fileContent: file.content,
600+
transpilers: [],
601+
compilerId: compilerId || '',
602+
}));
603+
const res = await transpileYaml(reqDataArr);
604+
605+
return files.map((file, index) => {
606+
errorsReport.inFile(file);
607+
if (!res[index]) { // This should not happen in theory but just to be safe
608+
errorsReport.error(`No transpilation result received for the file ${file.fileName}.`);
609+
return undefined;
610+
}
611+
errorsReport.addErrors(res[index].errors);
612+
errorsReport.addWarnings(res[index].warnings as unknown as SyntaxErrorInterface[]);
613+
errorsReport.exitFile();
614+
615+
file.content = res[index].code;
616+
file.convertedToJs = true;
617+
618+
return { ...file };
619+
});
620+
}
621+
566622
private async transpileJsFile(
567623
file: FileContent,
568624
errorsReport: ErrorReporter,

0 commit comments

Comments
 (0)