| 1 |
{ |
| 2 |
Copyright 2014-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Merging of some special Android project files. } |
| 17 |
unit ToolAndroidMerging; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, Generics.Collections, DOM, |
| 22 |
CastleUtils, CastleStringUtils, |
| 23 |
ToolUtils; |
| 24 |
|
| 25 |
type |
| 26 |
ECannotMergeManifest = class(Exception); |
| 27 |
ECannotMergeBuildGradle = class(Exception); |
| 28 |
|
| 29 |
procedure MergeAndroidManifest(const Source, Destination: string; |
| 30 |
const ReplaceMacros: TReplaceMacros); |
| 31 |
procedure MergeStringsXml(const Source, Destination: string; |
| 32 |
const ReplaceMacros: TReplaceMacros); |
| 33 |
procedure MergeAppend(const Source, Destination: string; |
| 34 |
const ReplaceMacros: TReplaceMacros); |
| 35 |
procedure MergeAndroidMainActivity(const Source, Destination: string; |
| 36 |
const ReplaceMacros: TReplaceMacros); |
| 37 |
procedure MergeBuildGradle(const Source, Destination: string; |
| 38 |
const ReplaceMacros: TReplaceMacros); |
| 39 |
|
| 40 |
implementation |
| 41 |
|
| 42 |
uses Classes, StrUtils, XMLRead, XMLWrite, |
| 43 |
CastleXmlUtils, CastleUriUtils, CastleFilesUtils; |
| 44 |
|
| 45 |
{ globals -------------------------------------------------------------------- } |
| 46 |
|
| 47 |
procedure ReadXmlExpandingMacros(out Document: TXMLDocument; const FileName: string; |
| 48 |
const ReplaceMacros: TReplaceMacros); |
| 49 |
var |
| 50 |
StringStream: TStringStream; |
| 51 |
begin |
| 52 |
StringStream := TStringStream.Create( |
| 53 |
ReplaceMacros(FileToString(FilenameToUriSafe(FileName)))); |
| 54 |
try |
| 55 |
ReadXMLFile(Document, StringStream); |
| 56 |
finally FreeAndNil(StringStream) end; |
| 57 |
end; |
| 58 |
|
| 59 |
procedure MergeAndroidManifest(const Source, Destination: string; |
| 60 |
const ReplaceMacros: TReplaceMacros); |
| 61 |
var |
| 62 |
SourceXml, DestinationXml: TXMLDocument; |
| 63 |
|
| 64 |
procedure MergeApplication(const SourceApplication: TDOMElement); |
| 65 |
var |
| 66 |
DestinationApplication: TDOMElement; |
| 67 |
SourceNodes: TDOMNodeList; |
| 68 |
SourceAttribs: TDOMNamedNodeMap; |
| 69 |
I: Integer; |
| 70 |
begin |
| 71 |
DestinationApplication := DestinationXml.DocumentElement.ChildElement('application'); |
| 72 |
|
| 73 |
// GetChildNodes includes child comments, elements, everything... except attributes |
| 74 |
SourceNodes := SourceApplication.GetChildNodes; |
| 75 |
for I := 0 to SourceNodes.Count - 1 do |
| 76 |
begin |
| 77 |
// if Verbose then |
| 78 |
// Writeln('Appending node ', SourceNodes[I].NodeName, ' of type ', SourceNodes[I].NodeType); |
| 79 |
DestinationApplication.AppendChild( |
| 80 |
SourceNodes[I].CloneNode(true, DestinationXml)); |
| 81 |
end; |
| 82 |
|
| 83 |
SourceAttribs := SourceApplication.Attributes; |
| 84 |
for I := 0 to SourceAttribs.Length - 1 do |
| 85 |
begin |
| 86 |
if SourceAttribs[I].NodeType <> ATTRIBUTE_NODE then |
| 87 |
raise ECannotMergeManifest.Create('Attribute node does not have NodeType = ATTRIBUTE_NODE: ' + |
| 88 |
SourceAttribs[I].NodeName8); |
| 89 |
// if Verbose then |
| 90 |
// Writeln('Appending attribute ', SourceAttribs[I].NodeName); |
| 91 |
DestinationApplication.SetAttribute( |
| 92 |
SourceAttribs[I].NodeName, SourceAttribs[I].NodeValue); |
| 93 |
end; |
| 94 |
end; |
| 95 |
|
| 96 |
procedure MergeUsesPermission(const SourceUsesPermission: TDOMElement); |
| 97 |
var |
| 98 |
SourceName: string; |
| 99 |
I: TXMLElementIterator; |
| 100 |
begin |
| 101 |
SourceName := SourceUsesPermission.AttributeString('android:name'); |
| 102 |
|
| 103 |
I := DestinationXml.DocumentElement.ChildrenIterator; |
| 104 |
try |
| 105 |
while I.GetNext do |
| 106 |
begin |
| 107 |
if (I.Current.TagName = 'uses-permission') and |
| 108 |
I.Current.HasAttribute('android:name') and |
| 109 |
(I.Current.AttributeString('android:name') = SourceName) then |
| 110 |
begin |
| 111 |
// if Verbose then |
| 112 |
// Writeln('Main AndroidManifest.xml already uses-permission with ' + SourceName); |
| 113 |
Exit; |
| 114 |
end; |
| 115 |
end; |
| 116 |
finally FreeAndNil(I) end; |
| 117 |
|
| 118 |
DestinationXml.DocumentElement.AppendChild( |
| 119 |
SourceUsesPermission.CloneNode(true, DestinationXml)); |
| 120 |
end; |
| 121 |
|
| 122 |
procedure MergeSupportsScreens(const SourceElement: TDOMElement); |
| 123 |
begin |
| 124 |
if DestinationXml.DocumentElement.ChildElement(SourceElement.TagName8, false) <> nil then |
| 125 |
raise ECannotMergeManifest.Create( |
| 126 |
'Cannot merge AndroidManifest.xml, only one <' + SourceElement.TagName8 + '> is allowed'); |
| 127 |
DestinationXml.DocumentElement.AppendChild(SourceElement.CloneNode(true, DestinationXml)); |
| 128 |
end; |
| 129 |
|
| 130 |
var |
| 131 |
I: TXMLElementIterator; |
| 132 |
begin |
| 133 |
// if Verbose then |
| 134 |
// Writeln('Merging "', Source, '" into "', Destination, '"'); |
| 135 |
|
| 136 |
try |
| 137 |
{ Do not simply read Source by |
| 138 |
ReadXMLFile(SourceXml, Source); |
| 139 |
because we want to call ReplaceMacros() on source contents. } |
| 140 |
ReadXMLExpandingMacros(SourceXml, Source, ReplaceMacros); // this nils SourceXml in case of error |
| 141 |
try |
| 142 |
ReadXMLFile(DestinationXml, Destination); // this nils DestinationXml in case of error |
| 143 |
|
| 144 |
if SourceXml.DocumentElement.TagName <> 'manifest' then |
| 145 |
raise ECannotMergeManifest.Create('Cannot merge AndroidManifest.xml, root element must be <manifest>'); |
| 146 |
|
| 147 |
I := SourceXml.DocumentElement.ChildrenIterator; |
| 148 |
try |
| 149 |
while I.GetNext do |
| 150 |
begin |
| 151 |
if I.Current.TagName = 'application' then |
| 152 |
MergeApplication(I.Current) |
| 153 |
else |
| 154 |
if (I.Current.TagName = 'uses-permission') and |
| 155 |
I.Current.HasAttribute('android:name') then |
| 156 |
MergeUsesPermission(I.Current) |
| 157 |
else |
| 158 |
if I.Current.TagName = 'supports-screens' then |
| 159 |
MergeSupportsScreens(I.Current) |
| 160 |
else |
| 161 |
raise ECannotMergeManifest.Create('Cannot merge AndroidManifest.xml element <' + I.Current.TagName8 + '>'); |
| 162 |
end; |
| 163 |
finally FreeAndNil(I) end; |
| 164 |
|
| 165 |
WriteXMLFile(DestinationXml, Destination); |
| 166 |
finally FreeAndNil(DestinationXml) end; |
| 167 |
finally FreeAndNil(SourceXml) end; |
| 168 |
end; |
| 169 |
|
| 170 |
procedure MergeStringsXml(const Source, Destination: string; |
| 171 |
const ReplaceMacros: TReplaceMacros); |
| 172 |
var |
| 173 |
SourceXml, DestinationXml: TXMLDocument; |
| 174 |
I: TXMLElementIterator; |
| 175 |
begin |
| 176 |
// if Verbose then |
| 177 |
// Writeln('Merging "', Source, '" into "', Destination, '"'); |
| 178 |
|
| 179 |
try |
| 180 |
{ Do not simply read Source by |
| 181 |
ReadXMLFile(SourceXml, Source); |
| 182 |
because we want to call ReplaceMacros() on source contents. } |
| 183 |
ReadXMLExpandingMacros(SourceXml, Source, ReplaceMacros); // this nils SourceXml in case of error |
| 184 |
try |
| 185 |
ReadXMLFile(DestinationXml, Destination); // this nils DestinationXml in case of error |
| 186 |
|
| 187 |
if SourceXml.DocumentElement.TagName <> 'resources' then |
| 188 |
raise ECannotMergeManifest.Create('Cannot merge strings.xml, root element must be <resources>'); |
| 189 |
|
| 190 |
I := SourceXml.DocumentElement.ChildrenIterator; |
| 191 |
try |
| 192 |
while I.GetNext do |
| 193 |
DestinationXml.DocumentElement.AppendChild( |
| 194 |
I.Current.CloneNode(true, DestinationXml)); |
| 195 |
finally FreeAndNil(I) end; |
| 196 |
|
| 197 |
WriteXMLFile(DestinationXml, Destination); |
| 198 |
finally FreeAndNil(DestinationXml) end; |
| 199 |
finally FreeAndNil(SourceXml) end; |
| 200 |
end; |
| 201 |
|
| 202 |
procedure MergeAppend(const Source, Destination: string; |
| 203 |
const ReplaceMacros: TReplaceMacros); |
| 204 |
var |
| 205 |
SourceContents, DestinationContents: string; |
| 206 |
begin |
| 207 |
// if Verbose then |
| 208 |
// Writeln('Merging "', Source, '" into "', Destination, '"'); |
| 209 |
|
| 210 |
SourceContents := ReplaceMacros(FileToString(FilenameToUriSafe(Source))); |
| 211 |
DestinationContents := FileToString(FilenameToUriSafe(Destination)); |
| 212 |
DestinationContents := DestinationContents + NL + SourceContents; |
| 213 |
StringToFile(Destination, DestinationContents); |
| 214 |
end; |
| 215 |
|
| 216 |
procedure MergeAndroidMainActivity(const Source, Destination: string; |
| 217 |
const ReplaceMacros: TReplaceMacros); |
| 218 |
const |
| 219 |
InsertMarker = '/* ANDROID-SERVICES-INITIALIZATION */'; |
| 220 |
var |
| 221 |
SourceContents, DestinationContents: string; |
| 222 |
MarkerPos: Integer; |
| 223 |
begin |
| 224 |
// if Verbose then |
| 225 |
// Writeln('Merging "', Source, '" into "', Destination, '"'); |
| 226 |
|
| 227 |
SourceContents := ReplaceMacros(FileToString(FilenameToUriSafe(Source))); |
| 228 |
DestinationContents := FileToString(FilenameToUriSafe(Destination)); |
| 229 |
MarkerPos := Pos(InsertMarker, DestinationContents); |
| 230 |
if MarkerPos = 0 then |
| 231 |
raise ECannotMergeManifest.CreateFmt('Cannot find marker "%s" in MainActivity.java', [InsertMarker]); |
| 232 |
Insert(SourceContents, DestinationContents, MarkerPos); |
| 233 |
StringToFile(Destination, DestinationContents); |
| 234 |
end; |
| 235 |
|
| 236 |
procedure MergeBuildGradle(const Source, Destination: string; |
| 237 |
const ReplaceMacros: TReplaceMacros); |
| 238 |
var |
| 239 |
DestinationContents: string; |
| 240 |
Doc: TXMLDocument; |
| 241 |
|
| 242 |
{ Modify DestinationContents to add information specified in source XML file. } |
| 243 |
procedure MergeItems(const ListElement, ChildElement, Marker: string; const Indent: Integer); |
| 244 |
var |
| 245 |
I: TXMLElementIterator; |
| 246 |
E: TDOMElement; |
| 247 |
MarkerPos: Integer; |
| 248 |
begin |
| 249 |
E := Doc.DocumentElement.ChildElement(ListElement, false); |
| 250 |
if E <> nil then |
| 251 |
begin |
| 252 |
MarkerPos := Pos(Marker, DestinationContents); |
| 253 |
if MarkerPos = 0 then |
| 254 |
raise ECannotMergeBuildGradle.CreateFmt('The destination build.gradle does not contain "%s" marker.', |
| 255 |
[Marker]); |
| 256 |
MarkerPos += Length(Marker); |
| 257 |
|
| 258 |
I := E.ChildrenIterator(ChildElement); |
| 259 |
try |
| 260 |
while I.GetNext do |
| 261 |
begin |
| 262 |
Insert(NL + DupeString(' ', Indent) + I.Current.TextData, DestinationContents, MarkerPos); |
| 263 |
end; |
| 264 |
finally FreeAndNil(I) end; |
| 265 |
end; |
| 266 |
end; |
| 267 |
|
| 268 |
var |
| 269 |
SourceContents: string; |
| 270 |
SStream: TStringStream; |
| 271 |
begin |
| 272 |
SourceContents := ReplaceMacros(FileToString(FilenameToUriSafe(Source))); |
| 273 |
DestinationContents := ReplaceMacros(FileToString(FilenameToUriSafe(Destination))); |
| 274 |
|
| 275 |
SStream := TStringStream.Create(SourceContents); |
| 276 |
try |
| 277 |
try |
| 278 |
ReadXMLFile(Doc, SStream); // ReadXMLFile within "try" clause, as it initializes Doc always |
| 279 |
if Doc.DocumentElement.TagName <> 'build_gradle_merge' then |
| 280 |
raise ECannotMergeBuildGradle.Create('The source file from which to merge build.gradle must be XML with root <build_gradle_merge>'); |
| 281 |
MergeItems('dependencies', 'dependency', '// MERGE-DEPENDENCIES', 1); |
| 282 |
MergeItems('plugins', 'plugin', '// MERGE-PLUGINS', 1); |
| 283 |
MergeItems('repositories', 'repository', '// MERGE-REPOSITORIES', 1); |
| 284 |
finally FreeAndNil(Doc) end; |
| 285 |
finally FreeAndNil(SStream) end; |
| 286 |
|
| 287 |
StringToFile(Destination, DestinationContents); |
| 288 |
end; |
| 289 |
|
| 290 |
end. |