Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Fix OptionalTooling so it works on .NET Core (#2084)
Browse files Browse the repository at this point in the history
* Fix OptionalTooling so it works on .NET Core

Need to add support for MSBuild projects instead of project.json files, so builds running on .NET Core can restore optional tooling.

Partial change for https://github.com/dotnet/corefx/issues/30758

* Set NuGet.config's globalPackagesFolder so the optional tooling gets put in the correct packages folder.

* PR feedback.
  • Loading branch information
eerhardt authored Jul 2, 2018
1 parent 0b0e935 commit c290105
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class GenerateEncryptedNuGetConfig : BuildTask
[Required]
public ITaskItem[] Sources { get; set; }

public string PackagesDir { get; set; }

public override bool Execute()
{
Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath));
Expand All @@ -33,6 +35,11 @@ public override bool Execute()
Path.GetDirectoryName(ConfigPath),
Path.GetFileName(ConfigPath));

if (!string.IsNullOrEmpty(PackagesDir))
{
settings.SetValue("config", "globalPackagesFolder", Path.GetFullPath(PackagesDir));
}

var sourceProvider = new PackageSourceProvider(settings);

// Add source with username and encrypted password.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<Target Name="RestoreOptionalToolingPackages"
DependsOnTargets="RestoreOptionalToolingEncrypted;
RestoreOptionalToolingUnencrypted;
CleanOptionalToolingRestore"
BeforeTargets="Sync"
Condition="'$(OptionalToolSource)'!=''" />
Expand All @@ -28,73 +27,63 @@
</Target>

<!--
Determine the paths of the optional tool runtime project.json and lockfile.
Determine the paths of the optional tool runtime project.json/MSBuild project and lockfile.
Note: Either project.json ($(OptionalToolingJsonPath)) or MSBuild project ($(OptionalToolingProjectPath))
should be set at one time.
-->
<Target Name="GetOptionalToolingPaths">
<PropertyGroup>
<OptionalToolingDir>$(ToolsDir)optional-tool-runtime\</OptionalToolingDir>
</PropertyGroup>
<PropertyGroup Condition="'$(OptionalToolingProjectPath)' == ''">
<OptionalToolingJsonPath Condition="'$(OptionalToolingJsonPath)' == ''">$(OptionalToolingDir)optional.json</OptionalToolingJsonPath>
<OptionalToolingProjectJsonPath>$(OptionalToolingDir)project.json</OptionalToolingProjectJsonPath>
<OptionalToolingProjectLockJsonPath>$(OptionalToolingDir)project.lock.json</OptionalToolingProjectLockJsonPath>
</PropertyGroup>
<PropertyGroup Condition="'$(OptionalToolingProjectPath)' != ''">
<OptionalToolingProjectAssetsJsonPath>$(OptionalToolingDir)project.assets.json</OptionalToolingProjectAssetsJsonPath>
</PropertyGroup>
</Target>

<!--
Copy the mangled "optional.json" file to "project.json", so that we can restore it. The name
is optional.json so that recursive restore will not normally find the file: it contains
optional tooling that typical recursive restores do not provide, such as the buildtools build.
-->
<Target Name="PrepareOptionalToolProjectJson"
<Target Name="PrepareOptionalToolProject"
DependsOnTargets="GetOptionalToolingPaths">

<Copy SourceFiles="$(OptionalToolingJsonPath)"
<!--
Copy the mangled "optional.json" file to "project.json", so that we can restore it. The name
is optional.json so that recursive restore will not normally find the file: it contains
optional tooling that typical recursive restores do not provide, such as the buildtools build.
-->
<Copy Condition="'$(OptionalToolingProjectJsonPath)' != ''"
SourceFiles="$(OptionalToolingJsonPath)"
DestinationFiles="$(OptionalToolingProjectJsonPath)"
SkipUnchangedFiles="true" />
</Target>

<!--
Restore optional tooling using a NuGet.Config on disk with encrypted credentials inside.
Not supported on .NET Core.
-->
<Target Name="RestoreOptionalToolingEncrypted"
DependsOnTargets="CreateOptionalRestoreFeedItems;
PrepareOptionalToolProjectJson"
Condition="'$(MSBuildRuntimeType)'!='core'">
PrepareOptionalToolProject">
<PropertyGroup>
<GeneratedNuGetConfigDir>$(ObjDir)generatedNuGetConfig/</GeneratedNuGetConfigDir>
<OptionalRestoreConfigPath>$(GeneratedNuGetConfigDir)optional.NuGet.Config</OptionalRestoreConfigPath>
</PropertyGroup>

<GenerateEncryptedNuGetConfig ConfigPath="$(OptionalRestoreConfigPath)"
Sources="@(OptionalRestoreSource)" />
Sources="@(OptionalRestoreSource)"
PackagesDir="$(PackagesDir)" />

<EncryptedConfigNuGetRestore Inputs="$(OptionalToolingProjectJsonPath)"
<!-- restore the project.json file, if it is being used -->
<EncryptedConfigNuGetRestore Condition="'$(OptionalToolingProjectJsonPath)' != ''"
Inputs="$(OptionalToolingProjectJsonPath)"
ConfigFile="$(OptionalRestoreConfigPath)"
PackagesDir="$(PackagesDir)" />
</Target>

<!--
Restore optional tooling by generating the XML string of a NuGet.Config with plaintext
credentials, then injecting it into "dotnet restore" using /dev/stdin.
Not supported on Windows.
-->
<Target Name="RestoreOptionalToolingUnencrypted"
DependsOnTargets="CreateOptionalRestoreFeedItems;
PrepareOptionalToolProjectJson"
Condition="'$(MSBuildRuntimeType)'=='core'">
<PropertyGroup>
<!-- Create a new restore command with no source parameters, only configfile. -->
<OptionalRestoreCommand>"$(DnuToolPath)"</OptionalRestoreCommand>
<OptionalRestoreCommand>$(OptionalRestoreCommand) restore</OptionalRestoreCommand>
<OptionalRestoreCommand Condition="'$(UseNuGetHttpCache)'!='true'">$(OptionalRestoreCommand) --no-cache</OptionalRestoreCommand>
<OptionalRestoreCommand>$(OptionalRestoreCommand) --packages "$(PackagesDir.TrimEnd('/\'.ToCharArray()))"</OptionalRestoreCommand>
<OptionalRestoreCommand>$(OptionalRestoreCommand) --configfile $(OptionalRestoreConfigPath)</OptionalRestoreCommand>
</PropertyGroup>

<Error Text="Restoring optional tooling with in-memory credentials is not supported on Windows. Use the desktop framework for the encrypted credential flow."
Condition="'$(RunningOnUnix)'!='true'" />

<Error Text="Restoring optional tooling with in-memory credentials is not yet implemented. No cross-platform optional tooling exists yet." />

<!-- restore the MSBuild project file, if it is being used -->
<Exec Condition="'$(OptionalToolingProjectPath)' != ''"
Command="$(DotnetToolCommand) restore $(OptionalToolingProjectPath) --configfile $(OptionalRestoreConfigPath) /p:RestoreOutputPath=$(OptionalToolingDir)" />

</Target>

<!--
Expand All @@ -118,14 +107,19 @@
<Target Name="ResolveOptionalTools"
DependsOnTargets="GetOptionalToolingPaths">

<PropertyGroup>
<OptionalToolingProjectLockFilePath>$(OptionalToolingProjectLockJsonPath)</OptionalToolingProjectLockFilePath>
<OptionalToolingProjectLockFilePath Condition="'$(OptionalToolingProjectLockFilePath)' == ''">$(OptionalToolingProjectAssetsJsonPath)</OptionalToolingProjectLockFilePath>
</PropertyGroup>

<Error Text="Optional tooling has not been restored. Use the 'RestoreOptionalToolingPackages' target first."
Condition="!Exists('$(OptionalToolingProjectLockJsonPath)')" />
Condition="!Exists('$(OptionalToolingProjectLockFilePath)')" />

<ItemGroup>
<OptionalToolingTargetMoniker Include=".NETFramework,Version=v4.5" />
</ItemGroup>

<PrereleaseResolveNuGetPackageAssets ProjectLockFile="$(OptionalToolingProjectLockJsonPath)"
<PrereleaseResolveNuGetPackageAssets ProjectLockFile="$(OptionalToolingProjectLockFilePath)"
TargetMonikers="@(OptionalToolingTargetMoniker)">
<Output TaskParameter="ResolvedReferences" ItemName="ResolvedOptionalToolReferences" />
</PrereleaseResolveNuGetPackageAssets>
Expand Down

0 comments on commit c290105

Please sign in to comment.