1
+ <Project ToolsVersion =" 4.0" xmlns =" http://schemas.microsoft.com/developer/msbuild/2003" >
2
+
3
+
4
+ <UsingTask TaskName =" UpdatePListEntries" TaskFactory =" RoslynCodeTaskFactory" AssemblyFile =" $(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
5
+ <ParameterGroup >
6
+ <TargetFile ParameterType =" System.String" Required =" true" />
7
+ <Entries ParameterType =" Microsoft.Build.Framework.ITaskItem[]" Required =" true" />
8
+ <IconFile ParameterType =" System.String" Output =" true" />
9
+ </ParameterGroup >
10
+ <Task >
11
+ <Reference Include =" netstandard" />
12
+ <Code Type =" Class" Language =" cs" ><![CDATA[
13
+ using System;
14
+ using System.IO;
15
+ using System.Xml;
16
+ using System.Text;
17
+ using Microsoft.Build.Framework;
18
+ using Microsoft.Build.Utilities;
19
+
20
+ public class UpdatePListEntries : Task
21
+ {
22
+ [Required]
23
+ public string TargetFile { get; set; }
24
+ [Required]
25
+ public Microsoft.Build.Framework.ITaskItem[] Entries { get; set; }
26
+ [Output]
27
+ public string IconFile { get; set; }
28
+
29
+ XmlDocument xml;
30
+ XmlNode dict;
31
+
32
+ class NullSubsetXmlTextWriter : XmlTextWriter
33
+ {
34
+ XmlWriterSettings _settings;
35
+ public NullSubsetXmlTextWriter(string inputFileName, Encoding encoding)
36
+ : base(inputFileName, encoding)
37
+ {
38
+ Formatting = Formatting.Indented;
39
+ IndentChar = ' ';
40
+ Indentation = 2;
41
+ _settings = new XmlWriterSettings();
42
+ _settings.Encoding = Encoding.UTF8;
43
+ _settings.Indent = true;
44
+ _settings.IndentChars = " ";
45
+ _settings.NewLineChars = "\n";
46
+ _settings.NewLineHandling = NewLineHandling.Entitize;
47
+ }
48
+
49
+ public override XmlWriterSettings Settings { get { return _settings; } }
50
+
51
+ public override void WriteDocType(string name, string pubid, string sysid, string subset)
52
+ {
53
+ // fix issue writing doctype
54
+ if (subset == string.Empty)
55
+ subset = null;
56
+ base.WriteDocType(name, pubid, sysid, subset);
57
+ }
58
+ }
59
+
60
+ string GetStringProperty(string name)
61
+ {
62
+ var location = dict.SelectSingleNode("key[.='" + name + "']/following-sibling::string[1]");
63
+ return location.InnerText;
64
+ }
65
+
66
+ void AddStringProperty(string name, string value, bool force = false)
67
+ {
68
+ XmlNode node;
69
+
70
+ var exists = dict.SelectSingleNode("key[text()='" + name + "']") != null;
71
+ if (exists && !force)
72
+ return;
73
+
74
+ dict.AppendChild(node = xml.CreateNode(XmlNodeType.Element, "key", null));
75
+ node.InnerText = name;
76
+ dict.AppendChild(node = xml.CreateNode(XmlNodeType.Element, "string", null));
77
+ node.InnerText = value;
78
+ }
79
+
80
+ public override bool Execute()
81
+ {
82
+ xml = new XmlDocument();
83
+ xml.Load(TargetFile);
84
+
85
+ dict = xml.SelectSingleNode("plist/dict") as XmlElement;
86
+
87
+ foreach (var entry in Entries)
88
+ {
89
+ if (!bool.TryParse(entry.GetMetadata("Force"), out var force))
90
+ force = false;
91
+
92
+ AddStringProperty(entry.ItemSpec, entry.GetMetadata("Value"), force);
93
+ }
94
+
95
+ IconFile = GetStringProperty("CFBundleIconFile");
96
+
97
+ using (var sw = new NullSubsetXmlTextWriter(TargetFile, Encoding.UTF8))
98
+ xml.Save(sw);
99
+
100
+ return true;
101
+ }
102
+ }
103
+ ]]> </Code >
104
+ </Task >
105
+ </UsingTask >
106
+
107
+
108
+ </Project >
0 commit comments