GenerateAndroidProject.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/python
  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. # Copy dir
  41. project_dir = os.path.join(ctx.out_dir, "AndroidProject_%s" % ctx.target)
  42. if os.path.isdir(project_dir):
  43. raise Exception("Directory already exists: %s" % project_dir)
  44. this_script_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
  45. shutil.copytree(this_script_dir, project_dir)
  46. # RM the script
  47. os.remove(os.path.join(project_dir, "GenerateAndroidProject.py"))
  48. # Create the assets dir structure
  49. assets_dir = os.path.join(project_dir, "assets")
  50. os.mkdir(assets_dir)
  51. os.mkdir(os.path.join(project_dir, "assets/AnKi/"))
  52. os.symlink(os.path.join(this_script_dir, "../../AnKi/Shaders"), os.path.join(project_dir, "assets/AnKi/Shaders"))
  53. os.symlink(os.path.join(this_script_dir, "../../EngineAssets"), os.path.join(project_dir, "assets/EngineAssets"))
  54. os.mkdir(os.path.join(project_dir, "assets/ThirdParty/"))
  55. os.symlink(os.path.join(this_script_dir, "../../ThirdParty/Fsr"), os.path.join(project_dir,
  56. "assets/ThirdParty/Fsr"))
  57. os.symlink(ctx.asserts_dir, os.path.join(project_dir, "assets/Assets"))
  58. # Write the asset directory structure to a file
  59. dir_structure_file = open(os.path.join(assets_dir, "DirStructure.txt"), "w", newline="\n")
  60. for root, dirs, files in os.walk(assets_dir, followlinks=True):
  61. for f in files:
  62. if f.find("DirStructure.txt") >= 0:
  63. continue
  64. filename = os.path.join(root, f)
  65. filename = filename.replace(assets_dir, "")
  66. filename = filename.replace("\\", "/")
  67. if filename[0] == '/':
  68. filename = filename[1:]
  69. dir_structure_file.write("%s\n" % filename)
  70. dir_structure_file.close()
  71. # strings.xml
  72. replace_in_file(os.path.join(project_dir, "app/src/main/res/values/strings.xml"), "%APP_NAME%", ctx.target)
  73. # build.gradle
  74. build_gradle = os.path.join(project_dir, "app/build.gradle")
  75. replace_in_file(build_gradle, "%TARGET%", ctx.target)
  76. replace_in_file(build_gradle, "%PYTHON%", sys.executable)
  77. replace_in_file(build_gradle, "%CMAKE%", os.path.join(this_script_dir, "../../CMakeLists.txt"))
  78. # Manifest
  79. replace_in_file(os.path.join(project_dir, "app/src/main/AndroidManifest.xml"), "%TARGET%", ctx.target)
  80. # Done
  81. print("Generated project: %s" % project_dir)
  82. if __name__ == "__main__":
  83. main()