1+ //--------------------------------------------------//
2+ // Created by basicx-StrgV //
3+ // https://github.com/basicx-StrgV/ //
4+ //--------------------------------------------------//
15using System . IO ;
26using Serilog ;
37using Nuke . Common ;
711
812namespace BuildTool
913{
10- class Build : NukeBuild
14+ internal class Build : NukeBuild
1115 {
1216 public static int Main ( ) => Execute < Build > ( x => x . Pack ) ;
1317
14- [ Parameter ( "Version of the package " ) ]
15- public readonly string Version = "0.0.0" ;
18+ [ Parameter ( "[Optional] Update the major version (Default: Minor version will be updated) " ) ]
19+ public bool Major = false ;
1620
17- [ Parameter ( "The path of the doxygen bin directory (Default: C:\\ Program Files\\ doxygen\\ bin)" ) ]
21+ [ Parameter ( "[Optional] Update the patch version (Default: Minor version will be updated)" ) ]
22+ public bool Patch = false ;
23+
24+ [ Parameter ( "[Optional] Provide a new version for the package" ) ]
25+ public readonly string ? Version = null ;
26+
27+ [ Parameter ( "[Optional] The path of the doxygen bin directory (Default: C:\\ Program Files\\ doxygen\\ bin)" ) ]
1828 public readonly string DoxygenBin = "C:\\ Program Files\\ doxygen\\ bin" ;
1929
20- [ Parameter ( "Sets that no docs should be created" ) ]
30+ [ Parameter ( "[Optional] Sets that no docs should be created" ) ]
2131 public readonly bool NoDocs = false ;
2232
2333 [ Solution ]
24- public readonly Solution Solution ;
34+ public readonly Solution ? Solution ;
2535
2636 // Allways set config to release, because build is intended for creating ready to ship packages
2737 private readonly Configuration _configuration = Configuration . Release ;
2838
39+ private string ? _workingVersion = null ;
40+
2941 private static AbsolutePath _packages => RootDirectory / "nuget" / "packages" ;
3042 private static AbsolutePath _doxygenWorkingDir => RootDirectory / "doxygen" ;
3143 private static AbsolutePath _doxyfile => _doxygenWorkingDir / "Doxyfile" ;
44+ private static AbsolutePath _srcDirectory => RootDirectory / "src" ;
45+ private static AbsolutePath _configFile => _srcDirectory / "build.config.json" ;
46+
47+ Target LoadConfig => _ => _
48+ . Unlisted ( )
49+ . Before ( Info , Restore , Compile , Pack )
50+ . Executes ( ( ) =>
51+ {
52+ Log . Information ( "Loading the build config" ) ;
53+
54+ // Check if config exist
55+ if ( ! File . Exists ( _configFile ) )
56+ {
57+ if ( Version != null )
58+ {
59+ Log . Warning ( "Config file not found. The provided version ({version}) will still be used" , Version ) ;
60+ _workingVersion = Version ;
61+ }
62+ else
63+ {
64+ Log . Warning ( "Config file not found. The version \" 1.0.0\" will be used as a fallback" ) ;
65+ _workingVersion = "1.0.0" ;
66+ }
67+
68+ // Proceed with next target
69+ return ;
70+ }
71+
72+ // Try to load data from config
73+ ConfigHandler . BuildConfig ? buildConfig = ConfigHandler . LoadConfig ( _configFile ) ;
74+ if ( buildConfig == null )
75+ {
76+ if ( Version != null )
77+ {
78+ Log . Warning ( "Failed to load the config file. The provided version ({version}) will still be used" , Version ) ;
79+ _workingVersion = Version ;
80+ }
81+ else
82+ {
83+ Log . Warning ( "Failed to load the config file. The version \" 1.0.0\" will be used as a fallback" ) ;
84+ _workingVersion = "1.0.0" ;
85+ }
86+
87+ // Proceed with next target
88+ return ;
89+ }
90+
91+ // Try to parse the configured version
92+ bool parsingResult = System . Version . TryParse ( buildConfig . CurrentVersion , out System . Version ? version ) ;
93+ if ( version == null || ! parsingResult )
94+ {
95+ if ( Version != null )
96+ {
97+ Log . Warning ( "The version provided by the config file ({configVersion}) is invalid. The provided version ({version}) will still be used" , version , Version ) ;
98+ _workingVersion = Version ;
99+ }
100+ else
101+ {
102+ Log . Warning ( "The version provided by the config file ({configVersion}) is invalid. The version \" 1.0.0\" will be used as a fallback" , version ) ;
103+ _workingVersion = "1.0.0" ;
104+ }
105+
106+ // Proceed with next target
107+ return ;
108+ }
109+
110+ if ( Major && Patch )
111+ {
112+ // Cancel the build process
113+ Assert . Fail ( "Can't update major and patch at the same time" ) ;
114+ }
115+
116+ if ( Major || Patch )
117+ {
118+ if ( Major )
119+ {
120+ _workingVersion =
121+ new System . Version ( version . Major + 1 , 0 , 0 )
122+ . ToString ( ) ;
123+ }
124+ else
125+ {
126+ _workingVersion =
127+ new System . Version ( version . Major , version . Minor , version . Build + 1 )
128+ . ToString ( ) ;
129+ }
130+
131+ return ;
132+ }
133+
134+ _workingVersion =
135+ new System . Version ( version . Major , version . Minor + 1 , version . Build )
136+ . ToString ( ) ;
137+ } ) ;
32138
33139 Target Info => _ => _
34140 . Unlisted ( )
35141 . Before ( Restore , Compile , Pack )
36142 . Executes ( ( ) =>
37143 {
38144 Log . Information ( "Creating \" WGet.NET\" nuget package" ) ;
39- Log . Information ( "Version: {version}" , Version ) ;
145+ Log . Information ( "Version: {version}" , _workingVersion ) ;
40146 Log . Information ( "Output directory: {dir}" , _packages ) ;
41147 } ) ;
42148
43149 Target Restore => _ => _
44150 . Unlisted ( )
45151 . Executes ( ( ) =>
46152 {
153+ if ( Solution == null )
154+ {
155+ Assert . Fail ( "The solution was not loaded correctly" ) ;
156+ return ; // Normaly not needed but the compiler does not know that.
157+ }
158+
47159 DotNetTasks . DotNetRestore ( s => s
48160 . SetProjectFile ( Solution . GetProject ( "WGet.NET" ) ) ) ;
49161 } ) ;
50162
51163 Target Compile => _ => _
52- . DependsOn ( Restore )
164+ . DependsOn ( LoadConfig , Restore )
53165 . Executes ( ( ) =>
54166 {
167+ if ( Solution == null )
168+ {
169+ Assert . Fail ( "The solution was not loaded correctly" ) ;
170+ return ; // Normaly not needed but the compiler does not know that.
171+ }
172+
55173 DotNetTasks . DotNetBuild ( s => s
56174 . SetProjectFile ( Solution . GetProject ( "WGet.NET" ) )
57175 . SetConfiguration ( _configuration )
58176 . EnableNoRestore ( )
59- . SetVersion ( Version )
60- . SetFileVersion ( Version )
61- . SetAssemblyVersion ( Version ) ) ;
177+ . SetVersion ( _workingVersion )
178+ . SetFileVersion ( _workingVersion )
179+ . SetAssemblyVersion ( _workingVersion ) ) ;
62180 } ) ;
63181
64182 Target Pack => _ => _
65183 . DependsOn ( Info , Compile )
66184 . Triggers ( Docs )
67185 . Executes ( ( ) =>
68186 {
187+ if ( Solution == null )
188+ {
189+ Assert . Fail ( "The solution was not loaded correctly" ) ;
190+ return ; // Normaly not needed but the compiler does not know that.
191+ }
192+
69193 DotNetTasks . DotNetPack ( s => s
70194 . SetProject ( Solution . GetProject ( "WGet.NET" ) )
71195 . SetConfiguration ( _configuration )
72196 . SetNoBuild ( true )
73197 . SetOutputDirectory ( _packages )
74- . SetVersion ( Version )
75- . SetFileVersion ( Version )
76- . SetAssemblyVersion ( Version )
77- . SetProperty ( "PackageReleaseNotes" , $ "https://github.com/basicx-StrgV/WGet.NET/releases/tag/{ Version } ") ) ;
198+ . SetVersion ( _workingVersion )
199+ . SetFileVersion ( _workingVersion )
200+ . SetAssemblyVersion ( _workingVersion )
201+ . SetProperty ( "PackageReleaseNotes" , $ "https://github.com/basicx-StrgV/WGet.NET/releases/tag/{ _workingVersion } ") ) ;
78202 } ) ;
79203
80204 Target Docs => _ => _
81205 . Unlisted ( )
82206 . OnlyWhenDynamic ( ( ) => ! NoDocs )
83207 . Executes ( ( ) =>
84208 {
209+ if ( _workingVersion == null )
210+ {
211+ Assert . Fail ( $ "No version for the package is defined") ;
212+ return ; // Normaly not needed but the compiler does not know that.
213+ }
214+
85215 if ( ! Directory . Exists ( DoxygenBin ) )
86216 {
87217 Assert . Fail ( $ "The doxygen bin directory does not exist ({ DoxygenBin } )") ;
@@ -106,7 +236,7 @@ class Build : NukeBuild
106236
107237 DoxygenHandler doxygen = new ( _doxyfile , doxygenExe , _doxygenWorkingDir ) ;
108238
109- bool result = doxygen . GenerateDocs ( Version ) ;
239+ bool result = doxygen . GenerateDocs ( _workingVersion ) ;
110240
111241 if ( ! result )
112242 {
0 commit comments