From 1344cb26c21c0da664edec7f5d88bcfc1621bf7f Mon Sep 17 00:00:00 2001 From: Daniel Plaisted Date: Tue, 23 Oct 2018 23:15:58 -0700 Subject: [PATCH] Lay out components in publish directory --- .../ZipFileExtractToDirectory.cs | 85 ------------------- .../ExtractArchiveToDirectory.cs | 41 +++++---- .../TarGzFileCreateFromDirectory.cs | 0 .../ZipFileCreateFromDirectory.cs | 0 src/redist/targets/BuildCoreSdkTasks.targets | 1 + src/redist/targets/BundledComponents.targets | 3 + 6 files changed, 30 insertions(+), 100 deletions(-) delete mode 100644 old/build_projects/dotnet-cli-build/ZipFileExtractToDirectory.cs rename old/build_projects/dotnet-cli-build/TarGzFileExtractToDirectory.cs => src/core-sdk-tasks/ExtractArchiveToDirectory.cs (71%) rename {old/build_projects/dotnet-cli-build => src/core-sdk-tasks}/TarGzFileCreateFromDirectory.cs (100%) rename {old/build_projects/dotnet-cli-build => src/core-sdk-tasks}/ZipFileCreateFromDirectory.cs (100%) diff --git a/old/build_projects/dotnet-cli-build/ZipFileExtractToDirectory.cs b/old/build_projects/dotnet-cli-build/ZipFileExtractToDirectory.cs deleted file mode 100644 index 491abcf81..000000000 --- a/old/build_projects/dotnet-cli-build/ZipFileExtractToDirectory.cs +++ /dev/null @@ -1,85 +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 Microsoft.Build.Utilities; -using System; -using System.IO; -using System.IO.Compression; - -namespace Microsoft.DotNet.Build.Tasks -{ - public sealed class ZipFileExtractToDirectory : Task - { - /// - /// The path to the directory to be archived. - /// - [Required] - public string SourceArchive { get; set; } - - /// - /// The path of the archive to be created. - /// - [Required] - public string DestinationDirectory { get; set; } - - /// - /// Indicates if the destination directory should be cleaned if it already exists. - /// - public bool OverwriteDestination { get; set; } - - public override bool Execute() - { - try - { - if (Directory.Exists(DestinationDirectory)) - { - if (OverwriteDestination == true) - { - Log.LogMessage(MessageImportance.Low, "'{0}' already exists, trying to delete before unzipping...", DestinationDirectory); - Directory.Delete(DestinationDirectory, recursive: true); - } - } - - Log.LogMessage(MessageImportance.High, "Decompressing '{0}' into '{1}'...", SourceArchive, DestinationDirectory); - if (!Directory.Exists(Path.GetDirectoryName(DestinationDirectory))) - Directory.CreateDirectory(Path.GetDirectoryName(DestinationDirectory)); - - // match tar default behavior to overwrite by default - // Replace this code with ZipFile.ExtractToDirectory when https://github.com/dotnet/corefx/pull/14806 is available - using (ZipArchive archive = ZipFile.Open(SourceArchive, ZipArchiveMode.Read)) - { - DirectoryInfo di = Directory.CreateDirectory(DestinationDirectory); - string destinationDirectoryFullPath = di.FullName; - - foreach (ZipArchiveEntry entry in archive.Entries) - { - string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, entry.FullName)); - - if (Path.GetFileName(fileDestinationPath).Length == 0) - { - // If it is a directory: - Directory.CreateDirectory(fileDestinationPath); - } - else - { - // If it is a file: - // Create containing directory: - Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath)); - entry.ExtractToFile(fileDestinationPath, overwrite: true); - } - } - } - } - 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 occured while trying to decompress '{0}' into '{1}'.", SourceArchive, DestinationDirectory); - Log.LogMessage(MessageImportance.Low, e.ToString()); - return false; - } - return true; - } - } -} diff --git a/old/build_projects/dotnet-cli-build/TarGzFileExtractToDirectory.cs b/src/core-sdk-tasks/ExtractArchiveToDirectory.cs similarity index 71% rename from old/build_projects/dotnet-cli-build/TarGzFileExtractToDirectory.cs rename to src/core-sdk-tasks/ExtractArchiveToDirectory.cs index f5d70b9a7..e6f5d141e 100644 --- a/old/build_projects/dotnet-cli-build/TarGzFileExtractToDirectory.cs +++ b/src/core-sdk-tasks/ExtractArchiveToDirectory.cs @@ -10,7 +10,10 @@ using System.IO.Compression; namespace Microsoft.DotNet.Build.Tasks { - public sealed class TarGzFileExtractToDirectory : ToolTask + /// + /// Extracts a .zip or .tar.gz file to a directory. + /// + public sealed class ExtractArchiveToDirectory : ToolTask { /// /// The path to the archive to extract. @@ -27,7 +30,7 @@ namespace Microsoft.DotNet.Build.Tasks /// /// Indicates if the destination directory should be cleaned if it already exists. /// - public bool OverwriteDestination { get; set; } + public bool CleanDestination { get; set; } protected override bool ValidateParameters() { @@ -37,7 +40,7 @@ namespace Microsoft.DotNet.Build.Tasks if (Directory.Exists(DestinationDirectory)) { - if (OverwriteDestination == true) + if (CleanDestination == true) { Log.LogMessage(MessageImportance.Low, "'{0}' already exists, trying to delete before unzipping...", DestinationDirectory); Directory.Delete(DestinationDirectory, recursive: true); @@ -62,7 +65,25 @@ namespace Microsoft.DotNet.Build.Tasks public override bool Execute() { - bool retVal = base.Execute(); + bool retVal = true; + + // Inherits from ToolTask in order to shell out to tar. + // If the file is a .zip, then don't call the base Execute method, just run as a normal task + if (Path.GetExtension(SourceArchive).Equals(".zip", StringComparison.OrdinalIgnoreCase)) + { + if (ValidateParameters()) + { + ZipFile.ExtractToDirectory(SourceArchive, DestinationDirectory, overwriteFiles: true); + } + else + { + retVal = false; + } + } + else + { + retVal = base.Execute(); + } if (!retVal) { @@ -90,17 +111,7 @@ namespace Microsoft.DotNet.Build.Tasks protected override string GenerateCommandLineCommands() { - return $"xf {GetSourceArchive()} -C {GetDestinationDirectory()}"; - } - - private string GetSourceArchive() - { - return SourceArchive; - } - - private string GetDestinationDirectory() - { - return DestinationDirectory; + return $"xf {SourceArchive} -C {DestinationDirectory}"; } } } diff --git a/old/build_projects/dotnet-cli-build/TarGzFileCreateFromDirectory.cs b/src/core-sdk-tasks/TarGzFileCreateFromDirectory.cs similarity index 100% rename from old/build_projects/dotnet-cli-build/TarGzFileCreateFromDirectory.cs rename to src/core-sdk-tasks/TarGzFileCreateFromDirectory.cs diff --git a/old/build_projects/dotnet-cli-build/ZipFileCreateFromDirectory.cs b/src/core-sdk-tasks/ZipFileCreateFromDirectory.cs similarity index 100% rename from old/build_projects/dotnet-cli-build/ZipFileCreateFromDirectory.cs rename to src/core-sdk-tasks/ZipFileCreateFromDirectory.cs diff --git a/src/redist/targets/BuildCoreSdkTasks.targets b/src/redist/targets/BuildCoreSdkTasks.targets index 841e45efd..36d446cbc 100644 --- a/src/redist/targets/BuildCoreSdkTasks.targets +++ b/src/redist/targets/BuildCoreSdkTasks.targets @@ -17,4 +17,5 @@ + diff --git a/src/redist/targets/BundledComponents.targets b/src/redist/targets/BundledComponents.targets index 71caad51d..a9b560fd4 100644 --- a/src/redist/targets/BundledComponents.targets +++ b/src/redist/targets/BundledComponents.targets @@ -91,6 +91,9 @@ + +