1+ #tool "nuget:?package=xunit.runner.console&version=2.3.1"
2+ #tool "nuget:?package=GitVersion.CommandLine&version=3.6.5"
3+ #tool "nuget:?package=gitreleasemanager&version=0.7.1"
4+ #tool "nuget:?package=gitlink&version=2.4.0"
5+ #addin "nuget:?package=Cake.Incubator&version=3.0.0"
6+
7+ ///////////////////////////////////////////////////////////////////////////////
8+ // ARGUMENTS
9+ ///////////////////////////////////////////////////////////////////////////////
10+ var envBuildNumber = EnvironmentVariable < int > ( "APPVEYOR_BUILD_NUMBER" , 0 ) ;
11+ var gitHubUserName = EnvironmentVariable ( "GITHUB_USERNAME" ) ;
12+ var gitHubPassword = EnvironmentVariable ( "GITHUB_PASSWORD" ) ;
13+ var nugetSourceUrl = EnvironmentVariable ( "NUGET_SOURCE" ) ;
14+ var nugetApiKey = EnvironmentVariable ( "NUGET_API_KEY" ) ;
15+
16+ var target = Argument ( "target" , "Default" ) ;
17+ var configuration = Argument ( "configuration" , "Release" ) ;
18+ var buildNumber = Argument < int > ( "buildNumber" , envBuildNumber ) ;
19+
20+ ///////////////////////////////////////////////////////////////////////////////
21+ // VARIABLES
22+ ///////////////////////////////////////////////////////////////////////////////
23+
24+ // folders
25+ var artifactsDir = Directory ( "./artifacts" ) ;
26+ var nugetPackageDir = artifactsDir + Directory ( "nuget-packages" ) ;
27+ var srcDir = Directory ( "./src" ) ;
28+ var rootPath = MakeAbsolute ( Directory ( "./" ) ) ;
29+ var releaseNotesPath = rootPath . CombineWithFilePath ( "CHANGELOG.md" ) ;
30+
31+ // project specific
32+ var solutionFile = srcDir + File ( "servicestack.configuration.consul.sln" ) ;
33+ var gitHubRepositoryOwner = "wwwlicious" ;
34+ var gitHubRepositoryName = "servicestack-configuration-consul" ;
35+
36+ var isLocalBuild = BuildSystem . IsLocalBuild ;
37+ var isPullRequest = BuildSystem . AppVeyor . Environment . PullRequest . IsPullRequest ;
38+ var isMasterBranch = BuildSystem . AppVeyor . Environment . Repository . Branch . EqualsIgnoreCase ( "master" ) ;
39+ var isReleaseBranch = BuildSystem . AppVeyor . Environment . Repository . Branch . StartsWithIgnoreCase ( "release" ) ;
40+ var isHotFixBranch = BuildSystem . AppVeyor . Environment . Repository . Branch . StartsWithIgnoreCase ( "hotfix" ) ;
41+ var isTagged = BuildSystem . AppVeyor . Environment . Repository . Tag . IsTag && ! BuildSystem . AppVeyor . Environment . Repository . Tag . Name . IsNullOrEmpty ( ) ;
42+ var publishingError = false ;
43+
44+ var shouldPublishNuGet = ( ! isLocalBuild && ! isPullRequest && ( isMasterBranch || isReleaseBranch || isHotFixBranch ) && isTagged ) ;
45+ var shouldPublishGitHub = shouldPublishNuGet ;
46+
47+ var gitVersionResults = GitVersion ( new GitVersionSettings { UpdateAssemblyInfo = false } ) ;
48+ var semVersion = $ "{ gitVersionResults . MajorMinorPatch } .{ buildNumber } ";
49+
50+ Information ( "SemverVersion -> {0}" , semVersion ) ;
51+
52+ var projects = ParseSolution ( solutionFile ) . GetProjects ( ) . Select ( x => ParseProject ( x . Path , configuration ) ) ;
53+
54+ ///////////////////////////////////////////////////////////////////////////////
55+ // SETUP / TEARDOWN
56+ ///////////////////////////////////////////////////////////////////////////////
57+
58+ Setup ( ctx =>
59+ {
60+ // Executed BEFORE the first task.
61+ Information ( "Running tasks..." ) ;
62+
63+ if ( isMasterBranch && ( ctx . Log . Verbosity != Verbosity . Diagnostic ) ) {
64+ Information ( "Increasing verbosity to diagnostic." ) ;
65+ ctx . Log . Verbosity = Verbosity . Diagnostic ;
66+ }
67+ } ) ;
68+
69+ Teardown ( ctx =>
70+ {
71+ // Executed AFTER the last task.
72+ Information ( "Finished running tasks." ) ;
73+ } ) ;
74+
75+ ///////////////////////////////////////////////////////////////////////////////
76+ // TASKS
77+ ///////////////////////////////////////////////////////////////////////////////
78+
79+ Task ( "Default" )
80+ . IsDependentOn ( "Clean" )
81+ . IsDependentOn ( "Build" )
82+ . IsDependentOn ( "Test" ) ;
83+
84+ Task ( "Build" )
85+ . Does ( ( ) => {
86+ Information ( "Building {0}" , solutionFile ) ;
87+ var msbuildBinaryLogFile = artifactsDir + new FilePath ( solutionFile . Path . GetFilenameWithoutExtension ( ) + ".binlog" ) ;
88+
89+ MSBuild ( solutionFile . Path , settings => {
90+ settings
91+ . SetConfiguration ( configuration )
92+ . SetMaxCpuCount ( 0 ) // use as many cpu's as are available
93+ . WithRestore ( )
94+ . WithProperty ( "TreatresultsAsErrors" , "false" )
95+ . WithProperty ( "resultsAsErrors" , "3884" )
96+ . WithProperty ( "CodeContractsRunCodeAnalysis" , "true" )
97+ . WithProperty ( "RunCodeAnalysis" , "false" )
98+ . WithProperty ( "Version" , semVersion )
99+ . WithProperty ( "PackageVersion" , gitVersionResults . MajorMinorPatch )
100+ . WithProperty ( "PackageOutputPath" , MakeAbsolute ( nugetPackageDir ) . FullPath )
101+ . UseToolVersion ( MSBuildToolVersion . VS2017 )
102+ . SetNodeReuse ( false ) ;
103+
104+ // setup binary logging for solution to artifacts dir
105+ settings . ArgumentCustomization = arguments => {
106+ arguments . Append ( string . Format ( "/bl:{0}" , msbuildBinaryLogFile ) ) ;
107+ return arguments ;
108+ } ;
109+ } ) ;
110+ } ) ;
111+
112+ Task ( "Test" )
113+ . Does ( ( ) => {
114+ Information ( "Testing for {0}" , solutionFile ) ;
115+ var testProjects = projects . Where ( x => x . IsTestProject ( ) ) ;
116+ foreach ( var proj in testProjects ) {
117+ DotNetCoreTest ( proj . ProjectFilePath . FullPath ) ;
118+ }
119+ } ) ;
120+
121+ Task ( "ReleaseNotes" )
122+ . IsDependentOn ( "Create-Release-Notes" ) ;
123+
124+ Task ( "AppVeyor" )
125+ . IsDependentOn ( "Default" )
126+ . IsDependentOn ( "Upload-AppVeyor-Artifacts" )
127+ . IsDependentOn ( "Publish-Nuget-Packages" )
128+ . IsDependentOn ( "Publish-GitHub-Release" )
129+ . Finally ( ( ) =>
130+ {
131+ if ( publishingError )
132+ {
133+ throw new Exception ( $ "An error occurred during the publishing of { solutionFile . Path } . All publishing tasks have been attempted.") ;
134+ }
135+ } ) ;
136+
137+ Task ( "Create-Release-Notes" )
138+ . Does ( ( ) => {
139+ Information ( "Creating release notes for {0}" , semVersion ) ;
140+ gitHubUserName . ThrowIfNull ( nameof ( gitHubUserName ) ) ;
141+ gitHubPassword . ThrowIfNull ( nameof ( gitHubPassword ) ) ;
142+ GitReleaseManagerCreate ( gitHubUserName , gitHubPassword , gitHubRepositoryOwner , gitHubRepositoryName , new GitReleaseManagerCreateSettings {
143+ Milestone = gitVersionResults . MajorMinorPatch ,
144+ Name = gitVersionResults . MajorMinorPatch ,
145+ Prerelease = false ,
146+ TargetCommitish = "master" ,
147+ } ) ;
148+ } ) ;
149+
150+ Task ( "Export-Release-Notes" )
151+ . WithCriteria ( ( ) => ! isLocalBuild )
152+ . WithCriteria ( ( ) => BuildSystem . IsRunningOnAppVeyor && ! isPullRequest )
153+ . WithCriteria ( ( ) => isMasterBranch || isReleaseBranch || isHotFixBranch )
154+ . WithCriteria ( ( ) => isTagged )
155+ . Does ( ( ) => {
156+ Information ( "Exporting release notes for {0}" , solutionFile ) ;
157+ gitHubUserName . ThrowIfNull ( nameof ( gitHubUserName ) ) ;
158+ gitHubPassword . ThrowIfNull ( nameof ( gitHubPassword ) ) ;
159+
160+ GitReleaseManagerExport ( gitHubUserName , gitHubPassword , gitHubRepositoryOwner , gitHubRepositoryName , releaseNotesPath ,
161+ new GitReleaseManagerExportSettings {
162+ TagName = gitVersionResults . MajorMinorPatch
163+ } ) ;
164+ } ) ;
165+
166+ Task ( "Publish-GitHub-Release" )
167+ . IsDependentOn ( "Export-Release-Notes" )
168+ . WithCriteria ( ( ) => shouldPublishGitHub )
169+ . Does ( ( ) => {
170+ Information ( "Publishing github release for {0}" , solutionFile ) ;
171+ gitHubUserName . ThrowIfNull ( nameof ( gitHubUserName ) ) ;
172+ gitHubPassword . ThrowIfNull ( nameof ( gitHubPassword ) ) ;
173+
174+ // upload packages as assets
175+ foreach ( var package in GetFiles ( nugetPackageDir . Path + "/*" ) )
176+ {
177+ GitReleaseManagerAddAssets ( gitHubUserName , gitHubPassword , gitHubRepositoryOwner , gitHubRepositoryName , gitVersionResults . MajorMinorPatch , package . ToString ( ) ) ;
178+ }
179+
180+ // close the release
181+ GitReleaseManagerClose ( gitHubUserName , gitHubPassword , gitHubRepositoryOwner , gitHubRepositoryName , gitVersionResults . MajorMinorPatch ) ;
182+ } ) ;
183+
184+ Task ( "Publish-Nuget-Packages" )
185+ . WithCriteria ( ( ) => shouldPublishNuGet )
186+ . WithCriteria ( ( ) => DirectoryExists ( nugetPackageDir ) )
187+ . Does ( ( ) => {
188+
189+ Information ( "Publishing NuGet Packages for {0}" , solutionFile ) ;
190+
191+ nugetSourceUrl . ThrowIfNull ( nameof ( nugetSourceUrl ) ) ;
192+ nugetApiKey . ThrowIfNull ( nameof ( nugetApiKey ) ) ;
193+ var nupkgFiles = GetFiles ( nugetPackageDir . Path + "/**/*.nupkg" ) ;
194+
195+ foreach ( var nupkgFile in nupkgFiles )
196+ {
197+ // Push the package.
198+ NuGetPush ( nupkgFile , new NuGetPushSettings {
199+ Source = nugetSourceUrl ,
200+ ApiKey = nugetApiKey ,
201+ } ) ;
202+ }
203+ } ) ;
204+
205+
206+ Task ( "Upload-AppVeyor-Artifacts" )
207+ . IsDependentOn ( "Export-Release-Notes" )
208+ . WithCriteria ( ( ) => BuildSystem . IsRunningOnAppVeyor )
209+ . WithCriteria ( ( ) => DirectoryExists ( nugetPackageDir ) )
210+ . Does ( ( ) => {
211+ Information ( "Uploading AppVeyor artifacts for {0}" , solutionFile ) ;
212+ foreach ( var package in GetFiles ( nugetPackageDir . Path + "/*" ) )
213+ {
214+ AppVeyor . UploadArtifact ( package ) ;
215+ }
216+ } ) ;
217+
218+ Task ( "Sample" )
219+ . Does ( ( ) => {
220+ Information ( "Restoring NuGet Packages for {0}" , solutionFile ) ;
221+ } ) ;
222+
223+ Task ( "Clean" )
224+ . Does ( ( ) => {
225+ CleanDirectories ( new DirectoryPath [ ] {
226+ artifactsDir ,
227+ nugetPackageDir
228+ } ) ;
229+ } ) ;
230+
231+ RunTarget ( target ) ;
0 commit comments