RSCG–AMS – About My software –work– part 2
So now it is time to work at implementation This will be a standard RSCG – generating code. I make also a test console to display the values.
The implementation will consider the fact that we can have many Source Control providers – each one with his ideas about variables. So I made 2 classes – one base abstract
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | abstract class AMS { public AMS(GeneratorExecutionContext context) { AssemblyName = context.Compilation.AssemblyName; GeneratedDate = DateTime.UtcNow; } public string AssemblyName { get ; internal set ; } public DateTime GeneratedDate { get ; internal set ; } public string CommitId { get ; internal set ; } public string RepoUrl { get ; internal set ; } } |
and one implementation for Github
1 2 3 4 5 6 7 8 9 | class AMSGitHub : AMS { public AMSGitHub(GeneratorExecutionContext context): base (context) { CommitId = Environment.GetEnvironmentVariable( "GITHUB_SHA" ); RepoUrl = Environment.GetEnvironmentVariable( "GITHUB_SERVER_URL" ) + "/" + Environment.GetEnvironmentVariable( "GITHUB_REPOSITORY" ); } } |
The code for generator is a bit more complicated:
01 02 03 04 05 06 07 08 09 10 11 12 13 | var nameSpace = "AMS" ; var ams = new AMSGitHub(context); var classDef=$ @" using System; namespace {nameSpace} {{ public class AboutMySoftware{{ public string AssemblyName {{ get {{ return ""{ams.AssemblyName}"" ; }} }} public DateTime DateGenerated {{ get {{ return DateTime.ParseExact(""{ams.GeneratedDate.ToString(" yyyyMMddHHmmss")} "" , "" yyyyMMddHHmmss "" , null ); }} }} public string CommitId {{ get {{ return "" {ams.CommitId} "" ; }}}} public string RepoUrl {{ get {{ return "" {ams.RepoUrl} "" ; }}}} }} }}"; |
The console to test has the following code
1 2 3 4 5 6 7 8 9 | static void Main( string [] args) { Console.WriteLine( "Show About My Software versions" ); var ams = new AboutMySoftware(); Console.WriteLine($ "{nameof(ams.AssemblyName)} : {ams.AssemblyName}" ); Console.WriteLine($ "{nameof(ams.DateGenerated)} : {ams.DateGenerated}" ); Console.WriteLine($ "{nameof(ams.CommitId)} : {ams.CommitId}" ); Console.WriteLine($ "{nameof(ams.RepoUrl)} : {ams.RepoUrl}" ); } |
and the output , in GitHub actions , is
Show About My Software versions AssemblyName : AMSConsole DateGenerated : 06/24/2021 03:16:51 CommitId : d8cb041470d93f68a4dc7fca7d131c207db8ab69 RepoUrl : https://github.com/ignatandrei/RSCG_AMS
Leave a Reply