GenerateAndroidProject.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python3
  2. # Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  3. # All rights reserved.
  4. # Code licensed under the BSD License.
  5. # http://www.anki3d.org/LICENSE
  6. import os
  7. import optparse
  8. import shutil
  9. import fileinput
  10. import sys
  11. class Context:
  12. target = ""
  13. asserts_dir = ""
  14. out_dir = ""
  15. def parse_commandline():
  16. """ Parse the command line arguments """
  17. parser = optparse.OptionParser(usage="usage: %prog [options]", description="Generate an Android gradle project")
  18. parser.add_option("-o", "--out-dir", dest="out_dir", type="string", help="Where to create the project")
  19. parser.add_option("-t", "--target", dest="target", type="string", help="The name of .so to package")
  20. parser.add_option("-a", "--assets", dest="assets", type="string", help="Assets directory")
  21. (options, args) = parser.parse_args()
  22. required = "target assets out_dir".split()
  23. for r in required:
  24. if options.__dict__[r] is None:
  25. parser.print_help()
  26. parser.error("parameter \"%s\" required" % r)
  27. ctx = Context()
  28. ctx.target = options.target
  29. ctx.asserts_dir = os.path.abspath(options.assets)
  30. ctx.out_dir = os.path.abspath(options.out_dir)
  31. return ctx
  32. def replace_in_file(filename, to_replace, to_replace_with):
  33. to_replace_with = to_replace_with.replace("\\", "\\\\")
  34. with fileinput.FileInput(filename, inplace=True) as file:
  35. for line in file:
  36. print(line.replace(to_replace, to_replace_with), end="")
  37. def main():
  38. """ The main """
  39. ctx = parse_commandline()
  40. this_script_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
  41. # Copy dir
  42. project_dir = os.path.join(ctx.out_dir, "AndroidProject_%s" % ctx.target)
  43. if not os.path.isdir(project_dir):
  44. shutil.copytree(this_script_dir, project_dir)
  45. # RM the script
  46. try:
  47. os.remove(os.path.join(project_dir, "GenerateAndroidProject.py"))
  48. except OSError:
  49. pass
  50. # Create the assets dir structure
  51. assets_dir = os.path.join(project_dir, "assets")
  52. if not os.path.isdir(assets_dir):
  53. os.mkdir(assets_dir)
  54. os.mkdir(os.path.join(project_dir, "assets/AnKi/"))
  55. os.symlink(os.path.join(this_script_dir, "../../AnKi/Shaders"),
  56. os.path.join(project_dir, "assets/AnKi/Shaders"))
  57. os.symlink(os.path.join(this_script_dir, "../../EngineAssets"),
  58. os.path.join(project_dir, "assets/EngineAssets"))
  59. os.mkdir(os.path.join(project_dir, "assets/ThirdParty/"))
  60. os.symlink(os.path.join(this_script_dir, "../../ThirdParty/FidelityFX"),
  61. os.path.join(project_dir, "assets/ThirdParty/FidelityFX"))
  62. os.symlink(ctx.asserts_dir, os.path.join(project_dir, "assets/Assets"))
  63. # Write the asset directory structure to a file
  64. dir_structure_file = open(os.path.join(assets_dir, "DirStructure.txt"), "w", newline="\n")
  65. for root, dirs, files in os.walk(assets_dir, followlinks=True):
  66. for f in files:
  67. if f.find("DirStructure.txt") >= 0:
  68. continue
  69. filename = os.path.join(root, f)
  70. filename = filename.replace(assets_dir, "")
  71. filename = filename.replace("\\", "/")
  72. if filename[0] == '/':
  73. filename = filename[1:]
  74. dir_structure_file.write("%s\n" % filename)
  75. dir_structure_file.close()
  76. # strings.xml
  77. replace_in_file(os.path.join(project_dir, "app/src/main/res/values/strings.xml"), "%APP_NAME%", ctx.target)
  78. # build.gradle
  79. build_gradle = os.path.join(project_dir, "app/build.gradle")
  80. replace_in_file(build_gradle, "%TARGET%", ctx.target)
  81. replace_in_file(build_gradle, "%PYTHON%", sys.executable)
  82. replace_in_file(build_gradle, "%CMAKE%", os.path.join(this_script_dir, "../../CMakeLists.txt"))
  83. # Manifest
  84. replace_in_file(os.path.join(project_dir, "app/src/main/AndroidManifest.xml"), "%TARGET%", ctx.target)
  85. # Done
  86. print("Generated project: %s" % project_dir)
  87. if __name__ == "__main__":
  88. main()