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.Delegator" ) ;
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+ // Clean solution directories.
66+ foreach ( var project in projects )
67+ {
68+ Information ( "Cleaning {0}" , project ) ;
69+ DotNetCoreClean ( project . FullPath ) ;
70+ }
71+ } ) ;
72+
73+ Task ( "Restore" )
74+ . Description ( "Restores all the NuGet packages that are used by the specified solution." )
75+ . Does ( ( ) =>
76+ {
77+ var settings = new DotNetCoreRestoreSettings
78+ {
79+ ArgumentCustomization = args => args
80+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
81+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
82+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
83+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
84+ } ;
85+
86+ // Restore all NuGet packages.
87+ foreach ( var solution in solutions )
88+ {
89+ Information ( "Restoring {0}..." , solution ) ;
90+
91+ DotNetCoreRestore ( solution . FullPath , settings ) ;
92+ }
93+ } ) ;
94+
95+ Task ( "Build" )
96+ . Description ( "Builds all the different parts of the project." )
97+ . IsDependentOn ( "Clean" )
98+ . IsDependentOn ( "Restore" )
99+ . Does ( ( ) =>
100+ {
101+ var settings = new DotNetCoreBuildSettings
102+ {
103+ Configuration = configuration ,
104+ ArgumentCustomization = args => args
105+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
106+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
107+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
108+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
109+ } ;
110+
111+ // Build all solutions.
112+ foreach ( var solution in solutions )
113+ {
114+ Information ( "Building {0}" , solution ) ;
115+
116+ DotNetCoreBuild ( solution . FullPath , settings ) ;
117+ }
118+ } ) ;
119+
120+ Task ( "Test" )
121+ . Description ( "Execute all unit test projects." )
122+ . IsDependentOn ( "Build" )
123+ . Does ( ( ) =>
124+ {
125+ var projects = GetFiles ( "./Tests/**/*.Tests.csproj" ) ;
126+ var settings = new DotNetCoreTestSettings
127+ {
128+ Configuration = configuration ,
129+ NoBuild = true ,
130+ } ;
131+
132+ foreach ( var project in projects )
133+ {
134+ DotNetCoreTest ( project . FullPath , settings ) ;
135+ }
136+ } ) ;
137+
138+ Task ( "Pack" )
139+ . IsDependentOn ( "Test" )
140+ . Does ( ( ) =>
141+ {
142+ var projects = GetFiles ( "./src/**/*.csproj" ) ;
143+ var settings = new DotNetCorePackSettings
144+ {
145+ NoBuild = true ,
146+ Configuration = configuration ,
147+ ArgumentCustomization = ( args ) => args
148+ . Append ( "/p:Version={0}" , gitVersion . LegacySemVerPadded )
149+ . Append ( "/p:AssemblyVersion={0}" , gitVersion . MajorMinorPatch )
150+ . Append ( "/p:FileVersion={0}" , gitVersion . MajorMinorPatch )
151+ . Append ( "/p:AssemblyInformationalVersion={0}" , gitVersion . InformationalVersion )
152+ } ;
153+
154+ foreach ( var project in projects )
155+ {
156+ DotNetCorePack ( project . ToString ( ) , settings ) ;
157+ }
158+ } ) ;
159+
160+ Task ( "PublishMyGet" )
161+ . WithCriteria ( ( ) => BuildParameters . Instance . ShouldPublishMyGet )
162+ . IsDependentOn ( "Pack" )
163+ . Does ( ( ) =>
164+ {
165+ var nupkgs = GetFiles ( "./**/*.nupkg" ) ;
166+
167+ foreach ( var nupkgFile in nupkgs )
168+ {
169+ Information ( "Pulishing to myget {0}" , nupkgFile ) ;
170+
171+ NuGetPush ( nupkgFile , new NuGetPushSettings
172+ {
173+ Source = BuildParameters . Instance . MyGetFeed ,
174+ ApiKey = BuildParameters . Instance . MyGetApiKey
175+ } ) ;
176+ }
177+ } ) ;
178+
179+ Task ( "PublishNuGet" )
180+ . WithCriteria ( ( ) => BuildParameters . Instance . ShouldPublishNuGet )
181+ . IsDependentOn ( "Pack" )
182+ . Does ( ( ) =>
183+ {
184+ var nupkgs = GetFiles ( "./**/*.nupkg" ) ;
185+
186+ foreach ( var nupkgFile in nupkgs )
187+ {
188+ Information ( "Pulishing to nuget {0}" , nupkgFile ) ;
189+ NuGetPush ( nupkgFile , new NuGetPushSettings
190+ {
191+ Source = BuildParameters . Instance . NuGetFeed ,
192+ ApiKey = BuildParameters . Instance . NuGetApiKey
193+ } ) ;
194+ }
195+ } ) ;
196+
197+
198+ ///////////////////////////////////////////////////////////////////////////////
199+ // TARGETS
200+ ///////////////////////////////////////////////////////////////////////////////
201+
202+ Task ( "Default" )
203+ . Description ( "This is the default task which will be ran if no specific target is passed in." )
204+ . IsDependentOn ( "PublishNuGet" )
205+ . IsDependentOn ( "PublishMyGet" ) ;
206+
207+ ///////////////////////////////////////////////////////////////////////////////
208+ // EXECUTION
209+ ///////////////////////////////////////////////////////////////////////////////
210+
211+ RunTarget ( target ) ;
212+
213+ public class BuildParameters
214+ {
215+ private static BuildParameters _buildParameters ;
216+
217+ public static BuildParameters Instance => _buildParameters ;
218+
219+ private ICakeContext _context ;
220+
221+ private BuildParameters ( ICakeContext context )
222+ {
223+ _context = context ;
224+ }
225+
226+ public static void Initialize ( ICakeContext context )
227+ {
228+ if ( _buildParameters != null )
229+ {
230+ return ;
231+ }
232+
233+ _buildParameters = new BuildParameters ( context ) ;
234+ }
235+
236+ public bool IsAppVeyorBuild => _context . BuildSystem ( ) . AppVeyor . IsRunningOnAppVeyor ;
237+
238+ public bool IsLocalBuild => _context . BuildSystem ( ) . IsLocalBuild ;
239+
240+ public string BranchName
241+ {
242+ get
243+ {
244+ return IsLocalBuild
245+ ? _context . GitBranchCurrent ( "." ) . FriendlyName
246+ : _context . BuildSystem ( ) . AppVeyor . Environment . Repository . Branch ;
247+ }
248+ }
249+
250+ public string MyGetFeed => _context . EnvironmentVariable ( "MYGET_SOURCE" ) ;
251+
252+ public string MyGetApiKey => _context . EnvironmentVariable ( "MYGET_API_KEY" ) ;
253+
254+ public string NuGetFeed => _context . EnvironmentVariable ( "NUGET_SOURCE" ) ;
255+
256+ public string NuGetApiKey => _context . EnvironmentVariable ( "NUGET_API_KEY" ) ;
257+
258+ public bool IsMasterBranch => StringComparer . OrdinalIgnoreCase . Equals ( "master" , BranchName ) ;
259+
260+ public bool IsDevBranch => StringComparer . OrdinalIgnoreCase . Equals ( "dev" , BranchName ) ;
261+
262+ public bool IsReleaseBranch => BranchName . StartsWith ( "release" , StringComparison . OrdinalIgnoreCase ) ;
263+
264+ public bool IsHotFixBranch => BranchName . StartsWith ( "hotfix" , StringComparison . OrdinalIgnoreCase ) ;
265+
266+ public bool ShouldPublishMyGet => ! string . IsNullOrWhiteSpace ( MyGetApiKey ) && ! string . IsNullOrWhiteSpace ( MyGetFeed ) ;
267+
268+ public bool ShouldPublishNuGet => ! string . IsNullOrWhiteSpace ( NuGetApiKey )
269+ && ! string . IsNullOrWhiteSpace ( NuGetFeed )
270+ && IsMasterBranch
271+ && IsHotFixBranch ;
272+ }
0 commit comments