Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Useful MSBuild inline tasks and targets. Import the common.tasks and/or common.t

Usage examples

Zip task:
Zip task (folders hierarchy ignored):

```xml
<Target Name="BeforeBuild">
Expand All @@ -19,11 +19,37 @@ Zip task:
</Target>
```

Zip task (folders hierarchy maintained relative to InputBaseDirectory):

```xml
<Target Name="BeforeBuild">
<ItemGroup>
<FilesToZip Include="$(ProjectDir)\PayloadUnzipped\*.*" />
</ItemGroup>
<Zip
InputFileNames="@(FilesToZip)"
InputBaseDirectory="$(ProjectDir)\PayloadUnzipped"
OutputFileName="$(ProjectDir)$(TargetZipFile)"
OverwriteExistingFile="true" />
</Target>
```
ZipDir task (folders hierarchy maintained relative to InputBaseDirectory):

```xml
<Target Name="BeforeBuild">
<ZipDir
InputBaseDirectory="$(ProjectDir)\PayloadUnzipped"
OutputFileName="$(ProjectDir)$(TargetZipFile)"
OverwriteExistingFile="true"
IncludeBaseDirectory="false" />
</Target>
```

SafeGitClean:

```
msbuild common.targets /p:BackupDir=C:\temp\backup /p:DeleteBackupDir=true
```

If BackupDir is not provided a directory in %TEMP% will be created
The default value for DeleteBackupDir is 'false'
The default value for DeleteBackupDir is 'false'
52 changes: 32 additions & 20 deletions common.tasks
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,40 @@
<UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputFileNames ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<InputBaseDirectory ParameterType="System.String" Required="false" />
<OutputFileName ParameterType="System.String" Required="true" />
<OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression" />
<Using Namespace="System.IO.Compression" />
<Code Type="Fragment" Language="cs">
<![CDATA[
<![CDATA[
Log.LogMessage(MessageImportance.High, "=== Zipping files ===");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove - I don't think it is useful. The logs will contain the name of the target being invoked. The user can also do this in their code if they find it useful.


var fileMode = OverwriteExistingFile ? FileMode.Create : FileMode.CreateNew;
using (var archive = new ZipArchive(new FileStream(OutputFileName, fileMode), ZipArchiveMode.Create))
var baseDir = string.IsNullOrWhiteSpace(InputBaseDirectory) ? string.Empty : Path.GetFullPath(InputBaseDirectory);
var filesToZip = InputFileNames.Select(f => Path.GetFullPath(f.ItemSpec)).Where(n => n.StartsWith(baseDir));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would the user provided a collection containing files they don't want to zip? The idea is that InputFileNames contains only files that they user wants to zip. If they don't want to zip a file it should not be in the collection.
The target is a basic building block and I am afraid we are now trying to be too smart. The result is that the user now needs to know what is happening inside the target because it is hard to tell how it behaves by just looking at parameters.


using (var archive = new ZipArchive(new FileStream(OutputFileName, fileMode), ZipArchiveMode.Create, leaveOpen: false))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove leaveOpen: false. false is the value passed by the ctor that takes the first 2 parameters so I don't know why we would want to pass this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as you asked this question on my first pull request, so other people would not have to ask this question again or check the documentation to clearify this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I remember the conversation. I was wrong. I don't think it adds any value. Sorry!

{
foreach (var inputFileName in InputFileNames.Select(f => f.ItemSpec))
foreach (var inputFileName in filesToZip)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please use 4 space indent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon?! Is "nit" in Polish or in English? What do you mean?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit = nitpick. we use it at work for small/cosmetic issues

{
var entryName = baseDir.Length == 0
? Path.GetFileName(inputFileName)
: inputFileName.Substring(baseDir.Length + 1);

Log.LogMessage(MessageImportance.High, "Zipping '" + entryName + "' from " + inputFileName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is useful. This will just clutter the logs especially where there is a lot of files to zip.


var archiveEntry = archive.CreateEntry(entryName);
using (var fs = new FileStream(inputFileName, FileMode.Open))
{
var archiveEntry = archive.CreateEntry(Path.GetFileName(inputFileName));
using (var fs = new FileStream(inputFileName, FileMode.Open))
{
using (var zipStream = archiveEntry.Open())
{
fs.CopyTo(zipStream);
}
}
using (var zipStream = archiveEntry.Open())
{
fs.CopyTo(zipStream);
}
}
}
}
]]>
</Code>
Expand All @@ -38,7 +50,7 @@
<OutputFileName ParameterType="System.String" Required="true" />
<OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
<IncludeBaseDirectory ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
Expand All @@ -47,16 +59,16 @@
<![CDATA[
if (File.Exists(OutputFileName))
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no changes below apart from formatting. Please revert.

if (!OverwriteExistingFile)
{
return false;
}
File.Delete(OutputFileName);
if (!OverwriteExistingFile)
{
return false;
}
File.Delete(OutputFileName);
}
ZipFile.CreateFromDirectory
(
InputBaseDirectory, OutputFileName,
CompressionLevel.Optimal, IncludeBaseDirectory
InputBaseDirectory, OutputFileName,
CompressionLevel.Optimal, IncludeBaseDirectory
);
]]>
</Code>
Expand Down