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

Commit 2135d77

Browse files
generator-aspnetcore-spa support for selecting between project.json and .csproj
1 parent 62b6761 commit 2135d77

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

templates/package-builder/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"author": "Microsoft",
1111
"license": "Apache-2.0",
1212
"dependencies": {
13+
"@types/chalk": "^0.4.31",
1314
"@types/semver": "^5.3.30",
1415
"diff": "^2.2.2",
1516
"gitignore-parser": "0.0.2",

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as rimraf from 'rimraf';
88
import * as childProcess from 'child_process';
99

1010
const isWindows = /^win/.test(process.platform);
11-
const textFileExtensions = ['.gitignore', 'template_gitignore', '.config', '.cs', '.cshtml', 'Dockerfile', '.html', '.js', '.json', '.jsx', '.md', '.nuspec', '.ts', '.tsx', '.xproj'];
11+
const textFileExtensions = ['.gitignore', 'template_gitignore', '.config', '.cs', '.cshtml', '.csproj', 'Dockerfile', '.html', '.js', '.json', '.jsx', '.md', '.nuspec', '.ts', '.tsx', '.xproj'];
1212
const yeomanGeneratorSource = './src/yeoman';
1313

1414
// For the Angular 2 template, we want to bundle prebuilt dist dev-mode files, because the VS template can't auto-run
@@ -88,14 +88,19 @@ function buildYeomanNpmPackage() {
8888

8989
// Copy template files
9090
const filenameReplacements = [
91-
{ from: /.*\.xproj$/, to: 'tokenreplace-namePascalCase.xproj' }
91+
{ from: /.*\.xproj$/, to: 'tokenreplace-namePascalCase.xproj' },
92+
{ from: /.*\.csproj$/, to: 'tokenreplace-namePascalCase.csproj' }
9293
];
9394
const contentReplacements = [
95+
// .xproj items
9496
{ from: /\bWebApplicationBasic\b/g, to: '<%= namePascalCase %>' },
9597
{ from: /<ProjectGuid>[0-9a-f\-]{36}<\/ProjectGuid>/g, to: '<ProjectGuid><%= projectGuid %></ProjectGuid>' },
9698
{ from: /<RootNamespace>.*?<\/RootNamespace>/g, to: '<RootNamespace><%= namePascalCase %></RootNamespace>'},
9799
{ from: /\s*<BaseIntermediateOutputPath.*?<\/BaseIntermediateOutputPath>/g, to: '' },
98100
{ from: /\s*<OutputPath.*?<\/OutputPath>/g, to: '' },
101+
102+
// global.json items
103+
{ from: /1\.0\.0-preview2-1-003177/, to: '<%= sdkVersion %>' }
99104
];
100105
_.forEach(templates, (templateConfig, templateName) => {
101106
const outputDir = path.join(outputTemplatesRoot, templateName);
@@ -119,6 +124,7 @@ function buildDotNetNewNuGetPackage() {
119124
const sourceProjectName = 'WebApplicationBasic';
120125
const projectGuid = '00000000-0000-0000-0000-000000000000';
121126
const filenameReplacements = [
127+
// TODO: For dotnetnew templates, switch to csproj. No need for SDK choice as it can be Preview3+ only.
122128
{ from: /.*\.xproj$/, to: `${sourceProjectName}.xproj` },
123129
{ from: /\btemplate_gitignore$/, to: '.gitignore' }
124130
];

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

+34-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as yeoman from 'yeoman-generator';
44
import * as uuid from 'node-uuid';
55
import * as glob from 'glob';
66
import * as semver from 'semver';
7+
import * as chalk from 'chalk';
78
import { execSync } from 'child_process';
89
import npmWhich = require('npm-which');
910
const yosay = require('yosay');
@@ -42,6 +43,20 @@ const templates = [
4243
{ value: 'react-redux', name: 'React with Redux', tests: false }
4344
];
4445

46+
// Once everyone is on .csproj-compatible tooling, we might be able to remove the global.json files and eliminate
47+
// this SDK choice altogether. That would be good because then it would work with whatever SDK version you have
48+
// installed. For now, we need to specify an SDK version explicitly, because there's no support for wildcards, and
49+
// preview3+ tooling doesn't support project.json at all.
50+
const sdkChoices = [{
51+
value: '1.0.0-preview2-1-003177', // Current released version
52+
name: 'project.json' + chalk.gray(' (compatible with .NET Core tooling preview 2 and Visual Studio 2015)'),
53+
includeFiles: [/^project.json$/, /\.xproj$/]
54+
}, {
55+
value: '1.0.0-preview3-004056', // Version that ships with VS2017RC
56+
name: '.csproj' + chalk.gray(' (compatible with .NET Core tooling preview 3 and Visual Studio 2017)'),
57+
includeFiles: [/\.csproj$/]
58+
}];
59+
4560
class MyGenerator extends yeoman.Base {
4661
private _answers: any;
4762
private _optionOrPrompt: YeomanPrompt;
@@ -65,8 +80,13 @@ class MyGenerator extends yeoman.Base {
6580
name: 'framework',
6681
message: 'Framework',
6782
choices: templates
68-
}], frameworkAnswer => {
69-
const frameworkChoice = templates.filter(t => t.value === frameworkAnswer.framework)[0];
83+
}, {
84+
type: 'list',
85+
name: 'sdkVersion',
86+
message: 'What type of project do you want to create?',
87+
choices: sdkChoices
88+
}], firstAnswers => {
89+
const frameworkChoice = templates.filter(t => t.value === firstAnswers.framework)[0];
7090
const furtherQuestions = [{
7191
type: 'input',
7292
name: 'name',
@@ -84,9 +104,10 @@ class MyGenerator extends yeoman.Base {
84104
}
85105

86106
this._optionOrPrompt(furtherQuestions, answers => {
87-
answers.framework = frameworkAnswer.framework;
107+
answers.framework = firstAnswers.framework;
88108
this._answers = answers;
89-
this._answers.framework = frameworkAnswer.framework;
109+
this._answers.framework = firstAnswers.framework;
110+
this._answers.sdkVersion = firstAnswers.sdkVersion;
90111
this._answers.namePascalCase = toPascalCase(answers.name);
91112
this._answers.projectGuid = this.options['projectguid'] || uuid.v4();
92113
done();
@@ -95,7 +116,8 @@ class MyGenerator extends yeoman.Base {
95116
}
96117

97118
writing() {
98-
var templateRoot = this.templatePath(this._answers.framework);
119+
const templateRoot = this.templatePath(this._answers.framework);
120+
const chosenSdk = sdkChoices.filter(sdk => sdk.value === this._answers.sdkVersion)[0];
99121
glob.sync('**/*', { cwd: templateRoot, dot: true, nodir: true }).forEach(fn => {
100122
// Token replacement in filenames
101123
let outputFn = fn.replace(/tokenreplace\-([^\.\/]*)/g, (substr, token) => this._answers[token]);
@@ -105,9 +127,14 @@ class MyGenerator extends yeoman.Base {
105127
outputFn = path.join(path.dirname(fn), '.gitignore');
106128
}
107129

108-
// Exclude test-specific files (unless the user has said they want tests)
130+
// Decide whether to emit this file
109131
const isTestSpecificFile = testSpecificPaths.some(regex => regex.test(outputFn));
110-
if (this._answers.tests || !isTestSpecificFile) {
132+
const isSdkSpecificFile = sdkChoices.some(sdk => sdk.includeFiles.some(regex => regex.test(outputFn)));
133+
const matchesChosenSdk = chosenSdk.includeFiles.some(regex => regex.test(outputFn));
134+
const emitFile = (matchesChosenSdk || !isSdkSpecificFile)
135+
&& (this._answers.tests || !isTestSpecificFile);
136+
137+
if (emitFile) {
111138
let inputFullPath = path.join(templateRoot, fn);
112139
let destinationFullPath = this.destinationPath(outputFn);
113140
if (path.basename(fn) === 'package.json') {

templates/package-builder/src/yeoman/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"url": "https://github.com/aspnet/JavaScriptServices.git"
1919
},
2020
"dependencies": {
21+
"chalk": "^1.1.3",
2122
"glob": "^7.0.3",
2223
"node-uuid": "^1.4.7",
2324
"npm-which": "^3.0.1",

0 commit comments

Comments
 (0)