Skip to content

Commit

Permalink
build task for NuGet packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rune580 committed Dec 13, 2023
1 parent fcb0409 commit 86a2a52
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
69 changes: 69 additions & 0 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Cake.Common.IO;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Common.Tools.DotNet.Pack;
using Cake.Core;
using Cake.Frosting;
using dotenv.net;
Expand Down Expand Up @@ -262,6 +264,73 @@ public override void Run(BuildContext context)
}
}

[TaskName("BuildNuGet")]
[IsDependentOn(typeof(BuildTask))]
public sealed class BuildNuGetPackage : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
AbsolutePath manifestFile = (AbsolutePath)"../" / "manifest.json";
var manifest = JsonSerializer.Deserialize<ThunderStoreManifest>(File.ReadAllText(manifestFile));
if (manifest is null)
throw new InvalidOperationException();

AbsolutePath readmeFile = (AbsolutePath)"../" / "README.md";
File.Copy(readmeFile, context.BuildDir / "README.md", true);

AbsolutePath iconFile = (AbsolutePath)"../" / "icon.png";
File.Copy(iconFile, context.BuildDir / "icon.png", true);

AbsolutePath licenseFile = (AbsolutePath)"../" / "LICENSE";
File.Copy(licenseFile, context.BuildDir / "LICENSE", true);

var dllFile = $"{context.Project.Name}.dll";
var nuspecContent = $"""
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>{context.ManifestAuthor}.{manifest.name}</id>
<version>{manifest.version_number}</version>
<description>{manifest.description}</description>
<authors>{context.ManifestAuthor}</authors>
<projectUrl>{manifest.website_url}</projectUrl>
<readme>README.md</readme>
<iconUrl>https://cdn.rune580.dev/icons/lethalcompany_inpututils/icon.png</iconUrl>
<icon>icon.png</icon>
<license type="file">LICENSE</license>
</metadata>
<files>
<file src="{dllFile}" target="lib/{dllFile}" />
<file src="README.md" />
<file src="icon.png" />
<file src="LICENSE" />
</files>
</package>
""";

var nuspecFile = context.BuildDir / $"{context.Project.Name}.nuspec";
File.WriteAllText(nuspecFile, nuspecContent);

var msBuildSettings = new DotNetMSBuildSettings
{
Properties =
{
["NuspecFile"] = [nuspecFile]
}
};

var packSettings = new DotNetPackSettings
{
NoBuild = true,
Configuration = "Release",
OutputDirectory = (string)(context.BuildDir / "artifacts"),
MSBuildSettings = msBuildSettings
};

context.DotNetPack(context.Project.Directory, packSettings);
}
}

[TaskName("Default")]
[IsDependentOn(typeof(BuildTask))]
public class DefaultTask : FrostingTask;
2 changes: 2 additions & 0 deletions build/Utils/CSharpProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ namespace build.Utils;

public class CSharpProject
{
public AbsolutePath FilePath { get; }
public string Name { get; }
public AbsolutePath Directory { get; }

public CSharpProject(AbsolutePath filePath)
{
FilePath = filePath;
Name = Path.GetFileNameWithoutExtension(filePath);
Directory = Path.GetDirectoryName(filePath)!;
}
Expand Down

0 comments on commit 86a2a52

Please sign in to comment.