From 1174882a7550fe6b95a6bca114e18fb4d38fdeee Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 19 May 2016 20:31:48 +0100
Subject: [PATCH] Refactor main program to add support for version text.

---
 SpritePacker/Program.cs                       | 59 ++++++++++++++-----
 .../Resources/{Help.txt => HelpText.txt}      |  2 +-
 SpritePacker/Resources/VersionText.txt        |  5 ++
 SpritePacker/SpritePacker.csproj              |  3 +-
 4 files changed, 51 insertions(+), 18 deletions(-)
 rename SpritePacker/Resources/{Help.txt => HelpText.txt} (83%)
 create mode 100644 SpritePacker/Resources/VersionText.txt

diff --git a/SpritePacker/Program.cs b/SpritePacker/Program.cs
index d2bc138..df82a86 100644
--- a/SpritePacker/Program.cs
+++ b/SpritePacker/Program.cs
@@ -6,38 +6,65 @@ using System.Reflection;
 
 namespace SpritePacker
 {
+	enum ProgramMode
+	{
+		Normal,
+		DisplayHelpText,
+		DisplayVersionText
+	};
+
 	class Program
 	{
+		private static ProgramMode programMode = ProgramMode.Normal;
+		private static string helpTextFilename = "SpritePacker.Resources.HelpText.txt";
+		private static string versionTextFilename = "SpritePacker.Resources.VersionText.txt";
+		private static List<string> values = new List<string>();
+
 		public static void Main(string[] args)
 		{
-			Utilities.GetProgramBuildDate();
-			List<string> values = new List<string>();
 			for(int i = 0; i < args.Length; i++)
 			{
 				switch(args[i])
 				{
 					case "--help":
-						string helpText = Utilities.GetEmbeddedResourceContent("SpritePacker.Resources.Help.txt");
-						string commitHash = Utilities.GetEmbeddedResourceContent("SpritePacker.latest-commit-hash.txt").Trim();
-						Dictionary<string, string> templateValues = new Dictionary<string, string>() {
-							{ "version", Utilities.GetProgramVersion().ToString() },
-							{ "build-time", Utilities.GetProgramBuildDate().ToString("dd/MM/yyyy h:mmtt") },
-							{ "commit-hash", commitHash.Substring(commitHash.Length - 7) }
-						};
-						foreach (KeyValuePair<string, string> replacePair in templateValues)
-							helpText = helpText.Replace(string.Format("{{{0}}}", replacePair.Key), replacePair.Value);
-						
-						Console.WriteLine(helpText);
-						return;
+						programMode = ProgramMode.DisplayHelpText;
+						break;
 					case "--version":
-						Console.WriteLine("Version text coming soon!");
-						return;
+						programMode = ProgramMode.DisplayVersionText;
+						break;
 					default:
 						values.Add(args[i]);
 						break;
 				}
 			}
 
+			switch(programMode)
+			{
+				case ProgramMode.Normal:
+					RunNormal();
+					break;
+				case ProgramMode.DisplayHelpText:
+				case ProgramMode.DisplayVersionText:
+					string textFileName = programMode == ProgramMode.DisplayHelpText ? helpTextFilename : versionTextFilename;
+					string helpText = Utilities.GetEmbeddedResourceContent(textFileName);
+					string commitHash = Utilities.GetEmbeddedResourceContent("SpritePacker.latest-commit-hash.txt").Trim();
+					Dictionary<string, string> templateValues = new Dictionary<string, string>() {
+						{ "version", Utilities.GetProgramVersion().ToString() },
+						{ "build-date", Utilities.GetProgramBuildDate().ToString("dd/MM/yyyy h:mmtt") },
+						{ "commit-hash", commitHash.Substring(commitHash.Length - 7) },
+						{ "commit-url", string.Format("https://git.starbeamrainbowlabs.com/sbrl/SpritePacker/commit/{0}", commitHash) }
+					};
+					foreach (KeyValuePair<string, string> replacePair in templateValues)
+						helpText = helpText.Replace(string.Format("{{{0}}}", replacePair.Key), replacePair.Value);
+
+					Console.WriteLine(helpText);
+					break;
+			}
+
+		}
+
+		public static void RunNormal()
+		{
 			Packer spritePacker = new Packer();
 			foreach(string filename in values)
 			{
diff --git a/SpritePacker/Resources/Help.txt b/SpritePacker/Resources/HelpText.txt
similarity index 83%
rename from SpritePacker/Resources/Help.txt
rename to SpritePacker/Resources/HelpText.txt
index 6da2e5a..fd0b805 100644
--- a/SpritePacker/Resources/Help.txt
+++ b/SpritePacker/Resources/HelpText.txt
@@ -1,5 +1,5 @@
 ﻿SpritePacker v{version}, by Starbeamrainbowlabs
-Built at {build-time} from git commit {commit-hash}
+Built at {build-date} from git commit {commit-hash}
 
 Usage:
     mono ./SpritePacker.exe [flags] [filenames]
diff --git a/SpritePacker/Resources/VersionText.txt b/SpritePacker/Resources/VersionText.txt
new file mode 100644
index 0000000..57becbe
--- /dev/null
+++ b/SpritePacker/Resources/VersionText.txt
@@ -0,0 +1,5 @@
+﻿SpritePacker v{version}, by Starbeamrainbowlabs
+
+Built at {build-date}
+Built from git commit hash {commit-hash}
+Commit url: {commit-url}
diff --git a/SpritePacker/SpritePacker.csproj b/SpritePacker/SpritePacker.csproj
index c467af4..83a7202 100644
--- a/SpritePacker/SpritePacker.csproj
+++ b/SpritePacker/SpritePacker.csproj
@@ -45,8 +45,9 @@
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
-    <EmbeddedResource Include="Resources\Help.txt" />
     <EmbeddedResource Include="latest-commit-hash.txt" />
+    <EmbeddedResource Include="Resources\VersionText.txt" />
+    <EmbeddedResource Include="Resources\HelpText.txt" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Resources\" />
