Skip to content

Commit ccfc558

Browse files
Add support for "cargo install" for crates and support for user specified cargo commands (#629)
* update msrustup install script and make cargotask more robust with respect to msrustup usage * update sdk task, msrustup install script --------- Co-authored-by: Andy Gerlicher <angerlic@microsoft.com>
1 parent 352f86c commit ccfc558

File tree

6 files changed

+57
-47
lines changed

6 files changed

+57
-47
lines changed

src/Cargo.UnitTests/CargoTest.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public void CompileIsExtensibleWithBeforeAfterTargets(string targetName)
4141
{
4242
ProjectCreator cargoProject = ProjectCreator.Templates.CargoProject(
4343
path: Path.Combine(TestRootPath, "Cargo", "rust.cargoproj"))
44-
.Target("CargoInstall")
45-
.Target("CargoFetch")
46-
.Target("CargoBuild")
47-
.Target(targetName)
48-
.TaskMessage("503CF1EBA6DC415F95F4DB630E7C1817", MessageImportance.High)
49-
.Save();
44+
.Target("InstallCargo")
45+
.Target("CargoFetch")
46+
.Target("CargoBuild")
47+
.Target(targetName)
48+
.TaskMessage("503CF1EBA6DC415F95F4DB630E7C1817", MessageImportance.High)
49+
.Save();
5050

5151
cargoProject.TryBuild(restore: true, out bool result, out BuildOutput buildOutput);
5252

@@ -60,13 +60,13 @@ public void CoreCompileIsExtensibleWithCoreCompileDependsOn()
6060
{
6161
ProjectCreator cargoProject = ProjectCreator.Templates.CargoProject(
6262
path: Path.Combine(TestRootPath, "Cargo", "rust.cargoproj"))
63-
.Property("CoreCompileDependsOn", "$(CoreCompileDependsOn);TestThatCoreCompileIsExtensible")
64-
.Target("CargoInstall")
65-
.Target("CargoFetch")
66-
.Target("CargoBuild")
67-
.Target("TestThatCoreCompileIsExtensible")
68-
.TaskMessage("35F1C217730445E0AC0F30E70F5C7826", MessageImportance.High)
69-
.Save();
63+
.Property("CoreCompileDependsOn", "$(CoreCompileDependsOn);TestThatCoreCompileIsExtensible")
64+
.Target("InstallCargo")
65+
.Target("CargoFetch")
66+
.Target("CargoBuild")
67+
.Target("TestThatCoreCompileIsExtensible")
68+
.TaskMessage("35F1C217730445E0AC0F30E70F5C7826", MessageImportance.High)
69+
.Save();
7070

7171
cargoProject.TryBuild(restore: true, out bool result, out BuildOutput buildOutput);
7272

@@ -80,14 +80,14 @@ public void CoreCompileIsExtensibleWithTargetsTriggeredByCompilation()
8080
{
8181
ProjectCreator cargoProject = ProjectCreator.Templates.CargoProject(
8282
path: Path.Combine(TestRootPath, "Cargo", "rust.cargoproj"))
83-
.Property("TargetsTriggeredByCompilation", "TestThatCoreCompileIsExtensible")
84-
.Property("TargetsTriggeredByCompilation", "TestThatCoreCompileIsExtensible")
85-
.Target("CargoInstall")
86-
.Target("CargoFetch")
87-
.Target("CargoBuild")
88-
.Target("TestThatCoreCompileIsExtensible")
83+
.Property("TargetsTriggeredByCompilation", "TestThatCoreCompileIsExtensible")
84+
.Property("TargetsTriggeredByCompilation", "TestThatCoreCompileIsExtensible")
85+
.Target("InstallCargo")
86+
.Target("CargoFetch")
87+
.Target("CargoBuild")
88+
.Target("TestThatCoreCompileIsExtensible")
8989
.TaskMessage("D031211C98F1454CA47A424ADC86A8F7", MessageImportance.High)
90-
.Save();
90+
.Save();
9191

9292
cargoProject.TryBuild(restore: true, out bool result, out BuildOutput buildOutput);
9393

@@ -110,11 +110,11 @@ public void DoNotReferenceOutputAssemblies()
110110

111111
ProjectCreator cargoProject = ProjectCreator.Templates.CargoProject(
112112
path: Path.Combine(TestRootPath, "Cargo", "rust.cargoproj"))
113-
.Target("CargoInstall")
114-
.Target("CargoFetch")
115-
.Target("CargoBuild")
116-
.ItemProjectReference(projectA)
117-
.Save();
113+
.Target("InstallCargo")
114+
.Target("CargoFetch")
115+
.Target("CargoBuild")
116+
.ItemProjectReference(projectA)
117+
.Save();
118118

119119
cargoProject.TryRestore(out bool result, out BuildOutput buildOutput);
120120

@@ -220,7 +220,6 @@ public void PropertiesHaveExpectedValues(string propertyName, string value, stri
220220
ProjectCreator.Templates.CargoProject(
221221
path: GetTempFileWithExtension(".cargoproj"))
222222
.Property(propertyName, value)
223-
224223
.Target("CargoFetch")
225224
.Target("CargoBuild")
226225
.Save()
@@ -243,8 +242,7 @@ public void PublishWithNoBuild(string projectExtension)
243242
.Target("TakeAction", afterTargets: "Build")
244243
.TaskMessage("2EA26E6FC5C842B682AA26096A769E07", MessageImportance.High);
245244
})
246-
247-
.Target("CargoInstall")
245+
.Target("InstallCargo")
248246
.Target("CargoFetch")
249247
.Target("CargoBuild")
250248
.Save()
@@ -277,7 +275,6 @@ public void SimpleBuild(string projectExtension)
277275
.TaskMessage("86F00AF59170450E9D687652D74A6394", MessageImportance.High);
278276
})
279277
.Property("GenerateDependencyFile", "false")
280-
281278
.Target("CargoFetch")
282279
.Target("CargoBuild")
283280
.Save()
@@ -315,7 +312,6 @@ public void SupportedTargetsExecute(string extension, string target)
315312
path: GetTempFileWithExtension(extension),
316313
projectCollection: projectCollection)
317314
.Property("GenerateDependencyFile", "false")
318-
319315
.Target("CargoFetch")
320316
.Target("CargoBuild")
321317
.Save()

src/Cargo/CargoTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CargoTask : Task
2727
private static readonly string _cargoConfigFilePath = Path.Combine(".cargo", "config.toml");
2828
private static readonly string _cargoFileName = "cargo.toml";
2929
private static readonly string _clearCacheCommand = "clearcargocache";
30-
private static readonly string _installCommand = "install";
30+
private static readonly string _installCommand = "installcargo";
3131
private static readonly string _fetchCommand = "fetch";
3232
private static readonly string _loginCommand = "login";
3333
private static readonly string _rustUpDownloadLink = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe";

src/Cargo/Microsoft.Build.Cargo.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
<None Include="sdk\Sdk.targets">
3535
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3636
</None>
37-
<None Include="sdk\CargoInstall.proj">
38-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39-
</None>
4037
<None Include="sdk\DisableCopyFilesMarkedCopyLocal.targets">
4138
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4239
</None>
4340
</ItemGroup>
41+
<ItemGroup>
42+
<None Include="sdk\InstallCargo.proj">
43+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
44+
</None>
45+
</ItemGroup>
4446
<ItemGroup>
4547
<FilesToSign Include="$(TargetPath)" Authenticode="Microsoft400" StrongName="StrongName" />
4648
</ItemGroup>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Import Project="$(MSBuildThisFileDirectory)Sdk.props"/>
33
<UsingTask TaskName="Microsoft.Build.Cargo.CargoTask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\net8.0\Microsoft.Build.Cargo.dll" Condition="'$(MSBuildRuntimeType)' == 'Core'" />
44
<UsingTask TaskName="Microsoft.Build.Cargo.CargoTask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\net472\Microsoft.Build.Cargo.dll" Condition="'$(MSBuildRuntimeType)' != 'Core'" />
5-
<Target Name="CargoInstall">
6-
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(MSBuildStartupDirectory)" Command="install" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
5+
<Target Name="InstallCargo">
6+
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(MSBuildStartupDirectory)" Command="installcargo" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
77
</Target>
88
</Project>

src/Cargo/sdk/Sdk.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@
8080
<BuildCommandArgs Condition="'$(BuildCommandArgs)' == ''"></BuildCommandArgs>
8181
<TestCommandArgs Condition="'$(TestCommandArgs)' == ''"></TestCommandArgs>
8282
<CleanCommandArgs Condition="'$(CleanCommandArgs)' == ''"></CleanCommandArgs>
83+
<ClearCargoCacheCommandArgs Condition="'$(ClearCargoCacheCommandArgs)' == ''"></ClearCargoCacheCommandArgs>
84+
<CargoCommandArgs Condition="'$(CargoCommandArgs)' == ''"></CargoCommandArgs>
8385
<RunCommandArgs Condition="'$(RunCommandArgs)' == ''"></RunCommandArgs>
86+
<CargoInstallCommandArgs Condition="'$(CargoInstallCommandArgs)' == ''"></CargoInstallCommandArgs>
8487
<DocCommandArgs Condition="'$(DocCommandArgs)' == ''"></DocCommandArgs>
8588
<StartupProj Condition="'$(StartupProj)' == ''">$(MSBuildProjectFullPath)</StartupProj>
8689
<RepoRoot Condition="'$(RepoRoot)' == ''">$(EnlistmentRoot)</RepoRoot>

src/Cargo/sdk/Sdk.targets

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,41 @@
108108
<!-- Rust specific targets -->
109109
<UsingTask TaskName="Microsoft.Build.Cargo.CargoTask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\net8.0\Microsoft.Build.Cargo.dll" Condition="'$(MSBuildRuntimeType)' == 'Core'" />
110110
<UsingTask TaskName="Microsoft.Build.Cargo.CargoTask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\net472\Microsoft.Build.Cargo.dll" Condition="'$(MSBuildRuntimeType)' != 'Core'" />
111-
<Target Name="CargoInstall" AfterTargets="_CollectRestoreInputs">
111+
<Target Name="InstallCargo" AfterTargets="_CollectRestoreInputs">
112112
<MSBuild
113-
Projects="$(MSBuildThisFileDirectory)CargoInstall.proj"
114-
Targets="CargoInstall"
113+
Projects="$(MSBuildThisFileDirectory)InstallCargo.proj"
114+
Targets="InstallCargo"
115115
Properties="EnableTelemetryLoggerCopy=$(EnableTelemetryLoggerCopy);TelemetryLoggerLocation=$(TelemetryLoggerLocation);TelemetryLoggerSourcePath=$(TelemetryLoggerSourcePath);TelemetryLoggerInstallId=$(TelemetryLoggerInstallId);RepoRoot=$(RepoRoot);CargoInstallationRoot=$(CargoInstallationRoot);MsRustupAuthType=$(MsRustupAuthType);CargoOutputDir=$(CargoOutputDir)"
116116
RemoveProperties="NuGetInteractive;MSBuildRestoreSessionId;TargetFramework;RuntimeIdentifier" />
117117
</Target>
118-
<Target Name="CargoFetch" AfterTargets="CargoInstall">
118+
<Target Name="CargoFetch" AfterTargets="InstallCargo">
119119
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(StartupProj)" Command="fetch" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
120120
</Target>
121121
<Target Name="CargoBuild" AfterTargets="CoreCompile">
122122
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(StartupProj)" Command="build" CommandArgs="$(BuildCommandArgs)" Configuration="$(Configuration)" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
123123
</Target>
124-
<Target Name="Test" DependsOnTargets="Cargo">
124+
<Target Name="CargoTest" DependsOnTargets="CargoFetch">
125125
<CargoTask StartupProj="$(StartupProj)" Command="test" CommandArgs="$(TestCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
126126
</Target>
127-
<Target Name="Clean">
128-
<CargoTask StartupProj="$(StartupProj)" Command="clean" CommandArgs="$(CleanCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
129-
</Target>
130-
<Target Name="Run" DependsOnTargets="Cargo">
127+
<Target Name="CargoRun" DependsOnTargets="CargoFetch">
131128
<CargoTask StartupProj="$(StartupProj)" Command="run" CommandArgs="$(RunCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
132129
</Target>
133-
<Target Name="Doc" DependsOnTargets="Cargo">
130+
<Target Name="CargoDoc" DependsOnTargets="CargoFetch">
134131
<CargoTask StartupProj="$(StartupProj)" Command="doc" CommandArgs="$(DocCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
135132
</Target>
133+
<Target Name="CargoUpdate" DependsOnTargets="CargoFetch">
134+
<CargoTask StartupProj="$(StartupProj)" Command="update" CommandArgs="$(UpdateCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
135+
</Target>
136+
<Target Name="CargoInstall" DependsOnTargets="CargoFetch">
137+
<CargoTask StartupProj="$(StartupProj)" Command="install" CommandArgs="$(CargoInstallCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
138+
</Target>
139+
<Target Name="CargoCommand" DependsOnTargets="CargoFetch">
140+
<CargoTask StartupProj="$(StartupProj)" Command="$(CargoCommand)" CommandArgs="$(CargoCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
141+
</Target>
142+
<Target Name="CargoClean">
143+
<CargoTask StartupProj="$(StartupProj)" Command="clean" CommandArgs="$(CleanCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
144+
</Target>
136145
<Target Name="ClearCargoCache">
137-
<CargoTask StartupProj="$(StartupProj)" Command="clearcargocache" CommandArgs="$(DocCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
146+
<CargoTask StartupProj="$(StartupProj)" Command="clearcargocache" CommandArgs="$(ClearCargoCacheCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" CargoOutputDir="$(CargoOutputDir)" />
138147
</Target>
139148
</Project>

0 commit comments

Comments
 (0)