summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.BuildTasks/AddFilesTo7Zip.cs
blob: 970adb008b6722d2f5a13bcf8033d65848e77f97 (plain)
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
//-----------------------------------------------------------------------
// <copyright file="AddFilesTo7Zip.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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 {
		/// <summary>
		/// Initializes a new instance of the <see cref="AddFilesTo7Zip"/> class.
		/// </summary>
		public AddFilesTo7Zip() {
			this.YieldDuringToolExecution = true;
		}

		[Required]
		public ITaskItem ZipFileName { get; set; }

		[Required]
		public ITaskItem[] Files { get; set; }

		public string WorkingDirectory { get; set; }

		/// <summary>
		/// Gets the name of the tool.
		/// </summary>
		/// <value>
		/// The name of the tool.
		/// </value>
		protected override string ToolName {
			get { return "7za.exe"; }
		}

		/// <summary>
		/// Generates the full path to tool.
		/// </summary>
		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();
		}

		/// <summary>
		/// Gets the response file switch.
		/// </summary>
		/// <param name="responseFilePath">The response file path.</param>
		protected override string GetResponseFileSwitch(string responseFilePath) {
			return "@" + responseFilePath;
		}

		/// <summary>
		/// Gets the response file encoding.
		/// </summary>
		/// <value>
		/// The response file encoding.
		/// </value>
		protected override Encoding ResponseFileEncoding {
			get { return Encoding.UTF8; }
		}

		/// <summary>
		/// Generates the response file commands.
		/// </summary>
		protected override string GenerateResponseFileCommands() {
			var args = new CommandLineBuilder();
			args.AppendFileNamesIfNotNull(this.Files.Select(GetWorkingDirectoryRelativePath).ToArray(), Environment.NewLine);
			return args.ToString();
		}

		/// <summary>
		/// Gets the working directory.
		/// </summary>
		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;
			}
		}
	}
}