/// <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 = null; try { 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; } 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); } }
/// <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); } }
/// <summary> /// Logs an error from the <see cref="ParsingException"/> using the given <see cref="ITaskLogger"/>. /// </summary> /// <param name="ex"></param> /// <param name="file"></param> /// <param name="position"></param> /// <param name="Log"></param> public static void LogErrorFromException(this ParsingException ex, ITaskLogger Log, string file, FilePosition position) { Log.LogError(null, ex.MessageCode, "", file, position, ex.Message, ex.MessageArgs); }
/// <summary> /// Logs an error from the <see cref="ParsingException"/> using the given <see cref="ITaskLogger"/>. /// </summary> /// <param name="ex"></param> /// <param name="Log"></param> public static void LogErrorFromException(this ParsingException ex, ITaskLogger Log) { Log.LogError(null, ex.MessageCode, "", ex.File, ex.LineNumber, ex.ColumnNumber, ex.EndLineNumber, ex.EndColumnNumber, ex.Message, ex.MessageArgs); }
/// <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); } }
/// <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); } }
/// <summary> /// Executes the task. /// </summary> /// <returns>true if successful</returns> public override bool Execute() { string errorCode = null; LogMessageLevel errorLevel = ErrorOnMismatch ? LogMessageLevel.Error : LogMessageLevel.Warning; AssemblyInfoData asmInfo = default; string assemblyInfoPath = null; if (this.BuildEngine != null) { Logger = new LogWrapper(Log, GetType().Name); } else { Logger = new MockTaskLogger(GetType().Name); } try { if (!Util.MatchVersions(AssemblyVersion, PluginVersion)) { Logger.Log(null, MessageCodes.CompareVersions.VersionMismatch, "", assemblyInfoPath, asmInfo.AssemblyVersionPosition, errorLevel, "PluginVersion {0} does not match AssemblyVersion {1}.", PluginVersion, AssemblyVersion); if (ErrorOnMismatch) { return(false); } } return(true); } catch (VersionMatchException ex) { if (BuildEngine != null) { int line = BuildEngine.LineNumberOfTaskNode; int column = BuildEngine.ColumnNumberOfTaskNode; Logger.LogError(null, MessageCodes.CompareVersions.InvalidVersion, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column, ex.Message); } else { Logger.LogError(null, MessageCodes.CompareVersions.InvalidVersion, null, null, 0, 0, 0, 0, ex.Message); } return(false); } catch (ParsingException ex) { if (string.IsNullOrEmpty(errorCode)) { errorCode = MessageCodes.CompareVersions.GeneralFailure; } if (BuildEngine != null) { int line = BuildEngine.LineNumberOfTaskNode; int column = BuildEngine.ColumnNumberOfTaskNode; Logger.LogError(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column, ex.Message); } else { Logger.LogError(null, errorCode, null, null, ex.LineNumber, ex.ColumnNumber, ex.EndLineNumber, ex.EndColumnNumber, ex.Message); } return(false); } catch (Exception ex) { if (string.IsNullOrEmpty(errorCode)) { errorCode = MessageCodes.CompareVersions.GeneralFailure; } 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); } }
/// <summary> /// Executes the task. /// </summary> /// <returns>true if successful</returns> public override bool Execute() { string errorCode = null; LogMessageLevel errorLevel = FailOnError ? LogMessageLevel.Error : LogMessageLevel.Warning; AssemblyInfoData asmInfo = default; string assemblyInfoPath = null; AssemblyVersion = MessageCodes.ErrorString; if (this.BuildEngine != null) { Logger = new LogWrapper(Log, GetType().Name); } else { Logger = new MockTaskLogger(GetType().Name); } try { string assemblyFileMsg = ""; if (string.IsNullOrWhiteSpace(AssemblyInfoPath)) { assemblyInfoPath = Path.Combine("Properties", "AssemblyInfo.cs"); } else { assemblyInfoPath = AssemblyInfoPath; } try { asmInfo = ParseAssembly(assemblyInfoPath, FailOnError); AssemblyVersion = asmInfo.AssemblyVersion; } catch (FileNotFoundException ex) { if (FailOnError) { errorCode = MessageCodes.GetAssemblyInfo.AssemblyInfoNotFound; throw; } else { Logger.LogError(ex.Message); } } assemblyFileMsg = " in " + assemblyInfoPath; if (AssemblyVersion == null || AssemblyVersion == ErrorString || AssemblyVersion.Length == 0) { Logger.LogError("AssemblyVersion could not be determined."); return(false); } return(true); } catch (ParsingException ex) { if (string.IsNullOrEmpty(errorCode)) { errorCode = MessageCodes.GetAssemblyInfo.GeneralFailure; } if (BuildEngine != null) { int line = BuildEngine.LineNumberOfTaskNode; int column = BuildEngine.ColumnNumberOfTaskNode; Logger.LogError(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column, ex.Message); } else { Logger.LogError(null, errorCode, null, null, ex.LineNumber, ex.ColumnNumber, ex.EndLineNumber, ex.EndColumnNumber, ex.Message); } return(false); } catch (Exception ex) { if (string.IsNullOrEmpty(errorCode)) { errorCode = MessageCodes.GetAssemblyInfo.GeneralFailure; } if (BuildEngine != null) { int line = BuildEngine.LineNumberOfTaskNode; int column = BuildEngine.ColumnNumberOfTaskNode; Logger.LogError(null, errorCode, null, BuildEngine.ProjectFileOfTaskNode, line, column, line, column, ex.Message); } else { Logger.LogError(null, errorCode, null, null, 0, 0, 0, 0, $"{ex.Message}"); } return(false); } }