214 lines
11 KiB
Python
214 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""Migrate domain-bound packages from fastbee-common to fastbee-domain."""
|
|
from __future__ import annotations
|
|
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
COMMON_JAVA = ROOT / "fastbee-common" / "src" / "main" / "java"
|
|
DOMAIN_JAVA = ROOT / "fastbee-domain" / "src" / "main" / "java"
|
|
|
|
|
|
def read_java_text(path: Path) -> str:
|
|
raw = path.read_bytes()
|
|
for enc in ("utf-8", "gb18030", "gbk"):
|
|
try:
|
|
return raw.decode(enc)
|
|
except UnicodeDecodeError:
|
|
continue
|
|
return raw.decode("utf-8", errors="replace")
|
|
|
|
|
|
# (src_dir under COMMON_JAVA, dest_dir under DOMAIN_JAVA, old_pkg_prefix, new_pkg_prefix)
|
|
TREES = [
|
|
("com/fastbee/common/core/notify", "com/fastbee/domain/notify", "com.fastbee.common.core.notify", "com.fastbee.domain.notify"),
|
|
("com/fastbee/common/core/thingsModel", "com/fastbee/domain/thingsmodel", "com.fastbee.common.core.thingsModel", "com.fastbee.domain.thingsmodel"),
|
|
("com/fastbee/common/wechat", "com/fastbee/domain/wechat", "com.fastbee.common.wechat", "com.fastbee.domain.wechat"),
|
|
("com/fastbee/common/core/mq", "com/fastbee/domain/mq", "com.fastbee.common.core.mq", "com.fastbee.domain.mq"),
|
|
("com/fastbee/common/enums/scenemodel", "com/fastbee/domain/enums/scenemodel", "com.fastbee.common.enums.scenemodel", "com.fastbee.domain.enums.scenemodel"),
|
|
]
|
|
|
|
SINGLE_FILES = [
|
|
("com/fastbee/common/core/text/IntArrayValuable.java", "com/fastbee/domain/support/IntArrayValuable.java"),
|
|
("com/fastbee/common/constant/HttpStatus.java", "com/fastbee/domain/constant/HttpStatus.java"),
|
|
("com/fastbee/common/exception/ErrorCode.java", "com/fastbee/domain/exception/ErrorCode.java"),
|
|
("com/fastbee/common/enums/GlobalErrorCodeConstants.java", "com/fastbee/domain/exception/GlobalErrorCodeConstants.java"),
|
|
("com/fastbee/common/core/domain/BaseDO.java", "com/fastbee/domain/base/BaseDO.java"),
|
|
("com/fastbee/common/core/domain/TenantBaseDO.java", "com/fastbee/domain/base/TenantBaseDO.java"),
|
|
("com/fastbee/common/core/domain/TreeEntity.java", "com/fastbee/domain/base/TreeEntity.java"),
|
|
("com/fastbee/common/core/domain/PageParam.java", "com/fastbee/domain/dto/page/PageParam.java"),
|
|
("com/fastbee/common/core/domain/PageResult.java", "com/fastbee/domain/dto/page/PageResult.java"),
|
|
("com/fastbee/common/core/domain/SortingField.java", "com/fastbee/domain/dto/page/SortingField.java"),
|
|
("com/fastbee/common/core/domain/TreeSelect.java", "com/fastbee/domain/dto/tree/TreeSelect.java"),
|
|
("com/fastbee/common/core/domain/ImportExcelVO.java", "com/fastbee/domain/dto/excel/ImportExcelVO.java"),
|
|
("com/fastbee/common/core/domain/OutputExcelVO.java", "com/fastbee/domain/dto/excel/OutputExcelVO.java"),
|
|
("com/fastbee/common/enums/IErrorCode.java", "com/fastbee/domain/enums/IErrorCode.java"),
|
|
]
|
|
|
|
|
|
def patch_after_prefix_replace(text: str) -> str:
|
|
text = text.replace("import com.fastbee.common.utils.DateUtils;\n", "")
|
|
text = text.replace(
|
|
"this.ts != null ? ts : DateUtils.getNowDate()",
|
|
"this.ts != null ? ts : new java.util.Date()",
|
|
)
|
|
# TreeEntity source already uses com.fastbee.domain.BaseEntity
|
|
return text
|
|
|
|
|
|
def rewrite_file(text: str, old_pre: str, new_pre: str) -> str:
|
|
return patch_replace(text, old_pre, new_pre)
|
|
|
|
|
|
def patch_replace(text: str, old_pre: str, new_pre: str) -> str:
|
|
return patch_after_prefix_replace(text.replace(old_pre, new_pre))
|
|
|
|
|
|
def copy_tree(src_dir: str, dest_dir: str, old_pre: str, new_pre: str):
|
|
s = COMMON_JAVA / src_dir
|
|
if not s.exists():
|
|
print("SKIP tree", s)
|
|
return
|
|
for f in sorted(s.rglob("*.java")):
|
|
rel = f.relative_to(s)
|
|
d = DOMAIN_JAVA / dest_dir / rel
|
|
d.parent.mkdir(parents=True, exist_ok=True)
|
|
text = read_java_text(f)
|
|
text = patch_replace(text, old_pre, new_pre)
|
|
d.write_text(text, encoding="utf-8")
|
|
print("->", d.relative_to(ROOT))
|
|
|
|
|
|
def copy_single_fixed():
|
|
for src_rel, dest_rel in SINGLE_FILES:
|
|
s = COMMON_JAVA / src_rel
|
|
d = DOMAIN_JAVA / dest_rel
|
|
if not s.exists():
|
|
print("SKIP", s)
|
|
continue
|
|
d.parent.mkdir(parents=True, exist_ok=True)
|
|
text = read_java_text(s)
|
|
if "GlobalErrorCodeConstants" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.enums", "com.fastbee.domain.exception")
|
|
elif "IntArrayValuable" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.text", "com.fastbee.domain.support")
|
|
elif "HttpStatus" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.constant", "com.fastbee.domain.constant")
|
|
elif "ErrorCode" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.exception", "com.fastbee.domain.exception")
|
|
elif "IErrorCode" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.enums", "com.fastbee.domain.enums")
|
|
elif "BaseDO.java" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.domain", "com.fastbee.domain.base")
|
|
elif "TenantBaseDO" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.domain", "com.fastbee.domain.base")
|
|
elif "TreeEntity" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.domain", "com.fastbee.domain.base")
|
|
elif "PageParam" in src_rel or "PageResult" in src_rel or "SortingField" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.domain", "com.fastbee.domain.dto.page")
|
|
elif "TreeSelect" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.domain", "com.fastbee.domain.dto.tree")
|
|
elif "ImportExcelVO" in src_rel or "OutputExcelVO" in src_rel:
|
|
text = patch_replace(text, "com.fastbee.common.core.domain", "com.fastbee.domain.dto.excel")
|
|
else:
|
|
text = patch_after_prefix_replace(text)
|
|
d.write_text(text, encoding="utf-8")
|
|
print("->", d.relative_to(ROOT))
|
|
|
|
|
|
def move_plain_enums():
|
|
ed = COMMON_JAVA / "com/fastbee/common/enums"
|
|
if not ed.exists():
|
|
return
|
|
for f in sorted(ed.glob("*.java")):
|
|
if f.name in ("NotifyChannelProviderEnum.java", "GlobalErrorCodeConstants.java"):
|
|
continue
|
|
d = DOMAIN_JAVA / "com/fastbee/domain/enums" / f.name
|
|
d.parent.mkdir(parents=True, exist_ok=True)
|
|
text = read_java_text(f)
|
|
text = patch_replace(text, "com.fastbee.common.enums", "com.fastbee.domain.enums")
|
|
text = text.replace("import com.fastbee.common.core.text.IntArrayValuable;", "import com.fastbee.domain.support.IntArrayValuable;")
|
|
text = text.replace("import com.fastbee.common.constant.HttpStatus;", "import com.fastbee.domain.constant.HttpStatus;")
|
|
text = text.replace("import com.fastbee.common.exception.ErrorCode;", "import com.fastbee.domain.exception.ErrorCode;")
|
|
d.write_text(text, encoding="utf-8")
|
|
print("enum ->", d.relative_to(ROOT))
|
|
|
|
|
|
def delete_sources():
|
|
for src_dir, _, _, _ in TREES:
|
|
p = COMMON_JAVA / src_dir
|
|
if p.exists():
|
|
shutil.rmtree(p)
|
|
print("del tree", p)
|
|
for src_rel, _ in SINGLE_FILES:
|
|
p = COMMON_JAVA / src_rel
|
|
if p.exists():
|
|
p.unlink()
|
|
print("del", p)
|
|
ed = COMMON_JAVA / "com/fastbee/common/enums"
|
|
if ed.exists():
|
|
for f in ed.glob("*.java"):
|
|
if f.name != "NotifyChannelProviderEnum.java":
|
|
f.unlink()
|
|
print("del", f)
|
|
|
|
|
|
def global_import_fix():
|
|
reps = [
|
|
("import com.fastbee.common.enums.", "import com.fastbee.domain.enums."),
|
|
("import com.fastbee.common.core.mq.", "import com.fastbee.domain.mq."),
|
|
("import com.fastbee.common.core.notify.", "import com.fastbee.domain.notify."),
|
|
("import com.fastbee.common.core.thingsModel.", "import com.fastbee.domain.thingsmodel."),
|
|
("import com.fastbee.common.wechat.", "import com.fastbee.domain.wechat."),
|
|
("import com.fastbee.common.core.domain.PageParam;", "import com.fastbee.domain.dto.page.PageParam;"),
|
|
("import com.fastbee.common.core.domain.PageResult;", "import com.fastbee.domain.dto.page.PageResult;"),
|
|
("import com.fastbee.common.core.domain.SortingField;", "import com.fastbee.domain.dto.page.SortingField;"),
|
|
("import com.fastbee.common.core.domain.TreeSelect;", "import com.fastbee.domain.dto.tree.TreeSelect;"),
|
|
("import com.fastbee.common.core.domain.ImportExcelVO;", "import com.fastbee.domain.dto.excel.ImportExcelVO;"),
|
|
("import com.fastbee.common.core.domain.OutputExcelVO;", "import com.fastbee.domain.dto.excel.OutputExcelVO;"),
|
|
("import com.fastbee.common.core.domain.BaseDO;", "import com.fastbee.domain.base.BaseDO;"),
|
|
("import com.fastbee.common.core.domain.TenantBaseDO;", "import com.fastbee.domain.base.TenantBaseDO;"),
|
|
("import com.fastbee.common.core.domain.TreeEntity;", "import com.fastbee.domain.base.TreeEntity;"),
|
|
("import com.fastbee.common.constant.HttpStatus;", "import com.fastbee.domain.constant.HttpStatus;"),
|
|
("import com.fastbee.common.exception.ErrorCode;", "import com.fastbee.domain.exception.ErrorCode;"),
|
|
("import com.fastbee.common.core.text.IntArrayValuable;", "import com.fastbee.domain.support.IntArrayValuable;"),
|
|
("import com.fastbee.common.enums.GlobalErrorCodeConstants;", "import com.fastbee.domain.exception.GlobalErrorCodeConstants;"),
|
|
("import com.fastbee.common.core.domain.BaseEntity;", "import com.fastbee.domain.BaseEntity;"),
|
|
]
|
|
for p in ROOT.rglob("*.java"):
|
|
if "target" in p.parts:
|
|
continue
|
|
if p.name == "NotifyChannelProviderEnum.java":
|
|
text = read_java_text(p)
|
|
text2 = text.replace("import com.fastbee.common.core.notify.", "import com.fastbee.domain.notify.")
|
|
text2 = text2.replace("import com.fastbee.common.utils.StringUtils;", "import org.apache.commons.lang3.StringUtils;")
|
|
if text2 != text:
|
|
p.write_text(text2, encoding="utf-8")
|
|
print("fix imports", p.relative_to(ROOT))
|
|
continue
|
|
text = read_java_text(p)
|
|
orig = text
|
|
for a, b in reps:
|
|
text = text.replace(a, b)
|
|
text = text.replace(
|
|
"com.fastbee.common.enums.GlobalErrorCodeConstants",
|
|
"com.fastbee.domain.exception.GlobalErrorCodeConstants",
|
|
)
|
|
if text != orig:
|
|
p.write_text(text, encoding="utf-8")
|
|
print("fix imports", p.relative_to(ROOT))
|
|
|
|
|
|
def main():
|
|
for src_dir, dest_dir, op, np in TREES:
|
|
copy_tree(src_dir, dest_dir, op, np)
|
|
copy_single_fixed()
|
|
move_plain_enums()
|
|
delete_sources()
|
|
global_import_fix()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|