@@ -12,20 +12,29 @@ static class Program
1212 {
1313 private static string ThisExecutableName => AppDomain . CurrentDomain . FriendlyName ;
1414 private static Version ? AssemblyVersion => Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version ;
15- private static string VersionNumber => AssemblyVersion ? . ToString ( ) ?? String . Empty ;
15+ private static string VersionNumber => AssemblyVersion ? . ToString ( ) ?? string . Empty ;
1616 private static string BuildDate
1717 {
1818 get {
19+ DateTime buildDateTime ;
1920 Version ? version = AssemblyVersion ;
20- return new DateTime ( 2000 , 1 , 1 )
21- . AddDays ( version . Build )
22- . AddSeconds ( version . Revision * 2 ) . ToString ( "o" ) ;
21+ if ( version == null )
22+ {
23+ buildDateTime = File . GetLastWriteTime ( AppContext . BaseDirectory ) ;
24+ }
25+ else
26+ {
27+ buildDateTime = new DateTime ( 2000 , 1 , 1 ) . AddDays ( version . Build ) . AddSeconds ( version . Revision * 2 ) ;
28+ }
29+
30+ return buildDateTime . ToString ( "o" ) ;
2331 }
2432 }
2533
2634 private const string cncProgramFileExtension = ".CNC" ;
2735 private const string defaultCNCprogramName = "Unknown" ;
2836 private const char programDelimiter = '%' ;
37+ private static readonly char [ ] subFolderTrim = { ' ' , '/' } ;
2938 private const int minimumProgramSize = 7 ;
3039
3140 /// <summary>
@@ -67,7 +76,7 @@ static async Task<int> Main(string[] args)
6776 }
6877
6978 // Make a subfolder named like the filename to hold all the programs we split out of it
70- string outputFolder = Path . Combine ( backupFile . DirectoryName ?? String . Empty , Path . GetFileNameWithoutExtension ( backupFile . Name ) ) ;
79+ string outputFolder = Path . Combine ( backupFile . DirectoryName ?? string . Empty , Path . GetFileNameWithoutExtension ( backupFile . Name ) ) ;
7180 try
7281 {
7382 Directory . CreateDirectory ( outputFolder ) ;
@@ -142,7 +151,7 @@ private static string GetProgramNameFromHeader(string cncProgramText)
142151 }
143152
144153 // Strip out the directory flag and slashes to get just the folder name.
145- subFolder = Regex . Replace ( line , directoryFlag , string . Empty ) . Trim ( '/' ) ;
154+ subFolder = Regex . Replace ( line , directoryFlag , string . Empty ) . Trim ( subFolderTrim ) ;
146155 Directory . CreateDirectory ( Path . Combine ( outputFolder , subFolder ) ) ;
147156
148157 // Don't append notation to next program
@@ -170,21 +179,22 @@ private static string GetProgramNameFromHeader(string cncProgramText)
170179 static string CncProgramText ( StringBuilder content )
171180 {
172181 // Prevent IndexOutOfBounds exceptions if final program is empty
173- if ( content . Length < minimumProgramSize )
182+ if ( content . Length > minimumProgramSize )
174183 {
175- return content . ToString ( ) ;
176- }
184+ // Add % to the top
185+ if ( content . ToString ( ) [ 0 ] != programDelimiter )
186+ {
187+ content . Insert ( 0 , Environment . NewLine ) ;
188+ content . Insert ( 0 , programDelimiter ) ;
189+ }
177190
178- // Add % to the top
179- if ( content . ToString ( ) [ 0 ] != programDelimiter )
180- {
181- content . Insert ( 0 , Environment . NewLine ) ;
182- content . Insert ( 0 , programDelimiter ) ;
191+ // Add % to the bottom when missing
192+ if ( content . ToString ( ) . TrimEnd ( ) [ ^ 1 ] != programDelimiter )
193+ {
194+ content . AppendLine ( programDelimiter . ToString ( ) ) ;
195+ }
183196 }
184197
185- // Add % to the bottom when missing
186- if ( content . ToString ( ) . TrimEnd ( ) [ ^ 1 ] != programDelimiter ) { content . AppendLine ( programDelimiter . ToString ( ) ) ; }
187-
188198 return content . ToString ( ) ;
189199 }
190200
0 commit comments