1+ ///////////////////////////////////////////////////////////////////////////////
2+ // ADDINS/TOOLS
3+ ///////////////////////////////////////////////////////////////////////////////
4+ #tool "nuget:?package=GitVersion.CommandLine"
5+ #addin nuget : ? package= Cake . Git
6+
7+ ///////////////////////////////////////////////////////////////////////////////
8+ // ARGUMENTS
9+ ///////////////////////////////////////////////////////////////////////////////
10+
11+ var target = Argument < string > ( "target" , "Default" ) ;
12+ var configuration = Argument < string > ( "configuration" , "Release" ) ;
13+
14+ ///////////////////////////////////////////////////////////////////////////////
15+ // GLOBAL VARIABLES
16+ ///////////////////////////////////////////////////////////////////////////////
17+
18+ var solutions = GetFiles ( "./**/*.sln" ) ;
19+ var projects = GetFiles ( "./**/*.csproj" ) . Select ( x => x . GetDirectory ( ) ) ;
20+
21+ GitVersion gitVersion ;
22+
23+ ///////////////////////////////////////////////////////////////////////////////
24+ // SETUP / TEARDOWN
25+ ///////////////////////////////////////////////////////////////////////////////
26+
27+ Setup ( context =>
28+ {
29+ gitVersion = GitVersion ( new GitVersionSettings {
30+ UpdateAssemblyInfo = true
31+ } ) ;
32+
33+ BuildParameters . Initialize ( Context ) ;
34+
35+ // Executed BEFORE the first task.
36+ Information ( "Xer.Cqrs.Extensions.CommandStack.Attributes" ) ;
37+ Information ( "Parameters" ) ;
38+ Information ( "///////////////////////////////////////////////////////////////////////////////" ) ;
39+ Information ( "Branch: {0}" , BuildParameters . Instance . BranchName ) ;
40+ Information ( "Version semver: {0}" , gitVersion . LegacySemVerPadded ) ;
41+ Information ( "Version assembly: {0}" , gitVersion . MajorMinorPatch ) ;
42+ Information ( "Version informational: {0}" , gitVersion . InformationalVersion ) ;
43+ Information ( "Master branch: {0}" , BuildParameters . Instance . IsMasterBranch ) ;
44+ Information ( "Dev branch: {0}" , BuildParameters . Instance . IsDevBranch ) ;
45+ Information ( "Hotfix branch: {0}" , BuildParameters . Instance . IsHotFixBranch ) ;
46+ Information ( "Publish to myget: {0}" , BuildParameters . Instance . ShouldPublishMyGet ) ;
47+ Information ( "Publish to nuget: {0}" , BuildParameters . Instance . ShouldPublishNuGet ) ;
48+ Information ( "///////////////////////////////////////////////////////////////////////////////" ) ;
49+ } ) ;
50+
51+ Teardown ( context =>
52+ {
53+ // Executed AFTER the last task.
54+ Information ( "Finished running tasks." ) ;
55+ } ) ;
56+
57+ ///////////////////////////////////////////////////////////////////////////////
58+ // TASK DEFINITIONS
59+ ///////////////////////////////////////////////////////////////////////////////
60+
61+ Task ( "Clean" )
62+ . Description ( "Cleans all directories that are used during the build process." )
63+ . Does ( ( ) =>
64+ {
65+ if ( projects . Count ( ) == 0 )
66+ {
67+ Information ( "No projects found." ) ;
68+ return ;
69+ }
70+
71+ // Clean solution directories.
72+ foreach ( var project in projects )
73+ {
74+ Information ( "Cleaning {0}" , project ) ;
75+ DotNetCoreClean ( project . FullPath ) ;
76+ }
77+ } ) ;
78+
79+ Task ( "Restore" )
80+ . Description ( "Restores all the NuGet packages that are used by the specified solution." )
81+ . Does ( ( ) =>
82+ {
83+ if ( solutions . Count ( ) == 0 )
84+ {
85+ Information ( "No solutions found." ) ;
86+ return ;
87+ }
88+
89+ var settings = new DotNetCoreRestoreSettings
90+ {
91+ ArgumentCustomization = args => args
92+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
93+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
94+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
95+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
96+ } ;
97+
98+ // Restore all NuGet packages.
99+ foreach ( var solution in solutions )
100+ {
101+ Information ( "Restoring {0}..." , solution ) ;
102+
103+ DotNetCoreRestore ( solution . FullPath , settings ) ;
104+ }
105+ } ) ;
106+
107+ Task ( "Build" )
108+ . Description ( "Builds all the different parts of the project." )
109+ . IsDependentOn ( "Clean" )
110+ . IsDependentOn ( "Restore" )
111+ . Does ( ( ) =>
112+ {
113+ if ( solutions . Count ( ) == 0 )
114+ {
115+ Information ( "No solutions found." ) ;
116+ return ;
117+ }
118+
119+ var settings = new DotNetCoreBuildSettings
120+ {
121+ Configuration = configuration ,
122+ ArgumentCustomization = args => args
123+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
124+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
125+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
126+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
127+ } ;
128+
129+ // Build all solutions.
130+ foreach ( var solution in solutions )
131+ {
132+ Information ( "Building {0}" , solution ) ;
133+
134+ DotNetCoreBuild ( solution . FullPath , settings ) ;
135+ }
136+ } ) ;
137+
138+ Task ( "Test" )
139+ . Description ( "Execute all unit test projects." )
140+ . IsDependentOn ( "Build" )
141+ . Does ( ( ) =>
142+ {
143+ var projects = GetFiles ( "./Tests/**/*.Tests.csproj" ) ;
144+
145+ if ( projects . Count == 0 )
146+ {
147+ Information ( "No test projects found." ) ;
148+ return ;
149+ }
150+
151+ var settings = new DotNetCoreTestSettings
152+ {
153+ Configuration = configuration ,
154+ NoBuild = true ,
155+ } ;
156+
157+ foreach ( var project in projects )
158+ {
159+ DotNetCoreTest ( project . FullPath , settings ) ;
160+ }
161+ } ) ;
162+
163+ Task ( "Pack" )
164+ . IsDependentOn ( "Test" )
165+ . Does ( ( ) =>
166+ {
167+ var projects = GetFiles ( "./src/**/*.csproj" ) ;
168+
169+ if ( projects . Count ( ) == 0 )
170+ {
171+ Information ( "No projects found." ) ;
172+ return ;
173+ }
174+
175+ var settings = new DotNetCorePackSettings
176+ {
177+ NoBuild = true ,
178+ Configuration = configuration ,
179+ ArgumentCustomization = ( args ) => args
180+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
181+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
182+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
183+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
184+ } ;
185+
186+ foreach ( var project in projects )
187+ {
188+ DotNetCorePack ( project . ToString ( ) , settings ) ;
189+ }
190+ } ) ;
191+
192+ Task ( "PublishMyGet" )
193+ . WithCriteria ( ( ) => BuildParameters . Instance . ShouldPublishMyGet )
194+ . IsDependentOn ( "Pack" )
195+ . Does ( ( ) =>
196+ {
197+ var nupkgs = GetFiles ( "./**/*.nupkg" ) ;
198+
199+ if ( nupkgs . Count ( ) == 0 )
200+ {
201+ Information ( "No nupkgs found." ) ;
202+ return ;
203+ }
204+
205+ foreach ( var nupkgFile in nupkgs )
206+ {
207+ Information ( "Pulishing to myget {0}" , nupkgFile ) ;
208+
209+ NuGetPush ( nupkgFile , new NuGetPushSettings
210+ {
211+ Source = BuildParameters . Instance . MyGetFeed ,
212+ ApiKey = BuildParameters . Instance . MyGetApiKey
213+ } ) ;
214+ }
215+ } ) ;
216+
217+ Task ( "PublishNuGet" )
218+ . WithCriteria ( ( ) => BuildParameters . Instance . ShouldPublishNuGet )
219+ . IsDependentOn ( "Pack" )
220+ . Does ( ( ) =>
221+ {
222+ var nupkgs = GetFiles ( "./**/*.nupkg" ) ;
223+
224+ if ( nupkgs . Count ( ) == 0 )
225+ {
226+ Information ( "No nupkgs found." ) ;
227+ return ;
228+ }
229+
230+ foreach ( var nupkgFile in nupkgs )
231+ {
232+ Information ( "Pulishing to nuget {0}" , nupkgFile ) ;
233+ NuGetPush ( nupkgFile , new NuGetPushSettings
234+ {
235+ Source = BuildParameters . Instance . NuGetFeed ,
236+ ApiKey = BuildParameters . Instance . NuGetApiKey
237+ } ) ;
238+ }
239+ } ) ;
240+
241+
242+ ///////////////////////////////////////////////////////////////////////////////
243+ // TARGETS
244+ ///////////////////////////////////////////////////////////////////////////////
245+
246+ Task ( "Default" )
247+ . Description ( "This is the default task which will be ran if no specific target is passed in." )
248+ . IsDependentOn ( "PublishNuGet" )
249+ . IsDependentOn ( "PublishMyGet" ) ;
250+
251+ ///////////////////////////////////////////////////////////////////////////////
252+ // EXECUTION
253+ ///////////////////////////////////////////////////////////////////////////////
254+
255+ RunTarget ( target ) ;
256+
257+ public class BuildParameters
258+ {
259+ private static BuildParameters _buildParameters ;
260+
261+ public static BuildParameters Instance => _buildParameters ;
262+
263+ private ICakeContext _context ;
264+
265+ private BuildParameters ( ICakeContext context )
266+ {
267+ _context = context ;
268+ }
269+
270+ public static void Initialize ( ICakeContext context )
271+ {
272+ if ( _buildParameters != null )
273+ {
274+ return ;
275+ }
276+
277+ _buildParameters = new BuildParameters ( context ) ;
278+ }
279+
280+ public bool IsAppVeyorBuild => _context . BuildSystem ( ) . AppVeyor . IsRunningOnAppVeyor ;
281+
282+ public bool IsLocalBuild => _context . BuildSystem ( ) . IsLocalBuild ;
283+
284+ public string BranchName
285+ {
286+ get
287+ {
288+ return IsLocalBuild
289+ ? _context . GitBranchCurrent ( "." ) . FriendlyName
290+ : _context . BuildSystem ( ) . AppVeyor . Environment . Repository . Branch ;
291+ }
292+ }
293+
294+ public string MyGetFeed => _context . EnvironmentVariable ( "MYGET_SOURCE" ) ;
295+
296+ public string MyGetApiKey => _context . EnvironmentVariable ( "MYGET_API_KEY" ) ;
297+
298+ public string NuGetFeed => _context . EnvironmentVariable ( "NUGET_SOURCE" ) ;
299+
300+ public string NuGetApiKey => _context . EnvironmentVariable ( "NUGET_API_KEY" ) ;
301+
302+ public bool IsMasterBranch => StringComparer . OrdinalIgnoreCase . Equals ( "master" , BranchName ) ;
303+
304+ public bool IsDevBranch => StringComparer . OrdinalIgnoreCase . Equals ( "dev" , BranchName ) ;
305+
306+ public bool IsReleaseBranch => BranchName . StartsWith ( "release" , StringComparison . OrdinalIgnoreCase ) ;
307+
308+ public bool IsHotFixBranch => BranchName . StartsWith ( "hotfix" , StringComparison . OrdinalIgnoreCase ) ;
309+
310+ public bool ShouldPublishMyGet => ! string . IsNullOrWhiteSpace ( MyGetApiKey ) && ! string . IsNullOrWhiteSpace ( MyGetFeed ) ;
311+
312+ public bool ShouldPublishNuGet => ! string . IsNullOrWhiteSpace ( NuGetApiKey )
313+ && ! string . IsNullOrWhiteSpace ( NuGetFeed )
314+ && ( IsMasterBranch || IsHotFixBranch ) ;
315+ }
0 commit comments