/* * 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"], "Enables verbose output."); rootCommand.AddGlobalOption(verboseOption); var argsFileOption = new Option(["-argsfile"], "Read arguments from a file instead of the command line."); rootCommand.AddGlobalOption(argsFileOption); // Subcommands rootCommand.AddCommand(CreateWriteToolsIdCommand()); rootCommand.AddCommand(CreateCleanupCommand()); rootCommand.AddCommand(CreateAzSignCommand()); rootCommand.AddCommand(CreateCleanSdkCommand()); rootCommand.AddCommand(CreateCheckMsvcCommand()); rootCommand.AddCommand(CreateInstallMsvcCommand()); rootCommand.AddCommand(CreateDynDataCommand()); rootCommand.AddCommand(CreatePhAppPubGenCommand()); rootCommand.AddCommand(CreatePhntHeadersGenCommand()); rootCommand.AddCommand(CreateKphSignCommand()); rootCommand.AddCommand(CreateDecryptCommand()); rootCommand.AddCommand(CreateEncryptCommand()); rootCommand.AddCommand(CreateReflowCommand()); rootCommand.AddCommand(CreateReflowValidCommand(verboseOption)); rootCommand.AddCommand(CreateReflowRevertCommand()); rootCommand.AddCommand(CreateVtScanCommand()); rootCommand.AddCommand(CreateDevEnvBuildCommand()); rootCommand.AddCommand(CreateBinCommand(verboseOption)); rootCommand.AddCommand(CreatePipelineBuildCommand(verboseOption)); rootCommand.AddCommand(CreatePipelinePackageCommand(verboseOption)); rootCommand.AddCommand(CreatePipelineDeployCommand(verboseOption)); rootCommand.AddCommand(CreateMsixBuildCommand(verboseOption)); rootCommand.AddCommand(CreateSdkCommand(verboseOption)); rootCommand.AddCommand(CreateDebugCommand(verboseOption)); rootCommand.AddCommand(CreateReleaseCommand(verboseOption)); rootCommand.AddCommand(CreateCMakeBuildCommand(verboseOption)); rootCommand.AddCommand(CreateCMakeBinCommand(verboseOption)); rootCommand.AddCommand(CreateCMakeReleaseCommand(verboseOption)); rootCommand.AddCommand(CreateCMakePipelineBuildCommand(verboseOption)); rootCommand.AddCommand(CreateCMakePipelinePackageCommand(verboseOption)); rootCommand.AddCommand(CreateCMakePipelineDeployCommand(verboseOption)); rootCommand.AddCommand(CreateCheckThirdPartyCommand()); // Default handler when no command is provided rootCommand.SetHandler((bool verbose) => { Dictionary ProgramArgsHelp = new(StringComparer.OrdinalIgnoreCase) { { "-argsfile", "Read arguments from a file instead of the command line." }, { "-bin", "Builds the binary package." }, { "-check_msvc", "Check required build dependencies are installed." }, { "-install_msvc", "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)." }, { "-debug", "Builds the debug configuration." }, { "-decrypt", "Decrypts a file." }, { "-devenv-build", "Runs devenv build." }, { "-dyndata", "Builds dynamic data." }, { "-encrypt", "Encrypts a file." }, { "-help", "Shows this help message." }, { "-msix-build", "Builds MSIX store package." }, { "-phapppub_gen", "Generates public header files." }, { "-phnt_headers_gen", "Builds single native header." }, { "-pipeline-build", "Performs pipeline build operations." }, { "-pipeline-deploy", "Deploys pipeline artifacts." }, { "-pipeline-package", "Packages pipeline artifacts." }, { "-azsign", "Creates signature files for build." }, { "-kphsign", "Creates signature files for build." }, { "-reflow", "Exports the current export definitions." }, { "-reflowrevert", "Revert export definitions to previous state." }, { "-reflowvalid", "Validates the current export definitions." }, { "-sdk", "Builds the SDK package." }, { "-verbose", "Enables verbose output." }, { "-vtscan", "Uploads a file to VirusTotal for scanning." }, { "-write-tools-id", "Writes the tools id file (internal)." }, { "-check-thirdparty", "Checks thirdparty library versions against latest GitHub releases." }, }; PrintColorMessage("Error: Missing required arguments. Use -h or --help for valid commands.\r\n", ConsoleColor.Red, true); }, verboseOption); var parser = new CommandLineBuilder(rootCommand) .UseVersionOption() .UseHelp() //.UseEnvironmentVariableDirective() .UseParseDirective() .UseSuggestDirective() .RegisterWithDotnetSuggest() .UseTypoCorrections() .UseParseErrorReporting() .UseExceptionHandler() .CancelOnProcessTermination() .Build(); return parser.InvokeAsync(args); } /// /// Creates a command that writes the tools ID file. /// /// A command configured to execute the tools ID file writing operation. private static Command CreateWriteToolsIdCommand() { var cmd = new Command("-write-tools-id", "Writes the tools id file (internal)."); cmd.SetHandler(() => 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.SetHandler(() => { 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", "Path to sign"); cmd.AddArgument(pathArg); cmd.SetHandler(async (string path) => { BuildToolsId.CheckForOutOfDateTools(); if (!await BuildAzure.SignFiles(path)) Environment.Exit(1); }, pathArg); 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.SetHandler(() => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Build32bit | BuildFlags.Build64bit | BuildFlags.BuildArm64bit | BuildFlags.BuildVerbose | BuildFlags.BuildApi; if (!Build.BuildSolution("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.SetHandler(() => { 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.SetHandler(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", () => string.Empty, "Dynamic data argument (optional)"); cmd.AddArgument(arg); cmd.SetHandler((string a) => { BuildToolsId.CheckForOutOfDateTools(); if (!Build.BuildDynamicData(a)) Environment.Exit(1); }, arg); 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.SetHandler(() => { 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.SetHandler(() => { 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", "Key name"); cmd.AddArgument(arg); cmd.SetHandler((string k) => { BuildToolsId.CheckForOutOfDateTools(); if (!BuildVerify.CreateSigFile("kph", k, Build.BuildCanary)) Environment.Exit(1); }, arg); 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", "Config file"); cmd.AddOption(configOpt); cmd.SetHandler((string config) => { BuildToolsId.CheckForOutOfDateTools(); var vargs = Utils.ParseArgumentsFromFile(config); if (!BuildVerify.DecryptFile(vargs["-input"], vargs["-output"], vargs["-secret"], vargs["-salt"], vargs["-Iterations"])) Environment.Exit(1); }, configOpt); 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", "Config file"); cmd.AddOption(configOpt); cmd.SetHandler((string config) => { BuildToolsId.CheckForOutOfDateTools(); var vargs = Utils.ParseArgumentsFromFile(config); if (!BuildVerify.EncryptFile(vargs["-input"], vargs["-output"], vargs["-secret"], vargs["-salt"], vargs["-Iterations"])) Environment.Exit(1); }, configOpt); 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.SetHandler(() => { 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); if (!Build.BuildValidateExportDefinitions(flags)) Environment.Exit(1); }, verboseOption); 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.SetHandler(() => { 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", "File to scan"); cmd.AddOption(fileOpt); cmd.SetHandler(async (string file) => { BuildToolsId.CheckForOutOfDateTools(); await BuildVirusTotal.UploadScanFile(file); }, fileOpt); 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", "DevEnv command"); var cmd = new Command("-devenv-build", "Runs devenv build."); cmd.AddArgument(arg); cmd.SetHandler((string c) => { BuildToolsId.CheckForOutOfDateTools(); Build.SetupBuildEnvironment(true); Utils.ExecuteDevEnvCommand(c); Build.ShowBuildStats(); }, 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildSolution("SystemInformer.sln", flags)) Environment.Exit(1); if (!Build.BuildSolution("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(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); Build.CopySourceLink(true); if (!Build.BuildSolution("SystemInformer.sln", flags)) Environment.Exit(1); if (!Build.BuildSolution("plugins\\Plugins.sln", flags)) Environment.Exit(1); Build.CopyWow64Files(flags); Build.ShowBuildStats(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); BuildToolsId.CheckForOutOfDateTools(); Build.SetupBuildEnvironment(true); 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); Build.ShowBuildStats(); }, verboseOption); 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.SetHandler(async (bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildPdbZip(false, flags)) Environment.Exit(1); if (!await BuildDeploy.BuildUpdateServerConfig()) Environment.Exit(1); Build.ShowBuildStats(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | BuildFlags.BuildMsix | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); Build.CopySourceLink(true); if (!Build.BuildSolution("SystemInformer.sln", flags)) Environment.Exit(1); if (!Build.BuildSolution("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(); }, verboseOption); 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", "Use CMake"); var debugOpt = new Option("-Debug", "Debug build"); var releaseOpt = new Option("-release", "Release build"); var win32Opt = new Option("-Win32", "Win32"); var x64Opt = new Option("-x64", "x64"); var arm64Opt = new Option("-arm64", "arm64"); debugOpt.AddAlias("-debug"); releaseOpt.AddAlias("-Release"); win32Opt.AddAlias("-win32"); win32Opt.AddAlias("-WIN32"); x64Opt.AddAlias("-X64"); arm64Opt.AddAlias("-Arm64"); arm64Opt.AddAlias("-ARM64"); cmd.AddOption(cmakeOpt); cmd.AddOption(debugOpt); cmd.AddOption(releaseOpt); cmd.AddOption(win32Opt); cmd.AddOption(x64Opt); cmd.AddOption(arm64Opt); cmd.SetHandler((bool verbose, bool cmake, bool debug, bool release, bool win32, bool x64, bool arm64) => { 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.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(); }, verboseOption, cmakeOpt, debugOpt, releaseOpt, win32Opt, x64Opt, arm64Opt); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Debug | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildSolution("SystemInformer.sln", flags)) Environment.Exit(1); if (!Build.BuildSolution("plugins\\Plugins.sln", flags)) Environment.Exit(1); Build.ShowBuildStats(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildSolution("SystemInformer.sln", flags)) Environment.Exit(1); if (!Build.BuildSolution("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(); }, verboseOption); 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", "Generator"); var toolOpt = new Option("-toolchain", "Toolchain"); var confOpt = new Option("-config", "Config"); cmd.AddOption(genOpt); cmd.AddOption(toolOpt); cmd.AddOption(confOpt); cmd.SetHandler((bool verbose, string generatorArg, string toolchainArg, string configArg) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); var generator = Utils.GetGeneratorFromString(generatorArg); var toolchain = Utils.GetToolchainFromString(toolchainArg); if (!Build.BuildSolutionCMake("SystemInformer", generator, toolchain, flags)) Environment.Exit(1); if (!Build.BuildSolutionCMake("Plugins", generator, toolchain, flags)) Environment.Exit(1); Build.ShowBuildStats(); }, verboseOption, genOpt, toolOpt, confOpt); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildSolutionCMake("SystemInformer", BuildGenerator.Ninja, BuildToolchain.ClangMsvcAmd64, flags)) Environment.Exit(1); if (!Build.BuildSolutionCMake("Plugins", BuildGenerator.Ninja, BuildToolchain.ClangMsvcAmd64, 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(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildSolutionCMake("SystemInformer", BuildGenerator.Ninja, BuildToolchain.ClangMsvcAmd64, flags)) Environment.Exit(1); if (!Build.BuildSolutionCMake("Plugins", BuildGenerator.Ninja, BuildToolchain.ClangMsvcAmd64, 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(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); BuildToolsId.CheckForOutOfDateTools(); Build.SetupBuildEnvironment(true); Build.CopySourceLink(true); if (!Build.BuildSolutionCMake("SystemInformer", BuildGenerator.Ninja, BuildToolchain.ClangMsvcAmd64, flags)) Environment.Exit(1); Build.CopyWow64Files(flags); Build.ShowBuildStats(); }, verboseOption); 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.SetHandler((bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); 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(); }, verboseOption); 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.SetHandler(async (bool verbose) => { BuildToolsId.CheckForOutOfDateTools(); BuildFlags flags = BuildFlags.Release | BuildFlags.BuildCMake | (verbose ? BuildFlags.BuildVerbose : BuildFlags.None); Build.SetupBuildEnvironment(true); if (!Build.BuildPdbZip(false, flags)) Environment.Exit(1); if (!await BuildDeploy.BuildUpdateServerConfig()) Environment.Exit(1); Build.ShowBuildStats(); }, verboseOption); 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.SetHandler(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(); } } /// /// Prints a colorized message to the console using an interpolated string handler. /// /// The interpolated string builder. /// The console color to use. /// Whether to append a newline. /// Build flags for verbosity control. public static void PrintColorMessage(LogInterpolatedStringHandler builder, ConsoleColor Color, bool Newline = true, BuildFlags Flags = BuildFlags.BuildVerbose) { if ((Flags & BuildFlags.BuildVerbose) == 0) return; var formattedText = builder.GetFormattedText(); if (Build.BuildIntegration) { var colour_ansi = ToAnsiCode(Color); if (formattedText.Contains('\n', StringComparison.OrdinalIgnoreCase)) { var sb = new StringBuilder(formattedText.Length + 16); int start = 0; for (int i = 0; i < formattedText.Length; i++) { if (formattedText[i] == '\n') { sb.Append(colour_ansi); sb.Append(formattedText, start, i - start + 1); start = i + 1; } } if (start < formattedText.Length) sb.Append(colour_ansi).Append(formattedText, start, formattedText.Length - start); sb.Append("\e[0m"); if (Newline) Console.WriteLine(sb.ToString()); else Console.Write(sb.ToString()); } else { var color_reset = $"{colour_ansi}{formattedText}\e[0m"; if (Newline) Console.WriteLine(color_reset); else Console.Write(color_reset); } } else { Console.ForegroundColor = Color; if (Newline) Console.WriteLine(formattedText); else Console.Write(formattedText); 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 $"\x1B]8;;{Uri}\x1B\\{Text}\x1B]8;;\x1B\\"; } } }