11using Nuke . Common ;
22using Nuke . Common . IO ;
3- using Nuke . Common . ProjectModel ;
43using Nuke . Common . Tooling ;
54using Nuke . Common . Tools . MSBuild ;
5+ using Nuke . Common . Utilities . Collections ;
66using System ;
77using System . IO ;
8+ using System . Linq ;
9+ using System . Text ;
10+ using System . Text . Json ;
811using System . Text . RegularExpressions ;
912using System . Threading . Tasks ;
1013using static Nuke . Common . Tools . DocFX . DocFXTasks ;
14+ using static Nuke . Common . Tools . Git . GitTasks ;
1115using static Nuke . Common . Tools . MSBuild . MSBuildTasks ;
1216using static Nuke . Common . Tools . Unity . UnityTasks ;
1317using static UnityHelper ;
@@ -18,21 +22,24 @@ class Build : NukeBuild
1822
1923 public static int Main ( ) => Execute < Build > ( x => x . Compile ) ;
2024
25+ [ PathExecutable ] readonly Tool Gh = default ! ;
26+
2127 [ Parameter ( "Configuration to build - Default is 'Debug' (local) or 'Release' (server)" ) ]
2228 readonly Configuration Configuration = IsLocalBuild ? Configuration . Debug : Configuration . Release ;
2329
24- [ Solution ] readonly Solution Solution = default ! ;
25-
2630 [ Parameter ( "Password for Unity license" ) ] string ? UnityPassword ;
2731 [ Parameter ( "Email for Unity license" ) ] string ? UnityEmail ;
2832 [ Parameter ( "Serial for Unity license" ) ] string ? UnitySerial ;
2933
3034 [ Parameter ( "Are we running in CI" ) ] bool IsCi = false ;
3135
32- AbsolutePath UnityProjectPath => Solution . Directory / "UnityResourceGenerator" ;
33- AbsolutePath UnitySolution => UnityProjectPath / "UnityResourceGenerator.sln" ;
36+ string CurrentVersion { get ; set ; } = default ! ;
37+ bool IsNewestVersion { get ; set ; }
3438
35- string UnityVersion =>
39+ static AbsolutePath UnityProjectPath => RootDirectory / "UnityResourceGenerator" ;
40+ static AbsolutePath UnitySolution => UnityProjectPath / "UnityResourceGenerator.sln" ;
41+
42+ static string UnityVersion =>
3643 Regex . Match
3744 (
3845 File . ReadAllLines ( Path . Combine ( UnityProjectPath , "ProjectSettings" , "ProjectVersion.txt" ) ) [ 0 ] ,
@@ -116,4 +123,66 @@ async Task GenerateSolution()
116123 Target ServeDocs => _ => _
117124 . DependsOn ( BuildDocs )
118125 . Executes ( ( ) => DocFX ( $ "{ DocFxJsonPath } --serve") ) ;
126+
127+ Target CreateGithubRelease => _ => _
128+ . OnlyWhenDynamic ( ( ) => IsNewestVersion )
129+ . OnlyWhenDynamic ( ( ) => IsCi )
130+ . Executes ( ( ) =>
131+ {
132+ var version = CurrentVersion ;
133+
134+ var notes = File . ReadAllLines ( RootDirectory / "CHANGELOG.md" )
135+ . Skip ( 1 )
136+ . TakeUntil ( string . IsNullOrWhiteSpace )
137+ . Aggregate ( new StringBuilder ( ) , ( sb , l ) => sb . AppendLine ( l ) )
138+ . ToString ( ) ;
139+
140+ Gh ( $ "release create { version } -t { version } -n \" { notes } \" ") ;
141+ } ) ;
142+
143+
144+ protected override void OnBuildInitialized ( )
145+ {
146+ bool GetIsNewestVersion ( )
147+ {
148+ var currentVersion = new Version ( CurrentVersion ) ;
149+
150+ GitLogger = ( _ , s ) => Logger . Info ( s ) ;
151+
152+ Git ( "fetch --tags" ) ;
153+
154+ var maxPublishedVersion = Git ( "tag" )
155+ . Select ( o => new Version ( o . Text ) )
156+ . OrderBy ( v => v )
157+ . LastOrDefault ( ) ;
158+
159+ return currentVersion . CompareTo ( maxPublishedVersion ) > 0 ;
160+ }
161+
162+ string GetCurrentVersion ( )
163+ {
164+ var packagePath = UnityProjectPath / "Assets" / "AutSoft.UnityResourceGenerator" / "package.json" ;
165+
166+ if ( ! File . Exists ( packagePath ) ) throw new InvalidOperationException ( $ "package.json does not exist at path: { packagePath } ") ;
167+
168+ var jsonContent = File . ReadAllText ( packagePath ) ;
169+ var package = JsonSerializer . Deserialize < PackageJson > ( jsonContent , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
170+
171+ if ( package ? . Version is null ) throw new InvalidOperationException ( $ "Cloud not deserialize package.json:{ Environment . NewLine } { jsonContent } ") ;
172+
173+ return package . Version ;
174+ }
175+
176+ CurrentVersion = GetCurrentVersion ( ) ;
177+ IsNewestVersion = GetIsNewestVersion ( ) ;
178+
179+ base . OnBuildInitialized ( ) ;
180+ }
181+
182+ sealed class PackageJson
183+ {
184+ public PackageJson ( string version ) => Version = version ;
185+
186+ public string Version { get ; }
187+ }
119188}
0 commit comments