Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RootDirectory option to Zip task #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
34 changes: 31 additions & 3 deletions Source/MSBuild.Community.Tasks/Zip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ namespace MSBuild.Community.Tasks
/// ZipFileName="D:\svn\repo.zip" />
/// </Target>
/// ]]></code>
/// Create a zip file using a root directory.
/// <code><![CDATA[
/// <ItemGroup>
/// <RepoFiles Include="D:\svn\repo\**\*.*" />
/// </ItemGroup>
/// <Target Name="Zip">
/// <Zip Files="@(RepoFiles)"
/// RootDirectory="Repo"
/// ZipFileName="D:\svn\repo.zip" />
/// </Target>
/// ]]></code>
/// </example>
public class Zip : Task
{
Expand Down Expand Up @@ -137,11 +148,19 @@ public Zip()
/// </summary>
/// <value>The working directory.</value>
/// <remarks>
/// The working directory is the base of the zip file.
/// All files will be made relative from the working directory.
/// </remarks>
public string WorkingDirectory { get; set; }

/// <summary>
/// Gets or sets the root directory for the archive.
/// </summary>
/// <value>The root directory.</value>
/// <remarks>
/// The root directory is the base of the archive.
/// </remarks>
public string RootDirectory { get; set; }

/// <summary>
/// Gets or sets the password.
/// </summary>
Expand Down Expand Up @@ -263,7 +282,7 @@ private bool ZipFiles()
// maybe a directory
if (Directory.Exists(name))
{
var directoryEntry = zip.AddDirectory(name, directoryPathInArchive);
var directoryEntry = zip.AddDirectory(name, GetArchivePath(RootDirectory, directoryPathInArchive));
if (!Quiet)
Log.LogMessage(Resources.ZipAdded, directoryEntry.FileName);

Expand All @@ -279,7 +298,7 @@ private bool ZipFiles()
&& Path.GetFileName(directoryPathInArchive) == Path.GetFileName(name))
directoryPathInArchive = Path.GetDirectoryName(directoryPathInArchive);

var entry = zip.AddFile(name, directoryPathInArchive);
var entry = zip.AddFile(name, GetArchivePath(RootDirectory, directoryPathInArchive));
if (!Quiet)
Log.LogMessage(Resources.ZipAdded, entry.FileName);
}
Expand All @@ -297,6 +316,15 @@ private bool ZipFiles()
return true;
}

private static string GetArchivePath(string rootDirectory, string directoryPathInArchive)
{
return String.IsNullOrEmpty(rootDirectory)
? directoryPathInArchive
: String.IsNullOrEmpty(directoryPathInArchive)
? rootDirectory
: Path.Combine(rootDirectory, directoryPathInArchive);
}

private static string GetPath(string originalPath, string rootDirectory)
{
var relativePath = new List<string>();
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
> # Archived Repository 🚨
> **This project is no longer maintained and has been archived. No further issues, PRs, or updates will be made.**
---

#MSBuild Community Tasks

The MSBuild Community Tasks Project is an open source project for MSBuild tasks.
Expand Down