Browse Source

refactor & fix: HuanGetText添加过时检查

SongZihuan 3 years ago
parent
commit
0de4109a7f
2 changed files with 33 additions and 14 deletions
  1. 8 8
      lang/hgt.cmake
  2. 25 6
      lang/hgt.py

+ 8 - 8
lang/hgt.cmake

@@ -1,13 +1,13 @@
 find_package(Python3 REQUIRED)
 
-set(hgt_dir ${CMAKE_BINARY_DIR}/hgt)
-file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/hgt)
-
-to_native_path(${hgt_dir} hgt_dir_n)
-to_native_path(${CMAKE_CURRENT_LIST_DIR}/tr tr_n)
-to_native_path(${CMAKE_SOURCE_DIR}/src src_n)
-to_native_path(${CMAKE_SOURCE_DIR}/include include_n)
-set(hgt_name aFun)
+set(hgt_dir ${CMAKE_BINARY_DIR}/hgt)  # hgt 文件储存位置
+file(MAKE_DIRECTORY ${hgt_dir})
+
+to_native_path(${hgt_dir} hgt_dir_n)  # hgt 温江储存位置
+to_native_path(${CMAKE_CURRENT_LIST_DIR}/tr tr_n)  # tr  文件储存位置
+to_native_path(${CMAKE_SOURCE_DIR}/src src_n)  # src 文件储存位置 【需要遍历】
+to_native_path(${CMAKE_SOURCE_DIR}/include include_n)  # include 文件位置 【需要遍历】
+set(hgt_name aFun)  # ht 名称
 
 set(HGT_COMMAND
     "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/hgt.py"

+ 25 - 6
lang/hgt.py

@@ -78,7 +78,7 @@ def getFileFromPath(paths: List[str], file_type_: List[str]) -> FileList:
 
 # 获取宏 HT_aFunGetText的列表并计算
 file_list: FileList = getFileFromPath(input_dir, ['.c', '.cpp', '.h', '.hpp'])
-pattern = re.compile(f'HT_{base_name}GetText' + r'\(([\S]+),[\s]*(\"[^\"]*\")\)')  # 宏的定义
+pattern = re.compile(f'HT_{base_name}' + r'\(([\S]+),[\s]*(\"[^\"]*\")\)')  # 宏的定义
 flat_list: FlatList = {}
 for file in file_list:
     with open(file, "r", encoding="utf-8") as f:
@@ -104,7 +104,7 @@ for file in file_list:
 # 生成对应文件
 with open(os.path.join(output_dir, f"{base_name}_ht.py"), "w", encoding="utf-8") as fpy:
     with open(os.path.join(output_dir, f"{base_name}_ht.c"), "w", encoding="utf-8") as fc:
-        with open(os.path.join(output_dir, f"{base_name}_ht.h"), "w", encoding="utf-8") as fh:
+        with open(os.path.join(output_dir, f"{base_name}_ht.h.tmp"), "w", encoding="utf-8") as fh:
             head = f'''/*
  * File: {base_name}_ht.c/{base_name}_ht.c
  * The file is automatically generated by Huan-GetText
@@ -112,7 +112,7 @@ with open(os.path.join(output_dir, f"{base_name}_ht.py"), "w", encoding="utf-8")
  */'''
             fc.write(head + '\n\n')
             fc.write(f"#include \"{base_name}_ht.h\"\n")
-            fc.write(f"#undef HT_{base_name}GetText\n")
+            fc.write(f"#undef HT_{base_name}\n")
 
             fh.write(head + '\n\n')
             fh.write("#ifndef HT_GETTEXT_H\n")
@@ -121,12 +121,12 @@ with open(os.path.join(output_dir, f"{base_name}_ht.py"), "w", encoding="utf-8")
             fh.write('extern "C" {\n')
             fh.write("#endif\n")
 
-            fh.write(f"#ifdef HT_{base_name}GetText\n")
-            fh.write(f"#error \"Double define HT_{base_name}GetText\"\n")
+            fh.write(f"#ifdef HT_{base_name}\n")
+            fh.write(f"#error \"Double define HT_{base_name}\"\n")
             fh.write("#endif\n")
 
             fh.write(f"#include \"{export_h}.h\"\n")
-            fh.write(f"#define HT_{base_name}GetText(name, ...) ((char *)(HT_TEXT_{base_name}_ ## name))\n")
+            fh.write(f"#define HT_{base_name}(name, ...) ((char *)(HT_TEXT_{base_name}_ ## name))\n")
             fh.write(f"{export} int HT_init{base_name}GetText(char *lang);\n")
 
             fpy.write("# Example for tr file" + '\n\n')
@@ -163,6 +163,25 @@ int HT_init{base_name}GetText(char *lang) {{
             fh.write("#endif\n")
             fh.write("#endif\n")
 
+
+import hashlib
+
+with open(os.path.join(output_dir, f"{base_name}_ht.h.tmp"), "r", encoding="utf-8") as fht:
+    fht_data = fht.read()
+    fht_md5 = hashlib.md5(fht.read().encode('utf-8')).hexdigest()
+
+    try:
+        with open(os.path.join(output_dir, f"{base_name}_ht.h"), "r", encoding="utf-8") as fh:
+            fh_md5 = hashlib.md5(fh.read().encode('utf-8')).hexdigest()
+            if fh_md5 != fht_md5:
+                raise Exception
+    except:
+        with open(os.path.join(output_dir, f"{base_name}_ht.h"), "w", encoding="utf-8") as fh:
+            fh.write(fht.read())
+            print(f"Write file {base_name}_ht.h")
+    else:
+        print(f"File {base_name}_ht.h already update")
+
 for i in flat_list:
     print(f"TEXT: {i}")