-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGetSpreadsheetValues.cs
145 lines (121 loc) · 5.3 KB
/
GetSpreadsheetValues.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2012 Loren M Halvorson
* This source is subject to the Microsoft Public License (Ms-PL).
* See http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx.
* All other rights reserved.
*/
using System;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace XmlPreprocess.Tasks
{
/// <summary>
/// Retrieve values from an XmlPreprocess spreadsheet
/// </summary>
public class GetSpreadsheetValues : ToolTask
{
List<string> _results = new List<string>();
[Required]
public string Environment { get; set; }
[Required]
public string SettingName { get; set; }
// One of the following is required:
public ITaskItem SpreadsheetFile { get; set; }
public ITaskItem Database { get; set; }
public ITaskItem CustomDataSource { get; set; }
public string Delimiters { get; set; }
public string EnvironmentRow { get; set; }
public string FirstValueRow { get; set; }
public string SettingNameCol { get; set; }
public string DefaultValueCol { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="GetSpreadsheetValues"/> class.
/// </summary>
public GetSpreadsheetValues()
{
Delimiters = ";";
}
/// <summary>
/// Returns the fully qualified path to the executable file.
/// </summary>
/// <returns>
/// The fully qualified path to the executable file.
/// </returns>
protected override string GenerateFullPathToTool()
{
return ToolName;
}
/// <summary>
/// Gets the name of the executable file to run.
/// </summary>
/// <returns>
/// The name of the executable file to run.
/// </returns>
protected override string ToolName
{
get { return "XmlPreprocess.exe"; }
}
/// <summary>
/// Construct the command line from the task properties by using the CommandLineBuilder
/// </summary>
/// <returns></returns>
protected override string GenerateCommandLineCommands()
{
XmlPreprocess.ValidatePropertyIsNullOrInteger("EnvironmentRow", EnvironmentRow);
XmlPreprocess.ValidatePropertyIsNullOrInteger("FirstValueRow", FirstValueRow);
XmlPreprocess.ValidatePropertyIsNullOrInteger("SettingNameCol", SettingNameCol);
XmlPreprocess.ValidatePropertyIsNullOrInteger("DefaultValueCol", DefaultValueCol);
int dataSourceCount = 0;
if (SpreadsheetFile != null) dataSourceCount++;
if (Database != null) dataSourceCount++;
if (CustomDataSource != null) dataSourceCount++;
if (dataSourceCount != 1)
throw new ArgumentException("Exactly one of the following arguments must be passed: SpreadsheetFile, Database, or CustomDataSource");
CommandLineBuilder builder = new CommandLineBuilder();
builder.AppendSwitch("/nologo");
builder.AppendSwitchIfNotNull("/property:", SettingName);
builder.AppendSwitchIfNotNull("/environment:", Environment);
builder.AppendSwitchIfNotNull("/spreadsheet:", SpreadsheetFile);
builder.AppendSwitchIfNotNull("/database:", Database);
builder.AppendSwitchIfNotNull("/custom:", CustomDataSource);
if (!string.IsNullOrEmpty(Delimiters))
{
builder.AppendSwitchIfNotNull("/delimiters:", Delimiters);
}
builder.AppendSwitchIfNotNull("/environmentRow:", EnvironmentRow);
builder.AppendSwitchIfNotNull("/firstValueRow:", FirstValueRow);
builder.AppendSwitchIfNotNull("/settingNameCol:", SettingNameCol);
builder.AppendSwitchIfNotNull("/defaultValueCol:", DefaultValueCol);
// Log a High importance message stating the file that we are assembling
Log.LogMessage(MessageImportance.Normal, "Extracting the value of {0} from {1} for the {2} environment", SettingName, SpreadsheetFile, Environment);
// We have all of our switches added, return the commandline as a string
return builder.ToString();
}
/// <summary>
/// Parses a single line of text to identify any errors or warnings in canonical format.
/// </summary>
/// <param name="singleLine">A single line of text for the method to parse.</param>
/// <param name="messageImportance">A value of <see cref="T:Microsoft.Build.Framework.MessageImportance"/> that indicates the importance level with which to log the message.</param>
protected override void LogEventsFromTextOutput(String singleLine, MessageImportance messageImportance)
{
if (singleLine.StartsWith("Error XMLPP"))
{
Log.LogError(singleLine);
}
else
{
if (!string.IsNullOrEmpty(singleLine))
_results.Add(singleLine.Trim());
}
}
/// <summary>
/// Gets the values.
/// </summary>
[Output]
public string[] Values
{
get { return _results.ToArray(); }
}
}
}