From 04dba021d17d57d97772b775d5b33b3adc87a008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kurai=20Andr=C3=A1s?= Date: Thu, 8 Apr 2021 00:44:37 +0200 Subject: [PATCH] Get version from package.json --- .vscode/spellright.dict | 1 + CsprojToAsmdef.Build/Build.cs | 18 ++++++++++++------ CsprojToAsmdef.Build/PackageJson.cs | 6 ++++++ .../Assets/CsprojToAsmdef/CHANGELOG.md | 3 +++ .../Assets/CsprojToAsmdef/package.json | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 CsprojToAsmdef.Build/PackageJson.cs diff --git a/.vscode/spellright.dict b/.vscode/spellright.dict index 1065db0..23e316c 100644 --- a/.vscode/spellright.dict +++ b/.vscode/spellright.dict @@ -1 +1,2 @@ asmdef +package.json diff --git a/CsprojToAsmdef.Build/Build.cs b/CsprojToAsmdef.Build/Build.cs index 97cd148..a310049 100644 --- a/CsprojToAsmdef.Build/Build.cs +++ b/CsprojToAsmdef.Build/Build.cs @@ -4,9 +4,10 @@ using Nuke.Common.ProjectModel; using Nuke.Common.Tools.DotNet; using System; +using System.IO; using System.Linq; +using System.Text.Json; using static Nuke.Common.Tools.DotNet.DotNetTasks; -using static Nuke.Common.Tools.Git.GitTasks; [CheckBuildProjectConfigurations] [ShutdownDotNetAfterServerBuild] @@ -21,15 +22,20 @@ partial class Build : NukeBuild [Solution] readonly Solution Solution = default!; - static string CurrentVersion + string CurrentVersion { get { - var versionText = Git("describe --tags --always").First().Text; + var packagePath = Path.Combine(Solution.Directory, "CsprojToAsmdef", "Assets", "CsprojToAsmdef", "package.json"); - return Version.TryParse(versionText, out var version) - ? version.ToString() - : "0.1.0"; + if (!File.Exists(packagePath)) throw new InvalidOperationException($"package.json does not exist at path: {packagePath}"); + + var jsonContent = File.ReadAllText(packagePath); + var package = JsonSerializer.Deserialize(jsonContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + + if (package?.Version is null) throw new InvalidOperationException($"Cloud not deserialize package.json:\n{jsonContent}"); + + return package.Version; } } diff --git a/CsprojToAsmdef.Build/PackageJson.cs b/CsprojToAsmdef.Build/PackageJson.cs new file mode 100644 index 0000000..904fc25 --- /dev/null +++ b/CsprojToAsmdef.Build/PackageJson.cs @@ -0,0 +1,6 @@ +sealed class PackageJson +{ + public PackageJson(string version) => Version = version; + + public string Version { get; } +} diff --git a/CsprojToAsmdef/Assets/CsprojToAsmdef/CHANGELOG.md b/CsprojToAsmdef/Assets/CsprojToAsmdef/CHANGELOG.md index d3bced4..2399549 100644 --- a/CsprojToAsmdef/Assets/CsprojToAsmdef/CHANGELOG.md +++ b/CsprojToAsmdef/Assets/CsprojToAsmdef/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.3.6 +- Get version during build from package.json + # 0.3.5 - Drop GitVersion diff --git a/CsprojToAsmdef/Assets/CsprojToAsmdef/package.json b/CsprojToAsmdef/Assets/CsprojToAsmdef/package.json index bfd0be3..443bbb9 100644 --- a/CsprojToAsmdef/Assets/CsprojToAsmdef/package.json +++ b/CsprojToAsmdef/Assets/CsprojToAsmdef/package.json @@ -1,6 +1,6 @@ { "name": "com.kuraiandras.csprojtoasmdef", - "version": "0.3.5", + "version": "0.3.6", "displayName": "Csproj To Asmdef", "unity": "2019.2", "author": {