| |
|
|
| import json |
| import glob |
| import re |
| import os |
|
|
| print("Running validate.py\n") |
|
|
|
|
| def add_error(filename, msg): |
| global errors |
| filename = filename.replace("../src/", "@/") |
| errors.append(f"{filename}: {msg}") |
|
|
|
|
| def validate_plugin(ts_file, content): |
| |
|
|
| |
| if not ts_file.endswith("Plugin.vue"): |
| add_error( |
| ts_file, 'All files containing plugins must end in "Plugin.vue"', |
| ) |
|
|
| required_substrs = [ |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ( |
| ':infoPayload="infoPayload"', |
| 'The PluginComponent must define a title prop like this: :infoPayload="infoPayload"', |
| None, |
| ), |
| ( |
| 'v-model="open"', |
| 'The PluginComponent must define a v-model like this: v-model="open"', |
| [ |
| "RenameMolPlugin.vue", |
| "DeleteMolPlugin.vue", |
| "CloneExtractMolPlugin.vue", |
| ], |
| ), |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ( |
| "extends PluginParentClass", |
| "All plugins must extend PluginParentClass", |
| [ |
| "PubChemBioassaysPlugin.vue", |
| "PubChemNamesPlugin.vue", |
| "PubChemPropsPlugin.vue" |
| ], |
| ), |
| ( |
| "<PluginComponent", |
| "All plugins must use the PluginComponent component", |
| None, |
| ), |
| ( |
| '@onUserArgChanged="onUserArgChanged"', |
| 'All plugins must include @onUserArgChanged="onUserArgChanged"', |
| None, |
| ), |
| ( |
| 'getTests(', |
| 'All plugins must define a getTests function. If a test is not needed, return an empty array. You can use `FailingTest` as a placeholder if you do not want to make the test now.', |
| None |
| ) |
| ] |
|
|
| |
| |
| required_substrs.append( |
| ( |
| '@onMolCountsChanged="onMolCountsChanged"', |
| 'The PluginComponent must define an onMolCountsChanged event like this: @onMolCountsChanged="onMolCountsChanged"', |
| [ |
| |
| |
| |
| |
| |
| ], |
| ), |
| ) |
|
|
| if "noPopup = true" not in content: |
| required_substrs.append( |
| ( |
| '@onPopupDone="onPopupDone"', |
| 'The PluginComponent must define an onPopupDone event like this: @onPopupDone="onPopupDone"', |
| [ |
| "SimpleMsgPlugin.vue", |
| "SimpleTableDataPlugin.vue", |
| "RedoPlugin.vue", |
| "UndoPlugin.vue", |
| "ClearSelectionPlugin.vue", |
| ], |
| ), |
| ) |
|
|
| if "menuPath = null" not in content: |
| required_substrs.append( |
| ( |
| '..."', |
| 'Unless a plugin defines noPopup to be true, its menu item must end in "..." (use double quotes).', |
| [], |
| ), |
| ) |
|
|
| for strng, msg, exceptions in required_substrs: |
| if strng not in content: |
| |
| is_exception = False |
| if exceptions is not None: |
| is_exception = any(exception in ts_file for exception in exceptions) |
| if not is_exception: |
| add_error(ts_file, msg) |
|
|
| prohibited_substrings = [ |
| ( |
| "onUserArgChanged(", |
| "Plugins must not define onUserArgChanged function. That's reserved for the parent class. Use onUserArgChange instead, which is called by the parent class.", |
| None |
| ), |
| ] |
|
|
| for strng, msg, exceptions in prohibited_substrings: |
| if strng in content: |
| |
| is_exception = False |
| if exceptions is not None: |
| is_exception = any(exception in ts_file for exception in exceptions) |
| if not is_exception: |
| add_error(ts_file, msg) |
|
|
| |
| for word in re.findall(r"(\w+) extends PluginParentClass", content): |
| if f"{word}.vue" != os.path.basename(ts_file): |
| add_error( |
| ts_file, |
| f"Plugin class {word} is a plugin. Its filename must be {word}.vue.", |
| ) |
|
|
| |
| if "mounted(" in content: |
| add_error( |
| ts_file, |
| "Plugins should not define a mounted() function. Use onMounted() instead.", |
| ) |
|
|
| |
| if "/Core/" in ts_file and "Tag.All" not in content: |
| add_error( |
| ts_file, |
| 'Core plugins must use the tag "Tag.All".', |
| ) |
|
|
| |
| if "/Optional/" in ts_file and "Tag.All" in content: |
| add_error( |
| ts_file, |
| 'Optional plugins must not use the tag "Tag.All".', |
| ) |
|
|
|
|
| def is_test_file(filepath): |
| """Check if a file is a test file that should be skipped.""" |
| return filepath.endswith('.test.ts') |
|
|
|
|
| |
| ts_files = glob.glob("../src/**/*.ts", recursive=True) + glob.glob( |
| "../src/**/*.vue", recursive=True |
| ) |
|
|
| |
| ts_files = [f for f in ts_files if not is_test_file(f)] |
|
|
| errors = [] |
|
|
|
|
| for ts_file in ts_files: |
| |
| with open(ts_file, "r") as file: |
| content = file.read() |
|
|
| |
| if "LAST_NAME" in content: |
| add_error(ts_file, 'The string "LAST_NAME" must not appear anywhere in any file.') |
| |
| if "SOME_KEY" in content: |
| add_error(ts_file, 'The string "SOME_KEY" must not appear anywhere in any file.') |
|
|
| |
| if "No changes" in content: |
| add_error(ts_file, 'The string "No changes" should not appear anywhere.') |
|
|
| |
| if "fetch(" in content: |
| add_error(ts_file, "Use fetcher() instead of fetch.") |
|
|
| if "axios.get" in content and "Fetcher.ts" not in ts_file: |
| add_error(ts_file, "Use fetcher() instead of axios.get.") |
|
|
| if "localStorage." in content and "LocalStorage.ts" not in ts_file: |
| add_error(ts_file, "Use the LocalStorage class instead of localStorage.setItem or localStorage.getItem.") |
|
|
| if "location.reload" in content and "CloseAppUtils.ts" not in ts_file: |
| add_error(ts_file, "Use closeDownApp() class instead of location.reload.") |
|
|
| if "URLSearchParams(" in content and "UrlParams.ts" not in ts_file: |
| add_error(ts_file, "Use getUrlParam() instead of URLSearchParams.") |
|
|
| if 'href="#"' in content or 'href= "#"' in content: |
| add_error(ts_file, 'No <a> should have `href="#"`. Remove it and use `class="link-primary"` if needed.') |
|
|
| if "CloseAppUtils.ts" not in ts_file and ('window.location.href =' in content or 'window.location.href=' in content): |
| add_error( |
| ts_file, |
| 'No code should set `window.location.href = ...`. Use reloadPage instead.', |
| ) |
|
|
| if "workspace" in content.lower(): |
| add_error( |
| ts_file, |
| 'The word "workspace" should not be used. Use "project" instead.', |
| ) |
|
|
| |
| |
| if ".catch(" in content: |
| |
| for match in re.findall(r"\.catch\((.{0,200})", content, re.DOTALL): |
| if "throw" not in match and "reject" not in match: |
| add_error( |
| ts_file, |
| "If you use .catch(), you must throw an error or reject within the next few lines. Comment out 'throw err' in those rare cases where you want to ignore an error.", |
| ) |
| if "import(" in content and os.path.basename(ts_file) not in ["DynamicImports.ts", "AllPlugins.vue"]: |
| add_error(ts_file, "Use import() only in the DynamicImports.ts file.") |
|
|
| |
| content_without_dot_molmoda = content.replace("MOLMODA:", '') |
| content_without_dot_molmoda = content_without_dot_molmoda.lower() |
| content_without_dot_molmoda = content_without_dot_molmoda.replace('"molmoda"', '') |
| content_without_dot_molmoda = content_without_dot_molmoda.replace(".molmoda", "") |
| content_without_dot_molmoda = content_without_dot_molmoda.replace("/molmoda/", "") |
|
|
| |
| content_without_dot_molmoda = re.sub(r"//.*", "", content_without_dot_molmoda) |
| content_without_dot_molmoda = re.sub(r" \* .*", "", content_without_dot_molmoda) |
|
|
| has_mol_moda = re.search(r"\bmolmoda\b", content_without_dot_molmoda) |
| if has_mol_moda and "GlobalVars.ts" not in ts_file: |
| add_error( |
| ts_file, |
| 'The string "MolModa" should only be used in GlobalVars.ts. Import the app name from there.', |
| ) |
|
|
| |
| if ".get_svg" in content and os.path.basename(ts_file) != "Mol2DView.vue": |
| add_error( |
| ts_file, |
| 'The substring ".get_svg" is only allowed in Mol2DView.vue.' |
| ) |
|
|
| |
| |
| |
| popup_title_pattern = r'\.popupMessage\(\s*(["\'`])(.*?)\1' |
| for match in re.finditer(popup_title_pattern, content, re.DOTALL): |
| |
| title = match.group(2) |
| |
| |
| |
| if len(title.strip().split()) < 2: |
| add_error( |
| ts_file, |
| f'Popup titles must be more than two words long for clarity. Found: "{title}"' |
| ) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
|
|
| |
| if "/Plugins/" not in ts_file: |
| |
| continue |
| if "/Parents/" in ts_file: |
| |
| continue |
|
|
| if not ts_file.endswith(".vue"): |
| |
| continue |
|
|
| if os.path.basename(ts_file) in ["AllPlugins.vue"]: |
| |
| continue |
|
|
| if "<PluginComponent" not in content: |
| |
| continue |
|
|
| validate_plugin(ts_file, content) |
|
|
| errors = sorted(set(errors)) |
|
|
| |
| with open("../src/compile_errors.json", "w") as file: |
| json.dump(errors, file) |
|
|
| if errors: |
| print("Validation failed. See compile_errors.json for details.\n") |
| for err in errors: |
| print(err) |
| print("") |
| exit(1) |
| else: |
| print("Validation passed.\n") |
| exit(0) |