Skip to content

Commit

Permalink
Merge pull request #212 from AArnott/fix211
Browse files Browse the repository at this point in the history
Get NPM package working on mac/linux
  • Loading branch information
AArnott authored Aug 23, 2018
2 parents 166568d + fa1a669 commit 3c393ac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,6 @@
</ItemGroup>
</Target>

<Target Name="ExpandForNpmPackage" DependsOnTargets="GenerateNuSpec" AfterTargets="GenerateNuSpec">
<PropertyGroup>
<NpmPackageLayoutDir>..\nerdbank-gitversioning.npm\out\nbgv.nuget\</NpmPackageLayoutDir>
</PropertyGroup>
<ItemGroup>
<NpmPackageLayout Include="@(_PackageFiles)">
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' != '' ">$(NpmPackageLayoutDir)$([System.IO.Path]::GetDirectoryName('%(_PackageFiles.PackagePath)'))\%(FileName)%(Extension)</TargetPath>
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' == '' ">$(NpmPackageLayoutDir)%(FileName)%(Extension)</TargetPath>
</NpmPackageLayout>
</ItemGroup>
<Copy SourceFiles="@(NpmPackageLayout)" DestinationFiles="@(NpmPackageLayout->'%(TargetPath)')" />
</Target>

<ItemGroup>
<None Include="build\**">
<Pack>true</Pack>
Expand Down
6 changes: 5 additions & 1 deletion src/nbgv/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ private static ExitCodes OnGetVersionCommand(string projectPath, string format,
Console.WriteLine("NPM package Version: {0}", oracle.NpmPackageVersion);
break;
case "json":
Console.WriteLine(JsonConvert.SerializeObject(oracle, Formatting.Indented));
var converters = new JsonConverter[]
{
new Newtonsoft.Json.Converters.VersionConverter(),
};
Console.WriteLine(JsonConvert.SerializeObject(oracle, Formatting.Indented, converters));
break;
default:
Console.Error.WriteLine("Unsupported format: {0}", format);
Expand Down
13 changes: 13 additions & 0 deletions src/nbgv/nbgv.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@
<ProjectReference Include="..\NerdBank.GitVersioning\NerdBank.GitVersioning.csproj" />
</ItemGroup>

<Target Name="ExpandForNpmPackage" DependsOnTargets="GenerateNuSpec" AfterTargets="GenerateNuSpec">
<PropertyGroup>
<NpmPackageLayoutDir>..\nerdbank-gitversioning.npm\out\nbgv.cli\</NpmPackageLayoutDir>
</PropertyGroup>
<ItemGroup>
<NpmPackageLayout Include="@(_PackageFiles)">
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' != '' ">$(NpmPackageLayoutDir)$([System.IO.Path]::GetDirectoryName('%(_PackageFiles.PackagePath)'))\%(FileName)%(Extension)</TargetPath>
<TargetPath Condition=" '%(_PackageFiles.PackagePath)' == '' ">$(NpmPackageLayoutDir)%(FileName)%(Extension)</TargetPath>
</NpmPackageLayout>
</ItemGroup>
<Copy SourceFiles="@(NpmPackageLayout)" DestinationFiles="@(NpmPackageLayout->'%(TargetPath)')" />
</Target>

</Project>
6 changes: 6 additions & 0 deletions src/nerdbank-gitversioning.npm/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ gulp.task('default', ['package'], function() {
gulp.task('watch', ['tsc'], function() {
return gulp.watch('**/*.ts', ['tsc']);
});

gulp.task('test', ['tsc'], async function() {
var nbgv = require('./out');
var v = await nbgv.getVersion();
console.log(v);
});
25 changes: 8 additions & 17 deletions src/nerdbank-gitversioning.npm/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';
var camelCase = require('camel-case')
import {execAsync} from './asyncprocess';

const nbgvPath = 'nbgv.nuget';
const nbgvPath = 'nbgv.cli';

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

var varsRegEx = /^(\w+)\s*: (.+)/mg;
var match;
var directResult = JSON.parse(versionText.stdout);
var result = {};
while (match = varsRegEx.exec(versionText.stdout)) {
// Do a few type casts if appropriate.
let value = match[2];
if (value.toUpperCase() === 'TRUE') {
value = true;
} else if (value.toUpperCase() === 'FALSE') {
value = false;
}

result[camelCase(match[1])] = value;
for (var field in directResult) {
result[camelCase(field)] = directResult[field];
}

return <IGitVersion>result;
Expand All @@ -69,7 +60,7 @@ export async function getVersion(projectDirectory?: string): Promise<IGitVersion
/**
* Sets an NPM package version based on the git height and version.json.
* @param packageDirectory The directory of the package about to be published.
* @param srcDirectory The directory of the source code behind the package, if different than the packageDirectory.
* @param srcDirectory The directory of the source code behind the package, if different than the packageDirectory.
*/
export async function setPackageVersion(packageDirectory?: string, srcDirectory?: string) {
packageDirectory = packageDirectory || '.';
Expand All @@ -85,7 +76,7 @@ export async function setPackageVersion(packageDirectory?: string, srcDirectory?
/**
* Sets the package version to 0.0.0-placeholder, so as to obviously indicate
* that the version isn't set in the source code version of package.json.
* @param srcDirectory The directory of the source code behind the package, if different.
* @param srcDirectory The directory of the source code behind the package, if different.
*/
export async function resetPackageVersionPlaceholder(srcDirectory?: string) {
srcDirectory = srcDirectory || '.';
Expand Down

0 comments on commit 3c393ac

Please sign in to comment.