BuildGradle.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Text;
  5. using System;
  6. public class UniWebViewGradleConfig
  7. {
  8. private readonly string m_filePath;
  9. public UniWebViewGradleNode Root { get; }
  10. public UniWebViewGradleConfig(string filePath)
  11. {
  12. var file = File.ReadAllText(filePath);
  13. TextReader reader = new StringReader(file);
  14. m_filePath = filePath;
  15. Root = new UniWebViewGradleNode("root");
  16. var curNode = Root;
  17. var str = new StringBuilder();
  18. var indentationBuffer = new StringBuilder();
  19. var inDoubleQuote = false;
  20. var inSingleQuote = false;
  21. var inDollarVariable = false;
  22. var atLineStart = true;
  23. while (reader.Peek() > 0)
  24. {
  25. char c = (char)reader.Read();
  26. switch (c)
  27. {
  28. case '/':
  29. {
  30. if (!inDoubleQuote && !inSingleQuote && !inDollarVariable && reader.Peek() == '/')
  31. {
  32. var raw = str.ToString();
  33. var buffered = FormatStr(str);
  34. str = new StringBuilder();
  35. reader.Read();
  36. string comment = reader.ReadLine() ?? string.Empty;
  37. if (!string.IsNullOrEmpty(buffered))
  38. {
  39. curNode.AppendChildNode(new UniWebViewGradleContentNode(buffered + " //" + comment, curNode));
  40. }
  41. else
  42. {
  43. var commentNode = new UniWebViewGradleCommentNode(comment, curNode)
  44. {
  45. LeadingWhitespace = indentationBuffer.Length > 0 ? indentationBuffer.ToString() : ExtractLeadingWhitespace(raw)
  46. };
  47. curNode.AppendChildNode(commentNode);
  48. }
  49. indentationBuffer = new StringBuilder();
  50. inDollarVariable = false;
  51. atLineStart = false;
  52. break;
  53. }
  54. str.Append('/');
  55. atLineStart = false;
  56. break;
  57. }
  58. case '\n':
  59. case '\r':
  60. {
  61. var strf = FormatStr(str);
  62. if (!string.IsNullOrEmpty(strf))
  63. {
  64. curNode.AppendChildNode(new UniWebViewGradleContentNode(strf, curNode));
  65. }
  66. }
  67. inDollarVariable = false;
  68. str = new StringBuilder();
  69. indentationBuffer = new StringBuilder();
  70. atLineStart = true;
  71. break;
  72. case '\t':
  73. {
  74. if (atLineStart)
  75. {
  76. indentationBuffer.Append(" ");
  77. }
  78. else
  79. {
  80. str.Append(" ");
  81. atLineStart = false;
  82. }
  83. break;
  84. }
  85. case ' ':
  86. {
  87. if (atLineStart)
  88. {
  89. indentationBuffer.Append(' ');
  90. }
  91. else
  92. {
  93. str.Append(c);
  94. }
  95. break;
  96. }
  97. case '{':
  98. {
  99. if (inDoubleQuote || inSingleQuote) {
  100. str.Append(c);
  101. break;
  102. }
  103. var raw = str.ToString();
  104. var n = FormatStr(str);
  105. if (curNode != null) {
  106. // Create a node even when n is empty to preserve brace balance (e.g., nested blocks inside closures).
  107. var nodeName = n ?? string.Empty;
  108. UniWebViewGradleNode node = new UniWebViewGradleNode(nodeName, curNode);
  109. var leading = indentationBuffer.Length > 0 ? indentationBuffer.ToString() : ExtractLeadingWhitespace(raw);
  110. node.LeadingWhitespace = leading;
  111. curNode.AppendChildNode(node);
  112. curNode = node;
  113. }
  114. }
  115. str = new StringBuilder();
  116. indentationBuffer = new StringBuilder();
  117. atLineStart = false;
  118. break;
  119. case '}':
  120. {
  121. if (inDoubleQuote || inSingleQuote) {
  122. str.Append(c);
  123. break;
  124. }
  125. var strf = FormatStr(str);
  126. if (!string.IsNullOrEmpty(strf))
  127. {
  128. curNode.AppendChildNode(new UniWebViewGradleContentNode(strf, curNode));
  129. }
  130. if (indentationBuffer.Length > 0)
  131. {
  132. curNode.ClosingWhitespace = indentationBuffer.ToString();
  133. }
  134. if (curNode.Parent != null) {
  135. curNode = curNode.Parent;
  136. }
  137. }
  138. str = new StringBuilder();
  139. indentationBuffer = new StringBuilder();
  140. atLineStart = false;
  141. break;
  142. case '\"':
  143. if (inDollarVariable) {
  144. str.Append(c);
  145. break;
  146. }
  147. inDoubleQuote = !inDoubleQuote;
  148. str.Append(c);
  149. atLineStart = false;
  150. break;
  151. case '\'':
  152. if (inDollarVariable) {
  153. str.Append(c);
  154. break;
  155. }
  156. inSingleQuote = !inSingleQuote;
  157. str.Append(c);
  158. atLineStart = false;
  159. break;
  160. case '$':
  161. {
  162. if (inDoubleQuote || inSingleQuote) {
  163. str.Append(c);
  164. break;
  165. }
  166. inDollarVariable = true;
  167. str.Append(c);
  168. atLineStart = false;
  169. break;
  170. }
  171. default:
  172. str.Append(c);
  173. if (!char.IsWhiteSpace(c))
  174. {
  175. atLineStart = false;
  176. }
  177. break;
  178. }
  179. }
  180. // End of file.
  181. var endline = FormatStr(str);
  182. if (!string.IsNullOrEmpty(endline))
  183. {
  184. if (curNode != null) {
  185. curNode.AppendChildNode(new UniWebViewGradleContentNode(endline, curNode));
  186. }
  187. }
  188. //Debug.Log("Gradle parse done!");
  189. }
  190. public void Save(string path = null)
  191. {
  192. if (path == null) {
  193. path = m_filePath;
  194. }
  195. File.WriteAllText(path, Print());
  196. }
  197. private static string FormatStr(StringBuilder sb)
  198. {
  199. var str = sb.ToString();
  200. str = str.Trim();
  201. return str;
  202. }
  203. private static string ExtractLeadingWhitespace(string raw)
  204. {
  205. if (string.IsNullOrEmpty(raw)) return string.Empty;
  206. var sb = new StringBuilder();
  207. foreach (var ch in raw)
  208. {
  209. if (ch == '\t')
  210. {
  211. sb.Append(" ");
  212. }
  213. else if (ch == ' ')
  214. {
  215. sb.Append(' ');
  216. }
  217. else if (!char.IsWhiteSpace(ch))
  218. {
  219. break;
  220. }
  221. }
  222. return sb.ToString();
  223. }
  224. public string Print()
  225. {
  226. StringBuilder sb = new StringBuilder();
  227. PrintNode(sb, Root, -1);
  228. // Remove the first empty line.
  229. sb.Remove(0, 1);
  230. if (sb.Length > 0 && sb[sb.Length - 1] == '\n')
  231. {
  232. sb.Remove(sb.Length - 1, 1);
  233. }
  234. return sb.ToString();
  235. }
  236. private string GetLevelIndent(int level)
  237. {
  238. if (level <= 0) return "";
  239. return new string(' ', level * 4);
  240. }
  241. private void PrintNode(StringBuilder stringBuilder, UniWebViewGradleNode node, int level)
  242. {
  243. if (node.Parent != null) {
  244. var indent = node.LeadingWhitespace ?? GetLevelIndent(level);
  245. if (node is UniWebViewGradleCommentNode)
  246. {
  247. stringBuilder.Append("\n" + indent + @"//" + node.Name);
  248. }
  249. else
  250. {
  251. stringBuilder.Append("\n" + indent + node.Name);
  252. }
  253. }
  254. if (node is UniWebViewGradleContentNode || node is UniWebViewGradleCommentNode) return;
  255. if (node.Parent != null) {
  256. stringBuilder.Append(" {");
  257. }
  258. foreach (var c in node.Children) {
  259. PrintNode(stringBuilder, c, level + 1);
  260. }
  261. if (node.Parent != null) {
  262. var indent = node.ClosingWhitespace ?? GetLevelIndent(level);
  263. stringBuilder.Append("\n" + indent + "}");
  264. }
  265. }
  266. }
  267. public class UniWebViewGradleNode
  268. {
  269. protected string m_name;
  270. public UniWebViewGradleNode Parent { get; private set; }
  271. public string Name => m_name;
  272. public string LeadingWhitespace { get; set; }
  273. public string ClosingWhitespace { get; set; }
  274. public List<UniWebViewGradleNode> Children { get; private set; } = new List<UniWebViewGradleNode>();
  275. public UniWebViewGradleNode(string name, UniWebViewGradleNode parent = null)
  276. {
  277. Parent = parent;
  278. m_name = name;
  279. }
  280. public void Each(Action<UniWebViewGradleNode> f)
  281. {
  282. f(this);
  283. foreach (var n in Children)
  284. {
  285. n.Each(f);
  286. }
  287. }
  288. public void AppendChildNode(UniWebViewGradleNode node)
  289. {
  290. if (Children == null) Children = new List<UniWebViewGradleNode>();
  291. Children.Add(node);
  292. node.Parent = this;
  293. }
  294. public UniWebViewGradleNode TryGetNode(string path)
  295. {
  296. var subpath = path.Split('/');
  297. var cnode = this;
  298. foreach (var p in subpath)
  299. {
  300. if (string.IsNullOrEmpty(p)) continue;
  301. var tnode = cnode.FindChildNodeByName(p);
  302. if (tnode == null)
  303. {
  304. Debug.Log("Can't find Node:" + p);
  305. return null;
  306. }
  307. cnode = tnode;
  308. }
  309. return cnode;
  310. }
  311. public UniWebViewGradleNode FindChildNodeByName(string name)
  312. {
  313. foreach (var n in Children)
  314. {
  315. if (n is UniWebViewGradleCommentNode || n is UniWebViewGradleContentNode)
  316. continue;
  317. if (n.Name == name)
  318. return n;
  319. }
  320. return null;
  321. }
  322. public bool ReplaceContentStartsWith(string pattern, string value)
  323. {
  324. foreach (var n in Children)
  325. {
  326. if (!(n is UniWebViewGradleContentNode)) continue;
  327. if (n.m_name.StartsWith(pattern))
  328. {
  329. n.m_name = value;
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. public UniWebViewGradleContentNode ReplaceContentOrAddStartsWith(string pattern, string value)
  336. {
  337. foreach (var n in Children)
  338. {
  339. if (!(n is UniWebViewGradleContentNode)) continue;
  340. if (n.m_name.StartsWith(pattern))
  341. {
  342. n.m_name = value;
  343. return (UniWebViewGradleContentNode)n;
  344. }
  345. }
  346. return AppendContentNode(value);
  347. }
  348. public UniWebViewGradleContentNode AppendContentNode(string content)
  349. {
  350. foreach (var n in Children)
  351. {
  352. if (!(n is UniWebViewGradleContentNode)) continue;
  353. if (n.m_name == content)
  354. {
  355. Debug.Log("UniWebViewGradleContentNode with " + content + " already exists!");
  356. return null;
  357. }
  358. }
  359. UniWebViewGradleContentNode cnode = new UniWebViewGradleContentNode(content, this);
  360. AppendChildNode(cnode);
  361. return cnode;
  362. }
  363. public bool RemoveContentNode(string contentPattern)
  364. {
  365. for(int i=0;i<Children.Count;i++)
  366. {
  367. if (!(Children[i] is UniWebViewGradleContentNode)) continue;
  368. if(Children[i].m_name.Contains(contentPattern))
  369. {
  370. Children.RemoveAt(i);
  371. return true;
  372. }
  373. }
  374. return false;
  375. }
  376. }
  377. public sealed class UniWebViewGradleContentNode : UniWebViewGradleNode
  378. {
  379. public UniWebViewGradleContentNode(String content, UniWebViewGradleNode parent) : base(content, parent)
  380. {
  381. }
  382. public void SetContent(string content)
  383. {
  384. m_name = content;
  385. }
  386. }
  387. public sealed class UniWebViewGradleCommentNode : UniWebViewGradleNode
  388. {
  389. public UniWebViewGradleCommentNode(String content, UniWebViewGradleNode parent) : base(content, parent)
  390. {
  391. }
  392. public string GetComment()
  393. {
  394. return m_name;
  395. }
  396. }