Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 6baa2fa

Browse files
Split out built-in templates into new package Microsoft.DotNet.Web.Spa.ProjectTemplates
1 parent 918e7ed commit 6baa2fa

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

templates/package-builder/src/build/build.ts

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,28 @@ const isWindows = /^win/.test(process.platform);
1212
const textFileExtensions = ['.gitignore', 'template_gitignore', '.config', '.cs', '.cshtml', '.csproj', '.html', '.js', '.json', '.jsx', '.md', '.nuspec', '.ts', '.tsx'];
1313
const yeomanGeneratorSource = './src/yeoman';
1414

15-
// To support the "dotnet new" templates, we want to bundle prebuilt dist dev-mode files, because "dotnet new" can't auto-run
16-
// webpack on project creation. Note that these script entries are *not* the same as the project's usual prepublish
17-
// scripts, because here we want dev-mode builds (e.g., to support HMR), not prod-mode builds.
18-
const commonTemplatePrepublishSteps = [
19-
'npm install',
20-
'node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js',
21-
'node node_modules/webpack/bin/webpack.js'
22-
];
15+
const dotNetPackages = {
16+
builtIn: 'Microsoft.DotNet.Web.Spa.ProjectTemplates',
17+
extra: 'Microsoft.AspNetCore.SpaTemplates'
18+
};
19+
2320
const commonForceInclusionRegex = /^(wwwroot|ClientApp)\/dist\//; // Files to be included in template, even though gitignored
2421

25-
const templates: { [key: string]: { dir: string, dotNetNewId: string, displayName: string, prepublish?: string[], forceInclusion?: RegExp } } = {
26-
'angular': { dir: '../../templates/AngularSpa/', dotNetNewId: 'Angular', displayName: 'Angular' },
27-
'aurelia': { dir: '../../templates/AureliaSpa/', dotNetNewId: 'Aurelia', displayName: 'Aurelia' },
28-
'knockout': { dir: '../../templates/KnockoutSpa/', dotNetNewId: 'Knockout', displayName: 'Knockout.js' },
29-
'react-redux': { dir: '../../templates/ReactReduxSpa/', dotNetNewId: 'ReactRedux', displayName: 'React.js and Redux' },
30-
'react': { dir: '../../templates/ReactSpa/', dotNetNewId: 'React', displayName: 'React.js' },
31-
'vue': { dir: '../../templates/VueSpa/', dotNetNewId: 'Vue', displayName: 'Vue.js' }
32-
};
22+
interface TemplateConfig {
23+
dir: string;
24+
dotNetNewId: string;
25+
dotNetPackageId: string;
26+
displayName: string;
27+
}
3328

29+
const templates: { [key: string]: TemplateConfig } = {
30+
'angular': { dotNetPackageId: dotNetPackages.builtIn, dir: '../../templates/AngularSpa/', dotNetNewId: 'Angular', displayName: 'Angular' },
31+
'aurelia': { dotNetPackageId: dotNetPackages.extra, dir: '../../templates/AureliaSpa/', dotNetNewId: 'Aurelia', displayName: 'Aurelia' },
32+
'knockout': { dotNetPackageId: dotNetPackages.extra, dir: '../../templates/KnockoutSpa/', dotNetNewId: 'Knockout', displayName: 'Knockout.js' },
33+
'react-redux': { dotNetPackageId: dotNetPackages.builtIn, dir: '../../templates/ReactReduxSpa/', dotNetNewId: 'ReactRedux', displayName: 'React.js and Redux' },
34+
'react': { dotNetPackageId: dotNetPackages.builtIn, dir: '../../templates/ReactSpa/', dotNetNewId: 'React', displayName: 'React.js' },
35+
'vue': { dotNetPackageId: dotNetPackages.extra, dir: '../../templates/VueSpa/', dotNetNewId: 'Vue', displayName: 'Vue.js' }
36+
};
3437

3538
function isTextFile(filename: string): boolean {
3639
return textFileExtensions.indexOf(path.extname(filename).toLowerCase()) >= 0
@@ -111,7 +114,17 @@ function buildYeomanNpmPackage(outputRoot: string) {
111114
rimraf.sync(tempRoot);
112115
}
113116

114-
function buildDotNetNewNuGetPackage() {
117+
function buildDotNetNewNuGetPackages(outputDir: string) {
118+
const dotNetPackageIds = _.values(dotNetPackages);
119+
dotNetPackageIds.forEach(packageId => {
120+
const dotNetNewNupkgPath = buildDotNetNewNuGetPackage(packageId);
121+
122+
// Move the .nupkg file to the output dir
123+
fs.renameSync(dotNetNewNupkgPath, path.join(outputDir, path.basename(dotNetNewNupkgPath)));
124+
});
125+
}
126+
127+
function buildDotNetNewNuGetPackage(packageId: string) {
115128
const outputRoot = './dist/dotnetnew';
116129
rimraf.sync(outputRoot);
117130

@@ -124,6 +137,11 @@ function buildDotNetNewNuGetPackage() {
124137
];
125138
const contentReplacements = [];
126139
_.forEach(templates, (templateConfig, templateName) => {
140+
// Only include templates matching the output package ID
141+
if (templateConfig.dotNetPackageId !== packageId) {
142+
return;
143+
}
144+
127145
const templateOutputDir = path.join(outputRoot, 'Content', templateName);
128146
writeTemplate(templateConfig.dir, templateOutputDir, contentReplacements, filenameReplacements, commonForceInclusionRegex);
129147

@@ -134,8 +152,8 @@ function buildDotNetNewNuGetPackage() {
134152
fs.writeFileSync(path.join(templateConfigDir, 'template.json'), JSON.stringify({
135153
author: 'Microsoft',
136154
classifications: ['Web', 'MVC', 'SPA'],
137-
groupIdentity: `Microsoft.AspNetCore.SpaTemplates.${templateConfig.dotNetNewId}`,
138-
identity: `Microsoft.AspNetCore.SpaTemplates.${templateConfig.dotNetNewId}.CSharp`,
155+
groupIdentity: `${packageId}.${templateConfig.dotNetNewId}`,
156+
identity: `${packageId}.${templateConfig.dotNetNewId}.CSharp`,
139157
name: `MVC ASP.NET Core with ${templateConfig.displayName}`,
140158
preferNameDirectory: true,
141159
primaryOutputs: [{ path: `${sourceProjectName}.csproj` }],
@@ -213,8 +231,11 @@ function buildDotNetNewNuGetPackage() {
213231
// Invoke NuGet to create the final package
214232
const yeomanPackageVersion = JSON.parse(fs.readFileSync(path.join(yeomanGeneratorSource, 'package.json'), 'utf8')).version;
215233
writeTemplate('./src/dotnetnew', outputRoot, [
234+
{ from: /\{packageId\}/g, to: packageId },
216235
{ from: /\{version\}/g, to: yeomanPackageVersion },
217-
], [], null);
236+
], [
237+
{ from: /.*\.nuspec$/, to: `${packageId}.nuspec` },
238+
], null);
218239
const nugetExe = path.join(process.cwd(), './bin/NuGet.exe');
219240
const nugetStartInfo = { cwd: outputRoot, stdio: 'inherit' };
220241
if (isWindows) {
@@ -233,19 +254,18 @@ function buildDotNetNewNuGetPackage() {
233254

234255
function runAllPrepublishScripts() {
235256
Object.getOwnPropertyNames(templates).forEach(templateKey => {
236-
const templateInfo = templates[templateKey];
237-
238-
// First run standard prepublish steps
239-
runScripts(templateInfo.dir, commonTemplatePrepublishSteps);
240-
241-
// Second, run any template-specific prepublish steps
242-
if (templateInfo.prepublish) {
243-
runScripts(templateInfo.dir, templateInfo.prepublish);
244-
}
257+
// To support the "dotnet new" templates, we want to bundle prebuilt dist dev-mode files, because "dotnet new" can't auto-run
258+
// webpack on project creation. Note that these script entries are *not* the same as the project's usual prepublish
259+
// scripts, because here we want dev-mode builds (e.g., to support HMR), not prod-mode builds.
260+
runPrepublishScripts(templates[templateKey].dir, [
261+
'npm install',
262+
'node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js',
263+
'node node_modules/webpack/bin/webpack.js'
264+
]);
245265
});
246266
}
247267

248-
function runScripts(rootDir: string, scripts: string[]) {
268+
function runPrepublishScripts(rootDir: string, scripts: string[]) {
249269
console.log(`[Prepublish] In directory: ${ rootDir }`);
250270
scripts.forEach(script => {
251271
console.log(`[Prepublish] Running: ${ script }`);
@@ -262,10 +282,7 @@ rimraf.sync(distDir);
262282
mkdirp.sync(artifactsDir);
263283
runAllPrepublishScripts();
264284
buildYeomanNpmPackage(yeomanOutputRoot);
265-
const dotNetNewNupkgPath = buildDotNetNewNuGetPackage();
266-
267-
// Move the .nupkg file to the artifacts dir
268-
fs.renameSync(dotNetNewNupkgPath, path.join(artifactsDir, path.basename(dotNetNewNupkgPath)));
285+
buildDotNetNewNuGetPackages(artifactsDir);
269286

270287
// Finally, create a .tar.gz file containing the built generator-aspnetcore-spa.
271288
// The CI system can treat this as the final built artifact.

templates/package-builder/src/dotnetnew/Microsoft.AspNetCore.SpaTemplates.nuspec renamed to templates/package-builder/src/dotnetnew/templatePackages.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
4-
<id>Microsoft.AspNetCore.SpaTemplates</id>
4+
<id>{packageId}</id>
55
<version>{version}</version>
66
<description>Single Page Application templates for ASP.NET Core</description>
77
<authors>Microsoft</authors>

0 commit comments

Comments
 (0)