コード例 #1
0
        public void WhenGivenInputIsValid_ShouldLogVersionNumberParts()
        {
            _sut.VersionNumber = "14.2.3.18224";
            _sut.Execute();

            A.CallTo(() => _taskLogger.LogMessage(MessageImportance.High, "Major: 14")).MustHaveHappened();
            A.CallTo(() => _taskLogger.LogMessage(MessageImportance.High, "Minor: 2")).MustHaveHappened();
            A.CallTo(() => _taskLogger.LogMessage(MessageImportance.High, "Build: 3")).MustHaveHappened();
            A.CallTo(() => _taskLogger.LogMessage(MessageImportance.High, "Revision: 18224")).MustHaveHappened();
        }
コード例 #2
0
        /// <summary>
        /// Attempts to check if the repository has uncommitted changes.
        /// </summary>
        /// <param name="gitRunner"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public static GitInfo GetGitStatus(IGitRunner gitRunner, ITaskLogger logger)
        {
            GitInfo status = new GitInfo();

            try
            {
                string statusText = gitRunner.GetTextFromProcess(GitArgument.Status);
                Match  match      = StatusBranchSearch.Match(statusText);
                if (match.Success && match.Groups.Count > 1)
                {
                    string branch = match.Groups[1].Value;
                    status.Branch = branch;
                }
                else
                {
                    Match detatchedMatch = DetatchedBranchSearch.Match(statusText);
                    if (detatchedMatch.Success && detatchedMatch.Groups.Count > 1)
                    {
                        string branch = detatchedMatch.Groups[1].Value;
                        status.Branch = branch;
                        if (branch.StartsWith("pull/"))
                        {
                            status.IsPullRequest = true;
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(status.Branch))
                {
                    logger.LogMessage(MessageImportance.High, $"Unable to retrieve branch name from status text: \n{statusText}");
                }
                statusText = statusText.ToUpper();
                if (statusText.Contains(UnmodifiedText) || statusText.Contains(UntrackedOnlyText))
                {
                    status.Modified = "Unmodified";
                }
                else
                {
                    status.Modified = "Modified";
                }
            }
            catch (GitRunnerException ex)
            {
                logger?.LogWarning($"Error getting 'git status': {ex.Message}");
            }
            try
            {
                status.OriginUrl = gitRunner.GetTextFromProcess(GitArgument.OriginUrl);
                if (status.OriginUrl != null && status.OriginUrl.Length > 0)
                {
                    status.GitUser = GetGitHubUser(status.OriginUrl);
                }
            }
            catch (GitRunnerException ex)
            {
                logger?.LogWarning($"Error getting git origin URL: {ex.Message}");
            }
            return(status);
        }
コード例 #3
0
 private void WriteManifest(BsipaManifest manifest, string path)
 {
     try
     {
         FileInfo fileInfo = new FileInfo(path);
         path = fileInfo.FullName;
         if (!fileInfo.Directory.Exists)
         {
             Logger.LogMessage(MessageImportance.High, $"Creating manifest target directory '${fileInfo.Directory.FullName}'...");
         }
         fileInfo.Directory.Create();
         File.WriteAllText(path, manifest.ToJson());
     }
     catch (Exception ex)
     {
         throw new IOException($"Failed to write manifest to '${path}': {ex.Message}");
     }
 }
コード例 #4
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>true if successful</returns>
        public override bool Execute()
        {
            if (this.BuildEngine != null)
            {
                Logger = new LogWrapper(Log, GetType().Name);
            }
            else
            {
                Logger = new MockTaskLogger(GetType().Name);
            }
            PlatformID platform = Environment.OSVersion.Platform;

            if (platform != PlatformID.Win32NT)
            {
                Logger.LogMessage(MessageImportance.High, $"This task isn't supported on platform: '{platform}'");
                IsRunning = Fallback;
                return(true);
            }
            string errorCode = null;

            try
            {
                if (ProcessName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                {
                    ProcessName = Path.GetFileNameWithoutExtension(ProcessName);
                }
                if (string.IsNullOrEmpty(ProcessName))
                {
                    errorCode = MessageCodes.IsProcessRunning.EmptyProcessName;
                    throw new ArgumentNullException(nameof(ProcessName), $"{nameof(ProcessName)} cannot be empty.");
                }
                foreach (Process proc in Process.GetProcesses())
                {
                    if (proc.ProcessName.Contains(ProcessName))
                    {
                        IsRunning = true;
                        break;
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(errorCode))
                {
                    errorCode = MessageCodes.IsProcessRunning.GeneralFailure;
                }
                int    line        = 0;
                int    column      = 0;
                string projectFile = null;
                if (BuildEngine != null)
                {
                    line        = BuildEngine.LineNumberOfTaskNode;
                    column      = BuildEngine.ColumnNumberOfTaskNode;
                    projectFile = BuildEngine.ProjectFileOfTaskNode;
                }
                Logger.LogError(null, errorCode, null, projectFile, line, column, line, column, $"Error in {GetType().Name}: {ex.Message}");
                IsRunning = Fallback;
                return(true);
            }
        }
コード例 #5
0
 public void GivenNoInputString_LogsOnStart()
 {
     _sut.StringToDecrypt = null;
     _sut.Execute();
     A.CallTo(() => _taskLogger.LogMessage(MessageImportance.High, "Decrypting: [NULL]")).MustHaveHappened();
 }
コード例 #6
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>true if successful</returns>
        public override bool Execute()
        {
            if (this.BuildEngine != null)
            {
                Logger = new LogWrapper(Log);
            }
            else
            {
                Logger = new MockTaskLogger();
            }
            string errorCode = "";

            try
            {
                if (string.IsNullOrEmpty(File))
                {
                    errorCode = MessageCodes.ReplaceInFile.EmptyFile;
                    throw new ArgumentNullException(null, $"'{nameof(File)}' is null or empty.");
                }
                FileInfo sourceFile = new FileInfo(File);
                if (!sourceFile.Exists)
                {
                    errorCode = MessageCodes.ReplaceInFile.MissingSource;
                    throw new FileNotFoundException($"File '{sourceFile.FullName}' does not exist.");
                }
                if (string.IsNullOrEmpty(Pattern))
                {
                    errorCode = MessageCodes.ReplaceInFile.EmptyPattern;
                    throw new ArgumentNullException(null, $"{nameof(Pattern)} cannot be null or empty.");
                }
                if (Substitute == null)
                {
                    Substitute = "";
                }
                string fileText = System.IO.File.ReadAllText(sourceFile.FullName);
                if (EscapeBackslash)
                {
                    Substitute = Substitute.Replace(@"\", @"\\");
                }
                Logger.LogMessage(MessageImportance.High, $"Replacing '{Pattern}' with '{Substitute}' in {sourceFile.FullName}");
                fileText = ReplaceText(fileText);
                System.IO.File.WriteAllText(sourceFile.FullName, fileText);
                return(true);
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(errorCode))
                {
                    errorCode = MessageCodes.ReplaceInFile.ReplaceFailed;
                }
                if (BuildEngine != null)
                {
                    int line   = BuildEngine.LineNumberOfTaskNode;
                    int column = BuildEngine.ColumnNumberOfTaskNode;
                    Logger.LogError(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column, $"{GetType().Name} failed - {ex.Message}");
                }
                else
                {
                    Logger.LogError(null, errorCode, null, null, 0, 0, 0, 0, $"Error in {GetType().Name}: {ex.Message}");
                }
                return(false);
            }
        }
コード例 #7
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>true if successful</returns>
        public override bool Execute()
        {
            if (this.BuildEngine != null)
            {
                Logger = new LogWrapper(Log, GetType().Name, MessagePrefix);
            }
            else
            {
                Logger = new MockTaskLogger(GetType().Name);
            }
            string errorCode = null;

            try
            {
                FileInfo      zipFile = new FileInfo(DestinationFile);
                DirectoryInfo zipDir  = zipFile.Directory;

                zipDir.Create();
                zipDir.Refresh();
                if (zipFile.Exists)
                {
                    zipFile.Delete();
                }
                if (string.IsNullOrEmpty(SourceDirectory))
                {
                    errorCode = MessageCodes.ZipDir.ZipEmptySource;
                    throw new ArgumentNullException($"{nameof(SourceDirectory)} cannot be null or empty.");
                }
                if (string.IsNullOrEmpty(DestinationFile))
                {
                    errorCode = MessageCodes.ZipDir.ZipEmptyDestination;
                    throw new ArgumentNullException($"{nameof(DestinationFile)} cannot be null or empty.");
                }
                if (!Directory.Exists(SourceDirectory))
                {
                    errorCode = MessageCodes.ZipDir.ZipMissingSource;
                    throw new DirectoryNotFoundException($"{nameof(SourceDirectory)} '{SourceDirectory}' not found.");
                }
                Logger.LogMessage(MessageImportance.High, "Zipping Directory \"{0}\" to \"{1}\"", SourceDirectory, DestinationFile);
                ZipFile.CreateFromDirectory(SourceDirectory, DestinationFile);
                ZipPath = zipFile.FullName;
                return(true);
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(errorCode))
                {
                    errorCode = MessageCodes.ZipDir.ZipFailed;
                }
                if (BuildEngine != null)
                {
                    int line   = BuildEngine.LineNumberOfTaskNode;
                    int column = BuildEngine.ColumnNumberOfTaskNode;
                    Logger.LogError(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column, $"$Error in {GetType().Name}: {ex.Message}");
                }
                else
                {
                    Logger.LogError(null, errorCode, null, null, 0, 0, 0, 0, $"Error in {GetType().Name}: {ex.Message}");
                }
                return(false);
            }
        }
コード例 #8
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>true if successful</returns>
        public override bool Execute()
        {
            if (this.BuildEngine != null)
            {
                Logger = new LogWrapper(Log);
            }
            else
            {
                Logger = new MockTaskLogger();
            }
            if (GitRunner == null)
            {
                GitRunner = new GitCommandRunner(ProjectDir);
            }
            CommitHash = "local";
            string errorCode = null;

            string[] gitPaths = new string[] {
                Path.GetFullPath(Path.Combine(ProjectDir, GitDirectory)),
                Path.GetFullPath(Path.Combine(ProjectDir, "..", GitDirectory))
            };
            try
            {
                string commitHash = null;
                if (!NoGit && TryGetGitCommit(GitRunner, Logger, out commitHash) && commitHash.Length > 0)
                {
                    CommitHash = commitHash.Substring(0, Math.Min(commitHash.Length, HashLength));
                    if (!SkipStatus)
                    {
                        GitInfo gitStatus = GetGitStatus(GitRunner, Logger);
                        if (!string.IsNullOrWhiteSpace(gitStatus.Branch))
                        {
                            Branch = gitStatus.Branch;
                        }
                        if (!string.IsNullOrWhiteSpace(gitStatus.Modified))
                        {
                            Modified = gitStatus.Modified;
                        }
                        if (!string.IsNullOrWhiteSpace(gitStatus.OriginUrl))
                        {
                            OriginUrl = gitStatus.OriginUrl;
                        }
                        if (!string.IsNullOrWhiteSpace(gitStatus.GitUser))
                        {
                            GitUser = gitStatus.GitUser;
                        }
                    }
                    if (string.IsNullOrWhiteSpace(Branch))
                    {
                        if (TryGetCommitManual(gitPaths, out GitInfo manualInfo))
                        {
                            Branch = manualInfo.Branch;
                        }
                    }
                }
                else
                {
                    if (TryGetCommitManual(gitPaths, out GitInfo gitInfo))
                    {
                        commitHash = gitInfo.CommitHash;
                        if (commitHash.Length > 0)
                        {
                            CommitHash = commitHash
                                         .Substring(0,
                                                    Math.Min(commitHash.Length, HashLength));
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.Branch))
                        {
                            Branch = gitInfo.Branch;
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.Modified))
                        {
                            Modified = gitInfo.Modified;
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.OriginUrl))
                        {
                            OriginUrl = gitInfo.OriginUrl;
                        }
                        if (!string.IsNullOrWhiteSpace(gitInfo.GitUser))
                        {
                            GitUser = gitInfo.GitUser;
                        }
                    }
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(errorCode))
                {
                    errorCode = MessageCodes.GetCommitInfo.GitFailed;
                }
                if (BuildEngine != null)
                {
                    int line   = BuildEngine.LineNumberOfTaskNode;
                    int column = BuildEngine.ColumnNumberOfTaskNode;
                    Logger.LogMessage(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column,
                                      MessageImportance.High, $"Error in {GetType().Name}: {ex.Message}");
                }
                else
                {
                    Logger.LogMessage(null, errorCode, null, null, 0, 0, 0, 0,
                                      MessageImportance.High, $"Error in {GetType().Name}: {ex.Message}");
                }
            }
#pragma warning restore CA1031 // Do not catch general exception types
            if (CommitHash == "local")
            {
                if (BuildEngine != null)
                {
                    errorCode = MessageCodes.GetCommitInfo.GitNoRepository;
                    int line   = BuildEngine.LineNumberOfTaskNode;
                    int column = BuildEngine.ColumnNumberOfTaskNode;
                    Logger.LogMessage(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column,
                                      MessageImportance.High, "Project does not appear to be in a git repository.");
                }
                else
                {
                    Logger.LogMessage(null, errorCode, null, null, 0, 0, 0, 0,
                                      MessageImportance.High, "Project does not appear to be in a git repository.");
                }
            }
            return(true);
        }