//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.BuildTasks {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
public class AddFilesTo7Zip : ToolTask {
///
/// Initializes a new instance of the class.
///
public AddFilesTo7Zip() {
this.YieldDuringToolExecution = true;
}
[Required]
public ITaskItem ZipFileName { get; set; }
[Required]
public ITaskItem[] Files { get; set; }
public string WorkingDirectory { get; set; }
///
/// Gets the name of the tool.
///
///
/// The name of the tool.
///
protected override string ToolName {
get { return "7za.exe"; }
}
///
/// Generates the full path to tool.
///
protected override string GenerateFullPathToTool() {
return this.ToolPath;
}
protected override string GenerateCommandLineCommands() {
var args = new CommandLineBuilder();
args.AppendSwitch("a");
args.AppendSwitch("--");
args.AppendFileNameIfNotNull(this.ZipFileName);
return args.ToString();
}
///
/// Gets the response file switch.
///
/// The response file path.
protected override string GetResponseFileSwitch(string responseFilePath) {
return "@" + responseFilePath;
}
///
/// Gets the response file encoding.
///
///
/// The response file encoding.
///
protected override Encoding ResponseFileEncoding {
get { return Encoding.UTF8; }
}
///
/// Generates the response file commands.
///
protected override string GenerateResponseFileCommands() {
var args = new CommandLineBuilder();
args.AppendFileNamesIfNotNull(this.Files.Select(GetWorkingDirectoryRelativePath).ToArray(), Environment.NewLine);
return args.ToString();
}
///
/// Gets the working directory.
///
protected override string GetWorkingDirectory() {
if (!String.IsNullOrEmpty(this.WorkingDirectory)) {
return this.WorkingDirectory;
} else {
return base.GetWorkingDirectory();
}
}
private string GetWorkingDirectoryRelativePath(ITaskItem taskItem) {
if (taskItem.ItemSpec.StartsWith(this.WorkingDirectory, StringComparison.OrdinalIgnoreCase)) {
return taskItem.ItemSpec.Substring(this.WorkingDirectory.Length);
} else {
return taskItem.ItemSpec;
}
}
}
}