-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTarGzFileCreateFromDirectory.cs
157 lines (125 loc) · 4.69 KB
/
TarGzFileCreateFromDirectory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 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 System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.DotNet.SdkCustomHelix.Sdk
{
public sealed class TarGzFileCreateFromDirectory : ToolTask
{
/// <summary>
/// The path to the directory to be archived.
/// </summary>
[Required]
public string SourceDirectory { get; set; }
/// <summary>
/// The path of the archive to be created.
/// </summary>
[Required]
public string DestinationArchive { get; set; }
/// <summary>
/// Indicates if the destination archive should be overwritten if it already exists.
/// </summary>
public bool OverwriteDestination { get; set; }
/// <summary>
/// If zipping an entire folder without exclusion patterns, whether to include the folder in the archive.
/// </summary>
public bool IncludeBaseDirectory { get; set; }
/// <summary>
/// An item group of regular expressions for content to exclude from the archive.
/// </summary>
public ITaskItem[] ExcludePatterns { get; set; }
public bool IgnoreExitCode { get; set; }
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
int returnCode = base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
if (IgnoreExitCode)
{
returnCode = 0;
}
return returnCode;
}
protected override bool ValidateParameters()
{
base.ValidateParameters();
var retVal = true;
if (File.Exists(DestinationArchive))
{
if (OverwriteDestination == true)
{
Log.LogMessage(MessageImportance.Low, $"{DestinationArchive} will be overwritten");
}
else
{
Log.LogError($"'{DestinationArchive}' already exists. Did you forget to set '{nameof(OverwriteDestination)}' to true?");
retVal = false;
}
}
SourceDirectory = Path.GetFullPath(SourceDirectory);
SourceDirectory = SourceDirectory.EndsWith(Path.DirectorySeparatorChar.ToString())
? SourceDirectory
: SourceDirectory + Path.DirectorySeparatorChar;
if (!Directory.Exists(SourceDirectory))
{
Log.LogError($"SourceDirectory '{SourceDirectory} does not exist.");
retVal = false;
}
return retVal;
}
public override bool Execute()
{
return base.Execute();
}
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 $"{GetDestinationArchive()} {GetSourceSpecification()}";
}
private string GetSourceSpecification()
{
if (IncludeBaseDirectory)
{
var parentDirectory = Directory.GetParent(SourceDirectory).Parent.FullName;
var sourceDirectoryName = Path.GetFileName(Path.GetDirectoryName(SourceDirectory));
return $"--directory {parentDirectory} {sourceDirectoryName} {GetExcludes()}";
}
else
{
return $"--directory {SourceDirectory} {GetExcludes()} \".\"";
}
}
private string GetDestinationArchive()
{
return $"-czf {DestinationArchive}";
}
private string GetExcludes()
{
var excludes = String.Empty;
if (ExcludePatterns != null)
{
foreach (var excludeTaskItem in ExcludePatterns)
{
excludes += $" --exclude {excludeTaskItem.ItemSpec}";
}
}
return excludes;
}
protected override void LogToolCommand(string message)
{
base.LogToolCommand($"{base.GetWorkingDirectory()}> {message}");
}
}
}