Skip to content

Commit

Permalink
Warn user if cargo ament build is not installed (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
Signed-off-by: Michael X. Grey <greyxmike@gmail.com>
Co-authored-by: Michael X. Grey <greyxmike@gmail.com>
  • Loading branch information
luca-della-vedova and mxgrey authored Nov 25, 2024
1 parent 7bb8db4 commit 182fcab
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions colcon_ros_cargo/package_identification/ament_cargo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Licensed under the Apache License, Version 2.0

import subprocess

from catkin_pkg.package import parse_package
from colcon_cargo.package_identification.cargo \
import CargoPackageIdentification
Expand Down Expand Up @@ -43,6 +45,16 @@ def identify(self, metadata): # noqa: D102
'Got build type ament_cargo but could not find "Cargo.toml"')
return

ament_build = 'cargo ament-build --help'.split()
if subprocess.run(ament_build, capture_output=True).returncode != 0:
if _print_ament_cargo_warning_once():
logger.error(
'\n\nament_cargo package found but cargo ament-build was '
'not detected.'
'\n\nPlease install it by running:'
'\n $ cargo install cargo-ament-build\n')
return

metadata.type = 'ament_cargo'
pkg = _get_package(str(metadata.path))

Expand All @@ -54,3 +66,20 @@ def identify(self, metadata): # noqa: D102
{dep.name for dep in pkg.run_depends}
metadata.dependencies['test'] = \
{dep.name for dep in pkg.test_depends}


def _print_ament_cargo_warning_once():
global has_printed_ament_cargo_warning
try:
# The following line will throw an exception if the global variable
# has never been initialized
has_printed_ament_cargo_warning
except NameError:
# We want to initialize the global variable to false the first time
has_printed_ament_cargo_warning = False

if not has_printed_ament_cargo_warning:
has_printed_ament_cargo_warning = True
return True

return False

0 comments on commit 182fcab

Please sign in to comment.