-
Notifications
You must be signed in to change notification settings - Fork 10
/
pull-template-changes.ts
321 lines (281 loc) · 10.4 KB
/
pull-template-changes.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env node
import yargs from 'yargs';
import fs from 'fs';
import path from 'path';
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
import { hideBin } from 'yargs/helpers';
import { execSync } from 'child_process';
import semver from 'semver';
import {
copyProjectFolder,
copyProjectFolderDefaultOptions,
} from './updates/update-helper.ts';
import { updateVersion } from './updates/update-version.ts';
const args = yargs(
hideBin(process.argv).filter(
(x) =>
x !== 'ts-node' && x !== '--esm' && x !== 'npx' && !x.includes('.ts'),
),
)
.version('0.1')
.options({
name: {
alias: 'n',
type: 'string',
description: 'Name of the project (e.g. StudyApp)',
},
company: {
alias: 'c',
type: 'string',
description: 'Name of the root level namespace (e.g. MccSoft)',
},
})
//.demandOption(["name"])
.help()
.parseSync();
if (!fs.existsSync('./scripts/pull-template-changes.ts')) {
console.error('You should run the script from repository root folder');
process.exit();
}
const currentDir = process.cwd();
if (!currentDir.endsWith('_template')) {
const detectedInfo = detectProjectAndCompanyName();
const companyName = args.company || detectedInfo?.company;
const projectName = args.name || detectedInfo?.project;
if (!companyName) throw new Error('Unable to determine the company name');
if (!projectName) throw new Error('Unable to determine the project name');
// if we are run from project folder do the following:
// 1. Clone template and rename files according to project & company
// 2. Run pull-template-changes from cloned folder
const templateFolder = process.cwd() + '_template';
cloneTemplate(templateFolder);
renameFilesInTemplate(templateFolder, projectName, companyName);
console.log(`Calling pull-template-changes from template`);
execSync(`npx ts-node --esm scripts/pull-template-changes.ts`, {
cwd: templateFolder,
stdio: 'inherit',
});
process.exit();
}
const templateFolder = process.cwd();
process.chdir(templateFolder.replace('_template', ''));
const detectedInfo = detectProjectAndCompanyName();
const companyName = args.company || detectedInfo?.company;
const projectName = args.name || detectedInfo?.project;
if (!companyName) throw new Error('Unable to determine the company name');
if (!projectName) throw new Error('Unable to determine the project name');
const prefix = `${companyName}.${projectName}`;
console.log(`ProjectName: ${projectName}, CompanyName: ${companyName}`);
if (!projectName) {
console.error(
'Unable to determine the project name. Please, use --name option',
);
process.exit();
}
// run post-processor, so each specific project could modify template files before they are copied over
if (fs.existsSync('scripts/pull-template-post-processor.ts')) {
execSync(
`npx ts-node --esm scripts/pull-template-post-processor.ts --templateFolder "${templateFolder}"`,
);
} else {
execSync(
`node scripts/pull-template-post-processor.js --templateFolder "${templateFolder}"`,
);
}
console.log('Starting to copy files...');
copyProjectFolder(`.ci`, { ignorePattern: ['_stages', 'partial.'] });
copyProjectFolder(`scripts`, { ignorePattern: 'pull-template-post-processor' });
copyProjectFolder('webapi/Lib', copyProjectFolderDefaultOptions);
copyProjectFolder('docs');
copyProjectFolder(
`webapi/src/${prefix}.App/Setup`,
copyProjectFolderDefaultOptions,
);
syncPacketsInPackageJson('package.json');
syncPacketsInPackageJson('frontend/package.json');
syncPacketsInPackageJson('e2e/package.json');
syncReferencesInProjects(`webapi/src/${prefix}.App/${prefix}.App.csproj`);
syncReferencesInProjects(`webapi/src/${prefix}.Common/${prefix}.Common.csproj`);
syncReferencesInProjects(`webapi/src/${prefix}.Domain/${prefix}.Domain.csproj`);
syncReferencesInProjects(`webapi/src/${prefix}.Http/${prefix}.Http.csproj`);
syncReferencesInProjects(
`webapi/src/${prefix}.Persistence/${prefix}.Persistence.csproj`,
);
syncReferencesInProjects(
`webapi/tests/${prefix}.App.Tests/${prefix}.App.Tests.csproj`,
);
syncReferencesInProjects(
`webapi/tests/${prefix}.Domain.Tests/${prefix}.Domain.Tests.csproj`,
);
syncReferencesInProjects(
`webapi/tests/${prefix}.ComponentTests/${prefix}.ComponentTests.csproj`,
);
syncReferencesInProjects(
`webapi/tests/${prefix}.TestUtils/${prefix}.TestUtils.csproj`,
);
console.log(`folder syncing is finished`);
updateVersion(prefix);
console.log(`Finished successfully`);
function renameFilesInTemplate(
templateFolder: string,
projectName: string,
companyName: string,
) {
console.log('Calling `yarn install` in template...');
execSync(`yarn install`, {
cwd: templateFolder,
});
console.log('Renaming files in template...');
execSync(`yarn rename -n ${projectName} -c ${companyName}`, {
cwd: templateFolder,
});
console.log('Rename finished.');
}
function cloneTemplate(folder: string) {
if (fs.existsSync(folder)) {
fs.rmdirSync(folder, { recursive: true });
}
execSync(
`git clone --depth=1 https://github.com/mccsoft/backend-frontend-template.git \"${folder}\"`,
);
}
function detectProjectAndCompanyName(): {
company: string;
project: string;
} | null {
const regex = /(.*?)\.(.*)\.App/;
const appFolder = findFileMatching('webapi/src', regex);
console.log('Found App folder:', appFolder);
const result = appFolder?.match(regex);
if (!result) return null;
return {
company: result[1],
project: result[2],
};
}
function findFileMatching(dir: string, regex: RegExp) {
const files = fs.readdirSync(dir);
for (const file of files) {
if (file.match(regex)) {
return file;
}
}
return null;
}
export function syncReferencesInProjects(relativePathInsideProject: string) {
console.log(`syncReferencesInProjects '${relativePathInsideProject}'`);
const copyFrom = path.join(templateFolder, relativePathInsideProject);
const copyTo = path.join(process.cwd(), relativePathInsideProject);
doSyncReferencesInProjects(copyFrom, copyTo);
}
export function doSyncReferencesInProjects(src: string, dest: string) {
if (!fs.existsSync(src) || !fs.existsSync(dest)) return;
const sourceFileContent = fs.readFileSync(src).toString('utf8');
let destinationFileContent = fs.readFileSync(dest).toString('utf8');
const parser = new XMLParser({ ignoreAttributes: false });
const sourceXml = parser.parse(sourceFileContent);
const destinationXml = parser.parse(destinationFileContent);
const sourcePackageReferences = getPackageReferences(sourceXml);
const destinationPackageReferences = getPackageReferences(destinationXml);
let firstItemDestinationGroup = destinationXml.Project?.ItemGroup;
if (Array.isArray(firstItemDestinationGroup))
firstItemDestinationGroup = firstItemDestinationGroup[0];
for (const sourcePackageReference of sourcePackageReferences) {
const sourceVersion = sourcePackageReference['@_Version'];
if (!semver.valid(sourceVersion)) continue;
const found = destinationPackageReferences.find(
(x) => x['@_Include'] === sourcePackageReference['@_Include'],
);
if (found) {
const destinationVersion = found['@_Version'];
if (
semver.valid(destinationVersion) &&
semver.gt(sourceVersion, destinationVersion)
) {
found['@_Version'] = sourceVersion;
}
} else {
// add package to file
if (!firstItemDestinationGroup.PackageReference)
firstItemDestinationGroup.PackageReference = [];
if (!Array.isArray(firstItemDestinationGroup.PackageReference))
firstItemDestinationGroup.PackageReference = [
firstItemDestinationGroup.PackageReference,
];
firstItemDestinationGroup.PackageReference.push(sourcePackageReference);
}
}
const builder = new XMLBuilder({
ignoreAttributes: false,
format: true,
suppressEmptyNode: true,
});
destinationFileContent = builder.build(destinationXml);
fs.writeFileSync(dest, destinationFileContent.replaceAll(''', "'"));
}
function syncPacketsInPackageJson(relativePathInsideProject: string) {
console.log(`syncPacketsInPackageJson '${relativePathInsideProject}'`);
const copyFrom = path.join(templateFolder, relativePathInsideProject);
const copyTo = path.join(process.cwd(), relativePathInsideProject);
doSyncPacketsInPackageJson(copyFrom, copyTo);
}
function doSyncPacketsInPackageJson(src: string, dest: string) {
if (!fs.existsSync(src) || !fs.existsSync(dest)) return;
const sourceFileContent = fs.readFileSync(src);
const destinationFileContent = fs.readFileSync(dest);
const sourceJson = JSON.parse(sourceFileContent.toString());
const destJson = JSON.parse(destinationFileContent.toString());
if (sourceJson.dependencies) {
Object.keys(sourceJson.dependencies).forEach((key) => {
try {
const value = sourceJson.dependencies[key];
const sourceVersion = semver.coerce(value)?.version;
if (sourceVersion) {
const destVersion = semver.coerce(
destJson.dependencies[key] ?? '',
)?.version;
if (!destVersion || semver.gt(sourceVersion, destVersion)) {
destJson.dependencies[key] = value;
}
}
} catch (e) {
console.error(`Key: ${key}`);
throw e;
}
});
}
if (sourceJson.devDependencies) {
Object.keys(sourceJson.devDependencies).forEach((key) => {
try {
const value = sourceJson.devDependencies[key];
const sourceVersion = semver.coerce(value)?.version;
if (sourceVersion) {
const destVersion = semver.coerce(
destJson.dependencies?.[key] ?? '',
)?.version;
if (!destVersion || semver.gt(sourceVersion, destVersion)) {
destJson.devDependencies[key] = value;
}
}
} catch (e) {
console.error(`Key: ${key}`);
throw e;
}
});
}
fs.writeFileSync(dest, JSON.stringify(destJson, undefined, 2));
}
function getPackageReferences(root: any /* csproj parsed as XML*/) {
const packageReferences: any[] = [];
let itemGroups = root.Project?.ItemGroup;
if (!itemGroups) return [];
if (!Array.isArray(itemGroups)) itemGroups = [itemGroups];
itemGroups.forEach((x: any) => {
let groupPackageReferences = x.PackageReference;
if (!groupPackageReferences) return;
if (!Array.isArray(groupPackageReferences))
groupPackageReferences = [groupPackageReferences];
packageReferences.push(...groupPackageReferences);
});
return packageReferences;
}