Hán Trung Kiên
Committed by GitHub

update generate-asset-list.py (#1287)

@@ -11,60 +11,88 @@ and turns them as assets and writes them into ./pubspec.yaml @@ -11,60 +11,88 @@ and turns them as assets and writes them into ./pubspec.yaml
11 11
12 import os 12 import os
13 13
14 -  
15 def main(): 14 def main():
16 - target = "./assets"  
17 - excluded_ext = [  
18 - ".gitkeep",  
19 - ".onnx.json",  
20 - ".py",  
21 - ".sh",  
22 - "*.md",  
23 - "MODEL_CARD",  
24 - ".DS_Store",  
25 - ]  
26 - sep = " "  
27 - ss = []  
28 - for root, d, files in os.walk(target):  
29 - for f in files:  
30 - skip = False  
31 - for p in excluded_ext:  
32 - if f.endswith(p):  
33 - skip = True  
34 - break 15 + target = "./assets/"
  16 + space = " "
  17 + subfolders = []
  18 + patterns_to_skip = ["1.5x", "2.x", "3.x", "4.x"]
  19 + for root, dirs, files in os.walk(target):
  20 + for d in dirs:
  21 + path = os.path.join(root, d).replace("\\", "/")
  22 + if os.listdir(path):
  23 + path = path.lstrip('./')
  24 + if any(path.endswith(pattern) for pattern in patterns_to_skip):
  25 + continue
  26 + subfolders.append("{space}- {path}/".format(space=space, path=path))
35 27
36 - if skip:  
37 - continue 28 + assert subfolders, "The subfolders list is empty."
38 29
39 - t = os.path.join(root, f).replace("\\", "/")  
40 - ss.append("{sep}- {t}".format(sep=sep, t=t)) 30 + subfolders = sorted(subfolders)
  31 +
  32 + loc_of_flutter = -1
  33 + loc_of_flutter_asset = -1
  34 + loc_of_end_flutter_asset = -1
  35 + loc_of_end_flutter = -1
41 36
42 - # read pub.spec.yaml  
43 with open("./pubspec.yaml", encoding="utf-8") as f: 37 with open("./pubspec.yaml", encoding="utf-8") as f:
44 lines = f.readlines() 38 lines = f.readlines()
  39 + for index, line in enumerate(lines):
  40 + if line == "flutter:\n":
  41 + loc_of_flutter = index + 1
  42 + if index == len(lines) - 1:
  43 + loc_of_end_flutter = index + 2
  44 + continue
  45 + if loc_of_flutter >= 0 and loc_of_flutter_asset < 0 and line == " assets:\n":
  46 + loc_of_flutter_asset = index + 1
  47 + continue
45 48
46 - found_assets = False  
47 - with open("./pubspec.yaml", "w", encoding="utf-8") as f:  
48 - for line in lines:  
49 - if line == " assets:\n":  
50 - assert found_assets is False  
51 - found_assets = True  
52 - if len(ss) > 0:  
53 - f.write(line)  
54 -  
55 - if not found_assets:  
56 - f.write(line) 49 + with open("./pubspec.yaml", encoding="utf-8") as f:
  50 + lines = f.readlines()
  51 + for index, line in enumerate(lines):
  52 + if index < loc_of_flutter:
57 continue 53 continue
  54 + if loc_of_flutter_asset >= 0:
  55 + if line.startswith(" - assets/"):
  56 + loc_of_end_flutter_asset = index + 1
  57 + continue
  58 + else:
  59 + loc_of_end_flutter = index + 1
  60 + continue
  61 + else:
  62 + if line.startswith(" ") is False:
  63 + loc_of_end_flutter = index + 1
  64 + continue
  65 + else:
  66 + loc_of_end_flutter = index + 2
  67 + break
58 68
59 - for s in ss:  
60 - f.write("{s}\n".format(s=s))  
61 - break 69 + assert loc_of_flutter >= 0, "The 'flutter:' section is missing in the pubspec.yaml file."
62 70
63 - if not found_assets and ss:  
64 - f.write(" assets:\n")  
65 - for s in ss:  
66 - f.write("{s}\n".format(s=s)) 71 + with open("./pubspec.yaml", "w", encoding="utf-8") as f:
  72 + for index, line in enumerate(lines):
  73 + if loc_of_end_flutter_asset >= 0:
  74 + if index + 1 < loc_of_flutter_asset or index + 1 > loc_of_end_flutter_asset:
  75 + f.write(line)
  76 + if index + 1 == loc_of_flutter_asset:
  77 + f.write(" assets:\n")
  78 + for folder in subfolders:
  79 + f.write("{folder}\n".format(folder=folder))
  80 + else:
  81 + if index + 1 < loc_of_end_flutter or index + 1 > loc_of_end_flutter:
  82 + f.write(line)
  83 + if index + 1 == loc_of_end_flutter:
  84 + f.write(" assets:\n")
  85 + for indexOfFolder, folder in enumerate(subfolders):
  86 + f.write("{folder}\n".format(folder=folder))
  87 + if indexOfFolder == len(subfolders) - 1:
  88 + f.write("\n")
  89 + break
67 90
  91 + if loc_of_end_flutter == len(lines) + 1:
  92 + f.write("\n")
  93 + f.write(" assets:\n")
  94 + for folder in subfolders:
  95 + f.write("{folder}\n".format(folder=folder))
68 96
69 if __name__ == "__main__": 97 if __name__ == "__main__":
70 main() 98 main()