From b87920e5aa7a15ef721923048944f973552c1b88 Mon Sep 17 00:00:00 2001 From: icedream Date: Mon, 12 Jan 2015 18:28:15 +0100 Subject: [PATCH] Added "--version" switch for version printing. --- src/updater/CommandLineOptions.cs | 5 ++-- src/updater/Program.cs | 36 ++++++++++++++++++++++++++ src/updater/Properties/AssemblyInfo.cs | 1 + 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/updater/CommandLineOptions.cs b/src/updater/CommandLineOptions.cs index 14fdfc0..fb5d6d8 100644 --- a/src/updater/CommandLineOptions.cs +++ b/src/updater/CommandLineOptions.cs @@ -1,7 +1,5 @@ using System; using System.Diagnostics; -using System.IO; -using System.Linq; using System.Reflection; using CommandLine; using CommandLine.Text; @@ -23,6 +21,9 @@ namespace CitizenMP.Server.Installer [ValueOption(0)] public string OutputPath { get; set; } + [Option("version", DefaultValue=false, HelpText="Shows this tool's version.")] + public bool ShowVersion { get; set; } + [HelpOption] public string GetUsage() { diff --git a/src/updater/Program.cs b/src/updater/Program.cs index ae6b1f9..d354ede 100644 --- a/src/updater/Program.cs +++ b/src/updater/Program.cs @@ -67,6 +67,42 @@ namespace CitizenMP.Server.Installer return -2; } + if (options.ShowVersion) + { + var version = + Regex.Match( + Assembly.GetExecutingAssembly() + .GetCustomAttributes(typeof (AssemblyInformationalVersionAttribute), false) + .OfType().First().InformationalVersion, + @"^(?[0-9]+)\.(?[0-9]+)\.(?[0-9]+)(\-(?[A-z0-9\.]+))?(\+(?.+?))?$"); + + var meta = new Queue(version.Groups["meta"].Value.Split('.')); + while (meta.Any() && meta.First() != "Branch") + { + meta.Dequeue(); + } + + var metaDict = new Dictionary(); + while (meta.Any()) + { + var name = meta.Dequeue(); + var value = meta.Dequeue(); + if (meta.Any() && char.IsDigit(meta.First().First())) + { + value += "." + meta.Dequeue(); + } + metaDict.Add(name, value); + } + + Console.WriteLine("{0}.{1}.{2}{3}{4}", + version.Groups["major"].Value, + version.Groups["minor"].Value, + version.Groups["revision"].Value, + version.Groups["prerelease"].Success ? "-" + version.Groups["prerelease"].Value : "", + metaDict.Any() ? " (" + metaDict["Sha"].Substring(0, 7) + ")" : ""); + return 0; + } + if (string.IsNullOrEmpty(options.OutputPath)) { Console.Error.WriteLine("ERROR: No output directory given."); diff --git a/src/updater/Properties/AssemblyInfo.cs b/src/updater/Properties/AssemblyInfo.cs index 974cdfc..5c91512 100644 --- a/src/updater/Properties/AssemblyInfo.cs +++ b/src/updater/Properties/AssemblyInfo.cs @@ -1,5 +1,6 @@ using System.Reflection; using System.Runtime.InteropServices; + #if !NO_COMMANDLINE using CommandLine; #endif