From d6c8ade2fcad311350788172cf17d07cf1dadba5 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Mon, 8 Aug 2016 09:30:18 +0100
Subject: [PATCH] Utilise embedded git commit.

---
 cscz.sln                            |   2 +-
 cscz/Help.md                        |   2 +-
 cscz/Program.cs                     |  11 +-
 cscz/Properties/AssemblyInfo.cs     |   2 +-
 cscz/SBRLUtilities/EmbeddedFiles.cs | 171 ++++++++++++++++++++++++++++
 cscz/Utiliities.cs                  |  30 +++++
 cscz/cscz.csproj                    | 108 +++++++++---------
 7 files changed, 264 insertions(+), 62 deletions(-)
 create mode 100644 cscz/SBRLUtilities/EmbeddedFiles.cs
 create mode 100644 cscz/Utiliities.cs

diff --git a/cscz.sln b/cscz.sln
index c3e0ee5..c8cdd48 100644
--- a/cscz.sln
+++ b/cscz.sln
@@ -27,6 +27,6 @@ Global
 	EndGlobalSection
 	GlobalSection(MonoDevelopProperties) = preSolution
 		description = The C# Class Generator.
-		version = 0.2
+		version = 0.4
 	EndGlobalSection
 EndGlobal
diff --git a/cscz/Help.md b/cscz/Help.md
index 5ab1bb5..545d9f9 100644
--- a/cscz/Help.md
+++ b/cscz/Help.md
@@ -1,4 +1,4 @@
-﻿cscz - The C# Class Generator, v0.3
+﻿cscz - The C# Class Generator, {version}
 By Starbeamrainbowlabs (@SBRLabs)
 Built from https://git.starbeamrainbowlabs.com/sbrl/cscz/
 
diff --git a/cscz/Program.cs b/cscz/Program.cs
index e699eb2..9883c3b 100644
--- a/cscz/Program.cs
+++ b/cscz/Program.cs
@@ -4,6 +4,8 @@ using System.Diagnostics;
 using System.Reflection;
 using System.IO;
 using System.Collections.Generic;
+using SBRLUtilities;
+using System.Security.Policy;
 
 namespace cscz
 {
@@ -25,13 +27,8 @@ namespace cscz
 				switch(args[i].Trim('-'))
 				{
 					case "help":
-						Assembly asm = Assembly.GetExecutingAssembly();
-						/*** Debug - Lists the names of all embedded resources ***
-				foreach(string str in asm.GetManifestResourceNames())
-					Console.WriteLine(str);*/
-						StreamReader helpTextReader = new StreamReader(asm.GetManifestResourceStream(@"cscz.Help.md"));
-						string helpText = helpTextReader.ReadToEnd();
-						helpTextReader.Dispose();
+						string helpText = EmbeddedFiles.ReadAllText(@"cscz.Help.md");
+						helpText = helpText.Replace("{version}", Utilities.GetVersionText());
 						Console.WriteLine(helpText);
 						return 0;
 					case "private":
diff --git a/cscz/Properties/AssemblyInfo.cs b/cscz/Properties/AssemblyInfo.cs
index c241a7f..10c6326 100644
--- a/cscz/Properties/AssemblyInfo.cs
+++ b/cscz/Properties/AssemblyInfo.cs
@@ -17,7 +17,7 @@ using System.Runtime.CompilerServices;
 // The form "{Major}.{Minor}.*" will automatically update the build and revision,
 // and "{Major}.{Minor}.{Build}.*" will update just the revision.
 
-[assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("0.4.*")]
 
 // The following attributes are used to specify the signing key for the assembly,
 // if desired. See the Mono documentation for more information about signing.
diff --git a/cscz/SBRLUtilities/EmbeddedFiles.cs b/cscz/SBRLUtilities/EmbeddedFiles.cs
new file mode 100644
index 0000000..c585fad
--- /dev/null
+++ b/cscz/SBRLUtilities/EmbeddedFiles.cs
@@ -0,0 +1,171 @@
+﻿using System;
+using System.Reflection;
+using System.IO;
+using System.Net.Configuration;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+
+namespace SBRLUtilities
+{
+	/// <summary>
+	/// A collection of static methods for manipulating embedded resources.
+	/// </summary>
+	/// <description>
+	/// v0.2, by Starbeamrainbowlabs <feedback@starbeamrainbowlabs.com>
+	/// Last updated 8th August 2016.
+	/// Licensed under MPL-2.0.
+	/// 
+	/// Changelog:
+	/// v0.1 (25th July 2016):
+	/// 	- Initial release.
+	/// v0.2 (8th August 2016):
+	/// 	- Changed namespace.
+	/// </description>
+	public static class EmbeddedFiles
+	{
+		/// <summary>
+		/// An array of the filenames of all the resources embedded in the calling assembly.
+		/// </summary>
+		/// <value>The resource list.</value>
+		public static string[] ResourceList { get {
+				return Assembly.GetCallingAssembly().GetManifestResourceNames();
+			}}
+
+		public static string GetResourceListText()
+		{
+			StringWriter result = new StringWriter();
+			result.WriteLine("Files embedded in {0}:", Assembly.GetCallingAssembly().GetName().Name);
+			foreach (string filename in ResourceList)
+				result.WriteLine(" - {0}", filename);
+			return result.ToString();
+		}
+		/// <summary>
+		/// Writes a list of embedded resources to the Console's standard output.
+		/// </summary>
+		public static void WriteResourceList()
+		{
+			Console.WriteLine(GetResourceListText());
+		}
+
+		/// <summary>
+		/// Gets a StreamReader attached to the specified embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to a StreamReader for.</param>
+		/// <returns>A StreamReader attached to the specified embedded resource.</returns>
+		public static StreamReader GetReader(string filename)
+		{
+			return new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename));
+		}
+
+		/// <summary>
+		/// Gets the specified embedded resource's content as a byte array.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to get conteent of.</param>
+		/// <returns>The specified embedded resource's content as a byte array.</returns>
+		public static byte[] ReadAllBytes(string filename)
+		{
+			// Referencing the Result property will block until the async method completes
+			return ReadAllBytesAsync(filename).Result;
+		}
+		/// <summary>
+		/// Gets the specified embedded resource's content as a byte array asynchronously.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to get conteent of.</param>
+		/// <returns>The specified embedded resource's content as a byte array.</returns>
+		public static async Task<byte[]> ReadAllBytesAsync(string filename)
+		{
+			using (Stream resourceStream = Assembly.GetCallingAssembly().GetManifestResourceStream(filename))
+			using (MemoryStream temp = new MemoryStream())
+			{
+				await resourceStream.CopyToAsync(temp);
+				return temp.ToArray();
+			}
+		}
+
+		/// <summary>
+		/// Gets all the text stored in the specified embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename to fetch the content of.</param>
+		/// <returns>All the text stored in the specified embedded resource.</returns>
+		public static string ReadAllText(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				return resourceReader.ReadToEnd();
+			}
+		}
+		/// <summary>
+		/// Gets all the text stored in the specified embedded resource asynchronously.
+		/// </summary>
+		/// <param name="filename">The filename to fetch the content of.</param>
+		/// <returns>All the text stored in the specified embedded resource.</returns>
+		public static async Task<string> ReadAllTextAsync(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				return await resourceReader.ReadToEndAsync();
+			}
+		}
+
+		/// <summary>
+		/// Enumerates the lines of text in the specified embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to enumerate.</param>
+		/// <returns>An IEnumerator that enumerates the specified embedded resource.</returns>
+		public static IEnumerator<string> EnumerateLines(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				string nextLine;
+				while ((nextLine = resourceReader.ReadLine()) != null)
+				{
+					yield return nextLine;
+				}
+			}
+		}
+		/// <summary>
+		/// Enumerates the lines of text in the specified embedded resource asynchronously.
+		/// Each successive call returns a task that, when complete, returns the next line of text stored
+		/// in the embedded resource.
+		/// </summary>
+		/// <param name="filename">The filename of the embedded resource to enumerate.</param>
+		/// <returns>An IEnumerator that enumerates the specified embedded resource.</returns>
+		public static IEnumerable<Task<string>> EnumerateLinesAsync(string filename)
+		{
+			using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
+			{
+				while (!resourceReader.EndOfStream)
+				{
+					yield return resourceReader.ReadLineAsync();
+				}
+			}
+		}
+
+		/// <summary>
+		/// Gets all the lines of text in the specified embedded resource.
+		/// You might find EnumerateLines(string filename) more useful depending on your situation.
+		/// </summary>
+		/// <param name="filename">The filename to obtain the lines of text from.</param>
+		/// <returns>A list of lines in the specified embedded resource.</returns>
+		public static List<string> GetAllLines(string filename)
+		{
+			// Referencing the Result property will block until the async method completes
+			return GetAllLinesAsync(filename).Result;
+		}
+		/// <summary>
+		/// Gets all the lines of text in the specified embedded resource asynchronously.
+		/// </summary>
+		/// <param name="filename">The filename to obtain the lines of text from.</param>
+		/// <returns>A list of lines in the specified embedded resource.</returns>
+		public static async Task<List<string>> GetAllLinesAsync(string filename)
+		{
+			List<string> lines = new List<string>();
+			IEnumerable<Task<string>> lineIterator = EnumerateLinesAsync(filename);
+			foreach(Task<string> nextLine in lineIterator)
+			{
+				lines.Add(await nextLine);
+			}
+			return lines;
+		}
+	}
+}
diff --git a/cscz/Utiliities.cs b/cscz/Utiliities.cs
new file mode 100644
index 0000000..13a4edb
--- /dev/null
+++ b/cscz/Utiliities.cs
@@ -0,0 +1,30 @@
+﻿using System;
+using System.Reflection;
+
+using SBRLUtilities;
+
+namespace cscz
+{
+	public static class Utilities
+	{
+		public static string GetFullHash()
+		{
+			string hash = EmbeddedFiles.ReadAllText("cscz.git-hash.txt").Trim();
+			if (hash == string.Empty)
+				hash = new string('?', 40);
+			return hash;
+		}
+
+		public static string GetShortHash()
+		{
+			return GetFullHash().Substring(0, 7);
+		}
+
+		public static string GetVersionText()
+		{
+			Version asmVersion = Assembly.GetCallingAssembly().GetName().Version;
+			return $"v{asmVersion.Major}.{asmVersion.Minor}-{GetShortHash()}";
+		}
+	}
+}
+
diff --git a/cscz/cscz.csproj b/cscz/cscz.csproj
index b012b4a..cebb409 100644
--- a/cscz/cscz.csproj
+++ b/cscz/cscz.csproj
@@ -1,55 +1,59 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-	<PropertyGroup>
-		<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-		<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
-		<ProjectGuid>{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}</ProjectGuid>
-		<OutputType>Exe</OutputType>
-		<RootNamespace>cscz</RootNamespace>
-		<AssemblyName>cscz</AssemblyName>
-		<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
-		<Description>The C# Class Generator.</Description>
-		<ReleaseVersion>0.2</ReleaseVersion>
-	</PropertyGroup>
-	<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-		<DebugSymbols>true</DebugSymbols>
-		<DebugType>full</DebugType>
-		<Optimize>false</Optimize>
-		<OutputPath>bin\Debug</OutputPath>
-		<DefineConstants>DEBUG;</DefineConstants>
-		<ErrorReport>prompt</ErrorReport>
-		<WarningLevel>4</WarningLevel>
-		<Externalconsole>true</Externalconsole>
-		<PlatformTarget>x86</PlatformTarget>
-		<CustomCommands>
-			<CustomCommands>
-				<Command type="Execute" command="cat Carrot.cscz | ./cscz" workingdir="${TargetDir}" externalConsole="True" pauseExternalConsole="True" />
-			</CustomCommands>
-		</CustomCommands>
-	</PropertyGroup>
-	<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
-		<Optimize>true</Optimize>
-		<OutputPath>bin\Release</OutputPath>
-		<ErrorReport>prompt</ErrorReport>
-		<WarningLevel>4</WarningLevel>
-		<Externalconsole>true</Externalconsole>
-		<PlatformTarget>x86</PlatformTarget>
-	</PropertyGroup>
-	<ItemGroup>
-		<Reference Include="System" />
-	</ItemGroup>
-	<ItemGroup>
-		<Compile Include="Program.cs" />
-		<Compile Include="Properties\AssemblyInfo.cs" />
-		<Compile Include="ClassGenerator.cs" />
-	</ItemGroup>
-	<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
-	<ItemGroup>
-		<EmbeddedResource Include="Help.md" />
-		<EmbeddedResource Include="git-hash.txt" />
-	</ItemGroup>
-	<Target Name="BeforeBuild" BeforeTargets="Build">
-		<Exec Command="git rev-parse HEAD &gt;git-hash.txt" WorkingDirectory="$(ProjectDir)" IgnoreExitCode="true" />
-	</Target>
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProjectGuid>{3DE7A812-1AB9-483F-A785-4497FC8FDF2C}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>cscz</RootNamespace>
+    <AssemblyName>cscz</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <Description>The C# Class Generator.</Description>
+    <ReleaseVersion>0.4</ReleaseVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+    <PlatformTarget>x86</PlatformTarget>
+    <CustomCommands>
+      <CustomCommands>
+        <Command type="Execute" command="cat Carrot.cscz | ./cscz" workingdir="${TargetDir}" externalConsole="True" pauseExternalConsole="True" />
+      </CustomCommands>
+    </CustomCommands>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+    <PlatformTarget>x86</PlatformTarget>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="ClassGenerator.cs" />
+    <Compile Include="Utiliities.cs" />
+    <Compile Include="SBRLUtilities\EmbeddedFiles.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <ItemGroup>
+    <EmbeddedResource Include="Help.md" />
+    <EmbeddedResource Include="git-hash.txt" />
+  </ItemGroup>
+  <Target Name="BeforeBuild" BeforeTargets="Build">
+    <Exec Command="git rev-parse HEAD &gt;git-hash.txt" WorkingDirectory="$(ProjectDir)" IgnoreExitCode="true" />
+  </Target>
+  <ItemGroup>
+    <Folder Include="SBRLUtilities\" />
+  </ItemGroup>
 </Project>
-
