Skip to content

Commit 6a47dd9

Browse files
committed
Add support for renaming artifacts (#69)
1 parent 31b0cc9 commit 6a47dd9

8 files changed

+75
-19
lines changed

doc/readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ A group of packages in the TOML configuration is defined by:
506506
| `publish` | `bool` | You can disable a particular pack to be build/published.
507507
| `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.
508508
| `kinds` | `string` | Defines the kinds of package to create: `zip`, `tar`, `deb` or `rpm`.
509+
| `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.
509510

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

@@ -552,6 +553,15 @@ kinds = ["zip"]
552553

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

556+
You can also replace the generated filename by using a replacer:
557+
558+
```toml
559+
[[pack]]
560+
rid = ["win-x64"]
561+
kinds = ["zip"]
562+
renamer = [{ pattern = "win-x64", replace = "windows-amd64" }]
563+
```
564+
555565
___
556566
> `profile = "custom"`
557567

src/DotNetReleaser.Tests/BasicTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public async Task TestBuild()
7676
config += @"[[pack]]
7777
rid = ""win-x64""
7878
kinds = [""zip""]
79+
renamer = [ { pattern = ""win-x64"", replace = ""windows-amd64"" } ]
7980
[[pack]]
8081
rid = ""linux-x64""
8182
kinds = [""tar"", ""deb""]
@@ -98,7 +99,7 @@ public async Task TestBuild()
9899
"HelloWorld.0.1.0.linux-x64.deb",
99100
"HelloWorld.0.1.0.linux-x64.tar.gz",
100101
"HelloWorld.0.1.0.nupkg",
101-
"HelloWorld.0.1.0.win-x64.zip",
102+
"HelloWorld.0.1.0.windows-amd64.zip",
102103
}.OrderBy(x => x).ToList();
103104

104105
foreach (var file in files)

src/dotnet-releaser.sln

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1818
EndProject
1919
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetReleaser.Tests", "DotNetReleaser.Tests\DotNetReleaser.Tests.csproj", "{B34D6239-0F5A-429D-B710-D571CE6BE58B}"
2020
EndProject
21+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{258DCAB7-E550-499B-8682-21FF31324B7E}"
22+
ProjectSection(SolutionItems) = preProject
23+
..\doc\changelog_user_guide.md = ..\doc\changelog_user_guide.md
24+
..\doc\readme.md = ..\doc\readme.md
25+
EndProjectSection
26+
EndProject
2127
Global
2228
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2329
Debug|Any CPU = Debug|Any CPU

src/dotnet-releaser/Configuration/ChangelogConfiguration.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ChangelogConfiguration()
1919
//ChangeTitleEscape = @"\<*_&@";
2020
Owners = new List<string>();
2121
Autolabelers = new List<ChangelogAutolabeler>();
22-
Replacers = new List<ChangelogReplacer>();
22+
Replacers = new List<RegexReplacer>();
2323
Include = new ChangelogFilter();
2424
Exclude = new ChangelogFilter();
2525
BodyTemplate = @"# Changes
@@ -64,7 +64,7 @@ public ChangelogConfiguration()
6464
public List<ChangelogAutolabeler> Autolabelers { get; }
6565

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

6969
[DataMember(Name = "category")]
7070
public List<ChangelogCategory> Categories { get; }

src/dotnet-releaser/Configuration/ChangelogReplacer.cs

-14
This file was deleted.

src/dotnet-releaser/Configuration/PackagingConfiguration.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Runtime.Serialization;
3+
using System.Text.RegularExpressions;
34

45
namespace DotNetReleaser.Configuration;
56

@@ -9,13 +10,17 @@ public PackagingConfiguration()
910
{
1011
RuntimeIdentifiers = new List<string>();
1112
Kinds = new List<PackageKind>();
13+
Renamers = new List<RegexReplacer>();
1214
}
1315

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

1719
[DataMember(Name = "kinds")]
1820
public List<PackageKind> Kinds { get; }
21+
22+
[DataMember(Name = "renamer")]
23+
public List<RegexReplacer> Renamers { get; }
1924

2025
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)}";
2126

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Runtime.Serialization;
2+
using System.Text.RegularExpressions;
3+
4+
namespace DotNetReleaser.Configuration;
5+
6+
public class RegexReplacer
7+
{
8+
public RegexReplacer()
9+
{
10+
Pattern = string.Empty;
11+
Replace = string.Empty;
12+
}
13+
14+
[DataMember(Name = "pattern")]
15+
public string Pattern { get; set; }
16+
17+
[DataMember(Name = "replace")]
18+
public string Replace { get; set; }
19+
20+
internal string Run(string input) => Regex.Replace(input, Pattern, Replace);
21+
}

src/dotnet-releaser/ReleaserApp.AppPackaging.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Security.Cryptography;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89
using DotNetReleaser.Configuration;
910
using DotNetReleaser.Helpers;
@@ -72,7 +73,7 @@ private async Task<List<AppPackageInfo>> BuildAppPackages(BuildInformation build
7273
{
7374
foreach (var rid in pack.RuntimeIdentifiers)
7475
{
75-
var list = await PackPlatform(packageInfo, pack.Publish, rid, pack.Kinds.ToArray());
76+
var list = await PackPlatform(packageInfo, pack.Publish, rid, pack.Renamers.ToArray(), pack.Kinds.ToArray());
7677
if (HasErrors) goto exitPackOnError; // break on first errors
7778

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

253+
// Give a chance to rename the artifact file
254+
var folder = Path.GetDirectoryName(path)!;
255+
var oldFilename = Path.GetFileName(path);
256+
var filename = oldFilename;
257+
string? newPath = null;
258+
try
259+
{
260+
261+
foreach (var renamer in renamers)
262+
{
263+
filename = renamer.Run(filename);
264+
}
265+
266+
if (filename != oldFilename)
267+
{
268+
newPath = Path.Combine(folder, filename);
269+
File.Move(path, newPath);
270+
path = newPath;
271+
}
272+
}
273+
catch (Exception ex)
274+
{
275+
Error($"Error renaming file {path} to {newPath}: {ex.Message}");
276+
break;
277+
}
278+
252279
var sha256 = string.Join("", SHA256.HashData(await File.ReadAllBytesAsync(path)).Select(x => x.ToString("x2")));
253280

254281
var entry = new AppPackageInfo(

0 commit comments

Comments
 (0)