Skip to content

Commit

Permalink
Not Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-cpp committed Nov 7, 2023
1 parent dd1e024 commit 827fa3d
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

name: Build

on: [push, pull_request]

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Clone Repository
uses: actions/checkout@v4
- name: Build Solution
run: dotnet build
20 changes: 20 additions & 0 deletions BuildContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

namespace BuildScripts;

public class BuildContext : FrostingContext
{
public string ArtifactsDir { get; }

public PackContext PackContext { get; }

public BuildContext(ICakeContext context) : base(context)
{
ArtifactsDir = context.Arguments("artifactsDir", "artifacts").FirstOrDefault()!;
PackContext = new PackContext(context);

if (context.BuildSystem().IsRunningOnGitHubActions)
{
context.BuildSystem().GitHubActions.Commands.SetSecret(context.EnvironmentVariable("GITHUB_TOKEN"));
}
}
}
43 changes: 43 additions & 0 deletions MonoGame.Library.BuildScripts.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Resources/Icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<LogicalName>Icon.png</LogicalName>
</EmbeddedResource>

<EmbeddedResource Include="Resources/MonoGame.Library.X.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<LogicalName>MonoGame.Library.X.txt</LogicalName>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Using Include="Cake.Frosting" />
<Using Include="Cake.Common.Build" />
<Using Include="Cake.Common.Diagnostics" />
<Using Include="Cake.Common.Tools.DotNet" />
<Using Include="Cake.Common.Tools.DotNet.MSBuild" />
<Using Include="Cake.Common.Tools.DotNet.Pack" />
<Using Include="Cake.Common.IO" />
<Using Include="Cake.Common" />
<Using Include="Cake.Core" />
<Using Include="Cake.Core.Diagnostics" />
<Using Include="Cake.Core.IO" />
<Using Include="Cake.FileHelpers" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cake.FileHelpers" Version="6.1.3" />
<PackageReference Include="Cake.Frosting" Version="3.1.0" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions MonoGame.Library.BuildScripts.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoGame.Library.BuildScripts", "MonoGame.Library.BuildScripts.csproj", "{D2A7576F-64AF-4AA2-B6AA-DB36F21A3BA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D2A7576F-64AF-4AA2-B6AA-DB36F21A3BA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2A7576F-64AF-4AA2-B6AA-DB36F21A3BA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2A7576F-64AF-4AA2-B6AA-DB36F21A3BA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2A7576F-64AF-4AA2-B6AA-DB36F21A3BA4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {45681AE0-4A1A-40AA-ABFE-97F7B8B2B351}
EndGlobalSection
EndGlobal
37 changes: 37 additions & 0 deletions PackContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

namespace BuildScripts;

public class PackContext
{
public string LibraryName { get; }

public string LicensePath { get; }

public string? RepositoryOwner { get; }

public string? RepositoryUrl { get; }

public string Version { get; }

public bool IsTag { get; }

public PackContext(ICakeContext context)
{
LibraryName = context.Arguments("libraryname", "X").FirstOrDefault()!;
LicensePath = context.Arguments("licensepath", "").FirstOrDefault()!;
Version = "1.0.0";
IsTag = false;

if (context.BuildSystem().IsRunningOnGitHubActions)
{
RepositoryOwner = context.EnvironmentVariable("GITHUB_REPOSITORY_OWNER");
RepositoryUrl = $"https://github.com/{context.EnvironmentVariable("GITHUB_REPOSITORY")}";
IsTag = context.EnvironmentVariable("GITHUB_REF_TYPE") == "tag";

if (IsTag)
{
Version = context.EnvironmentVariable("GITHUB_REF_NAME");
}
}
}
}
Binary file added Resources/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions Resources/MonoGame.Library.X.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>NU5128</NoWarn>
<Title>MonoGame build of {X} Library</Title>
<Description>This package contains an {X} library built for distributing MonoGame games.</Description>
<PackageIcon>Icon.png</PackageIcon>
<IncludeBuildOutput>false</IncludeBuildOutput>
<PackageLicenseFile>{LicenceName}</PackageLicenseFile>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>

<ItemGroup>
<None Include="{LicencePath}" Pack="true" PackagePath="{LicenceName}" />
</ItemGroup>

<ItemGroup>
<None Include="Icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
{LibrariesToInclude}
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Tasks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

namespace BuildScripts;

[TaskName("BuildLibrary")]
public class BuildLibraryTask : FrostingTask { }

[TaskName("TestLibrary")]
[IsDependentOn(typeof(TestWindowsTask))]
[IsDependentOn(typeof(TestMacOSTask))]
[IsDependentOn(typeof(TestLinuxTask))]
public class TestLibraryTask : FrostingTask { }

[TaskName("Default")]
[IsDependentOn(typeof(BuildLibraryTask))]
[IsDependentOn(typeof(PublishLibraryTask))]
[IsDependentOn(typeof(TestLibraryTask))]
public class DefaultTask : FrostingTask { }
12 changes: 12 additions & 0 deletions Tasks/PrepTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace BuildScripts;

[TaskName("Prepare Build")]
public sealed class PrepTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.CleanDirectory(context.ArtifactsDir);
context.CreateDirectory(context.ArtifactsDir);
}
}
28 changes: 28 additions & 0 deletions Tasks/PublishLibraryTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Runtime.InteropServices;

namespace BuildScripts;

[TaskName("Publish Library")]
[IsDependentOn(typeof(PrepTask))]
public sealed class PublishLibraryTask : AsyncFrostingTask<BuildContext>
{
public override bool ShouldRun(BuildContext context) => context.BuildSystem().IsRunningOnGitHubActions;

public override async Task RunAsync(BuildContext context)
{
var rid = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
rid = "windows";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
rid = "osx";
else
rid = "linux";
rid += RuntimeInformation.ProcessArchitecture switch
{
Architecture.Arm or Architecture.Arm64 => "-arm64",
_ => "-x64",
};

await context.BuildSystem().GitHubActions.Commands.UploadArtifact(DirectoryPath.FromString(context.ArtifactsDir), $"artifacts-{rid}");
}
}
97 changes: 97 additions & 0 deletions Tasks/PublishPackageTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

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 = new[] {
"windows-x64",
"osx-x64",
"osx-arm64",
"linux-x64"
};

// Download built artifacts
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);
}
}

// Generate Project
var projectData = await ReadEmbeddedResourceAsync("MonoGame.Library.X.txt");
projectData = projectData.Replace("{X}", context.PackContext.LibraryName);
projectData = projectData.Replace("{LicencePath}", context.PackContext.LicensePath);

if (context.PackContext.LicensePath.EndsWith(".txt"))
projectData = projectData.Replace("{LicenceName}", "LICENSE.txt");
else if (context.PackContext.LicensePath.EndsWith(".md"))
projectData = projectData.Replace("{LicenceName}", "LICENSE.md");
else
projectData = projectData.Replace("{LicenceName}", "LICENSE");

var librariesToInclude = from rid in requiredRids from filePath in Directory.GetFiles($"runtimes/{rid}/native")
select $"<Content Include=\"{filePath}\"><PackagePath>runtimes/{rid}/native</PackagePath></Content>";
projectData = projectData.Replace("{LibrariesToInclude}", string.Join(Environment.NewLine, librariesToInclude));

await File.WriteAllTextAsync($"MonoGame.Library.{context.PackContext.LibraryName}.csproj", projectData);
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"
});
}
}
}
}
}
55 changes: 55 additions & 0 deletions Tasks/TestLinuxTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

namespace BuildScripts;

[TaskName("Test Linux")]
public sealed class TestLinuxTask : FrostingTask<BuildContext>
{
private static readonly string[] ValidLibs = {
"linux-vdso.so",
"libstdc++.so",
"libgcc_s.so",
"libc.so",
"libm.so",
"/lib/ld-linux-",
"/lib64/ld-linux-"
};

public override bool ShouldRun(BuildContext context) => context.IsRunningOnLinux();

public override void Run(BuildContext context)
{
foreach (var filePath in context.GetFiles(context.ArtifactsDir))
{
context.Information($"Checking: {filePath}");
context.StartProcess(
"ldd",
new ProcessSettings
{
Arguments = $"{filePath}",
RedirectStandardOutput = true
},
out IEnumerable<string> processOutput);

foreach (var line in processOutput)
{
var libPath = line.Trim().Split(' ')[0];
context.Information($"DEP: {libPath}");

var isValidLib = false;
foreach (var validLib in ValidLibs)
{
if (libPath.StartsWith(validLib))
{
isValidLib = true;
break;
}
}

if (!isValidLib)
throw new Exception($"Found a dynamic library ref: {libPath}");
}

context.Information("");
}
}
}
Loading

0 comments on commit 827fa3d

Please sign in to comment.