This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency on MSBuild.Community.Tasks
- Loading branch information
1 parent
c40b53f
commit a95249f
Showing
4 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
|
||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace XsdDocumentation.Build | ||
{ | ||
public sealed class Zip : Task | ||
{ | ||
public ITaskItem WorkingDirectory { get; set; } | ||
|
||
[Required] | ||
public ITaskItem ZipFileName { get; set; } | ||
|
||
[Required] | ||
public ITaskItem[] Files { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
try | ||
{ | ||
var workingDirectory = WorkingDirectory.ItemSpec; | ||
var zipFileName = ZipFileName.ItemSpec; | ||
var fileNames = Files.Select(f => f.ItemSpec); | ||
CreateZipArchive(workingDirectory, zipFileName, fileNames); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Log.LogErrorFromException(ex); | ||
} | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
private static void CreateZipArchive(string workingDirectory, string zipFileName, IEnumerable<string> fileNames) | ||
{ | ||
using (var fileStream = File.Create(zipFileName)) | ||
using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create)) | ||
{ | ||
foreach (var fileName in fileNames) | ||
{ | ||
var relativePath = GetRelativePath(fileName, workingDirectory); | ||
zipArchive.CreateEntryFromFile(fileName, relativePath, CompressionLevel.Optimal); | ||
} | ||
} | ||
} | ||
|
||
private static string GetRelativePath(string fileName, string workingDirectory) | ||
{ | ||
if (!fileName.StartsWith(workingDirectory, StringComparison.OrdinalIgnoreCase)) | ||
return Path.GetFileName(fileName); | ||
|
||
var backslashCompensation = workingDirectory.EndsWith("\\") ? 1 : 0; | ||
return fileName.Substring(workingDirectory.Length + backslashCompensation); | ||
} | ||
} | ||
} |