Skip to content

Commit

Permalink
Fix packaging for Rider (#175)
Browse files Browse the repository at this point in the history
* Fix packaging for Rider

* tidy
  • Loading branch information
robertcoltheart authored Aug 12, 2021
1 parent 5bf967a commit dc79631
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 130 deletions.
271 changes: 142 additions & 129 deletions build/build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,187 +3,190 @@
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;
using GlobExpressions;
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<GitVersion>(value);
}
return JsonSerializer.Deserialize<GitVersion>(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
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion build/build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit dc79631

Please sign in to comment.