gradleparser.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import os
  2. from openai import OpenAI
  3. def identify_gradle_files(project_path):
  4. gradle_files = []
  5. for root, dirs, files in os.walk(project_path):
  6. for file in files:
  7. if file.endswith('.gradle'):
  8. gradle_files.append(os.path.join(root, file))
  9. return gradle_files
  10. def read_gradle_file(gradle_file_path):
  11. with open(gradle_file_path, 'r') as file:
  12. content = file.readlines()
  13. return content
  14. def update_gradle_config_moudle(path):
  15. client = OpenAI(
  16. base_url='https://xiaoai.plus/v1',
  17. api_key='sk-locN2LBEu7eEPV6NT3822YJhTAe0TSXMkzzM90cmALL3tsVI'
  18. )
  19. prompt = """
  20. ## Configuration Requirements
  21. ### 1. Change ndkVersion to "23.2.0" if ndkVersion exists
  22. ### 2. Configure ABI Filters
  23. - **Instruction**: Add the `abiFilters` configuration to the `externalNativeBuild` block within the 'defaultConfig' of `android` section.
  24. - **Example Code**:
  25. ```gradle
  26. android {
  27. // Other configurations...
  28. externalNativeBuild {
  29. ndk {
  30. abiFilters 'riscv64'
  31. }
  32. }
  33. // Other configurations...
  34. }
  35. ### 5. Do not change others if the Requirements do not mention
  36. ### 6. Do not add anything that do not mention
  37. """
  38. text = ""
  39. with open(path, 'r', encoding='utf-8') as file:
  40. text = file.read()
  41. direct = "请你直接将修改后的文件输出,除此之外不用其他描述"
  42. completion = client.chat.completions.create(
  43. model="gpt-3.5-turbo",
  44. messages=[
  45. {"role": "system", "content": "You are an AI assistant tasked with updating Gradle configuration files for a project. The goal is to ensure that the project is set up correctly with the specified configurations."},
  46. {"role": "system", "content": prompt},
  47. {"role": "user", "content": text + direct}
  48. ]
  49. )
  50. content = completion.choices[0].message.content
  51. with open(path, 'w', encoding='utf-8') as file:
  52. file.write(content)
  53. with open(path, 'r', encoding='utf-8') as file:
  54. new_content = file.read()
  55. new_content = new_content.replace("gradle\n", "")
  56. new_content = new_content.replace("```", "")
  57. with open(path, 'w', encoding='utf-8') as file:
  58. file.write(new_content)
  59. def update_gradle_config_project(path):
  60. client = OpenAI(
  61. base_url='https://xiaoai.plus/v1',
  62. api_key='sk-locN2LBEu7eEPV6NT3822YJhTAe0TSXMkzzM90cmALL3tsVI'
  63. )
  64. prompt = """
  65. ## Configuration Requirements
  66. ### 1. Update Gradle Plugin Version
  67. - **Instruction**: If the `buildscript` exit, Set the Gradle plugin version to `com.android.tools.build:gradle:7.4.0-dev` and ensure the plugin path is correctly specified in the environment.
  68. ### 2. Change ndkVersion to "23.2.0" if ndkVersion exists
  69. ### 3. Configure the Local Maven Repository
  70. - **Instruction**: If the file does not contain 'buildscript' and 'allprojects' sections, skip this rule.Add the "maven { url 'C:\\Users\\zh907\\AppData\\Local\\Android\\plugin\\repo' }" configuration to 'repositories' part of the `buildscript` and `allprojects` sections of the Gradle file.
  71. ### 4. Do not change others if the Requirements do not mention
  72. ### 5. Do not add 'buildscript' part if the file do not have
  73. """
  74. text = ""
  75. with open(path, 'r', encoding='utf-8') as file:
  76. text = file.read()
  77. direct = "请你直接将修改后的文件输出,除此之外不用其他描述"
  78. completion = client.chat.completions.create(
  79. model="gpt-3.5-turbo",
  80. messages=[
  81. {"role": "system", "content": "You are an AI assistant tasked with updating Gradle configuration files for a project. The goal is to ensure that the project is set up correctly with the specified configurations."},
  82. {"role": "system", "content": prompt},
  83. {"role": "user", "content": text + direct}
  84. ]
  85. )
  86. content = completion.choices[0].message.content
  87. with open(path, 'w', encoding='utf-8') as file:
  88. file.write(content)
  89. with open(path, 'r', encoding='utf-8') as file:
  90. new_content = file.read()
  91. new_content = new_content.replace("gradle\n", "")
  92. new_content = new_content.replace("```", "")
  93. with open(path, 'w', encoding='utf-8') as file:
  94. file.write(new_content)
  95. def main():
  96. project_path = input("请输入项目路径:")
  97. gradle_files = identify_gradle_files(project_path)
  98. print("项目中的Gradle文件:")
  99. for file in gradle_files:
  100. print(file)
  101. if file == project_path + '\\build.gradle':
  102. update_gradle_config_project(file)
  103. elif file.endswith('build.gradle'):
  104. update_gradle_config_moudle(file)
  105. if __name__ == '__main__':
  106. main()