196 lines
7.2 KiB
Python
196 lines
7.2 KiB
Python
"""JNPF 模块目录结构和 pom.xml 生成器
|
||
|
||
从 code_generator.py 中提取的独立模块,负责:
|
||
- 创建 Maven 多模块项目目录结构
|
||
- 生成父 pom.xml 和子模块 (entity/biz/controller) pom.xml
|
||
|
||
使用方法:
|
||
from aibridge.utils.modulestructure import ModuleStructureCreator
|
||
from aibridge.config import JNPFConfig
|
||
|
||
config = JNPFConfig() # 或 config.jyzs.JNPFConfig()
|
||
creator = ModuleStructureCreator(
|
||
module_name="DataSourceConfig",
|
||
package="ssw.dr", # 模块包名
|
||
backend_base="/path/to/boot",
|
||
boot_parent="ssw-boot", # 父 pom artifactId
|
||
)
|
||
creator.create()
|
||
"""
|
||
|
||
import os
|
||
from typing import Optional
|
||
|
||
|
||
class ModuleStructureCreator:
|
||
"""创建 JNPF Maven 多模块项目的目录结构和 pom.xml"""
|
||
|
||
def __init__(
|
||
self,
|
||
module_name: str,
|
||
package: str,
|
||
backend_base: str,
|
||
boot_parent: str = "ssw-boot",
|
||
):
|
||
"""
|
||
Args:
|
||
module_name: 业务模块名(kebab-case),如 ssw-dr(artifactId)
|
||
package: Java 包名,如 ssw.dr
|
||
backend_base: 后端根目录,如 /path/to/boot
|
||
boot_parent: 父 pom 的 artifactId,如 ssw-boot
|
||
"""
|
||
self.module_name = module_name # 如 "ssw-dr"
|
||
self.package = package # 如 "ssw.dr"
|
||
self.backend_base = backend_base # 如 "/path/to/boot"
|
||
self.boot_parent = boot_parent # 如 "ssw-boot"
|
||
|
||
def create(self):
|
||
"""创建完整的模块目录结构和所有 pom.xml"""
|
||
base = self.backend_base
|
||
module = self.module_name
|
||
pkg = self.package
|
||
|
||
# 父模块
|
||
parent_dir = os.path.join(base, module)
|
||
os.makedirs(parent_dir, exist_ok=True)
|
||
self._write_file(os.path.join(parent_dir, 'pom.xml'), self._parent_pom())
|
||
|
||
# Entity 模块
|
||
entity_dir = os.path.join(parent_dir, f"{module}-entity", "src", "main", "java", *pkg.split('.'))
|
||
os.makedirs(os.path.join(entity_dir, 'entity'), exist_ok=True)
|
||
os.makedirs(os.path.join(entity_dir, 'model'), exist_ok=True)
|
||
self._write_file(os.path.join(parent_dir, f"{module}-entity", 'pom.xml'), self._entity_pom())
|
||
|
||
# Biz 模块
|
||
biz_dir = os.path.join(parent_dir, f"{module}-biz", "src", "main", "java")
|
||
os.makedirs(os.path.join(biz_dir, *pkg.split('.'), 'service'), exist_ok=True)
|
||
os.makedirs(os.path.join(biz_dir, 'jnpf', 'mapper'), exist_ok=True)
|
||
self._write_file(os.path.join(parent_dir, f"{module}-biz", 'pom.xml'), self._biz_pom())
|
||
|
||
# Controller 模块
|
||
controller_dir = os.path.join(parent_dir, f"{module}-controller", "src", "main", "java", *pkg.split('.'))
|
||
os.makedirs(os.path.join(controller_dir, 'controller'), exist_ok=True)
|
||
self._write_file(os.path.join(parent_dir, f"{module}-controller", 'pom.xml'), self._controller_pom())
|
||
|
||
def _write_file(self, path: str, content: str):
|
||
"""写文件(仅当文件不存在时)"""
|
||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||
if not os.path.exists(path):
|
||
with open(path, 'w', encoding='utf-8') as f:
|
||
f.write(content)
|
||
|
||
def _parent_pom(self) -> str:
|
||
return f'''<?xml version="1.0" encoding="UTF-8"?>
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||
<modelVersion>4.0.0</modelVersion>
|
||
<parent>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>{self.boot_parent}</artifactId>
|
||
<version>6.2.0-RELEASE</version>
|
||
</parent>
|
||
|
||
<artifactId>{self.module_name}</artifactId>
|
||
<packaging>pom</packaging>
|
||
<modules>
|
||
<module>{self.module_name}-entity</module>
|
||
<module>{self.module_name}-biz</module>
|
||
<module>{self.module_name}-controller</module>
|
||
</modules>
|
||
</project>
|
||
'''
|
||
|
||
def _entity_pom(self) -> str:
|
||
return f'''<?xml version="1.0" encoding="UTF-8"?>
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||
<parent>
|
||
<artifactId>{self.module_name}</artifactId>
|
||
<groupId>com.jnpf</groupId>
|
||
<version>6.2.0-RELEASE</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
<artifactId>{self.module_name}-entity</artifactId>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>jnpf-common-all</artifactId>
|
||
<version>${{project.version}}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>jnpf-generater-base</artifactId>
|
||
<version>${{project.version}}</version>
|
||
</dependency>
|
||
</dependencies>
|
||
</project>
|
||
'''
|
||
|
||
def _biz_pom(self) -> str:
|
||
return f'''<?xml version="1.0" encoding="UTF-8"?>
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||
<parent>
|
||
<artifactId>{self.module_name}</artifactId>
|
||
<groupId>com.jnpf</groupId>
|
||
<version>6.2.0-RELEASE</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
<artifactId>{self.module_name}-biz</artifactId>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>{self.module_name}-entity</artifactId>
|
||
<version>${{project.version}}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.alibaba</groupId>
|
||
<artifactId>fastjson</artifactId>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>org.apache.commons</groupId>
|
||
<artifactId>commons-lang3</artifactId>
|
||
<version>3.12.0</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>jnpf-common-all</artifactId>
|
||
<version>${{project.version}}</version>
|
||
</dependency>
|
||
</dependencies>
|
||
</project>
|
||
'''
|
||
|
||
def _controller_pom(self) -> str:
|
||
return f'''<?xml version="1.0" encoding="UTF-8"?>
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||
<parent>
|
||
<artifactId>{self.module_name}</artifactId>
|
||
<groupId>com.jnpf</groupId>
|
||
<version>6.2.0-RELEASE</version>
|
||
</parent>
|
||
<modelVersion>4.0.0</modelVersion>
|
||
<artifactId>{self.module_name}-controller</artifactId>
|
||
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>{self.module_name}-biz</artifactId>
|
||
<version>${{project.version}}</version>
|
||
</dependency>
|
||
<dependency>
|
||
<groupId>com.jnpf</groupId>
|
||
<artifactId>jnpf-common-all</artifactId>
|
||
<version>${{project.version}}</version>
|
||
</dependency>
|
||
</dependencies>
|
||
</project>
|
||
'''
|