Skip to content

Commit

Permalink
added basic colcon read based on default values (#56)
Browse files Browse the repository at this point in the history
This is an extreme oversimplification of what colcon can do and does not do the buildtool justice.

Signed-off-by: Noam Dori <TheNODO55@gmail.com>
  • Loading branch information
Noam-Dori committed Jun 14, 2021
1 parent 2491df7 commit 9cdde08
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
1 change: 0 additions & 1 deletion .idea/libraries/ROS.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 82 additions & 3 deletions src/main/java/ros/integrate/buildtool/ROSProfileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,39 @@ public void loadState(@NotNull List<ROSProfile> state) {
}
}

@State(name = "ColconProfiles", storages = @Storage("colcon.xml"))
static class ColconProfiles implements PersistentStateComponent<List<ROSProfile>> {
private final List<ROSProfile> data = new ArrayList<>();

@Override
public @NotNull List<ROSProfile> getState() {
return data;
}

@Override
public void loadState(@NotNull List<ROSProfile> state) {
data.clear();
data.addAll(state);
}
}

private static final Logger LOG = Logger.getInstance("#ros.integrate.buildtool.ROSProfileLoader");
@NotNull
private final ROSSettings settings;
@NotNull
private final CatkinMakeProfiles catkinMakeProfiles;
@NotNull
private final ColconProfiles colconProfiles;
private static final VirtualFileSystem FILE_SYSTEM = VirtualFileManager.getInstance()
.getFileSystem(LocalFileSystem.PROTOCOL);

public ROSProfileLoader(Project project) {
settings = ROSSettings.getInstance(project);
catkinMakeProfiles = new CatkinMakeProfiles();
colconProfiles = new ColconProfiles();
}

@NotNull
public List<ROSProfile> load(@NotNull ROSBuildTool buildTool) {
if (settings.getWorkspacePath().isEmpty()) {
return Collections.emptyList();
Expand All @@ -64,7 +84,51 @@ public List<ROSProfile> load(@NotNull ROSBuildTool buildTool) {

@NotNull
private List<ROSProfile> loadColcon() {
return Collections.emptyList();
// load existing profiles from persistent state component.
List<ROSProfile> ret = colconProfiles.getState();
// if there are no profiles in the XML, and the .catkin_workspace file exists, load that as a profile.
if (ret.isEmpty()) {
// find out if colcon is loaded
String defaultPath = System.getenv("COLCON_DEFAULTS_FILE");
if (defaultPath == null) {
defaultPath = System.getenv("COLCON_HOME");
if (defaultPath == null) {
return Collections.emptyList();
}
defaultPath += "/defaults.yaml";
}
VirtualFile colconDefaults = FILE_SYSTEM.findFileByPath(defaultPath);
if (colconDefaults == null) {
return Collections.emptyList();
}
Yaml config = new Yaml();
try {
Map<String, Object> data = config.load(colconDefaults.getInputStream());
ROSProfile profile = new ROSProfile();
profile.setGuiName("colcon_defaults");
profile.setGuiBuildtool(ROSBuildTool.COLCON);
profile.setIsolation(true);

@SuppressWarnings("unchecked")
Map<String, Object> buildVerb = (Map<String, Object>) data.get("build");
if (buildVerb != null) {
profile.setBuildDir(extendPath(buildVerb, "build-base", "build"));
profile.setInstallDir(extendPath(buildVerb, "install-base", "install"));
profile.setCmakeArgs(serialize(buildVerb, "cmake-args", ' '));

profile.setDenyList(serialize(buildVerb, "packages-skip", ' '));
profile.setAllowList(serialize(buildVerb, "packages-select", ' '));
profile.setSourceDir(extendPath(getFirst(buildVerb, "paths", "src")));
}
profile.setInstall(true);

profile.save();
ret.add(profile);
} catch (IOException e) {
LOG.error(String.format("Read of file [%s] failed with exception", colconDefaults.getPath()), e);
}
}
return ret;
}

@NotNull
Expand Down Expand Up @@ -130,8 +194,13 @@ private List<ROSProfile> loadCatkinTools() {

@SuppressWarnings("unchecked")
private String serialize(@NotNull Map<String, Object> data, String lookupKey, char delimiter) {
return PathListUtil.serializePathList((List<String>)
data.getOrDefault(lookupKey, Collections.emptyList()), delimiter);
Object list = data.get(lookupKey);
if (list instanceof List<?>) {
return PathListUtil.serializePathList((List<String>)
data.getOrDefault(lookupKey, Collections.emptyList()), delimiter);
} else {
return (String) data.getOrDefault(lookupKey, "");
}
}

private String extendPath(@NotNull Map<String, Object> data, String lookupKey, String def) {
Expand All @@ -142,4 +211,14 @@ private String extendPath(@NotNull Map<String, Object> data, String lookupKey, S
private String extendPath(@NotNull String raw) {
return raw.startsWith("/") ? raw : settings.getWorkspacePath() + "/" + raw;
}

@SuppressWarnings("SameParameterValue")
private String getFirst(@NotNull Map<String, Object> data, String lookupKey, String def) {
Object ret = data.getOrDefault(lookupKey, def);
if (ret instanceof List<?>) {
return (String) ((List<?>) ret).get(0);
} else {
return (String) ret;
}
}
}

0 comments on commit 9cdde08

Please sign in to comment.