AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
resource.cpp
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "auto_apms_util/resource.hpp"
16
17#include "ament_index_cpp/get_resource.hpp"
18#include "ament_index_cpp/get_resources.hpp"
19#include "auto_apms_util/exceptions.hpp"
20#include "auto_apms_util/string.hpp"
21#include "pluginlib/class_loader.hpp"
22
23namespace auto_apms_util
24{
25
26const std::string PLUGIN_RESOURCE_TYPE = _AUTO_APMS_UTIL__RESOURCE_TYPE_NAME__PLUGINLIB;
27
28std::set<std::string> getPackagesWithResourceType(
29 const std::string & resource_type, const std::set<std::string> & exclude_packages)
30{
31 std::set<std::string> packages;
32 for (const auto & [package, _] : ament_index_cpp::get_resources(resource_type)) {
33 packages.insert(package);
34 }
35 if (const std::set<std::string> common = getCommonElements(packages, exclude_packages); !common.empty()) {
36 for (const std::string & package_to_exclude : common) packages.erase(package_to_exclude);
37 }
38 return packages;
39}
40
41std::set<std::string> getPackagesWithPluginResources(const std::set<std::string> & exclude_packages)
42{
43 return getPackagesWithResourceType(PLUGIN_RESOURCE_TYPE, exclude_packages);
44}
45
46std::string getPluginXMLPath(const std::string & package)
47{
48 std::string content;
49 std::string base_path;
50 if (ament_index_cpp::get_resource(PLUGIN_RESOURCE_TYPE, package, content, &base_path)) {
51 std::vector<std::string> paths = splitString(content, "\n");
52 if (paths.size() != 1) {
53 throw exceptions::ResourceError(
54 "Invalid '" + PLUGIN_RESOURCE_TYPE + "' resource marker file installed by package '" + package +
55 "'. Must contain a single line with a relative path to the plugins.xml "
56 "manifest file with respect to the package's install prefix.");
57 }
58 return base_path + '/' + paths[0];
59 }
60 throw exceptions::ResourceError(
61 "Cannot find a plugin.xml file in package '" + package + "' (Plugin resource type is: '" + PLUGIN_RESOURCE_TYPE +
62 "').");
63}
64
65std::vector<std::string> collectPluginXMLPaths(const std::set<std::string> & exclude_packages)
66{
67 std::vector<std::string> xml_paths;
68 for (const std::string & package : getPackagesWithResourceType(PLUGIN_RESOURCE_TYPE, exclude_packages)) {
69 xml_paths.push_back(getPluginXMLPath(package));
70 }
71 return xml_paths;
72}
73
74} // namespace auto_apms_util
std::vector< std::string > collectPluginXMLPaths(const std::set< std::string > &exclude_packages={})
Collect the paths of plugins.xml manifest files generated by AutoAPMS.
Definition resource.cpp:65
std::string getPluginXMLPath(const std::string &package)
Get the path of a specific plugins.xml manifest file generated by AutoAPMS.
Definition resource.cpp:46
std::vector< std::string > splitString(const std::string &str, const std::string &delimiter, bool remove_empty=true)
Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multi...
Definition string.cpp:24
std::set< KeyT, CompareT, AllocatorT > getCommonElements(std::set< KeyT, CompareT, AllocatorT > c1, std::set< KeyT, CompareT, AllocatorT > c2)
Assemble common elements of two sets.
Definition container.hpp:53
std::set< std::string > getPackagesWithResourceType(const std::string &resource_type, const std::set< std::string > &exclude_packages={})
Get a list of all package names that register a certain type of ament_index resources.
Definition resource.cpp:28
std::set< std::string > getPackagesWithPluginResources(const std::set< std::string > &exclude_packages={})
Get a list of all package names that register AutoAPMS plugin resources.
Definition resource.cpp:41
Fundamental helper classes and utility functions.