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.EventStack.Extensions.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+ OutputDirectory = "./BuildArtifacts" ,
178+ NoBuild = true ,
179+ Configuration = configuration ,
180+ ArgumentCustomization = ( args ) => args
181+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
182+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
183+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
184+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
185+ } ;
186+
187+ foreach ( var project in projects )
188+ {
189+ DotNetCorePack ( project . ToString ( ) , settings ) ;
190+ }
191+ } ) ;
192+
193+ Task ( "PublishMyGet" )
194+ . WithCriteria ( ( ) => BuildParameters . Instance . ShouldPublishMyGet )
195+ . IsDependentOn ( "Pack" )
196+ . Does ( ( ) =>
197+ {
198+ var nupkgs = GetFiles ( "./**/*.nupkg" ) ;
199+
200+ if ( nupkgs . Count ( ) == 0 )
201+ {
202+ Information ( "No nupkgs found." ) ;
203+ return ;
204+ }
205+
206+ foreach ( var nupkgFile in nupkgs )
207+ {
208+ Information ( "Pulishing to myget {0}" , nupkgFile ) ;
209+
210+ NuGetPush ( nupkgFile , new NuGetPushSettings
211+ {
212+ Source = BuildParameters . Instance . MyGetFeed ,
213+ ApiKey = BuildParameters . Instance . MyGetApiKey
214+ } ) ;
215+ }
216+ } ) ;
217+
218+ Task ( "PublishNuGet" )
219+ . WithCriteria ( ( ) => BuildParameters . Instance . ShouldPublishNuGet )
220+ . IsDependentOn ( "Pack" )
221+ . Does ( ( ) =>
222+ {
223+ var nupkgs = GetFiles ( "./**/*.nupkg" ) ;
224+
225+ if ( nupkgs . Count ( ) == 0 )
226+ {
227+ Information ( "No nupkgs found." ) ;
228+ return ;
229+ }
230+
231+ foreach ( var nupkgFile in nupkgs )
232+ {
233+ Information ( "Pulishing to nuget {0}" , nupkgFile ) ;
234+ NuGetPush ( nupkgFile , new NuGetPushSettings
235+ {
236+ Source = BuildParameters . Instance . NuGetFeed ,
237+ ApiKey = BuildParameters . Instance . NuGetApiKey
238+ } ) ;
239+ }
240+ } ) ;
241+
242+
243+ ///////////////////////////////////////////////////////////////////////////////
244+ // TARGETS
245+ ///////////////////////////////////////////////////////////////////////////////
246+
247+ Task ( "Default" )
248+ . Description ( "This is the default task which will be ran if no specific target is passed in." )
249+ . IsDependentOn ( "PublishNuGet" )
250+ . IsDependentOn ( "PublishMyGet" ) ;
251+
252+ ///////////////////////////////////////////////////////////////////////////////
253+ // EXECUTION
254+ ///////////////////////////////////////////////////////////////////////////////
255+
256+ RunTarget ( target ) ;
257+
258+ public class BuildParameters
259+ {
260+ private static BuildParameters _buildParameters ;
261+
262+ public static BuildParameters Instance => _buildParameters ;
263+
264+ private ICakeContext _context ;
265+
266+ private BuildParameters ( ICakeContext context )
267+ {
268+ _context = context ;
269+ }
270+
271+ public static void Initialize ( ICakeContext context )
272+ {
273+ if ( _buildParameters != null )
274+ {
275+ return ;
276+ }
277+
278+ _buildParameters = new BuildParameters ( context ) ;
279+ }
280+
281+ public bool IsAppVeyorBuild => _context . BuildSystem ( ) . AppVeyor . IsRunningOnAppVeyor ;
282+
283+ public bool IsLocalBuild => _context . BuildSystem ( ) . IsLocalBuild ;
284+
285+ public string BranchName
286+ {
287+ get
288+ {
289+ return IsLocalBuild
290+ ? _context . GitBranchCurrent ( "." ) . FriendlyName
291+ : _context . BuildSystem ( ) . AppVeyor . Environment . Repository . Branch ;
292+ }
293+ }
294+
295+ public string MyGetFeed => _context . EnvironmentVariable ( "MYGET_SOURCE" ) ;
296+
297+ public string MyGetApiKey => _context . EnvironmentVariable ( "MYGET_API_KEY" ) ;
298+
299+ public string NuGetFeed => _context . EnvironmentVariable ( "NUGET_SOURCE" ) ;
300+
301+ public string NuGetApiKey => _context . EnvironmentVariable ( "NUGET_API_KEY" ) ;
302+
303+ public bool IsMasterBranch => StringComparer . OrdinalIgnoreCase . Equals ( "master" , BranchName ) ;
304+
305+ public bool IsDevBranch => StringComparer . OrdinalIgnoreCase . Equals ( "dev" , BranchName ) ;
306+
307+ public bool IsReleaseBranch => BranchName . StartsWith ( "release" , StringComparison . OrdinalIgnoreCase ) ;
308+
309+ public bool IsHotFixBranch => BranchName . StartsWith ( "hotfix" , StringComparison . OrdinalIgnoreCase ) ;
310+
311+ public bool ShouldPublishMyGet => ! string . IsNullOrWhiteSpace ( MyGetApiKey ) && ! string . IsNullOrWhiteSpace ( MyGetFeed ) ;
312+
313+ public bool ShouldPublishNuGet => ! string . IsNullOrWhiteSpace ( NuGetApiKey )
314+ && ! string . IsNullOrWhiteSpace ( NuGetFeed )
315+ && ( IsMasterBranch || IsHotFixBranch ) ;
316+ }
0 commit comments