public static IProcess Execute(this ICakeContext context, FilePath exe, string args) => context.ProcessRunner.Start( exe, new ProcessSettings { Arguments = ProcessArgumentBuilder.FromString(args) } );
public void BuildManifestImage(params string[] platforms) { if (platforms.Any() == false) { throw new ArgumentException( $"{nameof( platforms )} can not be empty", nameof(platforms) ); } { StringBuilder arguments = new StringBuilder(); arguments.Append($"manifest create {DockerConstants.ImageName}:latest"); foreach (string platform in platforms) { arguments.Append($" --amend {DockerConstants.GetPlatformImageName( platform )}"); } ProcessArgumentBuilder argumentsBuilder = ProcessArgumentBuilder.FromString(arguments.ToString()); ProcessSettings settings = new ProcessSettings { Arguments = argumentsBuilder }; int exitCode = context.StartProcess("docker", settings); if (exitCode != 0) { throw new ApplicationException( "Error when running docker to build latest manifest image. Got error: " + exitCode ); } } { string arguments = $"tag {DockerConstants.ImageName}:latest {DockerConstants.ImageName}:{MedituConstants.Version}"; ProcessArgumentBuilder argumentsBuilder = ProcessArgumentBuilder.FromString(arguments.ToString()); ProcessSettings settings = new ProcessSettings { Arguments = argumentsBuilder }; int exitCode = context.StartProcess("docker", settings); if (exitCode != 0) { throw new ApplicationException( "Error when running docker to build versioned manifest image. Got error: " + exitCode ); } } }
// ---------------- Functions ---------------- /// <summary> /// Runs git and gets the number of commits on the current branch. /// </summary> /// <param name="config"> /// Configuration. If null, it grabs the configuration /// from the passed in command-line arguments. /// </param> /// <returns> /// The number of commits that have happend on the current git branch. /// </returns> public int Run(GitQueryRevisionNumberConfig config = null) { if (config == null) { config = ArgumentBinder.FromArguments <GitQueryRevisionNumberConfig>(this.context); } int revNumber = -1; string onStdOut(string line) { if (string.IsNullOrWhiteSpace(line) == false) { if (int.TryParse(line, out revNumber) == false) { revNumber = -1; } } return(line); }; ProcessSettings processSettings = new ProcessSettings { Arguments = ProcessArgumentBuilder.FromString("rev-list --count HEAD"), RedirectStandardOutput = true, RedirectedStandardOutputHandler = onStdOut }; this.Run(this.toolSettings, processSettings.Arguments, processSettings, null); if (revNumber < 0) { throw new InvalidOperationException( "Could not get rev number from git" ); } if (config.NoPrint == false) { context.Information("Current Revision Number: " + revNumber); } if (string.IsNullOrWhiteSpace(config.OutputFile) == false) { System.IO.File.WriteAllText(config.OutputFile, revNumber.ToString()); } return(revNumber); }
public void PushPlatformImage(string platform) { string arguments = $"push {DockerConstants.GetPlatformImageName( platform )}"; ProcessArgumentBuilder argumentsBuilder = ProcessArgumentBuilder.FromString(arguments); ProcessSettings settings = new ProcessSettings { Arguments = argumentsBuilder }; int exitCode = context.StartProcess("docker", settings); if (exitCode != 0) { throw new ApplicationException( $"Error when trying to push docker platform image {platform}. Got error: " + exitCode ); } }
// ---------------- Functions ---------------- /// <summary> /// Runs git on the local repository and returns /// the name of the current branch. /// </summary> /// <param name="config"> /// Configuration. If null, it grabs the configuration /// from the passed in command-line arguments. /// </param> public string Run(GitQueryCurrentBranchConfig config = null) { if (config == null) { config = ArgumentBinder.FromArguments <GitQueryCurrentBranchConfig>(this.context); } string branch = null; string onStdOut(string line) { if (string.IsNullOrWhiteSpace(line) == false) { branch = line; } return(line); }; ProcessSettings processSettings = new ProcessSettings { Arguments = ProcessArgumentBuilder.FromString("rev-parse --abbrev-ref HEAD"), RedirectStandardOutput = true, RedirectedStandardOutputHandler = onStdOut }; this.Run(this.toolSettings, processSettings.Arguments, processSettings, null); if (branch == null) { throw new InvalidOperationException( "Could not get the current branch from Git" ); } if (config.NoPrint == false) { context.Information($"Current Branch: {branch}"); } return(branch); }
public void PushManifest() { foreach (string version in new string[] { "latest", MedituConstants.VersionString }) { string arguments = $"push {DockerConstants.ImageName}:{version}"; ProcessArgumentBuilder argumentsBuilder = ProcessArgumentBuilder.FromString(arguments); ProcessSettings settings = new ProcessSettings { Arguments = argumentsBuilder }; int exitCode = context.StartProcess("docker", settings); if (exitCode != 0) { throw new ApplicationException( $"Error when trying to push docker manifest image. Got error: " + exitCode ); } } }
public void BuildPlatformImage(string platform) { FilePath dockerFile = "server.dockerfile"; string arguments = $"build --tag {DockerConstants.ImageName}:{MedituConstants.VersionString}_{platform} --file {dockerFile} ."; ProcessArgumentBuilder argumentsBuilder = ProcessArgumentBuilder.FromString(arguments); ProcessSettings settings = new ProcessSettings { Arguments = argumentsBuilder, WorkingDirectory = context.DockerPath }; int exitCode = context.StartProcess("docker", settings); if (exitCode != 0) { throw new ApplicationException( "Error when running docker to build. Got error: " + exitCode ); } }
public static void NSpec(this ICakeContext context, string pattern, int timeout) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var assemblies = context.Globber.GetFiles(pattern).ToArray(); var isOnMono = Type.GetType("Mono.Runtime") != null; var cakeNSpec = System.IO.Path.GetDirectoryName( new System.Uri(typeof(NSpecBinder).Assembly.CodeBase).LocalPath ); context.Debug($"nspec: setting working directory {cakeNSpec}"); if (assemblies.Length == 0) { context.Log.Warning($"nspec: The provided pattern did not match any files. ({pattern})"); return; } else { var proc = typeof(Runner).Assembly.GetName().Name + ".exe"; var procArgs = ""; if (isOnMono) { procArgs = proc + " "; proc = "mono"; } foreach (var asm in assemblies) { var fileName = asm.GetFilename(); var localProcArgs = procArgs + System.IO.Path.Combine(asm.GetDirectory().FullPath, fileName.FullPath); localProcArgs += " -f console"; context.Log.Information($"Testing Spec {fileName.FullPath}"); context.Log.Debug($"nspec: Found Assembly {asm.GetDirectory ().FullPath}/{fileName.FullPath}"); context.Log.Debug($"nspec: Starting {proc} {localProcArgs}"); var proccess = context.ProcessRunner.Start(proc, new ProcessSettings { Arguments = ProcessArgumentBuilder.FromString(localProcArgs), WorkingDirectory = cakeNSpec }); if (timeout > 0) { proccess.WaitForExit((int)timeout); } else { proccess.WaitForExit(); } var errorCode = proccess.GetExitCode(); if (errorCode != 0) { throw new CakeException($"Runner {fileName} exited with {errorCode}"); } else { context.Log.Debug($"{fileName} passed."); } } } }
// ---------------- Functions ---------------- /// <summary> /// Runs git on the local repository and returns /// the <see cref="DateTime"/> of the last commit. /// </summary> /// <param name="config"> /// Configuration. If null, it grabs the configuration /// from the passed in command-line arguments. /// </param> public DateTime Run(GitQueryLastCommitDateConfig config = null) { if (config == null) { config = ArgumentBinder.FromArguments <GitQueryLastCommitDateConfig>(this.context); } DateTime?timeStamp = null; string onStdOut(string line) { if (string.IsNullOrWhiteSpace(line) == false) { if (DateTime.TryParse(line, out DateTime foundTimeStamp)) { timeStamp = foundTimeStamp; } } return(line); }; ProcessSettings processSettings = new ProcessSettings { Arguments = ProcessArgumentBuilder.FromString("show -s --format=%cI"), RedirectStandardOutput = true, RedirectedStandardOutputHandler = onStdOut }; this.Run(this.toolSettings, processSettings.Arguments, processSettings, null); if (timeStamp == null) { throw new InvalidOperationException( "Could not get timestamp from git" ); } string timeStampStr; if (string.IsNullOrEmpty(config.DateTimeFormat)) { timeStampStr = timeStamp.Value.ToString(); } else { timeStampStr = timeStamp.Value.ToString(config.DateTimeFormat); } if (config.NoPrint == false) { context.Information($"Last commit was at: {timeStampStr}"); } if (string.IsNullOrWhiteSpace(config.OutputFile) == false) { System.IO.File.WriteAllText(config.OutputFile, timeStampStr); } return(timeStamp.Value); }