Henry vor 1 Jahr
Commit
e785761d51
5 geänderte Dateien mit 218 neuen und 0 gelöschten Zeilen
  1. 76 0
      README.md
  2. 142 0
      gradleparser.py
  3. BIN
      image-1.png
  4. BIN
      image-2.png
  5. BIN
      image.png

+ 76 - 0
README.md

@@ -0,0 +1,76 @@
+# 项目概述
+
+使用 OpenAI 的 GPT-3.5-turbo 模型,自动更新 Gradle 配置文件,以满足特定的配置要求。项目提供了两种配置提示(prompt_Project 和 prompt_Module),分别用于项目级和模块级的 Gradle 配置更新。
+
+# 使用说明
+
+## 安装依赖
+
+确保已安装 openai 库。如果尚未安装 openai 库,可以通过以下命令安装:
+
+```bash
+pip install openai
+```
+
+## 配置 OpenAI API 密钥
+
+在代码中将 api_key 替换为你的 OpenAI API 密钥:
+
+```python
+client = OpenAI(
+    base_url='YOUR_BASE_URL',
+    api_key='YOUR_API_KEY'
+)
+```
+
+## 运行脚本
+
+使用命令行运行脚本,并根据需要提供参数:
+
+指定要更新的项目路径。
+
+
+例如:
+
+```bash
+python gradleparser.py -p --path path/to/your/build.gradle
+```
+
+## 项目说明
+
+本项目旨在为 Android 项目中的 Gradle 配置文件提供自动化更新功能。通过识别项目中的 Gradle 文件,并根据预设的配置要求,自动修改相关配置项,以确保项目符合特定的开发环境和依赖要求。
+
+### Gradle文件识别
+
+遍历指定项目路径下的所有文件,查找以 .gradle 为后缀的 Gradle 配置文件,并将文件路径存储在列表中,方便后续处理。
+
+![alt text](image.png)
+
+### 模块级 Gradle 配置更新
+
+针对项目中的模块级 Gradle 文件(通常位于各模块目录下的 build.gradle 文件),根据预设的配置要求,调用 OpenAI 的 GPT 模型,生成更新后的配置内容,并将其写回原文件,实现配置的自动更新。
+
+#### Prompt关键点
+
++ 若存在 ndkVersion,将其修改为 "23.2.0"
++ 在 android 部分的 defaultConfig 中的 externalNativeBuild 块内,添加 abiFilters 'riscv64' 配置。
++ 除上述要求外,不对其他内容进行修改,也不添加未提及的配置
+
+#### 修改示例
+
+![alt text](image-1.png)
+
+### 项目级 Gradle 配置更新
+
+针对项目根目录下的 build.gradle 文件,根据预设的配置要求,同样调用 OpenAI 的 GPT 模型,生成更新后的配置内容,并写回原文件,以更新项目级的 Gradle 配置。
+
+#### Prompt关键点
+
++ 若存在 buildscript 部分,将 Gradle 插件版本更新为 com.android.tools.build:gradle:7.4.0-dev,并确保插件路径正确。
++ 若存在 ndkVersion,将其修改为 "23.2.0"。
++ 若文件包含 buildscript 和 allprojects 部分,在 buildscript 的 repositories 中添加 "maven { url 'C:\\Users\\zh907\\AppData\\Local\\Android\\plugin\\repo' }" 配置;在 allprojects 的 repositories 中也添加该配置。
++ 除上述要求外,不对其他内容进行修改,也不添加未提及的配置,且若文件本身没有 buildscript 部分,则不添加该部分。
+
+#### 修改示例
+
+![alt text](image-2.png)

+ 142 - 0
gradleparser.py

@@ -0,0 +1,142 @@
+import os
+from openai import OpenAI
+
+def identify_gradle_files(project_path):
+    gradle_files = []
+    for root, dirs, files in os.walk(project_path):
+        for file in files:
+            if file.endswith('.gradle'):
+                gradle_files.append(os.path.join(root, file))
+    return gradle_files
+
+def read_gradle_file(gradle_file_path):
+    with open(gradle_file_path, 'r') as file:
+        content = file.readlines()
+    return content
+
+def update_gradle_config_moudle(path):
+    client = OpenAI(
+        base_url='https://xiaoai.plus/v1',
+        api_key='sk-locN2LBEu7eEPV6NT3822YJhTAe0TSXMkzzM90cmALL3tsVI'
+    )
+    prompt = """
+    ## Configuration Requirements
+
+    ### 1. Change ndkVersion to "23.2.0" if ndkVersion exists
+
+    ### 2. Configure ABI Filters
+    - **Instruction**: Add the `abiFilters` configuration to the `externalNativeBuild` block within the 'defaultConfig' of `android` section.
+    - **Example Code**:
+    ```gradle
+    android {
+        // Other configurations...
+
+        externalNativeBuild {
+            ndk {
+                abiFilters 'riscv64'
+            }
+        }
+
+        // Other configurations...
+    }
+
+    ### 5. Do not change others if the Requirements do not mention
+
+    ### 6. Do not add anything that do not mention
+    """
+    
+    text = ""
+
+    with open(path, 'r', encoding='utf-8') as file:
+        text = file.read()
+
+    direct = "请你直接将修改后的文件输出,除此之外不用其他描述"
+
+    completion = client.chat.completions.create(
+    model="gpt-3.5-turbo",
+    messages=[
+        {"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."},
+        {"role": "system", "content": prompt},
+        {"role": "user", "content": text + direct}
+    ]
+    )
+
+    content =  completion.choices[0].message.content
+
+    with open(path, 'w', encoding='utf-8') as file:
+        file.write(content)
+    
+    with open(path, 'r', encoding='utf-8') as file:
+        new_content = file.read()
+    
+    new_content = new_content.replace("gradle\n", "")
+    new_content = new_content.replace("```", "")
+
+    with open(path, 'w', encoding='utf-8') as file:
+        file.write(new_content) 
+    
+def update_gradle_config_project(path):
+    client = OpenAI(
+        base_url='https://xiaoai.plus/v1',
+        api_key='sk-locN2LBEu7eEPV6NT3822YJhTAe0TSXMkzzM90cmALL3tsVI'
+    )
+    prompt = """
+    ## Configuration Requirements
+
+    ### 1. Update Gradle Plugin Version
+    - **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.
+
+    ### 2. Change ndkVersion to "23.2.0" if ndkVersion exists
+
+    ### 3. Configure the Local Maven Repository
+    - **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.
+
+    ### 4. Do not change others if the Requirements do not mention
+
+    ### 5. Do not add 'buildscript' part if the file do not have
+    """
+    
+    text = ""
+
+    with open(path, 'r', encoding='utf-8') as file:
+        text = file.read()
+
+    direct = "请你直接将修改后的文件输出,除此之外不用其他描述"
+
+    completion = client.chat.completions.create(
+    model="gpt-3.5-turbo",
+    messages=[
+        {"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."},
+        {"role": "system", "content": prompt},
+        {"role": "user", "content": text + direct}
+    ]
+    )
+
+    content =  completion.choices[0].message.content
+
+    with open(path, 'w', encoding='utf-8') as file:
+        file.write(content)
+    
+    with open(path, 'r', encoding='utf-8') as file:
+        new_content = file.read()
+    
+    new_content = new_content.replace("gradle\n", "")
+    new_content = new_content.replace("```", "")
+
+    with open(path, 'w', encoding='utf-8') as file:
+        file.write(new_content) 
+    
+def main():
+    project_path = input("请输入项目路径:")
+    gradle_files = identify_gradle_files(project_path)
+    print("项目中的Gradle文件:")
+    for file in gradle_files:
+        print(file)
+        if file == project_path + '\\build.gradle':
+            update_gradle_config_project(file)
+        elif file.endswith('build.gradle'):
+            update_gradle_config_moudle(file)
+     
+
+if __name__ == '__main__':
+    main()

BIN
image-1.png


BIN
image-2.png


BIN
image.png