Skip to content

Commit ac75a31

Browse files
In generator-aspnetcore-spa, add option to produce Angular 2.4.5 project (with 'experimental' caveat)
1 parent a3af5c0 commit ac75a31

File tree

2 files changed

+116
-10
lines changed

2 files changed

+116
-10
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "Angular2Spa",
3+
"version": "0.0.0",
4+
"scripts": {
5+
"test": "karma start ClientApp/test/karma.conf.js"
6+
},
7+
"dependencies": {
8+
"@angular/common": "^2.4.5",
9+
"@angular/compiler": "^2.4.5",
10+
"@angular/core": "^2.4.5",
11+
"@angular/forms": "^2.4.5",
12+
"@angular/http": "^2.4.5",
13+
"@angular/platform-browser": "^2.4.5",
14+
"@angular/platform-browser-dynamic": "^2.4.5",
15+
"@angular/platform-server": "^2.4.5",
16+
"@angular/router": "^3.0.2",
17+
"@types/node": "^6.0.42",
18+
"angular2-platform-node": "~2.0.11",
19+
"angular2-template-loader": "^0.6.0",
20+
"angular2-universal": "^2.1.0-rc.1",
21+
"angular2-universal-patch": "^0.2.1",
22+
"angular2-universal-polyfills": "^2.1.0-rc.1",
23+
"aspnet-prerendering": "^2.0.0",
24+
"aspnet-webpack": "^1.0.17",
25+
"awesome-typescript-loader": "3.0.0-beta.13 || ^3.0.0",
26+
"bootstrap": "^3.3.7",
27+
"css": "^2.2.1",
28+
"css-loader": "^0.25.0",
29+
"es6-shim": "^0.35.1",
30+
"event-source-polyfill": "^0.0.7",
31+
"expose-loader": "^0.7.1",
32+
"extract-text-webpack-plugin": "^2.0.0-rc",
33+
"file-loader": "^0.9.0",
34+
"html-loader": "^0.4.4",
35+
"isomorphic-fetch": "^2.2.1",
36+
"jquery": "^2.2.1",
37+
"json-loader": "^0.5.4",
38+
"preboot": "^4.5.2",
39+
"raw-loader": "^0.5.1",
40+
"rxjs": "^5.0.1",
41+
"style-loader": "^0.13.1",
42+
"to-string-loader": "^1.1.5",
43+
"typescript": "^2.0.3",
44+
"url-loader": "^0.5.7",
45+
"webpack": "^2.2.0",
46+
"webpack-hot-middleware": "^2.12.2",
47+
"webpack-merge": "^0.14.1",
48+
"zone.js": "^0.7.6"
49+
},
50+
"devDependencies": {
51+
"@types/chai": "^3.4.34",
52+
"@types/jasmine": "^2.5.37",
53+
"chai": "^3.5.0",
54+
"jasmine-core": "^2.5.2",
55+
"karma": "^1.3.0",
56+
"karma-chai": "^0.1.0",
57+
"karma-chrome-launcher": "^2.0.0",
58+
"karma-cli": "^1.0.1",
59+
"karma-jasmine": "^1.0.2",
60+
"karma-webpack": "^1.8.0"
61+
}
62+
}

templates/package-builder/src/yeoman/app/index.ts

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ const testSpecificNpmPackages = [
3535
type YeomanPrompt = (opt: yeoman.IPromptOptions | yeoman.IPromptOptions[], callback: (answers: any) => void) => void;
3636
const optionOrPrompt: YeomanPrompt = require('yeoman-option-or-prompt');
3737

38-
const templates = [
39-
{ value: 'angular-2', name: 'Angular 2', tests: true },
40-
{ value: 'aurelia', name: 'Aurelia', tests: false },
41-
{ value: 'knockout', name: 'Knockout', tests: false },
42-
{ value: 'react', name: 'React', tests: false },
43-
{ value: 'react-redux', name: 'React with Redux', tests: false }
38+
interface TemplateConfig {
39+
value: string; // Internal unique ID for Yeoman prompt
40+
rootDir: string; // Which of the template root directories should be used
41+
name: string; // Display name
42+
tests: boolean;
43+
mapFilenames?: { [pattern: string]: string | boolean };
44+
}
45+
46+
const templates: TemplateConfig[] = [
47+
{ value: 'angular-2', rootDir: 'angular-2', name: 'Angular 2.0.2', tests: true, mapFilenames: { '^package\\-[\\d\\.]+.json$': false } },
48+
{ value: 'angular-245', rootDir: 'angular-2', name: 'Angular 2.4.5 (experimental)', tests: true, mapFilenames: { '^package.json$': false, '^package\\-2\\.4\\.5.json$': 'package.json' } },
49+
{ value: 'aurelia', rootDir: 'aurelia', name: 'Aurelia', tests: false },
50+
{ value: 'knockout', rootDir: 'knockout', name: 'Knockout', tests: false },
51+
{ value: 'react', rootDir: 'react', name: 'React', tests: false },
52+
{ value: 'react-redux', rootDir: 'react-redux', name: 'React with Redux', tests: false }
4453
];
4554

4655
// Once everyone is on .csproj-compatible tooling, we might be able to remove the global.json files and eliminate
@@ -88,15 +97,15 @@ class MyGenerator extends yeoman.Base {
8897
message: 'What type of project do you want to create?',
8998
choices: sdkChoices
9099
}], firstAnswers => {
91-
const frameworkChoice = templates.filter(t => t.value === firstAnswers.framework)[0];
100+
const templateConfig = templates.filter(t => t.value === firstAnswers.framework)[0];
92101
const furtherQuestions = [{
93102
type: 'input',
94103
name: 'name',
95104
message: 'Your project name',
96105
default: this.appname
97106
}];
98107

99-
if (frameworkChoice.tests) {
108+
if (templateConfig.tests) {
100109
furtherQuestions.unshift({
101110
type: 'confirm',
102111
name: 'tests',
@@ -109,6 +118,7 @@ class MyGenerator extends yeoman.Base {
109118
answers.framework = firstAnswers.framework;
110119
this._answers = answers;
111120
this._answers.framework = firstAnswers.framework;
121+
this._answers.templateConfig = templateConfig;
112122
this._answers.sdkVersion = firstAnswers.sdkVersion;
113123
this._answers.namePascalCase = toPascalCase(answers.name);
114124
this._answers.projectGuid = this.options['projectguid'] || uuid.v4();
@@ -122,7 +132,8 @@ class MyGenerator extends yeoman.Base {
122132
}
123133

124134
writing() {
125-
const templateRoot = this.templatePath(this._answers.framework);
135+
const templateConfig = this._answers.templateConfig as TemplateConfig;
136+
const templateRoot = this.templatePath(templateConfig.rootDir);
126137
const chosenSdk = sdkChoices.filter(sdk => sdk.value === this._answers.sdkVersion)[0];
127138
glob.sync('**/*', { cwd: templateRoot, dot: true, nodir: true }).forEach(fn => {
128139
// Token replacement in filenames
@@ -133,12 +144,22 @@ class MyGenerator extends yeoman.Base {
133144
outputFn = path.join(path.dirname(fn), '.gitignore');
134145
}
135146

147+
// Perform any filename replacements configured for the template
148+
const mappedFilename = applyFirstMatchingReplacement(outputFn, templateConfig.mapFilenames);
149+
let fileIsExcludedByTemplateConfig = false;
150+
if (typeof mappedFilename === 'string') {
151+
outputFn = mappedFilename;
152+
} else {
153+
fileIsExcludedByTemplateConfig = true;
154+
}
155+
136156
// Decide whether to emit this file
137157
const isTestSpecificFile = testSpecificPaths.some(regex => regex.test(outputFn));
138158
const isSdkSpecificFile = sdkChoices.some(sdk => sdk.includeFiles.some(regex => regex.test(outputFn)));
139159
const matchesChosenSdk = chosenSdk.includeFiles.some(regex => regex.test(outputFn));
140160
const emitFile = (matchesChosenSdk || !isSdkSpecificFile)
141-
&& (this._answers.tests || !isTestSpecificFile);
161+
&& (this._answers.tests || !isTestSpecificFile)
162+
&& !fileIsExcludedByTemplateConfig;
142163

143164
if (emitFile) {
144165
let inputFullPath = path.join(templateRoot, fn);
@@ -247,5 +268,28 @@ function rewritePackageJson(contents, includeTests) {
247268
return contents;
248269
}
249270

271+
function applyFirstMatchingReplacement(inputValue: string, replacements: { [pattern: string]: string | boolean }): string | boolean {
272+
if (replacements) {
273+
const replacementPatterns = Object.getOwnPropertyNames(replacements);
274+
for (let patternIndex = 0; patternIndex < replacementPatterns.length; patternIndex++) {
275+
const pattern = replacementPatterns[patternIndex];
276+
const regexp = new RegExp(pattern);
277+
if (regexp.test(inputValue)) {
278+
const replacement = replacements[pattern];
279+
280+
// To avoid bug-prone evaluation order dependencies, we only respond to the first name match per file
281+
if (typeof (replacement) === 'boolean') {
282+
return replacement;
283+
} else {
284+
return inputValue.replace(regexp, replacement);
285+
}
286+
}
287+
}
288+
}
289+
290+
// No match
291+
return inputValue;
292+
}
293+
250294
declare var module: any;
251295
(module).exports = MyGenerator;

0 commit comments

Comments
 (0)