Run tests against 'dotnet new' output instead of Yeoman output

This commit is contained in:
Steve Sanderson
2017-08-22 16:13:34 -07:00
parent e057cb35ec
commit cd9ad38a99
4 changed files with 75 additions and 62 deletions

View File

@@ -0,0 +1,63 @@
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as mkdirp from 'mkdirp';
const templatePackageName = 'Microsoft.DotNet.Web.Spa.ProjectTemplates';
const templatePackageArtifactsDir = '../templates/package-builder/dist/artifacts';
export function generateProjectSync(targetDir: string, templateName: string) {
installTemplatePackage(targetDir, templatePackageName, templateName);
executeDotNetNewTemplateSync(targetDir, templateName);
executeCommand('npm install', /* quiet */ false, targetDir);
}
function installTemplatePackage(targetDir: string, packageName: string, templateName: string) {
// First figure out which file is the latest one for this package
const packagePaths = fs.readdirSync(templatePackageArtifactsDir)
.filter(name => name.startsWith(templatePackageName + '.'))
.filter(name => path.extname(name) === '.nupkg')
.map(name => path.join(templatePackageArtifactsDir, name))
.sort();
const latestPackagePath = packagePaths[packagePaths.length - 1];
if (!latestPackagePath) {
throw new Error(`Could not find ${packageName}.*.nupkg in directory ${templatePackageArtifactsDir}`);
}
// Uninstall any older version so we can be sure the new one did install
try {
console.log(`Uninstalling any prior version of ${packageName}...`);
executeCommand(`dotnet new --uninstall ${packageName}`, /* quiet */ true);
} catch (ex) {
// Either no prior version existed, or we failed to uninstall. We'll determine
// which it was next.
}
try {
console.log(`Verifying that no prior version of ${packageName} is still installed...`);
executeDotNetNewTemplateSync(targetDir, templateName, /* quiet */ true);
throw new Error(`Failed to uninstall template package ${packageName}. The template '${templateName}' was not removed as expected.`);
} catch (ex) {
// Looks like we successfully uninstalled it
console.log(`Confirmed that no prior version of ${templatePackageName} remains installed.`);
}
// Now install the new version
console.log(`Installing new templates package at ${latestPackagePath}...`);
executeCommand(`dotnet new --install ${latestPackagePath}`, /* quiet */ true);
}
function executeDotNetNewTemplateSync(targetDir: string, templateName: string, quiet?: boolean) {
rimraf.sync(targetDir);
mkdirp.sync(targetDir);
executeCommand(`dotnet new ${templateName}`, quiet, targetDir);
}
function executeCommand(command: string, quiet?: boolean, cwd?: string) {
childProcess.execSync(command, {
cwd,
stdio: quiet ? null : 'inherit',
encoding: 'utf8'
});
}

View File

@@ -1,52 +0,0 @@
import * as childProcess from 'child_process';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as mkdirp from 'mkdirp';
const generatorDirRelative = '../templates/package-builder/dist/generator-aspnetcore-spa';
const yoPackageDirAbsolute = path.resolve('./node_modules/yo');
export interface GeneratorOptions {
framework: string;
name: string;
tests?: boolean;
}
export function generateProjectSync(targetDir: string, generatorOptions: GeneratorOptions) {
const generatorDirAbsolute = path.resolve(generatorDirRelative);
console.log(`Running NPM install to prepare Yeoman generator at ${ generatorDirAbsolute }`);
childProcess.execSync(`npm install`, { stdio: 'inherit', cwd: generatorDirAbsolute });
console.log(`Ensuring empty output directory at ${ targetDir }`);
rimraf.sync(targetDir);
mkdirp.sync(targetDir);
const yoExecutableAbsolute = findYeomanCliScript();
console.log(`Will invoke Yeoman at ${ yoExecutableAbsolute } to generate application in ${ targetDir } with options:`);
console.log(JSON.stringify(generatorOptions, null, 2));
const command = `node "${ yoExecutableAbsolute }" "${ path.resolve(generatorDirAbsolute, './app/index.js') }"`;
const args = makeYeomanCommandLineArgs(generatorOptions);
childProcess.execSync(`${ command } ${ args }`, {
stdio: 'inherit',
cwd: targetDir
});
}
function findYeomanCliScript() {
// On Windows, you can't invoke ./node_modules/.bin/yo from the shell for some reason.
// So instead, we'll locate the CLI entrypoint that yeoman would expose if it was installed globally.
const yeomanPackageJsonPath = path.join(yoPackageDirAbsolute, './package.json');
const yeomanPackageJson = require(yeomanPackageJsonPath);
const yeomanCliScriptRelative = yeomanPackageJson.bin.yo;
if (!yeomanCliScriptRelative) {
throw new Error(`Could not find Yeoman CLI script. Looked for a bin/yo entry in ${ yeomanPackageJsonPath }`);
}
return path.join(yoPackageDirAbsolute, yeomanCliScriptRelative);
}
function makeYeomanCommandLineArgs(generatorOptions: GeneratorOptions) {
return Object.getOwnPropertyNames(generatorOptions)
.map(key => `--${ key }="${ generatorOptions[key] }"`)
.join(' ');
}