AndroidGradleTemplate.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited.All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Text;
  13. using System.Xml;
  14. using System.IO;
  15. using System.Linq.Expressions;
  16. using System.Collections.Generic;
  17. using UnityEngine;
  18. /// <summary> A list of the android. </summary>
  19. internal class AndroidGradleTemplate
  20. {
  21. private enum EPatcherType
  22. {
  23. EPT_NONE = 0,
  24. EPT_PLUGIN_VERSION = 1,
  25. EPT_ADD_DEPENDENCIES = 2,
  26. EPT_REMOVE_DEPENDENCIES = 3,
  27. EPT_ADD_SUPPORT = 4,
  28. }
  29. private interface IGradlePatcher
  30. {
  31. void PreprocessLine(string line);
  32. bool ProcessLine(string line, ref string result);
  33. }
  34. private class GradlePluginVersionPatcher : IGradlePatcher
  35. {
  36. const string PLUGIN_VERSION_TOKEN = "com.android.tools.build:gradle:";
  37. private int mMajorVersionNum;
  38. private int mMiddleVersionNum;
  39. private int mMinorVersionNum;
  40. public GradlePluginVersionPatcher()
  41. {
  42. mMajorVersionNum = 0;
  43. mMiddleVersionNum = 0;
  44. mMinorVersionNum = 0;
  45. }
  46. public void SetMinPluginVersion(int major, int middle, int minor)
  47. {
  48. mMajorVersionNum = major;
  49. mMiddleVersionNum = middle;
  50. mMinorVersionNum = minor;
  51. }
  52. public void PreprocessLine(string line)
  53. {
  54. }
  55. public bool ProcessLine(string line, ref string result)
  56. {
  57. bool updateVersion = false;
  58. var idx = line.IndexOf(PLUGIN_VERSION_TOKEN);
  59. if (idx > 0)
  60. {
  61. string subLine = line.Substring(idx + PLUGIN_VERSION_TOKEN.Length);
  62. string subVersion = subLine.Substring(0, subLine.IndexOf('\''));
  63. Debug.LogFormat("subVersion : {0}", subVersion);
  64. string[] versions = subVersion.Split('.');
  65. if (versions.Length == 3)
  66. {
  67. int.TryParse(versions[0], out int vMain);
  68. int.TryParse(versions[1], out int vMiddle);
  69. int.TryParse(versions[2], out int vMin);
  70. if (vMain < mMajorVersionNum)
  71. {
  72. updateVersion = true;
  73. }
  74. else if(vMain == mMajorVersionNum)
  75. {
  76. if(vMiddle < mMiddleVersionNum)
  77. {
  78. updateVersion = true;
  79. }
  80. else if(vMiddle == mMiddleVersionNum)
  81. {
  82. if(vMin < mMinorVersionNum)
  83. {
  84. updateVersion = true;
  85. }
  86. }
  87. }
  88. if (updateVersion)
  89. {
  90. result = line.Replace(subVersion, "3.4.3");
  91. Debug.LogFormat("update gradle setting : {0} --> {1}", subVersion, "3.4.3");
  92. }
  93. }
  94. }
  95. return updateVersion;
  96. }
  97. }
  98. private class GradleAddDependenciesPatcher : IGradlePatcher
  99. {
  100. const string DEPS_MARK = "**DEPS**";
  101. private List<string> mDependencies;
  102. public GradleAddDependenciesPatcher()
  103. {
  104. mDependencies = new List<string>();
  105. }
  106. public void AddDependency(string dependency)
  107. {
  108. mDependencies.Add(dependency);
  109. }
  110. public void PreprocessLine(string line)
  111. {
  112. for(int i = mDependencies.Count - 1; i >= 0; i--)
  113. {
  114. if(line.Contains(mDependencies[i]))
  115. {
  116. //this dependency is already in the gradle file
  117. mDependencies.RemoveAt(i);
  118. }
  119. }
  120. }
  121. public bool ProcessLine(string line, ref string result)
  122. {
  123. if(mDependencies.Count > 0 && line.Contains(DEPS_MARK))
  124. {
  125. result = " " + string.Join("\n ", mDependencies);
  126. result = result + "\n" + line;
  127. return true;
  128. }
  129. return false;
  130. }
  131. }
  132. private class GradleRemoveDependenciesPatcher : IGradlePatcher
  133. {
  134. private List<string> mDependencies;
  135. public GradleRemoveDependenciesPatcher()
  136. {
  137. mDependencies = new List<string>();
  138. }
  139. public void RemoveDependency(string dependency)
  140. {
  141. mDependencies.Add(dependency);
  142. }
  143. public void PreprocessLine(string line)
  144. {
  145. }
  146. public bool ProcessLine(string line, ref string result)
  147. {
  148. bool includeDeps = false;
  149. for (int i = 0; i < mDependencies.Count; i++)
  150. {
  151. if (line.Contains(mDependencies[i]))
  152. {
  153. includeDeps = true;
  154. //remove this line
  155. result = null;
  156. break;
  157. }
  158. }
  159. return includeDeps;
  160. }
  161. }
  162. private class GradleAddSupportPatcher : IGradlePatcher
  163. {
  164. private Dictionary<string, bool> mKeyTokenAlreadyInFile = new Dictionary<string, bool>();
  165. private bool mIsFirst = true;
  166. private List<string> mTokenList = new List<string>();
  167. public void PreprocessLine(string line)
  168. {
  169. for(int i = 0; i < mTokenList.Count; i++)
  170. {
  171. var token = mTokenList[i];
  172. if (line.Contains(token))
  173. {
  174. mKeyTokenAlreadyInFile[token] = true;
  175. }
  176. }
  177. }
  178. public bool ProcessLine(string line, ref string result)
  179. {
  180. string tempLine = "";
  181. if (mIsFirst)
  182. {
  183. tempLine = GetSupportStringNotInFile();
  184. mIsFirst = false;
  185. }
  186. foreach (var pair in mKeyTokenAlreadyInFile)
  187. {
  188. if (pair.Value)
  189. {
  190. if (line.Contains(pair.Key))
  191. {
  192. result = tempLine + pair.Key + "=true";
  193. return true;
  194. }
  195. }
  196. }
  197. result = tempLine + line;
  198. return false;
  199. }
  200. private string GetSupportStringNotInFile()
  201. {
  202. string line = "";
  203. foreach (var pair in mKeyTokenAlreadyInFile)
  204. {
  205. if (!pair.Value)
  206. {
  207. line = string.Format("{0}=true\n{1}", pair.Key, line);
  208. }
  209. }
  210. return line;
  211. }
  212. public void AddSupport(string keyToken)
  213. {
  214. mKeyTokenAlreadyInFile.Add(keyToken, false);
  215. mTokenList.Add(keyToken);
  216. }
  217. }
  218. Dictionary<EPatcherType, IGradlePatcher> mPatchers = null;
  219. string m_Path;
  220. public AndroidGradleTemplate(string path)
  221. {
  222. m_Path = path;
  223. mPatchers = new Dictionary<EPatcherType, IGradlePatcher>();
  224. }
  225. private T GetOrAddPatcher<T>(EPatcherType type) where T : IGradlePatcher, new()
  226. {
  227. if (!mPatchers.TryGetValue(type, out IGradlePatcher patcher))
  228. {
  229. patcher = new T();
  230. mPatchers.Add(type, patcher);
  231. }
  232. return (T)patcher;
  233. }
  234. public void SetMinPluginVersion(int major, int middle, int minor)
  235. {
  236. GradlePluginVersionPatcher pluginVersionPatcher = GetOrAddPatcher<GradlePluginVersionPatcher>(
  237. EPatcherType.EPT_PLUGIN_VERSION);
  238. pluginVersionPatcher.SetMinPluginVersion(major, middle, minor);
  239. }
  240. public void AddDenpendency(string dependency)
  241. {
  242. GradleAddDependenciesPatcher addDepPatcher = GetOrAddPatcher<GradleAddDependenciesPatcher>(
  243. EPatcherType.EPT_ADD_DEPENDENCIES);
  244. addDepPatcher.AddDependency(dependency);
  245. }
  246. public void RemoveDependency(string dependency)
  247. {
  248. GradleRemoveDependenciesPatcher removeDepPatcher = GetOrAddPatcher<GradleRemoveDependenciesPatcher>(
  249. EPatcherType.EPT_REMOVE_DEPENDENCIES);
  250. removeDepPatcher.RemoveDependency(dependency);
  251. }
  252. public void AddSupport(string keyToken)
  253. {
  254. GradleAddSupportPatcher addSupportPatcher = GetOrAddPatcher<GradleAddSupportPatcher>(
  255. EPatcherType.EPT_ADD_SUPPORT);
  256. addSupportPatcher.AddSupport(keyToken);
  257. }
  258. public void PreprocessGradleFile()
  259. {
  260. if (mPatchers.Count <= 0)
  261. return;
  262. try
  263. {
  264. List<string> content = new List<string>();
  265. var lines = File.ReadAllLines(m_Path);
  266. string newLine = null;
  267. foreach (string line in lines)
  268. {
  269. foreach (var pair in mPatchers)
  270. {
  271. pair.Value.PreprocessLine(line);
  272. }
  273. }
  274. foreach (string line in lines)
  275. {
  276. newLine = line;
  277. foreach (var pair in mPatchers)
  278. {
  279. if(pair.Value.ProcessLine(line, ref newLine))
  280. {
  281. break;
  282. }
  283. }
  284. //Original line may be empty, not null
  285. if (newLine != null)
  286. {
  287. content.Add(newLine);
  288. }
  289. }
  290. File.WriteAllLines(m_Path, content);
  291. }
  292. catch (Exception ex)
  293. {
  294. Debug.LogErrorFormat("PreprocessGradleFile exception : {0}", ex.Message);
  295. }
  296. }
  297. }
  298. }