/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* dmex
*
*/
namespace CustomBuildTool
{
///
/// Provides the entry point and core command-line processing logic for the custom build tool application.
///
public static class Program
{
///
/// Initializes the build environment and command-line arguments.
///
/// An array of command-line arguments.
/// A task that represents the asynchronous operation. The task result contains the process exit code.
public static async Task Main(string[] Args)
{
if (!await Build.InitializeBuildEnvironment())
return 1;
return await Program.InitializeCommandLine(Args);
}
///
/// Initializes the command-line interface, configuring global options and subcommands.
///
/// Sets up global options such as verbose output and argument file support, registers
/// all available subcommands, and defines the default handler for missing arguments.
/// A task that represents the asynchronous operation and contains the process exit code.
private static Task InitializeCommandLine(string[] Args)
{
var rootCommand = new RootCommand("CustomBuildTool for System Informer.");
// Global Options
var verboseOption = new Option("-verbose", ["--verbose"])
{
Description = "Enables verbose output.",
Recursive = true
};
rootCommand.Add(verboseOption);
var argsFileOption = new Option("-argsfile")
{
Description = "Read arguments from a file instead of the command line.",
Recursive = true
};
rootCommand.Add(argsFileOption);
// Subcommands
rootCommand.Add(CreateWriteToolsIdCommand());
rootCommand.Add(CreateCleanupCommand());
rootCommand.Add(CreateAzSignCommand());
rootCommand.Add(CreateCleanSdkCommand());
rootCommand.Add(CreateCheckMsvcCommand());
rootCommand.Add(CreateInstallMsvcCommand());
rootCommand.Add(CreateDynDataCommand());
rootCommand.Add(CreatePhAppPubGenCommand());
rootCommand.Add(CreatePhntHeadersGenCommand());
rootCommand.Add(CreateKphSignCommand());
rootCommand.Add(CreateDecryptCommand());
rootCommand.Add(CreateEncryptCommand());
rootCommand.Add(CreateReflowCommand());
rootCommand.Add(CreateReflowValidCommand(verboseOption));
rootCommand.Add(CreateReflowRevertCommand());
rootCommand.Add(CreateVtScanCommand());
rootCommand.Add(CreateDevEnvBuildCommand());
rootCommand.Add(CreateBinCommand(verboseOption));
rootCommand.Add(CreatePipelineBuildCommand(verboseOption));
rootCommand.Add(CreatePipelinePackageCommand(verboseOption));
rootCommand.Add(CreatePipelineDeployCommand(verboseOption));
rootCommand.Add(CreateMsixBuildCommand(verboseOption));
rootCommand.Add(CreateSdkCommand(verboseOption));
rootCommand.Add(CreateDebugCommand(verboseOption));
rootCommand.Add(CreateReleaseCommand(verboseOption));
rootCommand.Add(CreateCMakeBuildCommand(verboseOption));
rootCommand.Add(CreateCMakeBinCommand(verboseOption));
rootCommand.Add(CreateCMakeReleaseCommand(verboseOption));
rootCommand.Add(CreateCMakePipelineBuildCommand(verboseOption));
rootCommand.Add(CreateCMakePipelinePackageCommand(verboseOption));
rootCommand.Add(CreateCMakePipelineDeployCommand(verboseOption));
rootCommand.Add(CreateCheckThirdPartyCommand());
// Default handler when no command is provided
rootCommand.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(verboseOption);
Dictionary ProgramArgsHelp = new(StringComparer.OrdinalIgnoreCase)
{
{ "-argsfile", "Read arguments from a file instead of the command line." },
{ "-build-debug", "Builds the debug configuration." },
{ "-build-release", "Builds the release configuration." },
{ "-build-devenv", "Builds using devenv configuration." },
{ "-build-msix", "Builds MSIX store package." },
{ "-build-zip", "Builds the binary package." },
{ "-check_msvc", "Check required build dependencies are installed." },
{ "-check-thirdparty", "Checks thirdparty library versions against latest GitHub releases." },
{ "-check-install", "Installs any missing build dependencies." },
{ "-cleanup", "Cleans up the build environment." },
{ "-cleansdk", "Cleans SDK build artifacts (internal)." },
{ "-cmake-bin", "Builds the binary package using CMake (clang)." },
{ "-cmake-pipeline-build", "Performs pipeline build operations using CMake (clang)." },
{ "-cmake-pipeline-deploy", "Deploys pipeline artifacts using CMake (clang)." },
{ "-cmake-pipeline-package", "Packages pipeline artifacts using CMake (clang)." },
{ "-cmake-release", "Builds release configuration using CMake (clang)." },
{ "-sign-az", "Creates signature files for build." },
{ "-sign-kd", "Creates signature files for build." },
{ "-decrypt", "Decrypts a file." },
{ "-encrypt", "Encrypts a file." },
{ "-sdk_headers_gen", "Generates public header files." },
{ "-sdk_header_gen", "Generates single native header." },
{ "-pipeline-build", "Performs pipeline build operations." },
{ "-pipeline-deploy", "Deploys pipeline artifacts." },
{ "-pipeline-package", "Packages pipeline artifacts." },
{ "-help", "Shows this help message." },
{ "-verbose", "Enables verbose output." },
{ "-dyndata", "Generates dynamic data." },
{ "-reflowrevert", "Revert export header to previous state." },
{ "-reflowvalid", "Validates the current export header." },
{ "-reflow", "Generates export header definitions." },
{ "-vtscan", "Uploads a file to VirusTotal for scanning." },
{ "-write-tools-id", "Writes the tools id file (internal)." },
};
PrintColorMessage("Error: Missing required arguments. Use -h or --help for valid commands.\r\n", ConsoleColor.Red);
});
rootCommand.Add(new VersionOption());
rootCommand.Add(new DiagramDirective());
rootCommand.Add(new System.CommandLine.Completions.SuggestDirective());
return rootCommand.Parse(Args).InvokeAsync(new InvocationConfiguration
{
EnableDefaultExceptionHandler = true,
ProcessTerminationTimeout = TimeSpan.FromSeconds(2)
});
}
///
/// Creates a command that writes the tool ID file.
///
/// A command configured to execute the tool ID file writing operation.
private static Command CreateWriteToolsIdCommand()
{
var cmd = new Command("-write-tools-id", "Writes the tools id file (internal).");
cmd.SetAction(_ => BuildToolsId.WriteToolsId());
return cmd;
}
///
/// Creates a command that cleans the build environment and displays build statistics.
///
/// A command configured to perform cleanup operations and show build statistics.
private static Command CreateCleanupCommand()
{
var cmd = new Command("-cleanup", "Cleans up the build environment.");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
Build.CleanupBuildEnvironment();
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that generates signature files for a specified build path.
///
/// A Command object configured to sign files using Azure.
private static Command CreateAzSignCommand()
{
var cmd = new Command("-azsign", "Creates signature files for build.");
var pathArg = new Argument("path")
{
Description = "Path to sign"
};
cmd.Add(pathArg);
cmd.SetAction(parseResult =>
{
string path = parseResult.GetValue(pathArg);
BuildToolsId.CheckForOutOfDateTools();
if (!BuildAzure.SignFiles(path)) Environment.Exit(1);
});
return cmd;
}
///
/// Creates a command that cleans SDK build artifacts for all supported architectures.
///
/// A command configured to clean SDK build artifacts.
private static Command CreateCleanSdkCommand()
{
var cmd = new Command("-cleansdk", "Cleans SDK build artifacts (internal).");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Build32bit | BuildFlags.Build64bit | BuildFlags.BuildArm64bit | BuildFlags.BuildVerbose | BuildFlags.BuildApi;
if (!Build.BuildSolutionParallel("SystemInformer.sln", flags)) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that verifies the required MSVC build dependencies are installed.
///
/// A command configured to check for MSVC build dependencies and display build statistics.
private static Command CreateCheckMsvcCommand()
{
var cmd = new Command("-check_msvc", "Check required build dependencies are installed.");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
if (!BuildVisualStudio.CheckBuildDependencies()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that installs any missing MSVC build dependencies.
///
/// A command configured to handle the installation of missing MSVC build dependencies.
private static Command CreateInstallMsvcCommand()
{
var cmd = new Command("-install_msvc", "Installs any missing build dependencies.");
cmd.SetAction(async _ =>
{
BuildToolsId.CheckForOutOfDateTools();
if (!await BuildVisualStudio.InstallBuildDependencies()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that processes dynamic data using a specified argument.
///
/// A command configured to handle dynamic data operations.
private static Command CreateDynDataCommand()
{
var cmd = new Command("-dyndata", "Builds dynamic data.");
var arg = new Argument("arg")
{
Description = "Dynamic data argument (optional)",
DefaultValueFactory = _ => string.Empty
};
cmd.Add(arg);
cmd.SetAction(parseResult =>
{
string a = parseResult.GetValue(arg);
BuildToolsId.CheckForOutOfDateTools();
if (!Build.BuildDynamicData(a))
Environment.Exit(1);
});
return cmd;
}
///
/// Creates a command that generates public header files.
///
/// A Command object configured to generate public header files.
private static Command CreatePhAppPubGenCommand()
{
var cmd = new Command("-phapppub_gen", "Generates public header files.");
cmd.Aliases.Add("-sdk_headers_gen");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
if (!Build.BuildPublicHeaderFiles()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that generates a single native header file.
///
/// A configured command for generating a single native header.
private static Command CreatePhntHeadersGenCommand()
{
var cmd = new Command("-phnt_headers_gen", "Builds single native header.");
cmd.Aliases.Add("-sdk_header_gen");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
if (!Build.BuildSingleNativeHeader()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that generates signature files for the build process using a specified key name.
///
/// A command configured to generate signature files with the provided key name.
private static Command CreateKphSignCommand()
{
var cmd = new Command("-kphsign", "Creates signature files for build.");
var arg = new Argument("keyName")
{
Description = "Key name"
};
cmd.Add(arg);
cmd.SetAction(ParseResult =>
{
string k = ParseResult.GetValue(arg);
BuildToolsId.CheckForOutOfDateTools();
if (!BuildVerify.CreateSigFile("kph", k, Build.BuildCanary)) Environment.Exit(1);
});
return cmd;
}
///
/// Creates a command that decrypts a file using parameters specified in a configuration file.
///
/// A command configured to perform file decryption.
private static Command CreateDecryptCommand()
{
var cmd = new Command("-decrypt", "Decrypts a file.");
var configOpt = new Option("-config")
{
Description = "Config file"
};
cmd.Add(configOpt);
cmd.SetAction(parseResult =>
{
string config = parseResult.GetValue(configOpt);
BuildToolsId.CheckForOutOfDateTools();
var vargs = Utils.ParseArgumentsFromFile(config);
if (!BuildVerify.DecryptFile(vargs["-input"], vargs["-output"], vargs["-secret"], vargs["-salt"], vargs["-Iterations"]))
Environment.Exit(1);
});
return cmd;
}
///
/// Creates a command that encrypts a file using options specified in a configuration file.
///
/// A command configured to perform file encryption.
private static Command CreateEncryptCommand()
{
var cmd = new Command("-encrypt", "Encrypts a file.");
var configOpt = new Option("-config")
{
Description = "Config file"
};
cmd.Add(configOpt);
cmd.SetAction(parseResult =>
{
string config = parseResult.GetValue(configOpt);
BuildToolsId.CheckForOutOfDateTools();
var vargs = Utils.ParseArgumentsFromFile(config);
if (!BuildVerify.EncryptFile(vargs["-input"], vargs["-output"], vargs["-secret"], vargs["-salt"], vargs["-Iterations"]))
Environment.Exit(1);
});
return cmd;
}
///
/// Creates a command that exports the current export definitions.
///
/// A Command object configured to perform the export operation.
private static Command CreateReflowCommand()
{
var cmd = new Command("-reflow", "Exports the current export definitions.");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
Build.ExportDefinitions(true);
});
return cmd;
}
///
/// Creates a command that validates the current export definitions.
///
/// Specifies whether to enable verbose output.
/// A command configured to validate export definitions.
private static Command CreateReflowValidCommand(Option VerboseOption)
{
var cmd = new Command("-reflowvalid", "Validates the current export definitions.");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(false);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildValidateExportDefinitions(flags)) Environment.Exit(1);
});
return cmd;
}
///
/// Creates a command that reverts export definitions to their previous state.
///
/// A command configured to execute the revert operation.
private static Command CreateReflowRevertCommand()
{
var cmd = new Command("-reflowrevert", "Revert export definitions to previous state.");
cmd.SetAction(_ =>
{
BuildToolsId.CheckForOutOfDateTools();
Build.ExportDefinitionsRevert();
});
return cmd;
}
///
/// Creates a command that uploads a specified file to VirusTotal for scanning.
///
/// A command configured to handle file uploads to VirusTotal.
private static Command CreateVtScanCommand()
{
var cmd = new Command("-vtscan", "Uploads a file to VirusTotal for scanning.");
var fileOpt = new Option("-file")
{
Description = "File to scan"
};
cmd.Add(fileOpt);
cmd.SetAction(async parseResult =>
{
string file = parseResult.GetValue(fileOpt);
BuildToolsId.CheckForOutOfDateTools();
await BuildVirusTotal.UploadScanFile(file);
});
return cmd;
}
///
/// Creates a command that executes the specified DevEnv build command.
///
/// A Command object configured to run the DevEnv build process.
private static Command CreateDevEnvBuildCommand()
{
var arg = new Argument("command")
{
Description = "DevEnv command"
};
var cmd = new Command("-devenv-build", "Runs devenv build.");
cmd.Aliases.Add("-build-devenv");
cmd.SetAction(parseResult =>
{
string c = parseResult.GetValue(arg);
BuildToolsId.CheckForOutOfDateTools();
Build.SetupBuildEnvironment(true);
Utils.ExecuteDevEnvCommand(c);
Build.ShowBuildStats();
});
cmd.Add(arg);
return cmd;
}
///
/// Creates a command that builds the binary package with optional verbose output.
///
/// Specifies whether to enable verbose output during the build process.
/// A Command object configured to build the binary package.
private static Command CreateBinCommand(Option VerboseOption)
{
var cmd = new Command("-bin", "Builds the binary package.");
cmd.Aliases.Add("-build-zip");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildSolutionParallel("SystemInformer.sln", flags))
Environment.Exit(1);
if (!Build.BuildSolutionParallel("plugins\\Plugins.sln", flags))
Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags))
Environment.Exit(1);
if (!Build.BuildBinZip(flags))
Environment.Exit(1);
if (!Build.CopyTextFiles(false, flags))
Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that executes pipeline build operations, including environment setup, solution builds, and
/// post-build tasks.
///
/// Specifies whether to enable verbose output during the build process.
/// A command configured to perform pipeline build operations.
private static Command CreatePipelineBuildCommand(Option VerboseOption)
{
var cmd = new Command("-pipeline-build", "Performs pipeline build operations.");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
Win32.SetLowIntegrityForProcesses();
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
Build.CopySourceLink(true);
if (!Build.BuildSolutionParallel("SystemInformer.sln", flags))
Environment.Exit(1);
if (!Build.BuildSolutionParallel("plugins\\Plugins.sln", flags))
Environment.Exit(1);
Build.CopyWow64Files(flags);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that packages pipeline artifacts with optional verbose output.
///
/// Enables verbose output during packaging when set to true.
/// A command configured to package pipeline artifacts.
private static Command CreatePipelinePackageCommand(Option VerboseOption)
{
var cmd = new Command("-pipeline-package", "Packages pipeline artifacts.");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
BuildToolsId.CheckForOutOfDateTools();
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.ResignFiles("bin")) Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags)) Environment.Exit(1);
if (!Build.BuildBinZip(flags)) Environment.Exit(1);
foreach (var (channel, _) in BuildConfig.Build_Channels)
{
if (!Build.BuildSetupExe(channel, flags))
Environment.Exit(1);
}
if (!Build.CopyTextFiles(false, flags)) Environment.Exit(1);
if (!Build.BuildPdbZip(false, flags)) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that deploys pipeline artifacts with optional verbose output.
///
/// Specifies whether to enable verbose logging during deployment.
/// A command configured to deploy pipeline artifacts.
private static Command CreatePipelineDeployCommand(Option VerboseOption)
{
var cmd = new Command("-pipeline-deploy", "Deploys pipeline artifacts.");
cmd.SetAction(async parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!await BuildDeploy.BuildUpdateServerConfig())
Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that builds an MSIX store package with optional verbose output.
///
/// Indicates whether verbose output is enabled during the build process.
/// A command configured to execute the MSIX build process.
private static Command CreateMsixBuildCommand(Option VerboseOption)
{
var cmd = new Command("-msix-build", "Builds MSIX store package.");
cmd.Aliases.Add("-build-msix");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | BuildFlags.BuildMsix | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
Build.CopySourceLink(true);
if (!Build.BuildSolutionParallel("SystemInformer.sln", flags)) Environment.Exit(1);
if (!Build.BuildSolutionParallel("plugins\\Plugins.sln", flags)) Environment.Exit(1);
if (!Build.CopyWow64Files(flags)) Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags)) Environment.Exit(1);
if (!Build.BuildStorePackage(flags)) Environment.Exit(1);
if (!Build.CopyTextFiles(false, flags)) Environment.Exit(1);
if (!Build.BuildPdbZip(true, flags)) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command for building the SDK package with configurable build options.
///
/// Specifies whether to enable verbose output during the build process.
/// A command configured to build the SDK package with the specified options.
private static Command CreateSdkCommand(Option VerboseOption)
{
var cmd = new Command("-sdk", "Builds the SDK package.");
var cmakeOpt = new Option("-cmake") { Description = "Use CMake" };
var debugOpt = new Option("-Debug", ["-debug"]) { Description = "Debug build" };
var releaseOpt = new Option("-release", ["-Release"]) { Description = "Release build" };
var win32Opt = new Option("-Win32", ["-win32", "-WIN32"]) { Description = "Win32" };
var x64Opt = new Option("-x64", ["-X64"]) { Description = "x64" };
var arm64Opt = new Option("-arm64", ["-Arm64", "-ARM64"]) { Description = "ARM64" };
cmd.Add(cmakeOpt); cmd.Add(debugOpt); cmd.Add(releaseOpt);
cmd.Add(win32Opt); cmd.Add(x64Opt); cmd.Add(arm64Opt);
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
bool cmake = parseResult.GetValue(cmakeOpt);
bool debug = parseResult.GetValue(debugOpt);
bool release = parseResult.GetValue(releaseOpt);
bool win32 = parseResult.GetValue(win32Opt);
bool x64 = parseResult.GetValue(x64Opt);
bool arm64 = parseResult.GetValue(arm64Opt);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.None;
Build.SetupBuildEnvironment(false);
if (verbose) flags |= BuildFlags.BuildVerbose;
if (cmake) flags |= BuildFlags.BuildCMake;
if (debug)
{
flags |= BuildFlags.BuildDebug;
if (win32) flags |= BuildFlags.Build32bit;
else if (x64) flags |= BuildFlags.Build64bit;
else if (arm64) flags |= BuildFlags.BuildArm64bit;
else Environment.Exit(1);
}
else if (release)
{
flags |= BuildFlags.BuildRelease;
if (win32) flags |= BuildFlags.Build32bit;
else if (x64) flags |= BuildFlags.Build64bit;
else if (arm64) flags |= BuildFlags.BuildArm64bit;
else Environment.Exit(1);
}
else Environment.Exit(1);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.CopyResourceFiles(flags)) Environment.Exit(1);
if (!Build.BuildSdk(flags)) Environment.Exit(1);
if (!Build.CopyKernelDriver(flags)) Environment.Exit(1);
if (!Build.CopyWow64Files(flags)) Environment.Exit(1);
if (verbose) Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that builds the debug configuration.
///
/// Specifies whether to enable verbose output.
/// A command configured to build the debug configuration.
private static Command CreateDebugCommand(Option VerboseOption)
{
var cmd = new Command("-debug", "Builds the debug configuration.");
cmd.Aliases.Add("-build-debug");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Debug | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildSolutionParallel("SystemInformer.sln", flags)) Environment.Exit(1);
if (!Build.BuildSolutionParallel("plugins\\Plugins.sln", flags)) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that builds the release configuration.
///
/// Specifies whether to enable verbose output.
/// A command configured to build the release configuration.
private static Command CreateReleaseCommand(Option VerboseOption)
{
var cmd = new Command("-release", "Builds the release configuration.");
cmd.Aliases.Add("-build-release");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildSolutionParallel("SystemInformer.sln", flags)) Environment.Exit(1);
if (!Build.BuildSolutionParallel("plugins\\Plugins.sln", flags)) Environment.Exit(1);
if (!Build.CopyWow64Files(flags)) Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags)) Environment.Exit(1);
if (!Build.BuildBinZip(flags)) Environment.Exit(1);
foreach (var (channel, _) in BuildConfig.Build_Channels)
{
if (!Build.BuildSetupExe(channel, flags)) Environment.Exit(1);
}
if (!Build.CopyTextFiles(false, flags)) Environment.Exit(1);
if (!Build.BuildPdbZip(false, flags)) Environment.Exit(1);
if (!Build.BuildChecksumsFile()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that builds the project using CMake.
///
/// Specifies whether to enable verbose output.
/// A command configured to perform a CMake build.
private static Command CreateCMakeBuildCommand(Option VerboseOption)
{
var cmd = new Command("-cmake-build", "Builds using CMake.");
var genOpt = new Option("-generator") { Description = "Generator" };
var toolOpt = new Option("-toolchain") { Description = "Toolchain" };
var confOpt = new Option("-config") { Description = "Config" };
cmd.Add(genOpt); cmd.Add(toolOpt); cmd.Add(confOpt);
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
string generatorArg = parseResult.GetValue(genOpt);
string toolchainArg = parseResult.GetValue(toolOpt);
string configArg = parseResult.GetValue(confOpt);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
var generator = Utils.GetGeneratorFromString(generatorArg);
var toolchain = Utils.GetToolchainFromString(toolchainArg);
if (!Build.BuildSolutionCMake("SystemInformer", generator, toolchain, flags))
Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that builds the binary package using CMake (clang).
///
/// Specifies whether to enable verbose output.
/// A command configured to build the binary package using CMake.
private static Command CreateCMakeBinCommand(Option VerboseOption)
{
var cmd = new Command("-cmake-bin", "Builds the binary package using CMake (clang).");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildSolutionCMakeMatrix("SystemInformer", BuildGenerator.Ninja,
[BuildToolchain.ClangMsvcX86, BuildToolchain.ClangMsvcAmd64, BuildToolchain.ClangMsvcArm64], flags))
Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags)) Environment.Exit(1);
if (!Build.BuildBinZip(flags)) Environment.Exit(1);
if (!Build.CopyTextFiles(false, flags)) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that builds the release configuration using CMake (clang).
///
/// Specifies whether to enable verbose output.
/// A command configured to perform a CMake release build.
private static Command CreateCMakeReleaseCommand(Option VerboseOption)
{
var cmd = new Command("-cmake-release", "Builds release configuration using CMake (clang).");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildSolutionCMakeMatrix("SystemInformer", BuildGenerator.Ninja,
[BuildToolchain.ClangMsvcX86, BuildToolchain.ClangMsvcAmd64, BuildToolchain.ClangMsvcArm64], flags))
Environment.Exit(1);
if (!Build.CopyWow64Files(flags)) Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags)) Environment.Exit(1);
if (!Build.BuildBinZip(flags)) Environment.Exit(1);
foreach (var (channel, _) in BuildConfig.Build_Channels)
{
if (!Build.BuildSetupExe(channel, flags)) Environment.Exit(1);
}
if (!Build.CopyTextFiles(false, flags)) Environment.Exit(1);
if (!Build.BuildPdbZip(false, flags)) Environment.Exit(1);
if (!Build.BuildChecksumsFile()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that performs pipeline build operations using CMake (clang).
///
/// Specifies whether to enable verbose output.
/// A command configured for CMake pipeline builds.
private static Command CreateCMakePipelineBuildCommand(Option VerboseOption)
{
var cmd = new Command("-cmake-pipeline-build", "Performs pipeline build operations using CMake (clang).");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
BuildToolsId.CheckForOutOfDateTools();
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
Build.CopySourceLink(true);
if (!Build.BuildSolutionCMakeMatrix("SystemInformer", BuildGenerator.Ninja,
[BuildToolchain.ClangMsvcX86, BuildToolchain.ClangMsvcAmd64, BuildToolchain.ClangMsvcArm64], flags))
Environment.Exit(1);
Build.CopyWow64Files(flags);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that packages pipeline artifacts using CMake (clang).
///
/// Specifies whether to enable verbose output.
/// A command configured for CMake pipeline packaging.
private static Command CreateCMakePipelinePackageCommand(Option VerboseOption)
{
var cmd = new Command("-cmake-pipeline-package", "Packages pipeline artifacts using CMake (clang).");
cmd.SetAction(parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.ResignFiles("bin-cmake")) Environment.Exit(1);
if (!Build.CopyTextFiles(true, flags)) Environment.Exit(1);
if (!Build.BuildBinZip(flags)) Environment.Exit(1);
foreach (var (channel, _) in BuildConfig.Build_Channels)
{
if (!Build.BuildSetupExe(channel, flags)) Environment.Exit(1);
}
if (!Build.CopyTextFiles(false, flags)) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that deploys pipeline artifacts using CMake (clang).
///
/// Specifies whether to enable verbose output.
/// A command configured for CMake pipeline deployment.
private static Command CreateCMakePipelineDeployCommand(Option VerboseOption)
{
var cmd = new Command("-cmake-pipeline-deploy", "Deploys pipeline artifacts using CMake (clang).");
cmd.SetAction(async parseResult =>
{
bool verbose = parseResult.GetValue(VerboseOption);
BuildToolsId.CheckForOutOfDateTools();
BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None);
Build.SetupBuildEnvironment(true);
if (!Build.TryNormalizeBuildFlags(ref flags, true)) Environment.Exit(1);
if (!Build.BuildPdbZip(false, flags)) Environment.Exit(1);
if (!await BuildDeploy.BuildUpdateServerConfig()) Environment.Exit(1);
Build.ShowBuildStats();
});
return cmd;
}
///
/// Creates a command that checks thirdparty library versions against their latest GitHub releases.
///
/// A command configured to check thirdparty library versions.
private static Command CreateCheckThirdPartyCommand()
{
var cmd = new Command("-check-thirdparty", "Checks thirdparty library versions against latest GitHub releases.");
cmd.SetAction(async _ =>
{
BuildToolsId.CheckForOutOfDateTools();
await BuildThirdParty.CheckThirdPartyVersions();
Build.ShowBuildStats();
});
return cmd;
}
// ==========================================
// Console Helper Methods
// ==========================================
///
/// Prints a colorized message to the console.
///
/// The message to print.
/// The console color to use.
/// Whether to append a newline.
/// Build flags for verbosity control.
public static void PrintColorMessage(string Message, ConsoleColor Color, bool Newline = true, BuildFlags Flags = BuildFlags.BuildVerbose)
{
if ((Flags & BuildFlags.BuildVerbose) == 0) return;
if (Build.BuildIntegration)
{
var colour_ansi = ToAnsiCode(Color);
if (Message.Contains('\n', StringComparison.OrdinalIgnoreCase))
{
var text = new StringBuilder(Message.Length + 16);
int start = 0;
for (int i = 0; i < Message.Length; i++)
{
if (Message[i] == '\n')
{
text.Append(colour_ansi);
text.Append(Message, start, i - start + 1);
start = i + 1;
}
}
if (start < Message.Length) text.Append(colour_ansi).Append(Message, start, Message.Length - start);
text.Append("\e[0m");
if (Newline) Console.WriteLine(text.ToString());
else Console.Write(text.ToString());
}
else
{
var color_reset = $"{colour_ansi}{Message}\e[0m";
if (Newline) Console.WriteLine(color_reset);
else Console.Write(color_reset);
}
}
else
{
Console.ForegroundColor = Color;
if (Newline) Console.WriteLine(Message);
else Console.Write(Message);
Console.ResetColor();
}
}
///
/// Converts a to its ANSI escape sequence.
///
/// The console color.
/// The ANSI escape sequence string.
public static string ToAnsiCode(ConsoleColor Color)
{
return Color switch
{
ConsoleColor.Black => "\e[30m",
ConsoleColor.DarkBlue => "\e[34m",
ConsoleColor.DarkGreen => "\e[32m",
ConsoleColor.DarkCyan => "\e[36m",
ConsoleColor.DarkRed => "\e[31m",
ConsoleColor.DarkMagenta => "\e[35m",
ConsoleColor.DarkYellow => "\e[33m",
ConsoleColor.Gray => "\e[37m",
ConsoleColor.DarkGray => "\e[90m",
ConsoleColor.Blue => "\e[94m",
ConsoleColor.Green => "\e[92m",
ConsoleColor.Cyan => "\e[96m",
ConsoleColor.Red => "\e[91m",
ConsoleColor.Magenta => "\e[95m",
ConsoleColor.Yellow => "\e[93m",
ConsoleColor.White => "\e[97m",
_ => "\e[0m"
};
}
///
/// Creates a terminal hyperlink.
///
/// The target URI.
/// The display text.
/// A formatted string with the terminal escape sequence for a hyperlink.
public static string CreateConsoleHyperlink(string Uri, string Text)
{
return $"\e]8;;{Uri}\e\\{Text}\e]8;;\e\\";
}
}
}