update generate-asset-list.py (#1287)
This commit is contained in:
@@ -11,60 +11,88 @@ and turns them as assets and writes them into ./pubspec.yaml
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
target = "./assets"
|
target = "./assets/"
|
||||||
excluded_ext = [
|
space = " "
|
||||||
".gitkeep",
|
subfolders = []
|
||||||
".onnx.json",
|
patterns_to_skip = ["1.5x", "2.x", "3.x", "4.x"]
|
||||||
".py",
|
for root, dirs, files in os.walk(target):
|
||||||
".sh",
|
for d in dirs:
|
||||||
"*.md",
|
path = os.path.join(root, d).replace("\\", "/")
|
||||||
"MODEL_CARD",
|
if os.listdir(path):
|
||||||
".DS_Store",
|
path = path.lstrip('./')
|
||||||
]
|
if any(path.endswith(pattern) for pattern in patterns_to_skip):
|
||||||
sep = " "
|
continue
|
||||||
ss = []
|
subfolders.append("{space}- {path}/".format(space=space, path=path))
|
||||||
for root, d, files in os.walk(target):
|
|
||||||
for f in files:
|
|
||||||
skip = False
|
|
||||||
for p in excluded_ext:
|
|
||||||
if f.endswith(p):
|
|
||||||
skip = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if skip:
|
assert subfolders, "The subfolders list is empty."
|
||||||
continue
|
|
||||||
|
|
||||||
t = os.path.join(root, f).replace("\\", "/")
|
subfolders = sorted(subfolders)
|
||||||
ss.append("{sep}- {t}".format(sep=sep, t=t))
|
|
||||||
|
loc_of_flutter = -1
|
||||||
|
loc_of_flutter_asset = -1
|
||||||
|
loc_of_end_flutter_asset = -1
|
||||||
|
loc_of_end_flutter = -1
|
||||||
|
|
||||||
# read pub.spec.yaml
|
|
||||||
with open("./pubspec.yaml", encoding="utf-8") as f:
|
with open("./pubspec.yaml", encoding="utf-8") as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
for index, line in enumerate(lines):
|
||||||
found_assets = False
|
if line == "flutter:\n":
|
||||||
with open("./pubspec.yaml", "w", encoding="utf-8") as f:
|
loc_of_flutter = index + 1
|
||||||
for line in lines:
|
if index == len(lines) - 1:
|
||||||
if line == " assets:\n":
|
loc_of_end_flutter = index + 2
|
||||||
assert found_assets is False
|
continue
|
||||||
found_assets = True
|
if loc_of_flutter >= 0 and loc_of_flutter_asset < 0 and line == " assets:\n":
|
||||||
if len(ss) > 0:
|
loc_of_flutter_asset = index + 1
|
||||||
f.write(line)
|
|
||||||
|
|
||||||
if not found_assets:
|
|
||||||
f.write(line)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for s in ss:
|
with open("./pubspec.yaml", encoding="utf-8") as f:
|
||||||
f.write("{s}\n".format(s=s))
|
lines = f.readlines()
|
||||||
break
|
for index, line in enumerate(lines):
|
||||||
|
if index < loc_of_flutter:
|
||||||
|
continue
|
||||||
|
if loc_of_flutter_asset >= 0:
|
||||||
|
if line.startswith(" - assets/"):
|
||||||
|
loc_of_end_flutter_asset = index + 1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
loc_of_end_flutter = index + 1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if line.startswith(" ") is False:
|
||||||
|
loc_of_end_flutter = index + 1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
loc_of_end_flutter = index + 2
|
||||||
|
break
|
||||||
|
|
||||||
if not found_assets and ss:
|
assert loc_of_flutter >= 0, "The 'flutter:' section is missing in the pubspec.yaml file."
|
||||||
|
|
||||||
|
with open("./pubspec.yaml", "w", encoding="utf-8") as f:
|
||||||
|
for index, line in enumerate(lines):
|
||||||
|
if loc_of_end_flutter_asset >= 0:
|
||||||
|
if index + 1 < loc_of_flutter_asset or index + 1 > loc_of_end_flutter_asset:
|
||||||
|
f.write(line)
|
||||||
|
if index + 1 == loc_of_flutter_asset:
|
||||||
|
f.write(" assets:\n")
|
||||||
|
for folder in subfolders:
|
||||||
|
f.write("{folder}\n".format(folder=folder))
|
||||||
|
else:
|
||||||
|
if index + 1 < loc_of_end_flutter or index + 1 > loc_of_end_flutter:
|
||||||
|
f.write(line)
|
||||||
|
if index + 1 == loc_of_end_flutter:
|
||||||
|
f.write(" assets:\n")
|
||||||
|
for indexOfFolder, folder in enumerate(subfolders):
|
||||||
|
f.write("{folder}\n".format(folder=folder))
|
||||||
|
if indexOfFolder == len(subfolders) - 1:
|
||||||
|
f.write("\n")
|
||||||
|
break
|
||||||
|
|
||||||
|
if loc_of_end_flutter == len(lines) + 1:
|
||||||
|
f.write("\n")
|
||||||
f.write(" assets:\n")
|
f.write(" assets:\n")
|
||||||
for s in ss:
|
for folder in subfolders:
|
||||||
f.write("{s}\n".format(s=s))
|
f.write("{folder}\n".format(folder=folder))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user