AutoAPMS
Resilient Robot Mission Management
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 (packages.empty()) {
36 throw exceptions::ResourceError(
37 "Cannot find resources for type '" + resource_type + "' in any of the installed packages.");
38 }
39 if (const std::set<std::string> common = getCommonElements(packages, exclude_packages); !common.empty()) {
40 for (const std::string & package_to_exclude : common) packages.erase(package_to_exclude);
41 if (packages.empty()) {
42 throw exceptions::ResourceError(
43 "Resources for type '" + resource_type +
44 "' are only available in excluded but not in any other of the installed packages (Excluded packages containing "
45 "resources: [ " +
46 auto_apms_util::join(std::vector<std::string>(common.begin(), common.end()), ", ") + " ]).");
47 }
48 }
49 return packages;
50}
51
52std::set<std::string> getPackagesWithPluginResources(const std::set<std::string> & exclude_packages)
53{
54 return getPackagesWithResourceType(PLUGIN_RESOURCE_TYPE, exclude_packages);
55}
56
57std::string getPluginXMLPath(const std::string & package)
58{
59 std::string content;
60 std::string base_path;
61 if (ament_index_cpp::get_resource(PLUGIN_RESOURCE_TYPE, package, content, &base_path)) {
62 std::vector<std::string> paths = splitString(content, "\n");
63 if (paths.size() != 1) {
64 throw exceptions::ResourceError(
65 "Invalid '" + PLUGIN_RESOURCE_TYPE + "' resource marker file installed by package '" + package +
66 "'. Must contain a single line with a relative path to the plugins.xml "
67 "manifest file with respect to the package's install prefix.");
68 }
69 return base_path + '/' + paths[0];
70 }
71 throw exceptions::ResourceError(
72 "Cannot find a plugin.xml file in package '" + package + "' (Plugin resource type is: '" + PLUGIN_RESOURCE_TYPE +
73 "').");
74}
75
76std::vector<std::string> collectPluginXMLPaths(const std::set<std::string> & exclude_packages)
77{
78 std::vector<std::string> xml_paths;
79 for (const std::string & package : getPackagesWithResourceType(PLUGIN_RESOURCE_TYPE, exclude_packages)) {
80 xml_paths.push_back(getPluginXMLPath(package));
81 }
82 return xml_paths;
83}
84
85} // namespace auto_apms_util
std::vector< std::string > collectPluginXMLPaths(const std::set< std::string > &exclude_packages={})
Collect the paths of plugin.xml manifest files used for initializing pluginlib::ClassLoader objects.
Definition resource.cpp:76
std::string getPluginXMLPath(const std::string &package)
Get the path of a plugin.xml manifest file used for initializing pluginlib::ClassLoader objects.
Definition resource.cpp:57
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:52
Fundamental helper classes and utility functions.