chore(CI): Use environment vars

This commit is contained in:
MrGadget 2024-06-26 18:13:51 -04:00
parent 7dd1deb6d7
commit 9cedb7e089
4 changed files with 98 additions and 44 deletions

123
.github/UnityPack.csx vendored
View File

@ -1,10 +1,13 @@
/* /*
MIT License: The code in this script is mostly from https://github.com/MirageNet/unity-packer MIT License: The code in this script is mostly from https://github.com/MirageNet/unity-packer
which is the source for the nuget unity-packer from https://www.nuget.org/packages/unity-packer
Specifically the Pack method of the Packer class, related methods, plus the Utils.GreateGUID and Specifically the Pack method of the Packer class, related methods, plus the Utils.GreateGUID and
Archive.AddFilesRecursive methods, adjusted for use in a .csx script called from a GitHub Action. Archive.AddFilesRecursive methods, adjusted for use in a .csx script called from a GitHub Action.
Environment Variables are used instead of command line arguments to pass the output file name, assets, and dependencies.
The AddDependenciesFile method is added to create a packagemanagermanifest asset file with The AddDependenciesFile method is added to create a packagemanagermanifest asset file with
Newtonsoft.Json Unity Test Framework dependencies. Newtonsoft.Json and Unity Test Framework dependencies.
*/ */
#r "nuget: SharpZipLib, 1.4.2" #r "nuget: SharpZipLib, 1.4.2"
@ -13,57 +16,90 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.Json;
using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar; using ICSharpCode.SharpZipLib.Tar;
using YamlDotNet.RepresentationModel; using YamlDotNet.RepresentationModel;
var args = Environment.GetCommandLineArgs(); static StringSplitOptions stringSplitOptions = StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries;
for (int i = 0; i < args.Length; i++) enum CmdLineArgs : int { DotNetScriptDll, ScriptFullName, Version }
Console.WriteLine($"UnityPack: args[{i}]: {args[i]}"); static string[] args = Environment.GetCommandLineArgs();
if (args.Length < 6) //for (int i = 0; i < args.Length; i++)
// Console.WriteLine($"UnityPack: args[{i}]: {args[i]}");
// Output from GitHub Action logging for reference
// UnityPack: args[0]: C:\Users\runneradmin\.dotnet\tools\.store\dotnet-script\1.5.0\dotnet-script\1.5.0\tools\net8.0\any\dotnet-script.dll
// UnityPack: args[1]: .github/UnityPack.csx
// UnityPack: args[2]: 1.1.0
if (args.Length < 3)
{ {
Console.WriteLine("Usage: UnityPack.csx <outputFile> <version> <source1> <destination1> [<source2> <destination2>...]"); Console.WriteLine("Usage: UnityPack.csx <version>");
return; return;
} }
string outputFile = args[2]; // Get version argument
string versionArg = args[3]; static string version = args[(int)CmdLineArgs.Version];
// Get output file name
static string outputFile = Environment.GetEnvironmentVariable("UNITYPACK_OUTPUT") ?? "output.unitypackage";
if (!Path.IsPathRooted(outputFile)) if (!Path.IsPathRooted(outputFile))
outputFile = Path.GetFullPath(outputFile); outputFile = Path.GetFullPath(outputFile);
Console.WriteLine($"UnityPack: outputFile: {outputFile}"); Console.WriteLine($"UnityPack: outputFile:{outputFile} version:{version}");
var fileMap = new Dictionary<string, string>(); // Create assets dictionary
static Dictionary<string, string> assets = new Dictionary<string, string>();
var assetVars = Environment.GetEnvironmentVariables()
.Cast<System.Collections.DictionaryEntry>()
.Where(e => e.Key.ToString().StartsWith("UNITYPACK_ASSET"))
.ToDictionary(e => e.Key.ToString(), e => e.Value.ToString());
for (int i = 4; i < args.Length; i += 2) foreach (var kvp in assetVars)
{ {
string fromPath = args[i]; string[] parts = kvp.Value.Split(' ', stringSplitOptions);
string source = parts[0];
if (!Path.IsPathRooted(args[i])) string destination = parts.Length > 1 ? parts[1] : "";
fromPath = Path.GetFullPath(fromPath); assets[source] = destination;
string toPath = args[i + 1];
fileMap.Add(fromPath, toPath);
} }
Pack(fileMap, outputFile, versionArg); // Create dependencies dictionary
static Dictionary<string, string> dependencies = new Dictionary<string, string>();
var envVars = Environment.GetEnvironmentVariables()
.Cast<System.Collections.DictionaryEntry>()
.Where(e => e.Key.ToString().StartsWith("UNITYPACK_DEPENDENCY"))
.ToDictionary(e => e.Key.ToString(), e => e.Value.ToString());
static void Pack(IDictionary<string, string> files, string outputFile, string version) foreach (var kvp in envVars)
{ {
//string randomFile = Path.GetRandomFileName(); string[] parts = kvp.Value.Split(' ', stringSplitOptions);
string name = parts[0];
string value = parts.Length > 1 ? parts[1] : "";
dependencies[name] = value;
}
// Create testables list
static List<string> testables = Environment.GetEnvironmentVariable("UNITYPACK_TESTABLES")
?.Split(' ', stringSplitOptions)
.Where(t => !string.IsNullOrWhiteSpace(t))
.ToList() ?? new List<string>();
Pack();
static void Pack()
{
string tempPath = Path.Combine(Path.GetTempPath(), $"Mirror-{version}"); string tempPath = Path.Combine(Path.GetTempPath(), $"Mirror-{version}");
Directory.CreateDirectory(tempPath); Directory.CreateDirectory(tempPath);
Console.WriteLine($"UnityPack: tempPath: {tempPath}"); Console.WriteLine($"UnityPack: tempPath: {tempPath}");
AddAssets(files, tempPath); AddAssets(tempPath);
AddDependeciesFile(tempPath); AddDependenciesFile(tempPath);
if (File.Exists(outputFile)) if (File.Exists(outputFile))
File.Delete(outputFile); File.Delete(outputFile);
@ -74,9 +110,9 @@ static void Pack(IDictionary<string, string> files, string outputFile, string ve
Directory.Delete(tempPath, true); Directory.Delete(tempPath, true);
} }
static void AddAssets(IDictionary<string, string> files, string tempPath) static void AddAssets(string tempPath)
{ {
foreach (KeyValuePair<string, string> fileEntry in files) foreach (KeyValuePair<string, string> fileEntry in assets)
{ {
if (File.Exists(fileEntry.Key)) if (File.Exists(fileEntry.Key))
AddAsset(tempPath, fileEntry.Key, fileEntry.Value); AddAsset(tempPath, fileEntry.Key, fileEntry.Value);
@ -156,29 +192,27 @@ static YamlDocument GenerateMeta(string fromFile, string toFile)
{ {
// this is a folder // this is a folder
return new YamlDocument(new YamlMappingNode return new YamlDocument(new YamlMappingNode
{ {
{"guid", guid}, {"guid", guid},
{"fileFormatVersion", "2"}, {"fileFormatVersion", "2"},
{"folderAsset", "yes"} {"folderAsset", "yes"}
}); });
} }
else else
{ {
// this is a file // this is a file
return new YamlDocument(new YamlMappingNode return new YamlDocument(new YamlMappingNode
{ {
{"guid", guid}, {"guid", guid},
{"fileFormatVersion", "2"} {"fileFormatVersion", "2"}
}); });
} }
} }
static string GetGuid(YamlDocument meta) static string GetGuid(YamlDocument meta)
{ {
var mapping = (YamlMappingNode)meta.RootNode; var mapping = (YamlMappingNode)meta.RootNode;
var key = new YamlScalarNode("guid"); var key = new YamlScalarNode("guid");
var value = (YamlScalarNode)mapping[key]; var value = (YamlScalarNode)mapping[key];
return value.Value; return value.Value;
} }
@ -193,9 +227,7 @@ static string CreateGuid(string input)
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
foreach (byte b in hashBytes) foreach (byte b in hashBytes)
{
stringBuilder.Append(b.ToString("X2")); stringBuilder.Append(b.ToString("X2"));
}
return stringBuilder.ToString(); return stringBuilder.ToString();
} }
@ -214,9 +246,18 @@ static void SaveMeta(string metaPath, YamlDocument meta)
metaFileStream.SetLength(metaFile.Length - 3 - Environment.NewLine.Length); metaFileStream.SetLength(metaFile.Length - 3 - Environment.NewLine.Length);
} }
static void AddDependeciesFile(string tempPath) static void AddDependenciesFile(string tempPath)
{ {
string depenciesJson = "{\"dependencies\":{\"com.unity.nuget.newtonsoft-json\":\"3.0.0\"},\"testables\":[\"com.unity.test-framework.performance\"]}"; //string depenciesJson = "{\"dependencies\":{\"com.unity.nuget.newtonsoft-json\":\"3.0.0\"},\"testables\":[\"com.unity.test-framework.performance\"]}";
// Serialize the JSON object
var jsonObject = new
{
dependencies,
testables
};
string depenciesJson = JsonSerializer.Serialize(jsonObject, new JsonSerializerOptions { WriteIndented = true });
string depenciesPath = Path.Combine(tempPath, "packagemanagermanifest"); string depenciesPath = Path.Combine(tempPath, "packagemanagermanifest");
Directory.CreateDirectory(depenciesPath); Directory.CreateDirectory(depenciesPath);
Console.WriteLine($"UnityPack: Creating dependency file at {Path.Combine(depenciesPath, "asset")}"); Console.WriteLine($"UnityPack: Creating dependency file at {Path.Combine(depenciesPath, "asset")}");

View File

@ -16,7 +16,7 @@ jobs:
- 2021.3.38f1 - 2021.3.38f1
- 2022.3.33f1 - 2022.3.33f1
- 2023.2.20f1 - 2023.2.20f1
- 6000.0.5f1 - 6000.0.7f1
steps: steps:
- name: Checkout repository - name: Checkout repository

View File

@ -43,3 +43,13 @@ jobs:
--debug --debug
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
UNITYPACK_OUTPUT: "Mirror.unitypackage"
# source and destination separated by space
# add more assets as separate env vars using the naming pattern
UNITYPACK_ASSET1: "Assets/Mirror Assets/Mirror"
UNITYPACK_ASSET2: "LICENSE Assets/Mirror/LICENSE"
# name and version separated by space
# add more dependencies as separate env vars using the naming pattern
UNITYPACK_DEPENDENCY1: "com.unity.nuget.newtonsoft-json 3.0.0"
# package names separated by space (only one for now)
UNITYPACK_TESTABLES: "com.unity.test-framework.performance"

View File

@ -1,3 +1,6 @@
branches:
- name: master
verifyConditions: verifyConditions:
- "@semantic-release/github" - "@semantic-release/github"
@ -59,7 +62,7 @@ plugins:
# Create Unity package with Mirror, ScriptTemplates, and LICENSE # Create Unity package with Mirror, ScriptTemplates, and LICENSE
- - '@semantic-release/exec' - - '@semantic-release/exec'
- prepareCmd: "dotnet script .github/UnityPack.csx Mirror.unitypackage ${nextRelease.version} Assets/Mirror Assets/Mirror LICENSE Assets/Mirror/LICENSE" - prepareCmd: "dotnet script .github/UnityPack.csx ${nextRelease.version}"
# Create a new release on GitHub # Create a new release on GitHub
- - '@semantic-release/github' - - '@semantic-release/github'