-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathUpdatePList.targets
executable file
·108 lines (88 loc) · 3.46 KB
/
UpdatePList.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
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="UpdatePListEntries" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<TargetFile ParameterType="System.String" Required="true" />
<Entries ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<IconFile ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="netstandard" />
<Code Type="Class" Language="cs"><![CDATA[
using System;
using System.IO;
using System.Xml;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class UpdatePListEntries : Task
{
[Required]
public string TargetFile { get; set; }
[Required]
public Microsoft.Build.Framework.ITaskItem[] Entries { get; set; }
[Output]
public string IconFile { get; set; }
XmlDocument xml;
XmlNode dict;
class NullSubsetXmlTextWriter : XmlTextWriter
{
XmlWriterSettings _settings;
public NullSubsetXmlTextWriter(string inputFileName, Encoding encoding)
: base(inputFileName, encoding)
{
Formatting = Formatting.Indented;
IndentChar = ' ';
Indentation = 2;
_settings = new XmlWriterSettings();
_settings.Encoding = Encoding.UTF8;
_settings.Indent = true;
_settings.IndentChars = " ";
_settings.NewLineChars = "\n";
_settings.NewLineHandling = NewLineHandling.Entitize;
}
public override XmlWriterSettings Settings { get { return _settings; } }
public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
// fix issue writing doctype
if (subset == string.Empty)
subset = null;
base.WriteDocType(name, pubid, sysid, subset);
}
}
string GetStringProperty(string name)
{
var location = dict.SelectSingleNode("key[.='" + name + "']/following-sibling::string[1]");
return location.InnerText;
}
void AddStringProperty(string name, string value, bool force = false)
{
XmlNode node;
var exists = dict.SelectSingleNode("key[text()='" + name + "']") != null;
if (exists && !force)
return;
dict.AppendChild(node = xml.CreateNode(XmlNodeType.Element, "key", null));
node.InnerText = name;
dict.AppendChild(node = xml.CreateNode(XmlNodeType.Element, "string", null));
node.InnerText = value;
}
public override bool Execute()
{
xml = new XmlDocument();
xml.Load(TargetFile);
dict = xml.SelectSingleNode("plist/dict") as XmlElement;
foreach (var entry in Entries)
{
if (!bool.TryParse(entry.GetMetadata("Force"), out var force))
force = false;
AddStringProperty(entry.ItemSpec, entry.GetMetadata("Value"), force);
}
IconFile = GetStringProperty("CFBundleIconFile");
using (var sw = new NullSubsetXmlTextWriter(TargetFile, Encoding.UTF8))
xml.Save(sw);
return true;
}
}
]]></Code>
</Task>
</UsingTask>
</Project>