diff --git a/FrostingContextExtensions.cs b/FrostingContextExtensions.cs new file mode 100644 index 0000000..00edf15 --- /dev/null +++ b/FrostingContextExtensions.cs @@ -0,0 +1,37 @@ +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 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 + { + _ 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}\""; + context.Information($"Executing: {shellCommandPath} {string.Join(' ', _processSettings.Arguments)}"); + return context.StartProcess(shellCommandPath, _processSettings); + } +} \ No newline at end of file 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