Skip to content

Commit

Permalink
Improve NukeBuild script
Browse files Browse the repository at this point in the history
  • Loading branch information
ImoutoChan committed Jul 31, 2023
1 parent 51090ff commit 0d370ca
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }}

- name: Pack 7ZSfx installer
run: .\Tools\NukeBuild\build.cmd Pack7ZSfx
run: .\Tools\NukeBuild\build.cmd Test Pack7ZSfx

- name: Create github release
uses: actions/create-release@v1
Expand Down
2 changes: 1 addition & 1 deletion .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"properties": {
"Configuration": {
"type": "string",
"description": "Configuration to build - Default is 'Debug' (local) or 'Release' (server)",
"description": "Configuration to build - Default is 'Release'",
"enum": [
"Debug",
"Release"
Expand Down
4 changes: 2 additions & 2 deletions BuildAndDeploy.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
$ErrorActionPreference = 'Stop'

cd .\Tools\NukeBuild
.\build.cmd
.\build.cmd Test Publish

cd ..\..\Artifacts\latest
.\install-update.ps1

cd ..\..
cd ..\..
185 changes: 83 additions & 102 deletions Tools/NukeBuild/Build.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,87 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Nuke.Common;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Utilities.Collections;
using static Nuke.Common.Tools.DotNet.DotNetTasks;

public class CustomBuildCmdPathGitHubActionsAttribute : GitHubActionsAttribute
{
public CustomBuildCmdPathGitHubActionsAttribute(
string name,
GitHubActionsImage image,
params GitHubActionsImage[] images) : base(name, image, images)
{
}

protected override string BuildCmdPath => Build.RootDirectory / "Tools" / "NukeBuild" / "build.cmd";
}
using static Nuke.Common.IO.FileSystemTasks;
using static Z7Tasks;

[CustomBuildCmdPathGitHubActions(
"release",
GitHubActionsImage.WindowsLatest,
OnPushTags = new []{ "*"},
InvokedTargets = new[] { nameof(Pack7ZSfx) },
InvokedTargets = new[] { nameof(Test), nameof(Pack7ZSfx) },
AutoGenerate = false)]
class Build : NukeBuild
{
static readonly IReadOnlyCollection<string> ApplicationProjects = new[]
Project[] ProjectsToPublish => new[]
{
Solution.ImoutoRebirth_Arachne.ImoutoRebirth_Arachne_Host,
Solution.ImoutoRebirth_Harpy.ImoutoRebirth_Harpy_Host,
Solution.ImoutoRebirth_Kekkai.ImoutoRebirth_Kekkai,
Solution.ImoutoRebirth_Lilin.ImoutoRebirth_Lilin_Host,
Solution.ImoutoRebirth_Meido.ImoutoRebirth_Meido_Host,
Solution.ImoutoRebirth_Navigator.ImoutoRebirth_Navigator,
Solution.ImoutoRebirth_Room.ImoutoRebirth_Room_Webhost,
Solution.ImoutoRebirth_Tori.ImoutoRebirth_Tori,
Solution.Imouto_Viewer.ImoutoViewer
};

AbsolutePath[] NukeFilesToPublish => new[]
{
BuildAssemblyDirectory / "configuration.json",
BuildAssemblyDirectory / "install-update.ps1",
BuildAssemblyDirectory / "install-dependencies.ps1"
};

Dictionary<string, RelativePath[]> DirectoriesToDeleteForProject => new()
{
"ImoutoRebirth.Arachne.Host",
"ImoutoRebirth.Harpy.Host",
"ImoutoRebirth.Kekkai",
"ImoutoRebirth.Lilin.Host",
"ImoutoRebirth.Meido.Host",
"ImoutoRebirth.Navigator",
"ImoutoRebirth.Room.Webhost",
"ImoutoRebirth.Tori",
"ImoutoViewer",
{
"ImoutoRebirth.Navigator", new[]
{
(RelativePath)"de",
(RelativePath)"libvlc" / "win-x86"
}
}
};

public static int Main () => Execute<Build>(x => x.Publish);

[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
//readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Parameter("Configuration to build - Default is 'Release'")]
readonly Configuration Configuration = Configuration.Release;

AbsolutePath SourceDirectory => RootDirectory;

AbsolutePath OutputDirectory => RootDirectory / "Artifacts";

[Solution]
AbsolutePath OutputLatestDirectory => OutputDirectory / "latest";

[Solution(GenerateProjects = true)]
readonly Solution Solution;

[GitVersion]
[GitVersion]
readonly GitVersion GitVersion;

Target Clean => _ => _
.Before(Restore)
.Executes(() =>
{
SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(x => x.DeleteDirectory());
SourceDirectory
.GlobDirectories("**/bin", "**/obj")
.DeleteDirectories();
});

Target Restore => _ => _
.Executes(() =>
{
DotNetRestore(s => s.SetProjectFile(Solution).SetVerbosity(DotNetVerbosity.Quiet));
DotNetRestore(s => s.SetProjectFile(Solution)
.SetVerbosity(DotNetVerbosity.Quiet));
});

Target Compile => _ => _
Expand All @@ -89,6 +99,7 @@ class Build : NukeBuild

Target Test => _ => _
.DependsOn(Compile)
.Before(Publish)
.Executes(() =>
{
var testProjects = SourceDirectory.GlobFiles("**/*.Tests.csproj").ToList();
Expand All @@ -101,86 +112,56 @@ class Build : NukeBuild
});

Target Publish => _ => _
.DependsOn(Test)
.DependsOn(Compile)
.Executes(() =>
{
var output = OutputDirectory / "latest";
output.CreateOrCleanDirectory();
OutputLatestDirectory.CreateOrCleanDirectory();

var publishableProjects =
from projectToPublish in ProjectsToPublish
let projectName = projectToPublish.Directory.Parent!.Name
let projectOutput = OutputLatestDirectory / projectName
let directoriesToDelete = GetDirectoriesToDelete(projectName, projectOutput).ToList()
select new
{
Project = projectToPublish,
ProjectName = projectName,
ProjectOutput = projectOutput,
DirectoriesToDelete = directoriesToDelete
};

var apps = Solution.AllProjects.Where(p => ApplicationProjects.Contains(p.Name));
DotNetPublish(s => s
.SetVerbosity(DotNetVerbosity.Quiet)
.SetConfiguration(Configuration)
.SetAssemblyVersion(GitVersion.AssemblySemVer)
.SetVersion(GitVersion.NuGetVersionV2)
.CombineWith(publishableProjects, (_, v) => _
.SetProject(v.Project)
.SetOutput(v.ProjectOutput)));

foreach (var project in apps)
{
DotNetPublish(s => s
.SetVerbosity(DotNetVerbosity.Quiet)
.SetConfiguration(Configuration)
.SetAssemblyVersion(GitVersion.AssemblySemVer)
.SetVersion(GitVersion.NuGetVersionV2)
.SetOutput(output / project.Directory.Parent!.Name)
.SetProject(project));

if (project.Directory.Parent!.Name == "ImoutoRebirth.Navigator")
{
Directory.Delete(output / project.Directory.Parent!.Name / "de", true);
Directory.Delete(output / project.Directory.Parent!.Name / "libvlc" / "win-x86", true);
}
}
foreach (var directoryToDelete in publishableProjects.SelectMany(x => x.DirectoriesToDelete))
directoryToDelete.DeleteDirectory();

CopyFileToOutput("configuration.json");
CopyFileToOutput("install-update.ps1");
CopyFileToOutput("install-dependencies.ps1");
foreach (var nukeFileToPublish in NukeFilesToPublish)
CopyFileToDirectory(nukeFileToPublish, OutputLatestDirectory, FileExistsPolicy.Overwrite);

return;

void CopyFileToOutput(string fileName)
{
var configFilePath = BuildAssemblyDirectory / fileName;
var targetConfigFilePath = output / fileName;
File.Copy(configFilePath, targetConfigFilePath, overwrite: true);
}

AbsolutePath[] GetDirectoriesToDelete(string projectName, AbsolutePath projectOutput)
=> DirectoriesToDeleteForProject
.GetValueOrDefault(projectName, Array.Empty<RelativePath>())
.Select(x => projectOutput / x)
.ToArray();
});

Target Pack7ZSfx => _ => _
.DependsOn(Publish)
.Executes(() =>
{
var output = OutputDirectory / "latest";

ArchiveAs7Z(output);

return;

void ArchiveAs7Z(AbsolutePath output)
{
var strCmdText =
$"""
&'C:\Program Files\7-Zip\7z.exe' a -sfx '{output.Parent}\ImoutoRebirth.exe' '{output}\*'
""";

Serilog.Log.Warning("CommandToArchive: {Command}", strCmdText);

var psi = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-Command \"{strCmdText}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
RedirectStandardError = true
};

var process = Process.Start(psi);
var error = process!.StandardError.ReadToEnd();
process.WaitForExit();

if (!string.IsNullOrEmpty(error))
{
Serilog.Log.Error(error);
}
else
{
Serilog.Log.Information("Archive created");
}
}
PackAs7z(s => s
.CreateArchive()
.AsSfx()
.SetOutputArchiveFile(OutputLatestDirectory.Parent / "ImoutoRebirth.exe")
.SetSourceDirectory(OutputLatestDirectory));
});
}
13 changes: 13 additions & 0 deletions Tools/NukeBuild/CustomBuildCmdPathGitHubActionsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Nuke.Common.CI.GitHubActions;

public class CustomBuildCmdPathGitHubActionsAttribute : GitHubActionsAttribute
{
public CustomBuildCmdPathGitHubActionsAttribute(
string name,
GitHubActionsImage image,
params GitHubActionsImage[] images) : base(name, image, images)
{
}

protected override string BuildCmdPath => Build.RootDirectory / "Tools" / "NukeBuild" / "build.cmd";
}
74 changes: 74 additions & 0 deletions Tools/NukeBuild/Z7Tasks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Linq;
using Nuke.Common.IO;
using Nuke.Common.Tooling;

public static class Z7Tasks
{
public static void PackAs7z(Action<Z7SfxSettings> configurator)
{
var settings = new Z7SfxSettings();
configurator(settings);

ProcessTasks.StartProcess(settings).WaitForExit();
}

public class Z7SfxSettings : ToolSettings
{
public override string ProcessToolPath => @"C:\Program Files\7-Zip\7z.exe";

public override Action<ToolSettings, IProcess> ProcessExitHandler => (_, process) =>
{
if (process.Output.Any(x => x.Type == OutputType.Err))
{
Serilog.Log.Error(
string.Join("\n", process.Output.Where(x => x.Type == OutputType.Err).Select(x => x.Text)));
}
else
{
Serilog.Log.Information("Archive created");
}
};

public string Command { get; private set; }

public string Switch { get; private set; }

public string ArchiveName { get; private set; }

public string SourceName { get; private set; }

public Z7SfxSettings CreateArchive()
{
Command = "a";
return this;
}

public Z7SfxSettings AsSfx()
{
Switch = "-sfx";
return this;
}

public Z7SfxSettings SetOutputArchiveFile(AbsolutePath archiveName)
{
ArchiveName = archiveName;
return this;
}

public Z7SfxSettings SetSourceDirectory(string sourceName)
{
SourceName = sourceName;
return this;
}

protected override Arguments ConfigureProcessArguments(Arguments arguments)
{
return arguments
.Add(Command)
.Add(Switch)
.Add("\"{0}\"", ArchiveName)
.Add("\"{0}\\*\"", SourceName);
}
}
}

0 comments on commit 0d370ca

Please sign in to comment.