forked from andrewdavey/cassette
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cassette.targets
120 lines (99 loc) · 3.73 KB
/
Cassette.targets
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
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This task takes in a XDT transform file and transforms it, following any inheritance chain.
There should be at least one base transform for this to work; otherwise just use Microsoft's
regular TransformXml task.
See: https://gist.github.com/1918022 -->
<!-- EXAMPLE USAGE:
<TransformXmlHierarchy
Source="some-source.xml"
Destination="transformed.xml"
TaskPath="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.XmlTransform.dll"/>
-->
<UsingTask
TaskName="TransformXmlHierarchy"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Source Required="true" />
<Destination Required="true" />
<TaskPath Required="true"/>
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Using Namespace="System"/>
<Using Namespace="System.Linq"/>
<Using Namespace="System.IO" />
<Using Namespace="System.Xml"/>
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
if (!File.Exists(TaskPath))
throw new Exception("Could not load publishing tasks assembly");
Assembly taskAssembly = Assembly.UnsafeLoadFrom(TaskPath);
Func<XmlDocument, string, XmlDocument> transformer = (source, transform) =>
{
dynamic transformation = taskAssembly.CreateInstance(
"Microsoft.Web.XmlTransform.XmlTransformation", true, BindingFlags.CreateInstance,
null, new object[] { transform }, null, null);
if (transformation == null)
throw new Exception("Could not create instance of XmlTransformation");
transformation.Apply(source);
return source;
};
Func<XmlDocument, string> getParent = (source) =>
{
if (source == null) return null;
// Build dependency list
var nsmgr = new XmlNamespaceManager(source.NameTable);
nsmgr.AddNamespace("x", source.DocumentElement.NamespaceURI);
var attr = source.SelectSingleNode("x:package", nsmgr).Attributes["inherits"];
return attr == null ? null : attr.Value;
};
var rootDoc = new XmlDocument();
var sources = new List<string>();
var basePath = Path.GetDirectoryName(Source);
var parent = Path.GetFileName(Source);
if (basePath == null) {
throw new Exception("Could not find base directory of path " + Source);
}
do {
sources.Add(parent);
rootDoc.Load(Path.Combine(basePath, parent));
parent = getParent(rootDoc);
if (parent != null) {
rootDoc.Load(Path.Combine(basePath, parent));
}
} while (parent != null);
// Reverse chain
sources.Reverse();
var transformedDoc = sources.Skip(1).Aggregate(rootDoc,
(document, transform) => String.IsNullOrEmpty(transform)
? document
: transformer(document, Path.Combine(basePath, transform)),
(document) => document);
Log.LogMessage(MessageImportance.Normal, "Transformed " + Destination);
transformedDoc.Save(Destination);
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="GetAssemblyInformationalVersion"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Assembly ParameterType="System.String" Required="true" />
<Version ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.Diagnostics"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var version = FileVersionInfo.GetVersionInfo(Assembly);
Version = version.ProductVersion;
]]>
</Code>
</Task>
</UsingTask>
</Project>