1- #tool "nuget:?package=xunit.runner.console&version=2.1.0"
2-
31#addin "Cake.FileHelpers"
2+ #addin "Cake.Powershell"
43
54var target = Argument ( "target" , "Default" ) ;
65var configuration = Argument ( "configuration" , "Release" ) ;
@@ -19,8 +18,9 @@ Task("RestorePackages")
1918 . IsDependentOn ( "Clean" )
2019 . Does ( ( ) =>
2120{
22- DotNetCoreRestore ( solution ) ;
23- NuGetRestore ( solution ) ;
21+ DotNetCoreRestore ( solution , new DotNetCoreRestoreSettings {
22+ EnvironmentVariables = DotNetEnvironment
23+ } ) ;
2424} ) ;
2525
2626Task ( "Build" )
@@ -36,14 +36,14 @@ Task("RunTests")
3636 . IsDependentOn ( "Build" )
3737 . Does ( ( ) =>
3838{
39-
4039 var testProjects = new string [ ] { "SqlStreamStore.HAL.Tests" } ;
4140
4241 foreach ( var testProject in testProjects ) {
4342 var projectDir = srcDir + Directory ( testProject ) ;
4443 StartProcess ( "dotnet" , new ProcessSettings {
4544 Arguments = "xunit" ,
46- WorkingDirectory = projectDir
45+ WorkingDirectory = projectDir ,
46+ EnvironmentVariables = DotNetEnvironment
4747 } ) ;
4848 }
4949} ) ;
@@ -59,7 +59,8 @@ Task("NuGetPack")
5959 OutputDirectory = artifactsDir ,
6060 NoBuild = true ,
6161 Configuration = configuration ,
62- VersionSuffix = versionSuffix
62+ VersionSuffix = versionSuffix ,
63+ EnvironmentVariables = DotNetEnvironment
6364 } ) ;
6465} ) ;
6566
@@ -68,3 +69,80 @@ Task("Default")
6869 . IsDependentOn ( "NuGetPack" ) ;
6970
7071RunTarget ( target ) ;
72+
73+ Dictionary < string , string > DotNetEnvironment => new Dictionary < string , string > {
74+ { "PATH" , Path }
75+ } ;
76+
77+ private string _path ;
78+
79+ string Path => _path ?? ( _path = DownloadDotNetCoreIfNecessary ( ) ) ;
80+
81+ string DownloadDotNetCoreIfNecessary ( ) {
82+ var path = Context. Environment . GetEnvironmentVariable ( "PATH" ) ;
83+ var version = "2.0.0" ;
84+
85+ if ( DotNetVersion == System . Version . Parse ( version ) ) {
86+ return path ;
87+ }
88+
89+ var dotnetDirectory = Directory ( ".dotnet" ) ;
90+
91+ EnsureDirectoryExists ( dotnetDirectory ) ;
92+
93+ if ( IsRunningOnWindows ( ) ) {
94+ DownloadDotNetCoreForWindows ( dotnetDirectory , version ) ;
95+ } else {
96+ DownloadDotNetCoreForUnix ( dotnetDirectory , version ) ;
97+ }
98+ return $ "{ dotnetDirectory . Path . MakeAbsolute ( Context . Environment ) } ;{ path } ";
99+ }
100+
101+ void DownloadDotNetCoreForWindows ( ConvertableDirectoryPath dotnetDirectory , string version ) {
102+ var channel = "Current" ;
103+ var installer = "https://dot.net/dotnet-install.ps1" ;
104+ var installerPath = dotnetDirectory + File ( "dotnet-install.ps1" ) ;
105+
106+ Information ( installerPath ) ;
107+
108+ DownloadFile ( installer , installerPath ) ;
109+
110+ StartPowershellFile ( installerPath , args => {
111+ args . Append ( "Channel" , channel ) ;
112+ args . Append ( "Version" , version ) ;
113+ args . Append ( "InstallDir" , dotnetDirectory ) ;
114+ } ) ;
115+ }
116+
117+ void DownloadDotNetCoreForUnix ( DirectoryPath dotnetDirectory , string version ) {
118+ var channel = "Current" ;
119+ var installer = "https://dot.net/dotnet-install.sh" ;
120+ var installerPath = dotnetDirectory + File ( "dotnet-install.sh" ) ;
121+
122+ DownloadFile ( installer , installerPath ) ;
123+
124+ using ( var process = StartAndReturnProcess ( installerPath , new ProcessSettings {
125+ Arguments = $ "--channel { channel } --version { version } --install-dir { dotnetDirectory } "
126+ } ) ) {
127+ process . WaitForExit ( ) ;
128+ }
129+ }
130+
131+ Version DotNetVersion {
132+ get {
133+ using ( var process = StartAndReturnProcess ( "dotnet" , new ProcessSettings {
134+ Arguments = "--version" ,
135+ RedirectStandardOutput = true
136+ } ) ) {
137+ process . WaitForExit ( ) ;
138+
139+ var stdout = process . GetStandardOutput ( ) . FirstOrDefault ( ) ;
140+
141+ System . Version installedVersion ;
142+
143+ System . Version . TryParse ( stdout , out installedVersion ) ;
144+
145+ return installedVersion ;
146+ }
147+ }
148+ }
0 commit comments