@@ -23,18 +23,6 @@ namespace Microsoft.Build.Cargo
23
23
/// </summary>
24
24
public class CargoTask : Task
25
25
{
26
- private static readonly string ? _tempPath = Environment . GetEnvironmentVariable ( "TEMP" ) ?? throw new Exception ( "%TEMP% directory not defined" ) ;
27
- private static readonly string _rustUpBinary = Path . Combine ( _tempPath , "cargohome" , "bin" , "rustup.exe" ) ;
28
- private static readonly string _cargoPath = Path . Combine ( _tempPath , "cargohome" , "bin" , "cargo.exe" ) ;
29
- private static readonly string _rustInstallPath = Path . Combine ( _tempPath , "rustinstall" ) ;
30
- private static readonly string _rustUpInitBinary = Path . Combine ( _rustInstallPath , "rustup-init.exe" ) ;
31
- private static readonly string _cargoHome = Path . Combine ( _tempPath , "cargohome" ) ;
32
- private static readonly string _rustUpHome = Path . Combine ( _tempPath , "rustuphome" ) ;
33
- private static readonly string _cargoHomeBin = Path . Combine ( _tempPath , "cargohome" , "bin" ) ;
34
- private static readonly string _msRustUpBinary = Path . Combine ( _tempPath , "cargohome" , "bin" , "msrustup.exe" ) ;
35
- private static readonly Dictionary < string , string > _envVars = new ( ) { { "CARGO_HOME" , _cargoHome } , { "RUSTUP_HOME" , _rustUpHome } } ;
36
- private static readonly string _rustUpDownloadLink = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe" ;
37
- private static readonly string _checkSumVerifyUrl = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe.sha256" ;
38
26
private static readonly string _rustToolChainFileName = "rust-toolchain.toml" ;
39
27
private static readonly string _cargoConfigFilePath = Path . Combine ( ".cargo" , "config.toml" ) ;
40
28
private static readonly string _cargoFileName = "cargo.toml" ;
@@ -43,18 +31,34 @@ public class CargoTask : Task
43
31
private static readonly string _installCommand = "install" ;
44
32
private static readonly string _fetchCommand = "fetch" ;
45
33
private static readonly string _loginCommand = "login" ;
34
+ private static readonly string _rustUpDownloadLink = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe" ;
35
+ private static readonly string _checkSumVerifyUrl = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe.sha256" ;
46
36
private string ? _rustUpFile = Environment . GetEnvironmentVariable ( "MSRUSTUP_FILE" ) ;
47
37
private bool _shouldCleanRustPath = false ;
48
38
private bool _isMsRustUp = false ;
49
39
private string ? _currentRustUpInitExeCheckSum ;
50
40
private List < string > _cargoRegistries = new ( ) ;
41
+ private string _rustUpBinary = string . Empty ;
42
+ private string _cargoPath = string . Empty ;
43
+ private string _rustInstallPath = string . Empty ;
44
+ private string _rustUpInitBinary = string . Empty ;
45
+ private string _cargoHome = string . Empty ;
46
+ private string _rustUpHome = string . Empty ;
47
+ private string _cargoHomeBin = string . Empty ;
48
+ private string _msRustUpBinary = string . Empty ;
49
+ private Dictionary < string , string > _envVars = new ( ) ;
51
50
52
51
private enum ExitCode
53
52
{
54
53
Succeeded ,
55
54
Failed ,
56
55
}
57
56
57
+ /// <summary>
58
+ /// Gets or sets installation root path for rust.
59
+ /// <inheritdoc/>
60
+ public string CargoInstallationRoot { get ; set ; } = Environment . GetEnvironmentVariable ( "TEMP" ) ?? Path . GetTempPath ( ) ;
61
+
58
62
/// <summary>
59
63
/// Gets or sets a cargo command to execute.
60
64
/// </summary>
@@ -87,17 +91,36 @@ private enum ExitCode
87
91
/// </summary>
88
92
public string Configuration { get ; set ; } = string . Empty ;
89
93
94
+ /// <summary>
95
+ /// Gets or sets the MSRustup Authentication type.
96
+ /// </summary>
97
+ public string MsRustupAuthType { get ; set ; } = string . Empty ;
98
+
90
99
/// <inheritdoc/>
91
100
public override bool Execute ( )
92
101
{
102
+ if ( string . IsNullOrEmpty ( CargoInstallationRoot ) )
103
+ {
104
+ throw new InvalidOperationException ( "CargoInstallationRoot cannot be null or empty." ) ;
105
+ }
106
+
107
+ _rustUpBinary = Path . Combine ( CargoInstallationRoot , "cargohome" , "bin" , "rustup.exe" ) ;
108
+ _cargoPath = Path . Combine ( CargoInstallationRoot , "cargohome" , "bin" , "cargo.exe" ) ;
109
+ _rustInstallPath = Path . Combine ( CargoInstallationRoot , "rustinstall" ) ;
110
+ _rustUpInitBinary = Path . Combine ( _rustInstallPath , "rustup-init.exe" ) ;
111
+ _cargoHome = Path . Combine ( CargoInstallationRoot , "cargohome" ) ;
112
+ _rustUpHome = Path . Combine ( CargoInstallationRoot , "rustuphome" ) ;
113
+ _cargoHomeBin = Path . Combine ( CargoInstallationRoot , "cargohome" , "bin" ) ;
114
+ _msRustUpBinary = Path . Combine ( CargoInstallationRoot , "cargohome" , "bin" , "msrustup.exe" ) ;
115
+ _envVars = new ( ) { { "CARGO_HOME" , _cargoHome } , { "RUSTUP_HOME" , _rustUpHome } } ;
93
116
return ExecuteAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
94
117
}
95
118
96
- private static void CleanupRustPath ( )
119
+ private void CleanupRustPath ( )
97
120
{
98
- if ( Directory . Exists ( _rustUpInitBinary ) )
121
+ if ( File . Exists ( _rustUpInitBinary ) )
99
122
{
100
- Directory . Delete ( _rustUpInitBinary , true ) ;
123
+ File . Delete ( _rustUpInitBinary ) ;
101
124
}
102
125
}
103
126
@@ -108,7 +131,6 @@ private async Task<bool> ExecuteAsync()
108
131
_isMsRustUp = File . Exists ( Path . Combine ( RepoRoot , _rustToolChainFileName ) ) && IsMSToolChain ( Path . Combine ( RepoRoot , _rustToolChainFileName ) ) ;
109
132
}
110
133
111
- // download & install rust if necessary
112
134
if ( Command . Equals ( _installCommand , StringComparison . InvariantCultureIgnoreCase ) )
113
135
{
114
136
return await DownloadAndInstallRust ( ) ;
@@ -120,6 +142,7 @@ private async Task<bool> ExecuteAsync()
120
142
if ( string . IsNullOrEmpty ( _rustUpFile ) || ! File . Exists ( _rustUpFile ) )
121
143
{
122
144
Log . LogMessage ( $ "MSRUSTUP_FILE environment variable is not set or the file does not exist. Assuming local build.") ;
145
+ _envVars . Add ( "ADO_CREDENTIAL_PROVIDER" , MsRustupAuthType ) ;
123
146
}
124
147
else
125
148
{
@@ -199,7 +222,6 @@ private async Task<ExitCode> CargoRunCommandAsync(string command, string args)
199
222
return await ExecuteProcessAsync ( customCargoBin ! , $ "{ command } { args } --offline { ( isDebugConfiguration ? string . Empty : "--" + Configuration . ToLowerInvariant ( ) ) } --config { Path . Combine ( RepoRoot , _cargoConfigFilePath ) } ", "." , _envVars ) ;
200
223
}
201
224
202
- // if we don't have the toolchain, we need to install it.
203
225
return ExitCode . Failed ;
204
226
}
205
227
@@ -219,7 +241,7 @@ private async Task<bool> DownloadAndInstallRust()
219
241
}
220
242
}
221
243
222
- return downloadSuccess && installSuccess ;
244
+ return true ;
223
245
}
224
246
225
247
private async Task < bool > FetchCratesAsync ( string project )
@@ -463,6 +485,7 @@ private async Task<bool> InstallRust()
463
485
bool msRustupToolChainExists = useMsRustUp && ! string . IsNullOrEmpty ( GetCustomToolChainCargoPath ( ) ) ;
464
486
bool cargoPathAndRustPathsExists = Directory . Exists ( _cargoHome ) && Directory . Exists ( _rustUpHome ) ;
465
487
bool cargoBinaryExists = File . Exists ( _cargoPath ) ;
488
+
466
489
if ( ( msRustupToolChainExists && cargoPathAndRustPathsExists && useMsRustUp ) || cargoPathAndRustPathsExists && cargoBinaryExists && ! useMsRustUp )
467
490
{
468
491
return true ;
@@ -517,6 +540,7 @@ private async Task<bool> InstallRust()
517
540
if ( string . IsNullOrEmpty ( _rustUpFile ) || ! File . Exists ( _rustUpFile ) )
518
541
{
519
542
Log . LogMessage ( $ "MSRUSTUP_FILE environment variable is not set or the file does not exist. Assuming local build.") ;
543
+ _envVars . Add ( "ADO_CREDENTIAL_PROVIDER" , MsRustupAuthType ) ;
520
544
}
521
545
else
522
546
{
0 commit comments