From 9966f2de119642260b58222b1f40d702e92d5744 Mon Sep 17 00:00:00 2001 From: Christopher Whitley Date: Mon, 29 Apr 2024 16:53:16 -0400 Subject: [PATCH 1/4] Add proj to sln --- MonoGame.Tool.BuildScripts.sln | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/MonoGame.Tool.BuildScripts.sln b/MonoGame.Tool.BuildScripts.sln index 58ea566..88bbca6 100644 --- a/MonoGame.Tool.BuildScripts.sln +++ b/MonoGame.Tool.BuildScripts.sln @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Tool.BuildScripts", "MonoGame.Tool.BuildScripts.csproj", "{BADF2727-6775-4E00-93D2-6725D2E3F5BE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -11,4 +13,10 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BADF2727-6775-4E00-93D2-6725D2E3F5BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BADF2727-6775-4E00-93D2-6725D2E3F5BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BADF2727-6775-4E00-93D2-6725D2E3F5BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BADF2727-6775-4E00-93D2-6725D2E3F5BE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection EndGlobal From cafc7102b1b263056426824b6cb225462328f409 Mon Sep 17 00:00:00 2001 From: Christopher Whitley Date: Mon, 29 Apr 2024 17:11:51 -0400 Subject: [PATCH 2/4] Added extension for shell execution --- FrostingContextExtensions.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 FrostingContextExtensions.cs diff --git a/FrostingContextExtensions.cs b/FrostingContextExtensions.cs new file mode 100644 index 0000000..fa8de39 --- /dev/null +++ b/FrostingContextExtensions.cs @@ -0,0 +1,27 @@ +namespace BuildScripts; + +public static class FrostingContextExtensions +{ + private static readonly ProcessSettings _processSettings; + + static FrostingContextExtensions() => _processSettings = new ProcessSettings(); + + public static void SetShellWorkingDir(this FrostingContext context, string path) + { + _processSettings.WorkingDirectory = path; + } + + public static int ShellExecute(this FrostingContext context, string command, string environmentVariables = "") + { + string shellCommandPath = context switch + { + _ when context.IsRunningOnWindows() => @"C:\msys64\usr\bin\bash.exe", + _ when context.IsRunningOnLinux() => "sh", + _ when context.IsRunningOnMacOs() => "zsh", + _ => throw new PlatformNotSupportedException("Unsupported Platform") + }; + + _processSettings.Arguments = $"-c \"{environmentVariables} {command}\""; + return context.StartProcess(shellCommandPath, _processSettings); + } +} \ No newline at end of file From 99399d06404772193246100af83ad883db308eef Mon Sep 17 00:00:00 2001 From: Christopher Whitley Date: Mon, 29 Apr 2024 17:25:47 -0400 Subject: [PATCH 3/4] Remove static initializer and use expression body for setting working dir --- FrostingContextExtensions.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/FrostingContextExtensions.cs b/FrostingContextExtensions.cs index fa8de39..724323e 100644 --- a/FrostingContextExtensions.cs +++ b/FrostingContextExtensions.cs @@ -2,14 +2,9 @@ namespace BuildScripts; public static class FrostingContextExtensions { - private static readonly ProcessSettings _processSettings; + private static readonly ProcessSettings _processSettings = new(); - static FrostingContextExtensions() => _processSettings = new ProcessSettings(); - - public static void SetShellWorkingDir(this FrostingContext context, string path) - { - _processSettings.WorkingDirectory = path; - } + public static void SetShellWorkingDir(this FrostingContext context, string path) => _processSettings.WorkingDirectory = path; public static int ShellExecute(this FrostingContext context, string command, string environmentVariables = "") { From 23d2fc12a44a3493c071aa9c863ae82dfe29c693 Mon Sep 17 00:00:00 2001 From: Christopher Whitley Date: Mon, 29 Apr 2024 22:59:44 -0400 Subject: [PATCH 4/4] Added extension methods for FrostingContext --- FrostingContextExtensions.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/FrostingContextExtensions.cs b/FrostingContextExtensions.cs index 724323e..00edf15 100644 --- a/FrostingContextExtensions.cs +++ b/FrostingContextExtensions.cs @@ -3,10 +3,24 @@ namespace BuildScripts; public static class FrostingContextExtensions { private static readonly ProcessSettings _processSettings = new(); + private static string _environmentVariables = string.Empty; public static void SetShellWorkingDir(this FrostingContext context, string path) => _processSettings.WorkingDirectory = path; - public static int ShellExecute(this FrostingContext context, string command, string environmentVariables = "") + public static void SetShellEnvironmentVariables(this FrostingContext context, params string[] env) + { + _environmentVariables = string.Empty; + for(int i = 0; i < env.Length; i+= 2) + { + string key = env[i]; + string value = $"'{env[i + 1]}'"; + _environmentVariables += $"export {key}={value};"; + } + } + + public static void ClearShellEnvironmentVariables(this FrostingContext context) => _environmentVariables = string.Empty; + + public static int ShellExecute(this FrostingContext context, string command) { string shellCommandPath = context switch { @@ -16,7 +30,8 @@ _ when context.IsRunningOnMacOs() => "zsh", _ => throw new PlatformNotSupportedException("Unsupported Platform") }; - _processSettings.Arguments = $"-c \"{environmentVariables} {command}\""; + _processSettings.Arguments = $"-c \"{_environmentVariables} {command}\""; + context.Information($"Executing: {shellCommandPath} {string.Join(' ', _processSettings.Arguments)}"); return context.StartProcess(shellCommandPath, _processSettings); } } \ No newline at end of file