From dc79631e183db2b789126e18f7df2cf116c2e186 Mon Sep 17 00:00:00 2001 From: Robert Coltheart <13191652+robertcoltheart@users.noreply.github.com> Date: Thu, 12 Aug 2021 13:33:28 +0800 Subject: [PATCH] Fix packaging for Rider (#175) * Fix packaging for Rider * tidy --- build/build.cs | 271 ++++++++++++++++++++++++--------------------- build/build.csproj | 2 +- 2 files changed, 143 insertions(+), 130 deletions(-) diff --git a/build/build.cs b/build/build.cs index d2e3f034..9139e05a 100644 --- a/build/build.cs +++ b/build/build.cs @@ -3,6 +3,7 @@ using System.IO.Compression; using System.Linq; using System.Net.Http; +using System.Text; using System.Text.Json; using System.Xml.Linq; using System.Xml.XPath; @@ -10,180 +11,182 @@ using static Bullseye.Targets; using static SimpleExec.Command; -public class build +var version = GetGitVersion(); +var waveVersion = GetWaveVersion(); +var notes = GetReleaseNotes(); +var apiKey = Environment.GetEnvironmentVariable("JETBRAINS_API_KEY"); + +Target("clean", () => { - public static void Main(string[] args) + Run("dotnet", "clean"); + + if (Directory.Exists("artifacts")) { - var version = GetGitVersion(); - var waveVersion = GetWaveVersion(); - var notes = GetReleaseNotes(); - var apiKey = Environment.GetEnvironmentVariable("JETBRAINS_API_KEY"); + Directory.Delete("artifacts", true); + } +}); - Target("clean", () => - { - Run("dotnet", "clean"); +Target("restore", DependsOn("clean"), () => +{ + Run("dotnet", "restore"); +}); - if (Directory.Exists("artifacts")) - { - Directory.Delete("artifacts", true); - } - }); +Target("build", DependsOn("restore"), () => +{ + Run("dotnet", "build " + + "--no-restore " + + "--configuration Release " + + " /p:HostFullIdentifier= " + + $"/p:Version={version.SemVer} " + + $"/p:AssemblyVersion={version.AssemblySemVer} " + + $"/p:FileVersion={version.AssemblySemFileVer} " + + $"/p:InformationalVersion={version.InformationalVersion}"); +}); + +Target("test", DependsOn("build"), () => +{ + Run("dotnet", "test --configuration Release --no-restore --no-build"); +}); - Target("restore", DependsOn("clean"), () => - { - Run("dotnet", "restore"); - }); +Target("package", DependsOn("build", "test"), () => +{ + Run("dotnet", $"pack --configuration Release --no-restore --no-build --output artifacts /p:Version={version.SemVer}"); +}); - Target("build", DependsOn("restore"), () => - { - Run("dotnet", "build " + - "--no-restore " + - "--configuration Release " + - " /p:HostFullIdentifier= " + - $"/p:Version={version.SemVer} " + - $"/p:AssemblyVersion={version.AssemblySemVer} " + - $"/p:FileVersion={version.AssemblySemFileVer} " + - $"/p:InformationalVersion={version.InformationalVersion}"); - }); - - Target("test", DependsOn("build"), () => - { - Run("dotnet", "test --configuration Release --no-restore --no-build"); - }); +Target("zip", DependsOn("package"), () => +{ + var artifactPath = Path.Combine("artifacts", "machine-specifications"); + var dotnetPath = Path.Combine(artifactPath, "dotnet"); + var libPath = Path.Combine(artifactPath, "lib"); + var metaPath = Path.Combine(libPath, "META-INF"); + var jarPath = Path.Combine(libPath, $"machine-specifications-{version.SemVer}.jar"); + var zipPath = Path.Combine("artifacts", $"machine-specifications-{version.SemVer}.zip"); - Target("package", DependsOn("build", "test"), () => - { - Run("dotnet", $"pack --configuration Release --no-restore --no-build --output artifacts /p:Version={version.SemVer}"); - }); + Directory.CreateDirectory(dotnetPath); + Directory.CreateDirectory(metaPath); - Target("zip", DependsOn("package"), () => - { - var artifactPath = Path.Combine("artifacts", "machine-specifications"); - var metaPath = Path.Combine(artifactPath, "META-INF"); - var archivePath = Path.Combine("artifacts", $"machine-specifications-{version.SemVer}.zip" ); + var nupkg = Glob.Files("artifacts", "*.nupkg").First(); - Directory.CreateDirectory(metaPath); + using var archive = ZipFile.OpenRead(Path.Combine("artifacts", nupkg)); - var plugin = File.ReadAllText("plugin.xml") - .Replace("${Version}", version.SemVer) - .Replace("${SinceBuild}", waveVersion + ".0") - .Replace("${UntilBuild}", waveVersion + ".*") - .Replace("${ChangeNotes}", notes); + foreach (var entry in archive.Entries.Where(x => x.FullName.Contains("DotFiles"))) + { + entry.ExtractToFile(Path.Combine(dotnetPath, entry.Name)); + } - File.WriteAllText(Path.Combine(metaPath, "plugin.xml"), plugin); + var plugin = File.ReadAllText("plugin.xml") + .Replace("${Version}", version.SemVer) + .Replace("${SinceBuild}", waveVersion + ".0") + .Replace("${UntilBuild}", waveVersion + ".*") + .Replace("${ChangeNotes}", notes); - var packages = Glob.Files("artifacts", "**/*.nupkg").ToArray(); + File.WriteAllText(Path.Combine(metaPath, "plugin.xml"), plugin); + File.WriteAllText(Path.Combine(metaPath, "MANIFEST.MF"), "Manifest-Version: 1.0"); - foreach (var package in packages) - { - var source = Path.Combine("artifacts", package); - var target = Path.Combine(artifactPath, Path.GetFileName(package)); + ZipFile.CreateFromDirectory(metaPath, jarPath, CompressionLevel.Optimal, true, new UnixUTF8Encoding()); - File.Copy(source, target); - } + Directory.Delete(metaPath, true); - ZipFile.CreateFromDirectory(artifactPath, archivePath, CompressionLevel.Optimal, true); + ZipFile.CreateFromDirectory(artifactPath, zipPath, CompressionLevel.Optimal, true, new UnixUTF8Encoding()); - Console.WriteLine($"Created {archivePath}"); - }); + Console.WriteLine($"Created {zipPath}"); +}); - Target("publish-nuget", DependsOn("package"), () => - { - var githubToken = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); +Target("publish-nuget", DependsOn("package"), () => +{ + var githubToken = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); - var packages = Glob.Files("artifacts", "*.nupkg") - .ToArray(); + var packages = Glob.Files("artifacts", "*.nupkg") + .ToArray(); - Run("dotnet", $"nuget add source https://nuget.pkg.github.com/machine/index.json -n github -u machine -p {githubToken} --store-password-in-clear-text"); + Run("dotnet", $"nuget add source https://nuget.pkg.github.com/machine/index.json -n github -u machine -p {githubToken} --store-password-in-clear-text"); - foreach (var package in packages) - { - var path = Path.Combine("artifacts", package); + foreach (var package in packages) + { + var path = Path.Combine("artifacts", package); - Run("dotnet", $"nuget push {path} --source https://plugins.jetbrains.com/ --api-key {apiKey}"); - Run("dotnet", $"nuget push {path} --source github"); + Run("dotnet", $"nuget push {path} --source https://plugins.jetbrains.com/ --api-key {apiKey}"); + Run("dotnet", $"nuget push {path} --source github"); - Console.WriteLine($"Published plugin {package} to JetBrains hub"); - } - }); + Console.WriteLine($"Published plugin {package} to JetBrains hub"); + } +}); - Target("publish-zip", DependsOn("zip"), () => - { - using var client = new HttpClient(); +Target("publish-zip", DependsOn("zip"), () => +{ + using var client = new HttpClient(); - client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); + client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); - var plugins = Glob.Files("artifacts", "*.zip"); + var plugins = Glob.Files("artifacts", "*.zip"); - foreach (var file in plugins) - { - var filename = Path.GetFileName(file); + foreach (var file in plugins) + { + var filename = Path.GetFileName(file); - var content = new MultipartFormDataContent - { - { new StringContent("com.intellij.resharper.machine.specifications"), "xmlId" }, - { new ByteArrayContent(File.ReadAllBytes(Path.Combine("artifacts", file))), "file", filename }, - { new StringContent(notes), "notes" } - }; + var content = new MultipartFormDataContent + { + { new StringContent("com.intellij.resharper.machine.specifications"), "xmlId" }, + { new ByteArrayContent(File.ReadAllBytes(Path.Combine("artifacts", file))), "file", filename }, + { new StringContent(notes), "notes" } + }; - if (!string.IsNullOrEmpty(version.PreReleaseTag)) - { - content.Add(new StringContent("channel"), "Beta"); - } + if (!string.IsNullOrEmpty(version.PreReleaseTag)) + { + content.Add(new StringContent("channel"), "Beta"); + } - var response = client.PostAsync("https://plugins.jetbrains.com/plugin/uploadPlugin", content); - response.Wait(TimeSpan.FromMinutes(10)); - response.Result.EnsureSuccessStatusCode(); + var response = client.PostAsync("https://plugins.jetbrains.com/plugin/uploadPlugin", content); + response.Wait(TimeSpan.FromMinutes(10)); + response.Result.EnsureSuccessStatusCode(); - Console.WriteLine($"Published plugin {filename} to JetBrains hub"); - } - }); + Console.WriteLine($"Published plugin {filename} to JetBrains hub"); + } +}); - Target("publish", DependsOn("publish-nuget", "publish-zip")); +Target("publish", DependsOn("publish-nuget", "publish-zip")); - Target("default", DependsOn("zip")); +Target("default", DependsOn("zip")); - RunTargetsAndExit(args); - } +RunTargetsAndExit(args); - private static GitVersion GetGitVersion() - { - Run("dotnet", "tool restore"); +GitVersion GetGitVersion() +{ + Run("dotnet", "tool restore"); - var value = Read("dotnet", "dotnet-gitversion"); + var value = Read("dotnet", "dotnet-gitversion"); - return JsonSerializer.Deserialize(value); - } + return JsonSerializer.Deserialize(value); +} - private static string GetWaveVersion() - { - var value = GetXmlValue("SdkVersion"); +string GetWaveVersion() +{ + var value = GetXmlValue("SdkVersion"); - return $"{value.Substring(2,2)}{value.Substring(5,1)}"; - } + return $"{value.Substring(2,2)}{value.Substring(5,1)}"; +} - private static string GetReleaseNotes() - { - return GetXmlValue("PackageReleaseNotes"); - } +string GetReleaseNotes() +{ + return GetXmlValue("PackageReleaseNotes"); +} + +string GetXmlValue(string name) +{ + var projects = Glob.Files(Environment.CurrentDirectory, "src/**/*.csproj"); - private static string GetXmlValue(string name) + foreach (var project in projects) { - var projects = Glob.Files(Environment.CurrentDirectory, "src/**/*.csproj"); + var document = XDocument.Load(project); + var node = document.XPathSelectElement($"/Project/PropertyGroup/{name}"); - foreach (var project in projects) + if (!string.IsNullOrEmpty(node.Value)) { - var document = XDocument.Load(project); - var node = document.XPathSelectElement($"/Project/PropertyGroup/{name}"); - - if (!string.IsNullOrEmpty(node.Value)) - { - return node.Value; - } + return node.Value; } - - return string.Empty; } + + return string.Empty; } public class GitVersion @@ -198,3 +201,13 @@ public class GitVersion public string PreReleaseTag { get; set; } } + +public class UnixUTF8Encoding : UTF8Encoding +{ + public override byte[] GetBytes(string s) + { + s = s.Replace("\\", "/"); + + return base.GetBytes(s); + } +} diff --git a/build/build.csproj b/build/build.csproj index 8c46a175..1b94dc51 100644 --- a/build/build.csproj +++ b/build/build.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net5.0