public static IProcess AssertZeroExitCode( [AssertionCondition(AssertionConditionType.IS_NOT_NULL)][CanBeNull] this IProcess process) { const string indentation = " "; process.AssertWaitForExit(); if (process.ExitCode != 0) { var messageBuilder = new StringBuilder() .AppendLine($"Process '{Path.GetFileName(process.FileName)}' exited with code {process.ExitCode}.") .AppendLine("Invocation:") .AppendLine($"{indentation}{process.FileName.DoubleQuoteIfNeeded()} {process.Arguments}") .AppendLine("Working directory:") .AppendLine($"{indentation}{process.WorkingDirectory}"); var errorOutput = process.Output.Where(x => x.Type == OutputType.Err).ToList(); if (errorOutput.Count > 0) { messageBuilder.AppendLine("Error output:"); errorOutput.ForEach(x => messageBuilder.Append(indentation).AppendLine(x.Text)); } else if (Logger.LogLevel <= LogLevel.Trace) { messageBuilder.AppendLine("Standard output:"); process.Output.ForEach(x => messageBuilder.Append(indentation).AppendLine(x.Text)); } ControlFlow.Fail(messageBuilder.ToString()); } return(process); }
private static void AssertProcess(IProcess process, Xunit2Settings toolSettings) { process.AssertWaitForExit(); switch (process.ExitCode) { case 0: break; case 1: ControlFlow.Fail("One or more of the tests failed."); break; case 2: ControlFlow.Fail( "The help page was shown, either because it was requested, or because the user did not provide any command line arguments."); break; case 3: ControlFlow.Fail("There was a problem with one of the command line options passed to the runner."); break; case 4: ControlFlow.Fail( "There was a problem loading one or more of the test assemblies (for example, if a 64-bit only assembly is run with the 32-bit test runner)."); break; default: throw new NotSupportedException(); } }
/// <summary> /// Ensures that GitVersion has a system wide environment variable set. If not it will attempt to locate it and set the environment variable. /// </summary> /// <param name="targetEnvironment">Whether to target user or system/machine setting. You must run the app as administrator to use Machine.</param> /// <returns></returns> public static bool ValidateGetVersionEnvVariable(EnvironmentVariableTarget targetEnvironment = EnvironmentVariableTarget.Process) { string envGitVersion = Environment.GetEnvironmentVariable(ENV_GITVERSION); if (envGitVersion == null) { Logger.Warn("GitVersion environment variable not found. Will attempt to set."); string cmd = "where"; string cmdArgs = "gitversion.exe"; IProcess process = ProcessTasks.StartProcess(cmd, cmdArgs, logOutput: true); process.AssertWaitForExit(); ControlFlow.Assert(process.ExitCode == 0, "The " + ENV_GITVERSION + " environment variable is not set and attempt to fix it, failed because it appears GitVersion is not installed on the local machine. Install it and then re-run and/or set the environment variable manually"); // Set the environment variable now that we found it string value = process.Output.First().Text; Environment.SetEnvironmentVariable(ENV_GITVERSION, value, targetEnvironment); envGitVersion = Environment.GetEnvironmentVariable(ENV_GITVERSION); string val = ToolPathResolver.TryGetEnvironmentExecutable("GITVERSION_EXE"); Console.WriteLine("Toolpathresolver: " + val); Console.WriteLine(); string msg = "GitVersion Environment variable has been set! You will need to ensure you close the current console window before continuing to pickup the change."; Console.WriteWithGradient(msg, Color.Fuchsia, Color.Yellow, 16); Console.ReplaceAllColorsWithDefaults(); } return(true); }
/// <summary> /// Executes the requested Git Command AND returns the output. Returns True on success, false otherwise /// </summary> /// <param name="cmdArguments"></param> /// <param name="output"></param> /// <returns></returns> private bool ExecuteGit(string cmdArguments, out List <Output> output) { string command = "git"; // Log it Output outputCmd = new Output(); outputCmd.Text = GIT_COMMAND_MARKER + command + " " + cmdArguments; GitCommandOutputHistory.Add(outputCmd); IProcess process = ProcessTasks.StartProcess(command, cmdArguments, RootDirectory, logOutput: false); process.AssertWaitForExit(); output = process.Output.ToList(); // Copy output to history. GitCommandOutputHistory.AddRange(process.Output); if (process.ExitCode != 0) { return(false); } return(true); }
/// <summary> /// Executes the requested Git Command AND returns the output. Returns True on success, false otherwise /// </summary> /// <param name="cmdArguments"></param> /// <param name="output"></param> /// <returns></returns> private bool ExecuteGit(string cmdArguments, out List <LineOutColored> output) { string command = GIT_COMMAND; HasErrored = false; // Log it LineOutColored outputCmd = LineOutColored.Normal(GIT_COMMAND_MARKER + command + " " + cmdArguments); GitCommandOutputHistory.Add(outputCmd); //ProcessTasks.DefaultLogOutput = false; IProcess process = ProcessTasks.StartProcess(command, cmdArguments, CISession.RootDirectory, logInvocation: logInvocationLogging, logOutput: logOutputLogging); //,customLogger:GitProcessorLoggerNormal process.AssertWaitForExit(); output = process.Output.ToList(); // Copy output to history. GitCommandOutputHistory.AddRange(process.Output); if (process.ExitCode != 0) { HasErrored = true; return(false); } return(true); }
public static void AssertZeroExitCode([AssertionCondition(AssertionConditionType.IS_NOT_NULL)][CanBeNull] this IProcess process) { process.AssertWaitForExit(); ControlFlow.Assert(process.ExitCode == 0, new[] { $"Process '{Path.GetFileName(process.FileName)}' exited with code {process.ExitCode}. Please verify the invocation.", $"> {process.FileName.DoubleQuoteIfNeeded()} {process.Arguments}" }.JoinNewLine()); }
private bool MoveProjectStepB(VisualStudioProject project) { // Now add it back to project with new location string addParam = Path.Combine(project.NewPath, project.Namecsproj); IProcess sln = ProcessTasks.StartProcess(DotNetPath, "sln " + ExpectedSolutionPath + " add " + addParam, logOutput: true); sln.AssertWaitForExit(); ControlFlow.Assert(sln.ExitCode == 0, "Failed to re-add Project: " + project.Name + " to solution so we could complete the move"); Logger.Success("Project: {0} successfully relocated into proper new directory layout.", project.Name); return(true); }
public static IProcess AssertZeroExitCode( [AssertionCondition(AssertionConditionType.IS_NOT_NULL)][CanBeNull] this IProcess process) { process.AssertWaitForExit(); if (process.ExitCode != 0) { throw new ProcessException(process); } return(process); }
/// <summary> /// Moves a project of a solution: Moves it's folder location to new location and then updates the solution. /// </summary> /// <param name="project">VisualStudioProject object representing the project to move.</param> /// <returns></returns> private bool MoveProjectStepA(VisualStudioProject project) { // Move project to new location if (project.OriginalPath.ToString() != project.NewPath.ToString()) { Directory.Move(project.OriginalPath, project.NewPath); } // Remove from Solution string removeParam = Path.Combine(project.OriginalPath, project.Namecsproj); IProcess sln = ProcessTasks.StartProcess(DotNetPath, "sln " + CurrentSolutionPath + " remove " + removeParam, logOutput: true); sln.AssertWaitForExit(); ControlFlow.Assert(sln.ExitCode == 0, "Failed to remove Project: " + project.Name + " from solution so we could move it."); return(true); }
/// <summary> /// Run the typewriter publishing steps /// </summary> /// <returns></returns> protected override StageCompletionStatusEnum ExecuteProcess() { string command = "npm"; string npmArgs = "run publishTW"; CompletionStatus = StageCompletionStatusEnum.InProcess; foreach (SlugCIProject project in CISession.Projects) { AOT_Normal("Project: " + project.Name, Color.Magenta); AOT_Normal(" --> HasTypeWriterScripts: " + project.HasTypeWriterScripts, Color.Magenta); if (!project.HasTypeWriterScripts) { continue; } AbsolutePath scriptsFolder = project.VSProject.Directory / "_scripts"; IProcess process = ProcessTasks.StartProcess(command, npmArgs, scriptsFolder, customLogger: NPMLogger); process.AssertWaitForExit(); StageOutput.AddRange(process.Output); if (process.ExitCode != 0) { SetInprocessStageStatus(StageCompletionStatusEnum.Failure); } else { SetInprocessStageStatus(StageCompletionStatusEnum.Success); } } if (CompletionStatus == StageCompletionStatusEnum.InProcess) { CompletionStatus = StageCompletionStatusEnum.Success; } return(CompletionStatus); }
/// <summary> /// Run the Angular Build /// </summary> /// <returns></returns> protected override StageCompletionStatusEnum ExecuteProcess() { string command = "ng"; string ngArgs = "build"; if (CISession.SkipAngularBuild) { return(StageCompletionStatusEnum.Skipped); } CompletionStatus = StageCompletionStatusEnum.InProcess; foreach (AngularProject project in CISession.SlugCIConfigObj.AngularProjects) { AddOutputText("Project: " + project.Name, OutputType.Std); AbsolutePath angularProjectPath = CISession.AngularDirectory / project.Name; IProcess process = ProcessTasks.StartProcess(command, ngArgs, angularProjectPath, customLogger: AngularLogger); process.AssertWaitForExit(); StageOutput.AddRange(process.Output); if (process.ExitCode != 0) { SetInprocessStageStatus(StageCompletionStatusEnum.Failure); project.Results.CompileSuccess = false; } else { SetInprocessStageStatus(StageCompletionStatusEnum.Success); project.Results.CompileSuccess = true; } } if (CompletionStatus == StageCompletionStatusEnum.InProcess) { CompletionStatus = StageCompletionStatusEnum.Success; } return(CompletionStatus); }
public static IProcess AssertZeroExitCode( [AssertionCondition(AssertionConditionType.IS_NOT_NULL)][CanBeNull] this IProcess process) { process.AssertWaitForExit(); if (process.ExitCode != 0) { var messageBuilder = new StringBuilder() .AppendLine($"Process '{Path.GetFileName(process.FileName)}' exited with code {process.ExitCode}. Verify the invocation.") .AppendLine($"> {process.FileName.DoubleQuoteIfNeeded()} {process.Arguments}"); var errorOutput = process.Output.Where(x => x.Type == OutputType.Err).Select(x => x.Text).ToList(); if (errorOutput.Count > 0) { messageBuilder.AppendLine("Error output:"); errorOutput.ForEach(x => messageBuilder.AppendLine(x)); } ControlFlow.Fail(messageBuilder.ToString()); } return(process); }
private static void AssertProcess(IProcess process, UnityBaseSettings settings) { process.AssertWaitForExit(); AssertWatcherStopped(); if (process.ExitCode == 0) { return; } var message = new StringBuilder() .AppendLine($"Process '{Path.GetFileName(process.FileName)}' exited with code {process.ExitCode}. Verify the invocation.") .AppendLine($"> {process.FileName.DoubleQuoteIfNeeded()} {process.Arguments}") .ToString(); if (settings.StableExitCodes.Any(x => x == process.ExitCode)) { Logger.Warn(message); } else { ControlFlow.Fail(message); } }
/// <summary> /// Ensure the Directory Structure is correct. Projects are in proper place. /// </summary> /// <param name="solutionFile"></param> /// <returns></returns> private bool ProperDirectoryStructure(string solutionFile) { // Create src folder if it does not exist. if (!DirectoryExists(SourceDirectory)) { Directory.CreateDirectory(SourceDirectory.ToString()); } // Create Tests folder if it does not exist. if (!DirectoryExists(TestsDirectory)) { Directory.CreateDirectory(TestsDirectory.ToString()); } // Create Artifacts / Output folder if it does not exist. if (!DirectoryExists(OutputDirectory)) { Directory.CreateDirectory(OutputDirectory.ToString()); } // Query the solution for the projects that are in it. // We allow all tests to run, instead of failing at first failure. CurrentSolutionPath = (AbsolutePath)Path.GetDirectoryName(solutionFile); DotNetPath = ToolPathResolver.GetPathExecutable("dotnet"); IProcess slnfind = ProcessTasks.StartProcess(DotNetPath, "sln " + CurrentSolutionPath + " list", logOutput: true); slnfind.AssertWaitForExit(); IReadOnlyCollection <Output> output = slnfind.Output; // There are 2 things we need to check. // 1. Is solution in right folder? // 2. Are projects in right folder. // The Move process has to do the following: // 1. Move the project folder to proper place // 2. Remove the project from the solution // 3. Do steps 1, 2 for every project // 4. Move solution file to proper location // 5. Re-add all projects to solution bool solutionNeedsToMove = false; if (CurrentSolutionPath.ToString() != ExpectedSolutionPath.ToString()) { solutionNeedsToMove = true; } List <VisualStudioProject> movedProjects = new List <VisualStudioProject>(); // Step 3 foreach (Output outputRec in output) { if (outputRec.Text.EndsWith(".csproj")) { VisualStudioProject project = GetInitProject(outputRec.Text); Projects.Add(project); // Do we need to move the project? if ((project.OriginalPath.ToString() != project.NewPath.ToString()) || solutionNeedsToMove) { movedProjects.Add(project); MoveProjectStepA(project); } } } // Step 4: Is Solution in proper directory. If not move it. if (solutionNeedsToMove) { string slnFileCurrent = CurrentSolutionPath / Path.GetFileName(solutionFile); string slnFileFuture = ExpectedSolutionPath / Path.GetFileName(solutionFile); File.Move(slnFileCurrent, slnFileFuture); } // Step 5. Read project to solution if (movedProjects.Count > 0) { foreach (VisualStudioProject project in movedProjects) { MoveProjectStepB(project); } } return(true); }
/// <summary> /// Run the Typewriter build process /// </summary> /// <returns></returns> protected override StageCompletionStatusEnum ExecuteProcess() { CompletionStatus = StageCompletionStatusEnum.InProcess; // Read the package.json file if necessary... foreach (SlugCIProject project in CISession.Projects) { AOT_Normal("Project: " + project.Name, Color.Magenta); AOT_Normal(" --> HasTypeWriterScripts: " + project.HasTypeWriterScripts, Color.Magenta); if (!project.HasTypeWriterScripts) { continue; } AbsolutePath scriptsFolder = project.VSProject.Directory / "_scripts"; AOT_Normal(" --> Scripts Folder: " + scriptsFolder); AbsolutePath scriptsFile = scriptsFolder / "package.json"; TypeWriterConfig typeWriterConfig = null; if (FileExists(scriptsFile)) { string Json = File.ReadAllText(scriptsFile); typeWriterConfig = JsonSerializer.Deserialize <TypeWriterConfig>(Json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } else { AOT_Error(" --> package.json file was not found."); ControlFlow.Assert(true == false, "Package.json file was not found"); } // Make sure it has proper elements: bool updated = false; if (typeWriterConfig.Scripts.Pack == null || typeWriterConfig.Scripts.Pack != CMD_PACK) { updated = true; typeWriterConfig.Scripts.Pack = CMD_PACK; } if (typeWriterConfig.Scripts.PublishTW == null || typeWriterConfig.Scripts.PublishTW != CMD_PUBLISH) { updated = true; typeWriterConfig.Scripts.PublishTW = CMD_PUBLISH; } if (typeWriterConfig.Scripts.Copy == null || typeWriterConfig.Scripts.Copy != CMD_COPY) { updated = true; typeWriterConfig.Scripts.Copy = CMD_COPY; } if (typeWriterConfig.Version == null || typeWriterConfig.Version != CISession.VersionInfo.NPMVersion) { updated = true; typeWriterConfig.Version = CISession.VersionInfo.NPMVersion; typeWriterConfig.VersionFull = CISession.VersionInfo.SemVersionAsString; } if (updated) { string json = JsonSerializer.Serialize <TypeWriterConfig>(typeWriterConfig, new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); File.WriteAllText(scriptsFile, json); } // Pack the files string command = "npm"; string npmArgs = "run pack"; IProcess process = ProcessTasks.StartProcess(command, npmArgs, scriptsFolder); process.AssertWaitForExit(); StageOutput.AddRange(process.Output); if (process.ExitCode != 0) { SetInprocessStageStatus(StageCompletionStatusEnum.Failure); } else { CISession.GitProcessor.CommitChanges("TypeWriter Updates for project --> " + project.Name); SetInprocessStageStatus(StageCompletionStatusEnum.Success); } //SetInprocessStageStatus(StageCompletionStatusEnum.Success); } if (CompletionStatus == StageCompletionStatusEnum.InProcess) { CompletionStatus = StageCompletionStatusEnum.Skipped; } return(CompletionStatus); }