Use unzip task instead of custom implementation (#18086)

This commit is contained in:
Viktor Hofer 2024-01-03 15:39:28 +01:00 committed by GitHub
parent 54c1bddf90
commit 166b96405b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 107 deletions

View file

@ -6,7 +6,6 @@
<Import Project="$(GitInfoAllRepoPropsFile)" />
<UsingTask AssemblyFile="$(LeakDetectionTasksAssembly)" TaskName="MarkAndCatalogPackages" />
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="ZipFileExtractToDirectory" />
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="ReplaceTextInFile" />
<ItemGroup>
@ -113,12 +112,12 @@
DependsOnTargets="UnpackTarballs;BuildXPlatTasks"
Inputs="$(MSBuildProjectFullPath)"
Outputs="$(CompletedSemaphorePath)ExtractToolPackage.complete">
<ZipFileExtractToDirectory SourceArchive="$(PrebuiltSourceBuiltPackagesPath)Microsoft.DotNet.Arcade.Sdk.$(ARCADE_BOOTSTRAP_VERSION).nupkg"
DestinationDirectory="$(ArcadeBootstrapPackageDir)microsoft.dotnet.arcade.sdk/$(ARCADE_BOOTSTRAP_VERSION)/"
OverwriteDestination="true" />
<Unzip SourceFiles="$(PrebuiltSourceBuiltPackagesPath)Microsoft.DotNet.Arcade.Sdk.$(ARCADE_BOOTSTRAP_VERSION).nupkg"
DestinationFolder="$(ArcadeBootstrapPackageDir)microsoft.dotnet.arcade.sdk/$(ARCADE_BOOTSTRAP_VERSION)"
SkipUnchangedFiles="true" />
<!-- TODO: When unpacking using ZipFileExtractToDirectory, this executable file has the wrong
permissions. See https://github.com/dotnet/source-build/issues/2259 -->
<!-- When unpacking, this executable file has the wrong permissions on
non-windows systems: https://github.com/NuGet/Home/issues/13121. -->
<Exec Command="chmod 755 git-clone-to-dir.sh"
WorkingDirectory="$(ArcadeBootstrapPackageDir)microsoft.dotnet.arcade.sdk/$(ARCADE_BOOTSTRAP_VERSION)/tools/SourceBuild/" />

View file

@ -1,86 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Build.Framework;
using System;
using System.IO;
using System.IO.Compression;
namespace Microsoft.DotNet.Build.Tasks
{
public sealed class ZipFileExtractToDirectory : BuildTask
{
/// <summary>
/// The path to the archive to be extracted.
/// </summary>
[Required]
public string SourceArchive { get; set; }
/// <summary>
/// The path of the directory to extract into.
/// </summary>
[Required]
public string DestinationDirectory { get; set; }
/// <summary>
/// Indicates if the destination directory should be overwritten if it already exists.
/// </summary>
public bool OverwriteDestination { get; set; }
/// <summary>
/// File entries to include in the extraction. Entries are relative
/// paths inside the archive. If null or empty, all files are extracted.
/// </summary>
public ITaskItem[] Include { get; set; }
public override bool Execute()
{
try
{
if (Directory.Exists(DestinationDirectory))
{
if (OverwriteDestination)
{
Log.LogMessage(MessageImportance.Low, $"'{DestinationDirectory}' already exists, trying to delete before unzipping...");
Directory.Delete(DestinationDirectory, recursive: true);
}
else
{
Log.LogWarning($"'{DestinationDirectory}' already exists. Did you forget to set '{nameof(OverwriteDestination)}' to true?");
}
}
Log.LogMessage(MessageImportance.High, "Decompressing '{0}' into '{1}'...", SourceArchive, DestinationDirectory);
Directory.CreateDirectory(Path.GetDirectoryName(DestinationDirectory));
using (ZipArchive archive = ZipFile.OpenRead(SourceArchive))
{
if (Include?.Length > 0)
{
foreach (ITaskItem entryItem in Include)
{
ZipArchiveEntry entry = archive.GetEntry(entryItem.ItemSpec);
string destinationPath = Path.Combine(DestinationDirectory, entryItem.ItemSpec);
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
entry.ExtractToFile(destinationPath, overwrite: false);
}
}
else
{
archive.ExtractToDirectory(DestinationDirectory);
}
}
}
catch (Exception e)
{
// We have 2 log calls because we want a nice error message but we also want to capture the callstack in the log.
Log.LogError("An exception has occurred while trying to decompress '{0}' into '{1}'.", SourceArchive, DestinationDirectory);
Log.LogErrorFromException(e, /*show stack=*/ true, /*show detail=*/ true, DestinationDirectory);
return false;
}
return true;
}
}
}

View file

@ -14,7 +14,6 @@
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="WritePackageVersionsProps" />
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="WritePackageUsageData" />
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="WriteUsageReports" />
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="ZipFileExtractToDirectory" />
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="ReplaceTextInFile" />
<Target Name="BuildRepoReferences" Condition="'@(RepositoryReference)' != '' and '$(SkipRepoReferences)' != 'true'">
@ -272,10 +271,10 @@
<_NupkgDestinationPath Condition="$([System.String]::Copy(%(_BuiltIntermediatePackages.Identity)).Contains('source-build-reference-packages'))">$(ReferencePackagesDir)</_NupkgDestinationPath>
</PropertyGroup>
<ZipFileExtractToDirectory Condition="'@(_BuiltIntermediatePackages)' != ''"
SourceArchive="%(_BuiltIntermediatePackages.Identity)"
DestinationDirectory="$(SourceBuiltPackagesPath)extractArtifacts/%(_BuiltIntermediatePackages.FileName)/"
OverwriteDestination="true" />
<Unzip SourceFiles="@(_BuiltIntermediatePackages)"
DestinationFolder="$(SourceBuiltPackagesPath)extractArtifacts/%(_BuiltIntermediatePackages.FileName)/"
SkipUnchangedFiles="true"
Condition="'@(_BuiltIntermediatePackages)' != ''" />
<ItemGroup Condition="'@(_BuiltIntermediatePackages)' != ''">
<SourceBuiltNupkgFiles Include="$(SourceBuiltPackagesPath)extractArtifacts/**/artifacts/*.nupkg" />
@ -439,17 +438,18 @@
Id="%(BuiltSdkPackageOverride.Identity)" />
</ItemGroup>
<ZipFileExtractToDirectory SourceArchive="%(_ToolPackage.Identity)"
DestinationDirectory="$(SourceBuiltSdksDir)%(_ToolPackage.Id)\"
OverwriteDestination="true" />
<Unzip SourceFiles="%(_ToolPackage.Identity)"
DestinationFolder="$(SourceBuiltSdksDir)%(_ToolPackage.Id)\"
SkipUnchangedFiles="true" />
<ItemGroup>
<ExtractedToolFiles Include="$(SourceBuiltSdksDir)%(_ToolPackage.Id)/**/*netcore*/*.dll" />
</ItemGroup>
<Copy SourceFiles="@(ExtractedToolFiles)" DestinationFolder="$(SourceBuiltSdksDir)/" />
<!-- TODO: When unpacking using ZipFileExtractToDirectory, this executable file has the wrong
permissions. See https://github.com/dotnet/source-build/issues/2259 -->
<!-- When unpacking, this executable file has the wrong permissions on
non-windows systems: https://github.com/NuGet/Home/issues/13121. -->
<Exec Command="chmod 755 git-clone-to-dir.sh"
Condition=" '%(_ToolPackage.Id)' == 'Microsoft.DotNet.Arcade.Sdk' "
WorkingDirectory="$(SourceBuiltSdksDir)%(_ToolPackage.Id)/tools/SourceBuild/" />

View file

@ -35,10 +35,10 @@
</ItemGroup>
<MakeDir Directories="$(SourceBuildReferencePackagesDestination)" />
<ZipFileExtractToDirectory Condition="'@(SourceBuildReferencePackagesIntermediatePackage)' != ''"
SourceArchive="%(SourceBuildReferencePackagesIntermediatePackage.Identity)"
DestinationDirectory="$(SourceBuildReferencePackagesDestination)extractArtifacts/"
OverwriteDestination="true" />
<Unzip SourceFiles="@(SourceBuildReferencePackagesIntermediatePackage)"
DestinationFolder="$(SourceBuildReferencePackagesDestination)extractArtifacts"
SkipUnchangedFiles="true"
Condition="'@(SourceBuildReferencePackagesIntermediatePackage)' != ''" />
<ItemGroup>
<SourceBuildReferencePackagesNupkgFiles Include="$(SourceBuildReferencePackagesDestination)extractArtifacts/**/*.nupkg" />

View file

@ -40,6 +40,5 @@
<UsingTask TaskName="TarGzFileCreateFromDirectory" AssemblyFile="$(CoreSdkTaskDll)" />
<UsingTask TaskName="UpdateRuntimeConfig" AssemblyFile="$(CoreSdkTaskDll)" />
<UsingTask TaskName="ZipFileCreateFromDirectory" AssemblyFile="$(CoreSdkTaskDll)" />
<UsingTask TaskName="ZipFileExtractToDirectory" AssemblyFile="$(CoreSdkTaskDll)" />
</Project>