diff --git a/CHANGELOG.md b/CHANGELOG.md index 2317c19e12..effb6b63d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * Use Biocontainers API instead of quayi.io API for `nf-core modules create` [[#875](https://github.com/nf-core/tools/issues/875)] * Update `nf-core modules install` to handle different versions of modules [#1116](https://github.com/nf-core/tools/pull/1116) * Refactored `nf-core modules` command into one file per command [#1124](https://github.com/nf-core/tools/pull/1124) +* Updated `nf-core modules remove` to also remove entry in `modules.json` file ([#1115](https://github.com/nf-core/tools/issues/1115)) #### Sync diff --git a/nf_core/modules/remove.py b/nf_core/modules/remove.py index 3313343a8b..cfc0cdb940 100644 --- a/nf_core/modules/remove.py +++ b/nf_core/modules/remove.py @@ -1,5 +1,6 @@ import os import sys +import json import questionary import logging @@ -43,7 +44,6 @@ def remove(self, module): # Set the install folder based on the repository name install_folder = ["nf-core", "software"] - print(self.modules_repo.name) if not self.modules_repo.name == "nf-core/modules": install_folder = ["external"] @@ -58,5 +58,28 @@ def remove(self, module): log.info("Removing {}".format(module)) + # Remove entry from modules.json + self.remove_modules_json_entry(module) + # Remove the module return self.clear_module_dir(module_name=module, module_dir=module_dir) + + def remove_modules_json_entry(self, module): + # Load 'modules.json' + modules_json_path = os.path.join(self.dir, "modules.json") + try: + with open(modules_json_path, "r") as fh: + modules_json = json.load(fh) + except FileNotFoundError: + log.error("File 'modules.json' is missing") + return False + if module in modules_json.get("modules", {}): + modules_json["modules"].pop(module) + else: + log.error(f"Module '{module}' is missing from 'modules.json' file.") + return False + + with open(modules_json_path, "w") as fh: + json.dump(modules_json, fh, indent=4) + + return True