Skip to content

Commit

Permalink
Add command to remove packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mosa11aei committed Jul 10, 2024
1 parent d693817 commit 1583c19
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/fppm/cli/commands/remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fppm.cli.commands.registries as cmd_registries
import glob
import shutil
import os

def remove_package(args, context):
projectYamlPath = "./project.yaml"

if args.project_yaml_path is not None:
projectYamlPath = args.project_yaml_path

packageFolder = args.package.replace("/", ".")

print(f"[INFO]: Removing package [{args.package}]...")

try:
existingPackage = glob.glob(f"_fprime_packages/{packageFolder}*")
except Exception as e:
print(f"[ERR]: Error checking for existing package [{args.package}]: {e}")
return 1

for package in existingPackage:
try:
shutil.rmtree(package)
except Exception as e:
print(f"[ERR]: Error removing package [{args.package}]: {e}")
return 1

with open(f"_fprime_packages/CMakeLists.txt", "r") as f:
lines = f.readlines()

with open(f"_fprime_packages/CMakeLists.txt", "w") as f:
for line in lines:
if packageFolder not in line:
f.write(line)

fillables = glob.glob(f"{packageFolder}.fillables")

if len(fillables) > 0:
if input(f"[???] Remove the fillables directory for package [{args.package}]: ") == "y":
try:
shutil.rmtree(fillables[0])
except Exception as e:
print(f"[ERR]: Error removing fillables directory for package [{args.package}]: {e}")
return 1

print(f"[INFO]: Updating project.yaml file...")

projectYamlPath, projectYamlContent = cmd_registries.open_project_yaml(projectYamlPath)

if projectYamlContent == 1:
return 1

if projectYamlContent.get("packages") is not None:
for package in projectYamlContent["packages"]:
if package["name"] == args.package:
projectYamlContent["packages"].remove(package)
break
else:
print(f"[ERR]: No packages found in project.yaml file.")
return 1

write = cmd_registries.write_to_project_yaml(projectYamlPath, projectYamlContent)

print(f"[DONE]: Removed package [{args.package}]")



2 changes: 2 additions & 0 deletions src/fppm/cli/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fppm.cli.commands.init as cmd_init
import fppm.cli.commands.install as cmd_install
import fppm.cli.commands.config as cmd_config
import fppm.cli.commands.remove as cmd_remove
import sys

ROUTER = {
Expand All @@ -12,6 +13,7 @@
"init": cmd_init.create_project_yaml_file,
"install": cmd_install.install_package,
"config": cmd_config.config_entry,
"remove": cmd_remove.remove_package,
}


Expand Down
30 changes: 29 additions & 1 deletion src/fppm/cli/setup_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@
import sys
import fppm.cli.router as CMD_ROUTER

# set up the "remove" subcommand parser
def setup_remove_parser(subparsers) -> callable:
remove_parser = subparsers.add_parser(
"remove",
description="Remove a package from the project",
formatter_class=argparse.RawDescriptionHelpFormatter,
help="Remove a package from the project",
add_help=True,
)

remove_parser.add_argument(
"--package",
"-p",
type=str,
help="The name of the package to remove",
required=False,
)

remove_parser.add_argument(
"--project-yaml-path",
type=str,
help="The relative path to the project.yaml file",
required=False,
)

return remove_parser

# set up the "config" subcommand parser
def setup_config_parser(subparsers) -> callable:
config_parser = subparsers.add_parser(
Expand Down Expand Up @@ -49,6 +76,7 @@ def setup_install_parser(subparsers) -> callable:

install_parser.add_argument(
"--package",
"-p",
type=str,
help="The name (or Git URL) of the package to install",
required=False,
Expand All @@ -71,7 +99,6 @@ def setup_install_parser(subparsers) -> callable:

install_parser.add_argument(
"--project",
"-p",
type=str,
help="Install all packages in the project.yaml file",
required=False,
Expand Down Expand Up @@ -159,6 +186,7 @@ def start_cli_parser(args: list):
setup_new_parser(subparsers)
setup_registries_parser(subparsers)
setup_config_parser(subparsers)
setup_remove_parser(subparsers)

parsed, unknown = parser.parse_known_args(args)

Expand Down

0 comments on commit 1583c19

Please sign in to comment.