dotnet-installer/build_projects/dotnet-cli-build/TarGzFileExtractToDirectory.cs
Piotr Puszkiewicz 5ebc6a1ceb Decompose Crossgen, remove CleanPublishOutput, replace ExtractArchive with *FileExtractToDirectory (#3927)
* Eliminate CleanPublishOutput

* Decompose Crossgen Task

* WiP

* TarGzFileExtractToDirectory

* FixModeFlags --> CHMod

Also various eliminations of dead code

* Tasks cleanup

Move all tasks to .tasks file. There is little value in keepint them in each source file as they are already being used assumptively by files that happen to get executed later.

Also eliminating uses of <Exec> for DotNet invocations

* Move to BuildTools implementation of TarGzCreateFromDirectory

* Eliminate Command.cs and helpers

* Remove dead code

* Revert TarGz from BuildTools

Latest build tools package has not picked up the task, though it is checked in.

* Disable ChMod on Windows

* Windows bug fix

* PR Feedback

* Finish changing Chmod caps
2016-07-26 00:29:59 -04:00

112 lines
3.2 KiB
C#

// 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 TarGzFileExtractToDirectory : ToolTask
{
/// <summary>
/// The path to the archive to extract.
/// </summary>
[Required]
public string SourceArchive { get; set; }
/// <summary>
/// The path of the directory to which the archive should be extracted.
/// </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; }
protected override bool ValidateParameters()
{
base.ValidateParameters();
var retVal = true;
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);
}
else
{
Log.LogWarning("'{0}' already exists. Did you forget to set '{1}' to true?", DestinationDirectory, nameof(OverwriteDestination));
retVal = false;
}
}
if (!File.Exists(SourceArchive))
{
Log.LogError($"SourceArchive '{SourceArchive} does not exist.");
retVal = false;
}
if (retVal)
{
Log.LogMessage($"Creating Directory {DestinationDirectory}");
Directory.CreateDirectory(DestinationDirectory);
}
return retVal;
}
public override bool Execute()
{
bool retVal = base.Execute();
if (!retVal)
{
Log.LogMessage($"Deleting Directory {DestinationDirectory}");
Directory.Delete(DestinationDirectory);
}
return retVal;
}
protected override string ToolName
{
get { return "tar"; }
}
protected override MessageImportance StandardOutputLoggingImportance
{
get { return MessageImportance.High; } // or else the output doesn't get logged by default
}
protected override string GenerateFullPathToTool()
{
return "tar";
}
protected override string GenerateCommandLineCommands()
{
return $"xf {GetSourceArchive()} -C {GetDestinationDirectory()}";
}
private string GetSourceArchive()
{
return SourceArchive;
}
private string GetDestinationDirectory()
{
return DestinationDirectory;
}
}
}