11using System ;
22using System . IO ;
33using Cake . Common ;
4+ using Cake . Common . Diagnostics ;
45using Cake . Common . IO ;
56using Cake . Common . Tools . DotNet ;
67using Cake . Common . Tools . DotNet . Clean ;
78using Cake . Common . Tools . DotNet . Publish ;
89using Cake . Core ;
10+ using Cake . Core . IO ;
911using Cake . Frosting ;
1012using Cake . Json ;
1113using Newtonsoft . Json ;
@@ -27,10 +29,6 @@ public static int Main(string[] args)
2729public class BuildContext : FrostingContext
2830{
2931 public const string ProjectName = "SmithingPlus" ;
30- public string BuildConfiguration { get ; }
31- public string Version { get ; }
32- public string Name { get ; }
33- public bool SkipJsonValidation { get ; }
3432
3533 public BuildContext ( ICakeContext context )
3634 : base ( context )
@@ -41,21 +39,22 @@ public BuildContext(ICakeContext context)
4139 Version = modInfo . Version ;
4240 Name = modInfo . ModID ;
4341 }
42+
43+ public string BuildConfiguration { get ; }
44+ public string Version { get ; }
45+ public string Name { get ; }
46+ public bool SkipJsonValidation { get ; }
4447}
4548
4649[ TaskName ( "ValidateJson" ) ]
4750public sealed class ValidateJsonTask : FrostingTask < BuildContext >
4851{
4952 public override void Run ( BuildContext context )
5053 {
51- if ( context . SkipJsonValidation )
52- {
53- return ;
54- }
54+ if ( context . SkipJsonValidation ) return ;
5555
5656 var jsonFiles = context . GetFiles ( $ "../{ BuildContext . ProjectName } /assets/**/*.json") ;
5757 foreach ( var file in jsonFiles )
58- {
5958 try
6059 {
6160 var json = File . ReadAllText ( file . FullPath ) ;
@@ -66,7 +65,6 @@ public override void Run(BuildContext context)
6665 throw new Exception (
6766 $ "Validation failed for JSON file: { file . FullPath } { Environment . NewLine } { ex . Message } ", ex ) ;
6867 }
69- }
7068 }
7169}
7270
@@ -103,20 +101,60 @@ public override void Run(BuildContext context)
103101 context . CopyFiles ( $ "../{ BuildContext . ProjectName } /bin/{ context . BuildConfiguration } /Mods/mod/publish/*",
104102 $ "../Releases/{ context . Name } ") ;
105103 if ( context . DirectoryExists ( $ "../{ BuildContext . ProjectName } /assets") )
106- {
107104 context . CopyDirectory ( $ "../{ BuildContext . ProjectName } /assets", $ "../Releases/{ context . Name } /assets") ;
108- }
109105
110106 context . CopyFile ( $ "../{ BuildContext . ProjectName } /modinfo.json", $ "../Releases/{ context . Name } /modinfo.json") ;
111107 if ( context . FileExists ( $ "../{ BuildContext . ProjectName } /modicon.png") )
112- {
113108 context . CopyFile ( $ "../{ BuildContext . ProjectName } /modicon.png", $ "../Releases/{ context . Name } /modicon.png") ;
114- }
115109
116110 context . Zip ( $ "../Releases/{ context . Name } ", $ "../Releases/{ context . Name } _{ context . Version } .zip") ;
117111 }
118112}
119113
114+ [ TaskName ( "Release" ) ]
115+ [ IsDependentOn ( typeof ( PackageTask ) ) ]
116+ public sealed class ReleaseTask : FrostingTask < BuildContext >
117+ {
118+ public override void Run ( BuildContext context )
119+ {
120+ var version = context . Version ;
121+ var name = context . Name ;
122+ var tag = $ "v{ version } ";
123+ var zipPath = $ "../Releases/{ name } _{ version } .zip";
124+
125+ if ( ! context . FileExists ( zipPath ) )
126+ throw new Exception ( $ "Release asset not found at { zipPath } ") ;
127+
128+ var ghExe = context . Tools . Resolve ( "gh" ) ?? "gh" ;
129+
130+ // create the release (will fail if already exists)
131+ var createArgs = new ProcessArgumentBuilder ( )
132+ . Append ( "release create" )
133+ . Append ( tag )
134+ . AppendQuoted ( zipPath . Replace ( '\\ ' , '/' ) )
135+ . AppendSwitch ( "--title" , " " , tag )
136+ . Append ( "--generate-notes" ) ;
137+
138+ var exitCode = context . StartProcess ( ghExe , new ProcessSettings { Arguments = createArgs } ) ;
139+
140+ if ( exitCode != 0 )
141+ {
142+ // If the release already exists, just upload/replace the asset
143+ var uploadArgs = new ProcessArgumentBuilder ( )
144+ . Append ( "release upload" )
145+ . Append ( tag )
146+ . AppendQuoted ( zipPath . Replace ( '\\ ' , '/' ) )
147+ . Append ( "--clobber" ) ;
148+
149+ var uploadExit = context . StartProcess ( ghExe , new ProcessSettings { Arguments = uploadArgs } ) ;
150+ if ( uploadExit != 0 )
151+ throw new Exception ( "Failed to upload asset to existing release." ) ;
152+ }
153+
154+ context . Information ( $ "✅ Published GitHub release { tag } with asset { zipPath } ") ;
155+ }
156+ }
157+
120158[ TaskName ( "Default" ) ]
121159[ IsDependentOn ( typeof ( PackageTask ) ) ]
122160public class DefaultTask : FrostingTask
0 commit comments