Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 10 additions & 30 deletions crates/pixi_build_ros/src/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use miette::Diagnostic;
use rattler_conda_types::Subdir;
use thiserror::Error;

use crate::distro::Distro;

/// Errors that can occur during build script generation.
#[derive(Debug, Error, Diagnostic)]
pub enum BuildScriptError {
Expand All @@ -20,7 +22,7 @@ pub enum BuildScriptError {
/// variable substitution.
pub fn render_build_script(
build_type: &str,
distro: &str,
distro: &Distro,
source_dir: &Path,
) -> Result<String, BuildScriptError> {
// Use the current (build) platform, not the host/target platform.
Expand All @@ -31,7 +33,8 @@ pub fn render_build_script(
let src_dir_str = source_dir.display().to_string();
let rendered = template
.replace("@SRC_DIR@", &src_dir_str)
.replace("@DISTRO@", distro)
.replace("@DISTRO@", &distro.name)
.replace("@PACKAGE_PREFIX@", &distro.package_prefix())
.replace("@BUILD_DIR@", "build")
.replace("@BUILD_TYPE@", "Release");

Expand All @@ -57,36 +60,13 @@ mod tests {
use super::*;
use std::path::PathBuf;

#[test]
fn test_render_ament_cmake() {
let script =
render_build_script("ament_cmake", "humble", &PathBuf::from("/my/source")).unwrap();

assert!(script.contains("/my/source"));
assert!(script.contains("Release"));
assert!(!script.contains("@SRC_DIR@"));
assert!(!script.contains("@BUILD_TYPE@"));
}

#[test]
fn test_render_ament_python() {
let script = render_build_script("ament_python", "jazzy", &PathBuf::from("/src")).unwrap();

assert!(script.contains("/src"));
assert!(!script.contains("@SRC_DIR@"));
}

#[test]
fn test_render_catkin() {
let script = render_build_script("catkin", "noetic", &PathBuf::from("/pkg")).unwrap();

assert!(script.contains("/pkg"));
assert!(script.contains("noetic"));
}

#[test]
fn test_unsupported_build_type() {
let result = render_build_script("unknown_type", "jazzy", &PathBuf::from("/src"));
let result = render_build_script(
"unknown_type",
&Distro::new("jazzy"),
&PathBuf::from("/src"),
);
assert!(matches!(
result,
Err(BuildScriptError::UnsupportedBuildType { .. })
Expand Down
47 changes: 45 additions & 2 deletions crates/pixi_build_ros/src/distro.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! ROS distribution metadata.
//!
//! Maps a ROS distribution name to the ROS version it belongs to. That version
//! selects the distro mutex package, the `ROS_VERSION` build environment
//! variable, and whether `ros_workspace` is added as a build dependency.
//! selects the conda package names (`ros2-<name>` / `ros-<name>`), the distro
//! mutex package, the `ROS_VERSION` build environment variable, and whether
//! `ros_workspace` is added as a dependency.

/// The ROS 1 distributions.
///
Expand Down Expand Up @@ -78,6 +79,28 @@ impl Distro {
let version = RosVersion::for_distro(&name);
Self { name, version }
}

/// The prefix of the conda names of this distribution's ROS packages, as
/// RoboStack names them: `ros2` for ROS 2 (the names don't say which
/// distribution a package belongs to; the mutex does) and `ros-<distro>`
/// for ROS 1, whose channels (robostack-noetic) only have those names.
pub fn package_prefix(&self) -> String {
match self.version {
RosVersion::Ros1 => format!("ros-{}", self.name),
RosVersion::Ros2 => "ros2".to_string(),
}
}

/// The conda name of the ROS package `ros_name`, e.g. `ros2-demo-nodes-cpp`.
pub fn conda_name(&self, ros_name: &str) -> String {
format!("{}-{}", self.package_prefix(), ros_name.replace('_', "-"))
}

/// The distro mutex pinned to this distribution: RoboStack builds the mutex
/// of a distribution with the build string `<distribution>_<build number>`.
pub fn mutex_spec(&self) -> String {
format!("{} * {}_*", self.version.mutex_package_name(), self.name)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -133,6 +156,26 @@ mod tests {
assert_eq!(RosVersion::Ros2.mutex_package_name(), "ros2-distro-mutex");
}

#[test]
fn test_conda_names() {
assert_eq!(
Distro::new("jazzy").conda_name("demo_nodes_cpp"),
"ros2-demo-nodes-cpp"
);
assert_eq!(
Distro::new("noetic").conda_name("roscpp"),
"ros-noetic-roscpp"
);
assert_eq!(
Distro::new("jazzy").mutex_spec(),
"ros2-distro-mutex * jazzy_*"
);
assert_eq!(
Distro::new("noetic").mutex_spec(),
"ros-distro-mutex * noetic_*"
);
}

#[test]
fn test_env_value() {
assert_eq!(RosVersion::Ros1.as_env_value(), "1");
Expand Down
Loading
Loading