-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
185 lines (160 loc) · 5.32 KB
/
generator.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
'use strict';
const path = require('path');
const fs = require('fs');
const nunjucks = require('nunjucks');
const engine = nunjucks.configure({
autoescape: false,
watch: false,
});
let root;
const env_sample_name = process.env.FIBJSCI_SAMPLE_NAME;
// support npminstall path
if (__dirname.indexOf('.npminstall') >= 0) {
root = path.join(__dirname, '../../../../../..');
} else if (is_tested_locally(env_sample_name)) {
root = path.join(__dirname, `./samples/${env_sample_name}`);
} else {
root = process.env.TEST_CI_PATH ? path.resolve(process.env.TEST_CI_PATH) : path.resolve(__dirname, '../../..');
}
let pkg;
try {
pkg = require(path.join(root, 'package.json'));
} catch (err) {
console.error('read package.json error: %s', err.message);
console.error('[fibjs-ci] stop create ci yml');
process.exit(0);
}
const actions_os_arch_presets = [
{ os: 'windows-2019', arch: 'x64' },
{ os: 'windows-2019', arch: 'x86' },
{ os: 'windows-2022', arch: 'x64' },
{ os: 'windows-2022', arch: 'x86' },
{ os: 'windows-2022', arch: 'arm64' },
// { os: 'ubuntu-18.04', arch: 'arm' },
{ os: 'ubuntu-20.04', arch: 'x64' },
{ os: 'ubuntu-20.04', arch: 'x86' },
// { os: 'ubuntu-20.04', arch: 'arm64' },
{ os: 'ubuntu-22.04', arch: 'x64' },
{ os: 'ubuntu-22.04', arch: 'x86' },
// { os: 'ubuntu-22.04', arch: 'arm64' },
{ os: 'macos-10.15', arch: 'x64' },
{ os: 'macos-11', arch: 'x64' },
// { os: 'macos-11', arch: 'arm64' },
{ os: 'macos-12', arch: 'x64' },
// { os: 'macos-12', arch: 'arm64' },
{ os: 'macos-13', arch: 'x64' },
// { os: 'macos-13', arch: 'arm64' },
{ os: 'macos-14', arch: 'x64' },
{ os: 'macos-14', arch: 'arm64' },
];
const node_versions_preset = ['16', '18', '20'];
function is_allowed_os_arch (os, arch) {
return actions_os_arch_presets.some(preset => preset.os === os && preset.arch === arch);
}
function is_lt_0_37_0 (version) {
const [major, minor, patch] = version.split('.').map(Number)
return major < 0 || (major === 0 && minor < 37);
}
const config = Object.assign({
// default is actions
type: 'actions',
// default version to 0.36.0, 0.37.0
version: [
'0.36.0',
'0.37.0'
],
os: [
"windows-2019",
"ubuntu-20.04",
"macos-11",
],
node_version: '16',
arch: ["x64", "x86", "arm64"],
// default empty
/* travis services about: start */
/** @deprecated */
travis_services: [],
get use_travis_services () {
return !!this.travis_services.length
},
get use_travis_service__mysql () {
return ~this.travis_services.indexOf('mysql')
}
/* travis services about: end */
}, pkg.ci, pkg.fibjs_ci);
clean_config: {
config.types = arrayify(config.type);
config.versions = arrayify(config.version);
config.node_version = node_versions_preset.includes(config.node_version + '') ? config.node_version : node_versions_preset[0];
config.travis_services = arrayify(config.travis_services);
const actions_os = arrayify(config.os)
const actions_arch = arrayify(config.arch)
config.$actions_os_arch_version = [];
actions_os.forEach(os => {
actions_arch.forEach((arch) => {
if (!is_allowed_os_arch(os, arch)) {
console.warn(`[fibjs-ci] os: ${os}, arch: ${arch} is not supported in actions, ignored.`)
return ;
}
config.versions.forEach(fibjs => {
if (is_lt_0_37_0(fibjs) && arch === 'arm64') {
console.warn(`[fibjs-ci] fibjs ${fibjs} is not supported on arm64, ignored.`)
} else {
config.$actions_os_arch_version.push({ os, arch, fibjs })
}
})
});
});
// config.versions_lt_0_37_0 = [];
// config.versions_ge_0_37_0 = [];
// config.versions.forEach(v => {
// if (is_lt_0_37_0(v)) {
// config.versions_lt_0_37_0.push(v);
// } else {
// config.versions_ge_0_37_0.push(v);
// }
// })
// config.has_lt_0_37_0 = !!config.versions_lt_0_37_0.length;
// config.has_ge_0_37_0 = !!config.versions_ge_0_37_0.length;
}
let ymlName = '';
for (const type of config.types) {
if (type === 'actions') {
;[
'.github/workflows/run-ci.yml',
'.github/workflows/fns.sh',
'.github/workflows/set-env-vars.sh',
].forEach(file => {
const srcpath = path.resolve(__dirname, 'tpl', file);
const targetpath = path.resolve(root, file);
let content = fs.readFileSync(srcpath, 'utf8');
content = engine.renderString(content, config);
fs.mkdirSync(path.dirname(targetpath), { recursive: true });
fs.writeFileSync(targetpath, content);
console.log(`[fibjs-ci] create ${targetpath} success`);
})
continue ;
}
if (type === 'travis') {
ymlName = '.travis.yml';
} else if (type === 'appveyor') {
ymlName = 'appveyor.yml';
} else {
throw new Error(`${type} type not support`);
}
const tplPath = path.join(__dirname, 'tpl', ymlName);
const ymlPath = path.join(root, ymlName);
let ymlContent = fs.readFileSync(tplPath, 'utf8');
ymlContent = engine.renderString(ymlContent, config);
fs.writeFileSync(ymlPath, ymlContent);
console.log(`[fibjs-ci] create ${ymlPath} success`);
}
function arrayify(str) {
if (Array.isArray(str)) return str;
return str.split(/\s*,\s*/).filter(s => !!s);
}
function is_tested_locally (env_sample_name = process.env.FIBJSCI_SAMPLE_NAME) {
return !!env_sample_name && fs.existsSync(
path.resolve(__dirname, './samples/.lifecyclecheck')
)
}