-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbuild_modules.js
executable file
·202 lines (185 loc) · 7.49 KB
/
build_modules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env node
// Build the modules file based on the input filters
// and ordered to solve the dependencies
// ./build_modules.js modules --output=build --diff="clouds/bigquery/modules/sql/quadbin/QUADBIN_TOZXY.sql"
// ./build_modules.js modules --output=build --functions=ST_TILEENVELOPE
// ./build_modules.js modules --output=build --modules=quadbin
// ./build_modules.js modules --output=build --production --dropfirst
const fs = require('fs');
const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const inputDirs = argv._[0] && argv._[0].split(',');
const outputDir = argv.output || 'build';
const libsBuildDir = argv.libs_build_dir || '../libraries/javascript/build';
const diff = argv.diff || [];
const nodeps = argv.nodeps;
const libraryBucket = argv.librarybucket;
const makelib = argv.makelib;
let modulesFilter = (argv.modules && argv.modules.split(',')) || [];
let functionsFilter = (argv.functions && argv.functions.split(',')) || [];
let all = !(diff.length || modulesFilter.length || functionsFilter.length);
if (all) {
console.log('- Build all');
} else if (diff && diff.length) {
console.log(`- Build diff: ${argv.diff}`);
} else if (modulesFilter && modulesFilter.length) {
console.log(`- Build modules: ${argv.modules}`);
} else if (functionsFilter && functionsFilter.length) {
console.log(`- Build functions: ${argv.functions}`);
}
// Convert diff to modules
if (diff.length) {
const patternsAll = [
/\.github\/workflows\/bigquery\.yml/,
/clouds\/bigquery\/common\/.+/,
/clouds\/bigquery\/libraries\/.+/,
/clouds\/bigquery\/.*Makefile/,
/clouds\/bigquery\/version/
];
const patternModulesSql = /clouds\/bigquery\/modules\/sql\/([^\s]*?)\//g;
const patternModulesTest = /clouds\/bigquery\/modules\/test\/([^\s]*?)\//g;
const diffAll = patternsAll.some(p => diff.match(p));
if (diffAll) {
console.log('-- all');
all = diffAll;
} else {
const modulesSql = [...diff.matchAll(patternModulesSql)].map(m => m[1]);
const modulesTest = [...diff.matchAll(patternModulesTest)].map(m => m[1]);
const diffModulesFilter = [...new Set(modulesSql.concat(modulesTest))];
if (diffModulesFilter) {
console.log(`-- modules: ${diffModulesFilter}`);
modulesFilter = diffModulesFilter;
}
}
}
// Extract functions
const functions = [];
for (let inputDir of inputDirs) {
const sqldir = path.join(inputDir, 'sql');
const modules = fs.readdirSync(sqldir);
modules.forEach(module => {
const moduledir = path.join(sqldir, module);
if (fs.statSync(moduledir).isDirectory()) {
const files = fs.readdirSync(moduledir);
files.forEach(file => {
if (file.endsWith('.sql')) {
const name = path.parse(file).name;
const content = fs.readFileSync(path.join(moduledir, file)).toString().replace(/--.*\n/g, '');
functions.push({
name,
module,
content,
dependencies: []
});
}
});
}
});
}
// Check filters
modulesFilter.forEach(m => {
if (!functions.map(fn => fn.module).includes(m)) {
console.log(`ERROR: Module not found ${m}`);
process.exit(1);
}
});
functionsFilter.forEach(f => {
if (!functions.map(fn => fn.name).includes(f)) {
console.log(`ERROR: Function not found ${f}`);
process.exit(1);
}
});
// Extract function dependencies
if (!nodeps) {
functions.forEach(mainFunction => {
functions.forEach(depFunction => {
if (mainFunction.name != depFunction.name) {
const depFunctionMatches = [];
depFunctionMatches.push(...depFunction.content.replace(/(\r\n|\n|\r)/gm,' ').matchAll(new RegExp('(?<=(?<!TEMP )FUNCTION)(.*?)(?=AS |RETURNS)','g')));
depFunctionMatches.push(...depFunction.content.replace(/(\r\n|\n|\r)/gm,' ').matchAll(new RegExp('(?<=PROCEDURE)(.*?)(?=BEGIN)','g')));
const depFunctionNames = [];
depFunctionMatches.forEach((depFunctionMatch) => {
let qualifiedDepFunctName = depFunctionMatch[0].replace(/[ \p{Diacritic}]/gu, '').split('(')[0];
qualifiedDepFunctName = qualifiedDepFunctName.split('.');
depFunctionNames.push(qualifiedDepFunctName[qualifiedDepFunctName.length - 1]);
})
if (depFunctionNames.some((depFunctionName) => mainFunction.content.includes(`DATASET@@.${depFunctionName}\`(`))) {
mainFunction.dependencies.push(depFunction.name);
}
}
});
});
}
// Check circular dependencies
if (!nodeps) {
functions.forEach(mainFunction => {
functions.forEach(depFunction => {
if (mainFunction.dependencies.includes(depFunction.name) &&
depFunction.dependencies.includes(mainFunction.name)) {
console.log(`ERROR: Circular dependency between ${mainFunction.name} and ${depFunction.name}`);
process.exit(1);
}
});
});
}
// Filter and order functions
const output = [];
function add (f, include) {
include = include || all || functionsFilter.includes(f.name) || modulesFilter.includes(f.module);
for (const dependency of f.dependencies) {
add(functions.find(f => f.name === dependency), include);
}
if (!output.map(f => f.name).includes(f.name) && include) {
output.push({
name: f.name,
content: f.content
});
}
}
functions.forEach(f => add(f));
// Replace environment variables
let separator;
if (argv.production) {
separator = '\n';
} else {
separator = '\n-->\n'; // marker to future SQL split
}
let content = output.map(f => f.content).join(separator);
function apply_replacements (text) {
const libraries = [... new Set(text.match(new RegExp('@@BQ_LIBRARY_.*_BUCKET@@', 'g')))];
for (let library of libraries) {
let libraryName = library.replace('@@BQ_LIBRARY_', '').replace('_BUCKET@@', '').toLowerCase();
if (makelib == libraryName) {
continue;
}
libraryName += '.js';
const libraryPath = path.join(libsBuildDir, libraryName);
if (fs.existsSync(libraryPath)) {
const libraryBucketPath = libraryBucket + '_' + libraryName;
text = text.replace(new RegExp(library, 'g'), libraryBucketPath);
}
else {
console.log(`Warning: library "${libraryName}" does not exist. Run "make build-libraries" with the same filters.`);
process.exit(1);
}
}
const replacements = process.env.REPLACEMENTS.split(' ');
for (let replacement of replacements) {
if (replacement) {
const pattern = new RegExp(`@@${replacement}@@`, 'g');
text = text.replace(pattern, process.env[replacement]);
}
}
text = text.replace(/@@SKIP_DEP@@/g, '');
return text;
}
if (argv.dropfirst) {
const header = fs.readFileSync(path.resolve(__dirname, 'DROP_FUNCTIONS.sql')).toString();
content = header + separator + content
}
const footer = fs.readFileSync(path.resolve(__dirname, 'VERSION.sql')).toString();
content += separator + footer;
content = apply_replacements(content);
// Write modules.sql file
fs.writeFileSync(path.join(outputDir, 'modules.sql'), content);
console.log(`Write ${outputDir}/modules.sql`);