Lay out components in publish directory

This commit is contained in:
Daniel Plaisted 2018-10-23 23:15:58 -07:00
parent 7ab1aa5a6c
commit 1344cb26c2
6 changed files with 30 additions and 100 deletions

View file

@ -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
{
/// <summary>
/// The path to the directory to be archived.
/// </summary>
[Required]
public string SourceArchive { get; set; }
/// <summary>
/// The path of the archive to be created.
/// </summary>
[Required]
public string DestinationDirectory { get; set; }
/// <summary>
/// Indicates if the destination directory should be cleaned if it already exists.
/// </summary>
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;
}
}
}

View file

@ -10,7 +10,10 @@ using System.IO.Compression;
namespace Microsoft.DotNet.Build.Tasks
{
public sealed class TarGzFileExtractToDirectory : ToolTask
/// <summary>
/// Extracts a .zip or .tar.gz file to a directory.
/// </summary>
public sealed class ExtractArchiveToDirectory : ToolTask
{
/// <summary>
/// The path to the archive to extract.
@ -27,7 +30,7 @@ namespace Microsoft.DotNet.Build.Tasks
/// <summary>
/// Indicates if the destination directory should be cleaned if it already exists.
/// </summary>
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}";
}
}
}

View file

@ -17,4 +17,5 @@
<UsingTask TaskName="GetCurrentRuntimeInformation" AssemblyFile="$(CoreSdkTaskDll)" />
<UsingTask TaskName="DownloadFile" AssemblyFile="$(CoreSdkTaskDll)" />
<UsingTask TaskName="ExtractArchiveToDirectory" AssemblyFile="$(CoreSdkTaskDll)" />
</Project>

View file

@ -91,6 +91,9 @@
<DownloadFile Condition=" '@(BundledLayoutComponent)' != '' And '%(BundledLayoutComponent.ShouldDownload)' == 'true'"
Uri="%(BundledLayoutComponent.BaseUrl)/%(BundledLayoutComponent.DownloadFileName)"
DestinationPath="%(BundledLayoutComponent.DownloadDestination)" />
<ExtractArchiveToDirectory SourceArchive="%(BundledLayoutComponent.DownloadDestination)"
DestinationDirectory="$(PublishDir)" />
</Target>
</Project>