Skip to content

Commit fa1a669

Browse files
committed
Use dotnet CLI tool from NPM package
Instead of relying on powershell which is unlikely to "just work" on linux/mac, use the new dotnet CLI tool `nbgv` which will work on any operating system provided the .NET Core runtime has been installed. Closes #211
1 parent eebc00f commit fa1a669

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

src/Nerdbank.GitVersioning.Tasks/Nerdbank.GitVersioning.Tasks.csproj

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,6 @@
4646
</ItemGroup>
4747
</Target>
4848

49-
<Target Name="ExpandForNpmPackage" DependsOnTargets="GenerateNuSpec" AfterTargets="GenerateNuSpec">
50-
<PropertyGroup>
51-
<NpmPackageLayoutDir>..\nerdbank-gitversioning.npm\out\nbgv.nuget\</NpmPackageLayoutDir>
52-
</PropertyGroup>
53-
<ItemGroup>
54-
<NpmPackageLayout Include="@(_PackageFiles)">
55-
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' != '' ">$(NpmPackageLayoutDir)$([System.IO.Path]::GetDirectoryName('%(_PackageFiles.PackagePath)'))\%(FileName)%(Extension)</TargetPath>
56-
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' == '' ">$(NpmPackageLayoutDir)%(FileName)%(Extension)</TargetPath>
57-
</NpmPackageLayout>
58-
</ItemGroup>
59-
<Copy SourceFiles="@(NpmPackageLayout)" DestinationFiles="@(NpmPackageLayout->'%(TargetPath)')" />
60-
</Target>
61-
6249
<ItemGroup>
6350
<None Include="build\**">
6451
<Pack>true</Pack>

src/nbgv/nbgv.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,17 @@
2323
<ProjectReference Include="..\NerdBank.GitVersioning\NerdBank.GitVersioning.csproj" />
2424
</ItemGroup>
2525

26+
<Target Name="ExpandForNpmPackage" DependsOnTargets="GenerateNuSpec" AfterTargets="GenerateNuSpec">
27+
<PropertyGroup>
28+
<NpmPackageLayoutDir>..\nerdbank-gitversioning.npm\out\nbgv.cli\</NpmPackageLayoutDir>
29+
</PropertyGroup>
30+
<ItemGroup>
31+
<NpmPackageLayout Include="@(_PackageFiles)">
32+
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' != '' ">$(NpmPackageLayoutDir)$([System.IO.Path]::GetDirectoryName('%(_PackageFiles.PackagePath)'))\%(FileName)%(Extension)</TargetPath>
33+
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' == '' ">$(NpmPackageLayoutDir)%(FileName)%(Extension)</TargetPath>
34+
</NpmPackageLayout>
35+
</ItemGroup>
36+
<Copy SourceFiles="@(NpmPackageLayout)" DestinationFiles="@(NpmPackageLayout->'%(TargetPath)')" />
37+
</Target>
38+
2639
</Project>

src/nerdbank-gitversioning.npm/gulpfile.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ gulp.task('default', ['package'], function() {
6262
gulp.task('watch', ['tsc'], function() {
6363
return gulp.watch('**/*.ts', ['tsc']);
6464
});
65+
66+
gulp.task('test', ['tsc'], async function() {
67+
var nbgv = require('./out');
68+
var v = await nbgv.getVersion();
69+
console.log(v);
70+
});

src/nerdbank-gitversioning.npm/ts/index.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from 'path';
55
var camelCase = require('camel-case')
66
import {execAsync} from './asyncprocess';
77

8-
const nbgvPath = 'nbgv.nuget';
8+
const nbgvPath = 'nbgv.cli';
99

1010
/**
1111
* The various aspects of a version that can be calculated.
@@ -42,25 +42,16 @@ export interface IGitVersion {
4242
*/
4343
export async function getVersion(projectDirectory?: string): Promise<IGitVersion> {
4444
projectDirectory = projectDirectory || '.';
45-
var getVersionScriptPath = path.join(__dirname, nbgvPath, "tools", "Get-Version.ps1");
46-
var versionText = await execAsync(`powershell -ExecutionPolicy Bypass -Command "& '${getVersionScriptPath}' -ProjectDirectory '${projectDirectory}'"`)
45+
var getVersionScriptPath = path.join(__dirname, nbgvPath, "tools", "netcoreapp2.1", "any", "nbgv.dll");
46+
var versionText = await execAsync(`dotnet "${getVersionScriptPath}" get-version --project "${projectDirectory}" --format json`)
4747
if (versionText.stderr) {
4848
throw versionText.stderr;
4949
}
5050

51-
var varsRegEx = /^(\w+)\s*: (.+)/mg;
52-
var match;
51+
var directResult = JSON.parse(versionText.stdout);
5352
var result = {};
54-
while (match = varsRegEx.exec(versionText.stdout)) {
55-
// Do a few type casts if appropriate.
56-
let value = match[2];
57-
if (value.toUpperCase() === 'TRUE') {
58-
value = true;
59-
} else if (value.toUpperCase() === 'FALSE') {
60-
value = false;
61-
}
62-
63-
result[camelCase(match[1])] = value;
53+
for (var field in directResult) {
54+
result[camelCase(field)] = directResult[field];
6455
}
6556

6657
return <IGitVersion>result;
@@ -69,7 +60,7 @@ export async function getVersion(projectDirectory?: string): Promise<IGitVersion
6960
/**
7061
* Sets an NPM package version based on the git height and version.json.
7162
* @param packageDirectory The directory of the package about to be published.
72-
* @param srcDirectory The directory of the source code behind the package, if different than the packageDirectory.
63+
* @param srcDirectory The directory of the source code behind the package, if different than the packageDirectory.
7364
*/
7465
export async function setPackageVersion(packageDirectory?: string, srcDirectory?: string) {
7566
packageDirectory = packageDirectory || '.';
@@ -85,7 +76,7 @@ export async function setPackageVersion(packageDirectory?: string, srcDirectory?
8576
/**
8677
* Sets the package version to 0.0.0-placeholder, so as to obviously indicate
8778
* that the version isn't set in the source code version of package.json.
88-
* @param srcDirectory The directory of the source code behind the package, if different.
79+
* @param srcDirectory The directory of the source code behind the package, if different.
8980
*/
9081
export async function resetPackageVersionPlaceholder(srcDirectory?: string) {
9182
srcDirectory = srcDirectory || '.';

0 commit comments

Comments
 (0)