/// <summary> /// Initializes a new VersionUpdater instance. /// </summary> /// <param name="input"> /// The input string containing the AssemblyVersion attribute /// to update. /// </param> /// <param name="options">The options to use for updating the version number.</param> public VersionUpdater(string input, Options options) { // Save the input this._Input = input; this._Output = input; this._ActiveRegex = new Regex(options.AttributeName + "(?:Attribute)?\\(\\s*?\"(?<version>(?<major>[0-9]+)\\.(?<minor>[0-9]+)\\.(?<build>[0-9]+)\\.(?<revision>[0-9]+))\"\\s*?\\)"); this._ReplaceFormat = options.AttributeName + "(\"{0}\")"; try { Match match = this._ActiveRegex.Match(input); if(null != match) { string inputVersion = match.Groups["version"].Value; this.doUpdate(inputVersion, options); } } catch(Exception) { // There was a problem finding or updating the version number. this._Output = this._Input; throw; } }
protected override void ExecuteTask() { Options options = new Options(); options.BuildNumberType = BuildNumberType.Fixed; options.InputFilename = this.File; options.OutputFilename = this.File; options.RevisionNumberType = RevisionNumberType.Increment; options.AttributeName = "AssemblyVersion"; RunUpdateVersion version = new RunUpdateVersion(); version.execute(options); }
protected override void ExecuteTask() { Options options = new Options(); options.BuildNumberType = BuildNumberType.Fixed; options.InputFilename = this.File; options.OutputFilename = this.File; options.ReplacementVersion = this.Version; options.AttributeName = "AssemblyInformationalVersion"; RunUpdateVersion version = new RunUpdateVersion(); version.execute(options); }
/// <summary> /// Private helper method that gets the input string from the appropriate source. /// </summary> /// <param name="options">The command line options.</param> /// <returns> /// Returns the input string. /// </returns> public static string GetInput(Options opts) { string input = null; if(null == opts.InputFilename) { // The input file name was not specified on the command line wo we will // get the input from the standard input stream. input = Console.In.ReadToEnd(); } else { // An input file was specified on the command line. input = ReadFile(opts.InputFilename); } return input; }
public static int Main(string[] args) { Options options = null; string input = null; VersionUpdater updater = null; /////////////////////////////////////////////////////////////////// // Parse the command line try { options = new Options(args); } catch(Exception e) { Console.Error.WriteLine("Error parsing command line options."); Console.Error.WriteLine(e); Console.Error.WriteLine(); Console.Error.WriteLine(Options.Usage); return 1; } /////////////////////////////////////////////////////////////////// // Get the input try { input = GetInput(options); } catch(Exception e) { Console.Error.WriteLine("Error reading input."); Console.Error.WriteLine(e); return 2; } /////////////////////////////////////////////////////////////////// // Update the version number in the input try { updater = new VersionUpdater(input, options); } catch(Exception e) { Console.Error.WriteLine("Error updating version."); Console.Error.WriteLine(e); return 3; } /////////////////////////////////////////////////////////////////// // Write the output try { WriteOutput(updater.Output, options); } catch(Exception e) { Console.Error.WriteLine("Error writing output."); Console.Error.WriteLine(e); return 4; } // Return normally return 0; }
/// <summary> /// Writes the output string to the appropriate target. /// </summary> /// <param name="output"> /// The output string. /// </param> /// <param name="options"> /// The command line options. /// </param> private static void WriteOutput(string output, Options options) { if(null == output) throw new ArgumentNullException("output", "Output is null."); if(null == options.OutputFilename) { // The output file name was not specified on the command line wo we will // write the output to the standard output stream. Console.Out.Write(output); } else { // An output file was specified on the command line. So we will write the // output to the specified file. // using(StreamWriter writer = // File.CreateText(options.OutputFilename)) // { // writer.Write(output); // } using(StreamWriter writer = new StreamWriter(options.OutputFilename, false, Encoding.Default)) { writer.Write(output); } } }
/// <summary> /// The main entry point for the application. /// </summary> public void execute(Options opts) { string input = null; VersionUpdater updater = null; /////////////////////////////////////////////////////////////////// // Get the input input = GetInput(opts); /////////////////////////////////////////////////////////////////// // Update the version number in the input updater = new VersionUpdater(input, opts); /////////////////////////////////////////////////////////////////// // Write the output WriteOutput(updater.Output, opts); }
/// <summary> /// Private helper method that gets the input string from the appropriate source. /// </summary> /// <param name="options">The property options.</param> /// <returns> /// Returns the input string. /// </returns> private static string GetInput(Options options) { string input = null; if (null == options.InputFilename) { // The input file name was not specified on the property wo we will // get the input from the standard input stream. input = options.Input; } else { // An input file was specified on the property. input = ReadFile(options.InputFilename); } return input; }
public override bool Execute() { List<String> buildoptions = new List<string>(); Options options = null; string input = null; VersionUpdater updater = null; StringBuilder message = new StringBuilder(); /////////////////////////////////////////////////////////////////// // validate the options try { if (string.IsNullOrEmpty(m_startDateOption) == false) { buildoptions.Add("-s"); buildoptions.Add(m_startDateOption); } if (string.IsNullOrEmpty(m_buildOption) == false) { buildoptions.Add("-b"); buildoptions.Add(m_buildOption); } if (string.IsNullOrEmpty(m_pinOption) == false) { buildoptions.Add("-p"); buildoptions.Add(m_pinOption); } if (string.IsNullOrEmpty(m_revisionOption) == false) { buildoptions.Add("-r"); buildoptions.Add(m_revisionOption); } if (string.IsNullOrEmpty(m_inputFile) == false) { buildoptions.Add("-i"); buildoptions.Add(m_inputFile); } if (string.IsNullOrEmpty(m_outputFile) == false) { buildoptions.Add("-o"); buildoptions.Add(m_outputFile); } if (string.IsNullOrEmpty(m_versionOption) == false) { buildoptions.Add("-v"); buildoptions.Add(m_versionOption); } options = new Options(buildoptions.ToArray()); } catch (Exception e) { message.AppendLine("Error validating options."); message.AppendLine(e.Message); message.AppendLine(); message.AppendLine(Options.Usage); Log.LogError(message.ToString()); return false; } /////////////////////////////////////////////////////////////////// // Get the input try { input = GetInput(options); } catch (Exception e) { message.AppendLine("Error reading input."); message.AppendLine(e.Message); Log.LogError(message.ToString()); return false; } /////////////////////////////////////////////////////////////////// // Update the version number in the input try { updater = new VersionUpdater(input, options); } catch (Exception e) { message.AppendLine("Error updating version."); message.AppendLine(e.Message); Log.LogError(message.ToString()); return false; } /////////////////////////////////////////////////////////////////// // Write the output try { m_updatedInput = updater.Output; if (String.IsNullOrEmpty(options.OutputFilename) == false) { using (StreamWriter writer = new StreamWriter(options.OutputFilename, false, Encoding.Default)) { writer.Write(m_updatedInput); } } } catch (Exception e) { message.AppendLine("Error writing output."); message.AppendLine(e.Message); Log.LogError(message.ToString()); return false; } // Return normally return true; }
private void doUpdate(string inputVersion, Options options) { string replacement = ""; if (options.ReplacementVersion != null) { replacement = string.Format(this._ReplaceFormat, options.ReplacementVersion); } else if (null == inputVersion || string.Empty == inputVersion) { VersionCalculator calculator = new VersionCalculator("1.0.0.0"); calculator.StartDate = options.StartDate; calculator.BuildNumberType = options.BuildNumberType; calculator.RevisionNumberType = options.RevisionNumberType; string replacment = options.AttributeName + "(\"" + calculator.NewVersion.ToString() + "\")"; this._Output = this._Input.Replace(options.AttributeName + "(\"1.0.*\")", replacment); return; } else { VersionCalculator calculator = new VersionCalculator(inputVersion); calculator.StartDate = options.StartDate; calculator.BuildNumberType = options.BuildNumberType; calculator.RevisionNumberType = options.RevisionNumberType; replacement = string.Format(this._ReplaceFormat, calculator.NewVersion.ToString()); } string outputVersion = this._ActiveRegex.Replace(this._Input, replacement, 1); this._Output = outputVersion; }
/// <summary> /// Initializes a new VersionUpdater instance. /// </summary> /// <param name="input"> /// The input string containing the AssemblyVersion attribute /// to update. /// </param> /// <param name="options">The options to use for updating the version number.</param> public VersionUpdater(string input, Options options) { // Save the input this._Input = input; this._Output = input; if(VersionType.Assembly == options.VersionType) { this._ActiveRegex = AssemblyVersionRegex; this._ReplaceFormat = "AssemblyVersion(\"{0}\")"; } else if (VersionType.File == options.VersionType) { this._ActiveRegex = FileVersionRegex; this._ReplaceFormat = "AssemblyFileVersion(\"{0}\")"; } else { this._ActiveRegex = InformationalVersionRegex; this._ReplaceFormat = "AssemblyInformationalVersion(\"{0}\")"; } try { Match match = this._ActiveRegex.Match(input); if(null != match) { string inputVersion = match.Groups["version"].Value; if (options.VersionIsPinned) { string replacement = string.Format(this._ReplaceFormat, options.PinVersion); string outputVersion = this._ActiveRegex.Replace(input, replacement, 1); this._Output = outputVersion; } else if (null != inputVersion && string.Empty != inputVersion) { // We found an AssemblyVersion in the input string so let's update it. VersionCalculator calculator = new VersionCalculator(inputVersion); calculator.StartDate = options.StartDate; calculator.BuildNumberType = options.BuildNumberType; calculator.RevisionNumberType = options.RevisionNumberType; string replacement = string.Format(this._ReplaceFormat, calculator.NewVersion.ToString()); string outputVersion = this._ActiveRegex.Replace(input, replacement, 1); this._Output = outputVersion; } else { // There was no Version in the input string. Warn the user // and set the output string to the input string. Console.Error.WriteLine("Version not found in input. " + "Output equals input."); this._Output = input; } } } catch(Exception) { // There was a problem finding or updating the version number. this._Output = input; throw; } }