-
Notifications
You must be signed in to change notification settings - Fork 22
/
TemplateTarget.cs
61 lines (51 loc) · 2.35 KB
/
TemplateTarget.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// ReSharper disable StringLiteralTypo
// ReSharper disable ClassNeverInstantiated.Global
namespace Build;
internal class TemplateTarget(
Settings settings,
Commands commands,
Versions versions,
ITeamCityArtifactsWriter artifactsWriter)
: IInitializable, ITarget<string>
{
private const string ProjectName = "Pure.DI.Templates";
public Task InitializeAsync(CancellationToken cancellationToken) => commands.RegisterAsync(
this, "Creates and deploys templates", "template", "t");
public async Task<string> RunAsync(CancellationToken cancellationToken)
{
var packageVersion = versions.GetNext(new NuGetRestoreSettings("Pure.DI"), Settings.VersionRange, 0).ToString();
foreach (var jsonFile in new[]
{
"src/Pure.DI.Templates/Templates/Pure.DI.Template.ClassLibrary/.template.config/template.json",
"src/Pure.DI.Templates/Templates/Pure.DI.Template.ConsoleApp/.template.config/template.json"
})
{
var content = await File.ReadAllTextAsync(jsonFile, cancellationToken);
content = content.Replace("$(version)", packageVersion);
await File.WriteAllTextAsync(jsonFile, content, cancellationToken);
}
var props = new[]
{
("configuration", settings.Configuration),
("version", packageVersion)
};
var projectDirectory = Path.Combine("src", ProjectName);
await new DotNetPack()
.WithProject(Path.Combine(projectDirectory, $"{ProjectName}.csproj"))
.WithProps(props)
.BuildAsync(cancellationToken: cancellationToken).EnsureSuccess();
var targetPackage = Path.Combine(projectDirectory, "bin", $"{ProjectName}.{packageVersion}.nupkg");
artifactsWriter.PublishArtifact($"{targetPackage} => .");
if (string.IsNullOrWhiteSpace(settings.NuGetKey))
{
Warning($"The NuGet key was not specified, the package {targetPackage} will not be pushed.");
return targetPackage;
}
await new DotNetNuGetPush()
.WithPackage(targetPackage)
.WithSource("https://api.nuget.org/v3/index.json")
.WithApiKey(settings.NuGetKey)
.BuildAsync(cancellationToken: cancellationToken).EnsureSuccess();
return targetPackage;
}
}