-
Notifications
You must be signed in to change notification settings - Fork 5
/
DeploymentFileCompilerTask.cs
443 lines (374 loc) · 12.9 KB
/
DeploymentFileCompilerTask.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.DataTransformationServices.Project;
using Microsoft.DataTransformationServices.Project.ComponentModel;
using Microsoft.DataTransformationServices.Project.Serialization;
using Microsoft.DataWarehouse.VsIntegration.Shell.Project.Configuration;
using Microsoft.SqlServer.Dts.Runtime;
namespace Microsoft.SqlServer.IntegrationServices.Build
{
/// <summary>
/// Compiles SSIS Project Deployment files (.ispac) from one or
/// more SSIS Visual Studio project files (.dtproj).
/// </summary>
public class DeploymentFileCompilerTask : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Path(s) to the SSIS Visual Studio project files (.dtproj) to compile.
/// </summary>
[Required]
public ITaskItem[] InputProject { get; set; }
/// <summary>
/// The Visual Studio configuration to use.
/// </summary>
[Required]
public string Configuration { get; set; }
/// <summary>
/// On a successful build, this parameter will be populated with the
/// full paths to the project deployment files (.ispac) created during
/// this build.
/// </summary>
[Output]
public ITaskItem[] CreatedProjects { get; internal set; }
/// <summary>
/// (Optional) Sets the protection level for the output project (and all packages).
/// If not set, the protection level specified in the .dtproj is used. Must be a value
/// from the <see cref="DTSProtectionLevel"/> enum.
/// </summary>
public string ProtectionLevel
{
get
{
return m_protectionLevelString;
}
set
{
if (value != null)
{
// try to parse it
Enum.Parse(typeof (DTSProtectionLevel), value, true);
}
m_protectionLevelString = value;
}
}
/// <summary>
/// (Optional) This property is required when using a protection level that
/// requires a password.
/// </summary>
public string ProjectPassword { get; set; }
/// <summary>
/// (Optional) If set, deployment files will be created under this output directory.
/// If no value is provided, the .ispac file will be created under the Visual Studio
/// project's directory.
/// </summary>
public string RootOutputDirectory { get; set; }
/// <summary>
/// (Optional) When set, this version value will be used for the version information in
/// the .ispac files, overridding any version values set in the .dtproj file.
/// The format is "<Major>.<Minor>.<Build". Ex: 10.0.1
/// </summary>
public string Version { get; set; }
/// <summary>
/// (Optional) This value will populate the <see cref="Project.VersionComments"/> field.
/// If no value is provided, the text from the .dtproj file is used.
/// </summary>
public string VersionComments { get; set; }
#region Serialization classes
private ProjectSerialization VsProject { get; set; }
private ProjectManifest Manifest { get; set; }
private DataTransformationsConfiguration ProjectConfiguration
{
get
{
if (m_projectConfiguration == null)
{
GetProjectConfiguration();
}
return m_projectConfiguration;
}
}
private DataTransformationsConfiguration m_projectConfiguration;
private void GetProjectConfiguration()
{
foreach (var c in VsProject.Configurations)
{
var config = (DataTransformationsConfiguration)c;
if (config.Name.Equals(Configuration, StringComparison.OrdinalIgnoreCase))
{
m_projectConfiguration = config;
break;
}
}
if (m_projectConfiguration == null)
{
throw new Exception(String.Format(SR.ConfigNotFound, Configuration));
}
}
#endregion
private string m_protectionLevelString;
public override bool Execute()
{
bool result = true;
var outputProjects = new List<TaskItem>();
foreach (var projectFile in InputProject)
{
try
{
Log.LogMessage("------");
string projectDirectory = Path.GetDirectoryName(projectFile.ItemSpec);
string outputDirectory = string.IsNullOrEmpty(RootOutputDirectory) ? projectDirectory : RootOutputDirectory;
DeserializeProject(projectFile.ItemSpec);
if (VsProject.DeploymentModel == DeploymentModel.Project)
{
// Determine output directory
string projectOutputPath = GetOutputPath(outputDirectory);
Log.LogMessage(SR.OutputDirectory, projectOutputPath);
// Create project and set properties
var project = Project.CreateProject();
project.OfflineMode = true;
SetProjectProperties(project, Manifest);
// set the protection level
var protectionLevel = GetProtectionLevel(Manifest);
Log.LogMessage(SR.ProjectLevel, protectionLevel);
project.ProtectionLevel = protectionLevel;
if (PasswordNeeded(protectionLevel))
{
if (string.IsNullOrEmpty(ProjectPassword))
{
Log.LogError(SR.ProjectPasswordMissing);
result = false;
continue;
}
project.Password = ProjectPassword;
}
// Add parameters to project
string projectParameterPath = GetProjectParameterPath(projectDirectory);
var projectParameters = LoadProjectParameters(projectParameterPath);
foreach (var p in projectParameters.Parameters)
{
Log.LogMessage(SR.AddProjectParameter, p.Name);
var parameter = project.Parameters.Add(p.Name, (TypeCode) Int32.Parse(p.Properties["DataType"]));
parameter.LoadFromXML(p.GetXml(), new DefaultEvents());
}
// Set parameter values from configuration
var parameterSet = new Dictionary<string, ConfigurationSetting>();
foreach (string key in ProjectConfiguration.Options.ParameterConfigurationValues.Keys)
{
// check if it's a GUID
Guid guid;
if (Guid.TryParse(key, out guid))
{
var setting = ProjectConfiguration.Options.ParameterConfigurationValues[key];
var paramName = setting.Name.Replace("Project::", "");
Log.LogMessage(SR.ConfigProjectSetting, paramName);
project.Parameters[paramName].Value = setting.Value;
parameterSet.Add(key, setting);
}
}
// Add connections to project
var connectionManagerSerializer = new XmlSerializer(typeof (ProjectConnectionManager));
foreach (var c in Manifest.ConnectionManagers)
{
var path = GetConnectionManagerPath(projectDirectory, c);
Log.LogMessage(SR.LoadingConnectionManager, path);
var cmXml = File.ReadAllText(path);
var connMgr = (ProjectConnectionManager) connectionManagerSerializer.Deserialize(new StringReader(cmXml));
var cm = project.ConnectionManagerItems.Add(connMgr.CreationName, c.Name);
cm.Load(null, File.OpenRead(path));
}
// Add packages to project
foreach (var item in Manifest.Packages)
{
var packagePath = GetPackagePath(projectDirectory, item);
var package = LoadPackage(packagePath);
// check the protection level
if (package.ProtectionLevel != protectionLevel)
{
Log.LogMessage(SR.PackageProtectionLevel, protectionLevel);
package.ProtectionLevel = protectionLevel;
if (PasswordNeeded(protectionLevel))
{
package.PackagePassword = ProjectPassword;
}
}
// set package parameters
if (parameterSet.Count > 0)
{
SetParameterConfigurationValues(package.Parameters, parameterSet);
}
project.PackageItems.Add(package, item.Name);
project.PackageItems[item.Name].EntryPoint = item.EntryPoint;
}
// set project overrides
var version = GetProjectVersion();
if (version != null)
{
Log.LogMessage(SR.ProjectVersionChange, version);
project.VersionMajor = version.Major;
project.VersionMinor = version.Minor;
project.VersionBuild = version.Build;
}
if (VersionComments != null)
{
Log.LogMessage(SR.VersionComments);
project.VersionComments = VersionComments;
}
// Save project
string projectPath = GetProjectFilePath(projectOutputPath, project);
Log.LogMessage(SR.SavingProject, projectPath);
project.SaveTo(projectPath);
project.Dispose();
// Save the path to the project so it can be used as an output
outputProjects.Add(new TaskItem(projectPath));
}
}
catch (Exception e)
{
Log.LogErrorFromException(e, true);
result = false;
}
}
CreatedProjects = outputProjects.ToArray();
return result;
}
private bool PasswordNeeded(DTSProtectionLevel level)
{
return (level == DTSProtectionLevel.EncryptAllWithPassword ||
level == DTSProtectionLevel.EncryptSensitiveWithPassword);
}
private void SetParameterConfigurationValues(Parameters parameters, IDictionary<string, ConfigurationSetting> set)
{
foreach (Dts.Runtime.Parameter parameter in parameters)
{
if (set.ContainsKey(parameter.ID))
{
var configSetting = set[parameter.ID];
parameter.Value = configSetting.Value;
Log.LogMessage(SR.ConfigPackageSetting, configSetting.Name);
// remove parameter
set.Remove(parameter.ID);
if (set.Count == 0)
{
break;
}
}
}
}
private string GetOutputPath(string outputDirectory)
{
string outputPath = ProjectConfiguration.Options.OutputPath;
string path = Path.Combine(outputDirectory, outputPath, ProjectConfiguration.Name);
// make sure it exists
Directory.CreateDirectory(path);
return path;
}
private DTSProtectionLevel GetProtectionLevel(ProjectManifest manifest)
{
var level = manifest.ProtectionLevel;
if (ProtectionLevel != null)
{
level = (DTSProtectionLevel)Enum.Parse(typeof(DTSProtectionLevel), ProtectionLevel, true);
}
return level;
}
private string GetConnectionManagerPath(string projectDirectory, ConnectionManager connectionManager)
{
return Path.Combine(projectDirectory, connectionManager.Name);
}
private string GetProjectParameterPath(string projectDirectory)
{
return Path.Combine(projectDirectory, "Project.params");
}
private ProjectParameters LoadProjectParameters(string file)
{
var serializer = new XmlSerializer(typeof(ProjectParameters));
var fileStream = File.OpenRead(file);
return (ProjectParameters)serializer.Deserialize(fileStream);
}
private void SetProjectProperties(Project project, ProjectManifest manifest)
{
// set the properties we care about
foreach (var prop in manifest.Properties.Keys)
{
switch (prop)
{
case "Name":
project.Name = manifest.Properties[prop];
break;
case "VersionMajor":
project.VersionMajor = Int32.Parse(manifest.Properties[prop]);
break;
case "VersionMinor":
project.VersionMinor = Int32.Parse(manifest.Properties[prop]);
break;
case "VersionBuild":
project.VersionBuild = Int32.Parse(manifest.Properties[prop]);
break;
case "VersionComments":
project.VersionComments = manifest.Properties[prop];
break;
case "Description":
project.Description = manifest.Properties[prop];
break;
}
}
}
private string GetPackagePath(string projectDirectory, PackageManifest package)
{
return Path.Combine(projectDirectory, package.Name);
}
private string GetProjectFilePath(string outputDirectory, Project project)
{
string path = Path.Combine(outputDirectory, project.Name);
return Path.ChangeExtension(path, ".ispac");
}
private Version GetProjectVersion()
{
Version ver = null;
if (Version != null)
{
ver = new Version(Version);
}
return ver;
}
private Package LoadPackage(string path)
{
Package pkg;
Log.LogMessage(SR.LoadingPackage, path);
try
{
var xml = File.ReadAllText(path);
pkg = new Package { IgnoreConfigurationsOnLoad = true, CheckSignatureOnLoad = false, OfflineMode = true };
pkg.LoadFromXML(xml, null);
}
catch (Exception e)
{
Log.LogError(SR.ErrorLoadingPackage, path, e.Message);
throw;
}
return pkg;
}
private void DeserializeProject(string project)
{
Log.LogMessage(SR.LoadingProject, project);
var xmlOverrides = new XmlAttributeOverrides();
ProjectConfigurationOptions.PrepareSerializationOverrides(typeof(DataTransformationsProjectConfigurationOptions), SerializationLevel.Project, xmlOverrides);
// Read project file
var serializer = new XmlSerializer(typeof(ProjectSerialization), xmlOverrides);
var fileStream = File.OpenRead(project);
VsProject = (ProjectSerialization)serializer.Deserialize(fileStream);
// Read project deployment manifest
if (VsProject.DeploymentModel == DeploymentModel.Project)
{
serializer = new XmlSerializer(typeof(ProjectManifest));
var reader = new StringReader(VsProject.DeploymentModelSpecificXmlNode.InnerXml);
Manifest = (ProjectManifest)serializer.Deserialize(reader);
// TODO: read user settings - do we need to do this for MSBuild?
}
}
}
}