-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPublishPackageTask.cs
More file actions
139 lines (121 loc) · 5.59 KB
/
Copy pathPublishPackageTask.cs
File metadata and controls
139 lines (121 loc) · 5.59 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
namespace BuildScripts;
[TaskName("Package")]
public sealed class PublishPackageTask : AsyncFrostingTask<BuildContext>
{
private static async Task<string> ReadEmbeddedResourceAsync(string resourceName)
{
await using var stream = typeof(PublishPackageTask).Assembly.GetManifestResourceStream(resourceName)!;
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync();
}
private static async Task SaveEmbeddedResourceAsync(string resourceName, string outPath)
{
if (File.Exists(outPath))
File.Delete(outPath);
await using var stream = typeof(PublishPackageTask).Assembly.GetManifestResourceStream(resourceName)!;
await using var writer = File.Create(outPath);
await stream.CopyToAsync(writer);
writer.Close();
}
public override async Task RunAsync(BuildContext context)
{
var requiredRids = context.IsUniversalBinary ?
new string[]
{
"win-x64",
"linux-x64",
"osx",
}
: new string[] {
"win-x64",
"linux-x64",
"osx-x64",
"osx-arm64"
};
var optionalRids = new string[] {
"ios-arm64",
"iossimulator-x64",
"iossimulator-arm64",
"android-arm64",
"android-arm",
"android-x86",
"android-x64",
"linux-arm",
"linux-arm64",
"win-arm64",
};
// Download built artifacts
var downloadedRids = new List<string>();
if (context.BuildSystem().IsRunningOnGitHubActions)
{
foreach (var rid in requiredRids)
{
var directoryPath = $"runtimes/{rid}/native";
if (context.DirectoryExists(directoryPath))
continue;
context.CreateDirectory(directoryPath);
await context.BuildSystem().GitHubActions.Commands.DownloadArtifact($"artifacts-{rid}", directoryPath);
downloadedRids.Add (rid);
}
foreach (var rid in optionalRids)
{
var directoryPath = $"runtimes/{rid}/native";
if (context.DirectoryExists(directoryPath))
continue;
context.CreateDirectory(directoryPath);
try {
await context.BuildSystem().GitHubActions.Commands.DownloadArtifact($"artifacts-{rid}", directoryPath);
downloadedRids.Add (rid);
} catch {
// Rid not available, remove the directory.
context.DeleteDirectory (directoryPath,new DeleteDirectorySettings () { Recursive = true, Force = true});
}
}
}
var description = $"This package contains native libraries for {context.PackContext.LibraryName} built for usage with MonoGame.";
var readMeName = "README.md";
var readMePath = $"{readMeName}";
var licensePath = context.PackContext.LicensePath;
var licenseName = System.IO.Path.GetFileName(licensePath);
var librariesToInclude = from rid in downloadedRids from filePath in Directory.GetFiles($"runtimes/{rid}/native")
select $"<Content Include=\"{filePath}\"><PackagePath>runtimes/{rid}/native</PackagePath></Content>";
// Generate Project
var projectData = await ReadEmbeddedResourceAsync("MonoGame.Library.X.txt");
projectData = projectData.Replace("{X}", context.PackContext.LibraryName)
.Replace("{Description}", description)
.Replace("{LicensePath}", licensePath)
.Replace("{ReadMePath}", readMeName)
.Replace("{LicenseName}", licenseName)
.Replace("{ReadMeName}", readMeName)
.Replace("{LibrariesToInclude}", string.Join(Environment.NewLine, librariesToInclude));
await File.WriteAllTextAsync($"MonoGame.Library.{context.PackContext.LibraryName}.csproj", projectData);
await File.WriteAllTextAsync(readMePath, description);
await SaveEmbeddedResourceAsync("Icon.png", "Icon.png");
// Build
var dnMsBuildSettings = new DotNetMSBuildSettings();
dnMsBuildSettings.WithProperty("Version", context.PackContext.Version);
dnMsBuildSettings.WithProperty("RepositoryUrl", context.PackContext.RepositoryUrl);
context.DotNetPack($"MonoGame.Library.{context.PackContext.LibraryName}.csproj", new DotNetPackSettings
{
MSBuildSettings = dnMsBuildSettings,
Verbosity = DotNetVerbosity.Minimal,
Configuration = "Release"
});
// Upload Artifacts
if (context.BuildSystem().IsRunningOnGitHubActions)
{
foreach (var nugetPath in context.GetFiles("bin/Release/*.nupkg"))
{
await context.BuildSystem().GitHubActions.Commands.UploadArtifact(nugetPath, nugetPath.GetFilename().ToString());
if (context.PackContext.IsTag)
{
context.DotNetNuGetPush(nugetPath, new()
{
ApiKey = context.EnvironmentVariable("GITHUB_TOKEN"),
Source = $"https://nuget.pkg.github.com/{context.PackContext.RepositoryOwner}/index.json"
});
}
}
}
}
}