Skip to content

Commit

Permalink
Add support for renaming artifacts (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 19, 2024
1 parent 31b0cc9 commit 6a47dd9
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 19 deletions.
10 changes: 10 additions & 0 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ A group of packages in the TOML configuration is defined by:
| `publish` | `bool` | You can disable a particular pack to be build/published.
| `rid` | `string` | The target OS + CPU by defining its runtime identifier. See [https://docs.microsoft.com/en-us/dotnet/core/rid-catalog](https://docs.microsoft.com/en-us/dotnet/core/rid-catalog) for all the possible values.
| `kinds` | `string` | Defines the kinds of package to create: `zip`, `tar`, `deb` or `rpm`.
| `renamer` | `renamer[]` | A regex renamer for the final filename. A renamer is defined by the object `{ pattern = "regex", replace = "replacement" }`. This is useful to replace a string in the package name.

For each `rid` define in a pack, it will create the packages defined by `kinds`.

Expand Down Expand Up @@ -552,6 +553,15 @@ kinds = ["zip"]

By default, all packs declared are `publish = true`.

You can also replace the generated filename by using a replacer:

```toml
[[pack]]
rid = ["win-x64"]
kinds = ["zip"]
renamer = [{ pattern = "win-x64", replace = "windows-amd64" }]
```

___
> `profile = "custom"`
Expand Down
3 changes: 2 additions & 1 deletion src/DotNetReleaser.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public async Task TestBuild()
config += @"[[pack]]
rid = ""win-x64""
kinds = [""zip""]
renamer = [ { pattern = ""win-x64"", replace = ""windows-amd64"" } ]
[[pack]]
rid = ""linux-x64""
kinds = [""tar"", ""deb""]
Expand All @@ -98,7 +99,7 @@ public async Task TestBuild()
"HelloWorld.0.1.0.linux-x64.deb",
"HelloWorld.0.1.0.linux-x64.tar.gz",
"HelloWorld.0.1.0.nupkg",
"HelloWorld.0.1.0.win-x64.zip",
"HelloWorld.0.1.0.windows-amd64.zip",
}.OrderBy(x => x).ToList();

foreach (var file in files)
Expand Down
6 changes: 6 additions & 0 deletions src/dotnet-releaser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetReleaser.Tests", "DotNetReleaser.Tests\DotNetReleaser.Tests.csproj", "{B34D6239-0F5A-429D-B710-D571CE6BE58B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{258DCAB7-E550-499B-8682-21FF31324B7E}"
ProjectSection(SolutionItems) = preProject
..\doc\changelog_user_guide.md = ..\doc\changelog_user_guide.md
..\doc\readme.md = ..\doc\readme.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-releaser/Configuration/ChangelogConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ChangelogConfiguration()
//ChangeTitleEscape = @"\<*_&@";
Owners = new List<string>();
Autolabelers = new List<ChangelogAutolabeler>();
Replacers = new List<ChangelogReplacer>();
Replacers = new List<RegexReplacer>();
Include = new ChangelogFilter();
Exclude = new ChangelogFilter();
BodyTemplate = @"# Changes
Expand Down Expand Up @@ -64,7 +64,7 @@ public ChangelogConfiguration()
public List<ChangelogAutolabeler> Autolabelers { get; }

[DataMember(Name = "replacer")]
public List<ChangelogReplacer> Replacers { get; } // TBD: not implemented yet
public List<RegexReplacer> Replacers { get; } // TBD: not implemented yet

[DataMember(Name = "category")]
public List<ChangelogCategory> Categories { get; }
Expand Down
14 changes: 0 additions & 14 deletions src/dotnet-releaser/Configuration/ChangelogReplacer.cs

This file was deleted.

5 changes: 5 additions & 0 deletions src/dotnet-releaser/Configuration/PackagingConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace DotNetReleaser.Configuration;

Expand All @@ -9,13 +10,17 @@ public PackagingConfiguration()
{
RuntimeIdentifiers = new List<string>();
Kinds = new List<PackageKind>();
Renamers = new List<RegexReplacer>();
}

[DataMember(Name = "rid")]
public List<string> RuntimeIdentifiers { get; }

[DataMember(Name = "kinds")]
public List<PackageKind> Kinds { get; }

[DataMember(Name = "renamer")]
public List<RegexReplacer> Renamers { get; }

public static string ToStringRidAndKinds(List<string> rids, List<PackageKind> kinds) => $"platform{(rids.Count > 1 ? "s" : string.Empty)} [{string.Join(", ", rids)}] with [{string.Join(", ", kinds)}] package{(kinds.Count > 1 ? "s" : string.Empty)}";

Expand Down
21 changes: 21 additions & 0 deletions src/dotnet-releaser/Configuration/RegexReplacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace DotNetReleaser.Configuration;

public class RegexReplacer
{
public RegexReplacer()
{
Pattern = string.Empty;
Replace = string.Empty;
}

[DataMember(Name = "pattern")]
public string Pattern { get; set; }

[DataMember(Name = "replace")]
public string Replace { get; set; }

internal string Run(string input) => Regex.Replace(input, Pattern, Replace);
}
31 changes: 29 additions & 2 deletions src/dotnet-releaser/ReleaserApp.AppPackaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DotNetReleaser.Configuration;
using DotNetReleaser.Helpers;
Expand Down Expand Up @@ -72,7 +73,7 @@ private async Task<List<AppPackageInfo>> BuildAppPackages(BuildInformation build
{
foreach (var rid in pack.RuntimeIdentifiers)
{
var list = await PackPlatform(packageInfo, pack.Publish, rid, pack.Kinds.ToArray());
var list = await PackPlatform(packageInfo, pack.Publish, rid, pack.Renamers.ToArray(), pack.Kinds.ToArray());
if (HasErrors) goto exitPackOnError; // break on first errors

if (list is not null && pack.Publish)
Expand Down Expand Up @@ -100,7 +101,7 @@ private async Task<List<AppPackageInfo>> BuildAppPackages(BuildInformation build
/// <summary>
/// This is the part that handles the packaging for tar, zip, deb, rpm
/// </summary>
private async Task<List<AppPackageInfo>?> PackPlatform(ProjectPackageInfo projectPackageInfo, bool publish, string rid, params PackageKind[] kinds)
private async Task<List<AppPackageInfo>?> PackPlatform(ProjectPackageInfo projectPackageInfo, bool publish, string rid, RegexReplacer[] renamers, PackageKind[] kinds)
{
var properties = new Dictionary<string, object>(_config.MSBuild.Properties)
{
Expand Down Expand Up @@ -249,6 +250,32 @@ private async Task<List<AppPackageInfo>> BuildAppPackages(BuildInformation build
path = CopyToArtifacts(path);
}

// Give a chance to rename the artifact file
var folder = Path.GetDirectoryName(path)!;
var oldFilename = Path.GetFileName(path);
var filename = oldFilename;
string? newPath = null;
try
{

foreach (var renamer in renamers)
{
filename = renamer.Run(filename);
}

if (filename != oldFilename)
{
newPath = Path.Combine(folder, filename);
File.Move(path, newPath);
path = newPath;
}
}
catch (Exception ex)
{
Error($"Error renaming file {path} to {newPath}: {ex.Message}");
break;
}

var sha256 = string.Join("", SHA256.HashData(await File.ReadAllBytesAsync(path)).Select(x => x.ToString("x2")));

var entry = new AppPackageInfo(
Expand Down

0 comments on commit 6a47dd9

Please sign in to comment.