From dc30002f5a10a025386092cfc54b8c361ab7f5ea Mon Sep 17 00:00:00 2001 From: Andras Varga Date: Fri, 11 Oct 2024 18:04:15 +0200 Subject: [PATCH] omnetpp: above version 6.1, auto-import projects defined in $OPP_ENV_PROJECT_DEPS Based on the content of the OPP_ENV_PROJECT_DEPS environment variable. --- opp_env/database/omnetpp.py | 5 ++ opp_env/templates/metadata/startup.bsh | 82 ++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 opp_env/templates/metadata/startup.bsh diff --git a/opp_env/database/omnetpp.py b/opp_env/database/omnetpp.py index 7c892c77..dd971c60 100644 --- a/opp_env/database/omnetpp.py +++ b/opp_env/database/omnetpp.py @@ -203,6 +203,11 @@ def make_omnetpp_project_description(version, base_version=None, is_modernized=F "sed -i 's|^WITH_TKENV=yes|WITH_TKENV=no|' configure.user" if version >= "5.0" and version < "6.0" and is_macos and is_aarch64 else None, # on macos aarch64, tkenv is not supported f"sed -i '/^PERL =/i CFLAGS += {extra_cflags}' Makefile.inc.in" if extra_cflags and version >= "4.0" else # no Makefile.inc.in in 3.x yet f"sed -i 's/^CFLAGS=.*/CFLAGS=\\\"-O2 -DNDEBUG=1 {extra_cflags}\\\"/' configure.user" if extra_cflags and version < "4.0" else None, # no Makefile.inc.in in 3.x yet + + # use the opp_env workspace as the default IDE workspace (on 6.1 or later, where auto importing of projects are supported) + # and copy IDE startup script into it (auto-imports projects) + "sed -i 's|../samples|../..|' src/utils/opp_ide""" if version >= "6.1" else None, + "mkdir -p ../.metadata && cp \"$OPP_ENV_DIR/templates/metadata/startup.bsh\" ../.metadata" if version >= "6.1" else None, ] # More recent releases can handle parallel build diff --git a/opp_env/templates/metadata/startup.bsh b/opp_env/templates/metadata/startup.bsh new file mode 100644 index 00000000..4a7e9dcc --- /dev/null +++ b/opp_env/templates/metadata/startup.bsh @@ -0,0 +1,82 @@ +// +// This is the startup script for the OMNeT++ IDE, added by opp_env. +// It is automatically executed when the IDE starts up. It ensures that +// projects active in the opp_env session are imported and opened, +// and the project references are correctly set up. +// + +//System.out.println("startup.bsh: Running"); + +void importAndOpenProject(String projectName, String[] referencedProjectNames) { + workspace = org.eclipse.core.resources.ResourcesPlugin.getWorkspace(); + projectDir = workspace.getRoot().getLocation().append(projectName); + projectFile = projectDir.append(".project"); + if (projectFile.toFile().exists()) { + //System.out.println("startup.bsh: Importing/opening " + projectName); + description = workspace.loadProjectDescription(projectFile); + description.setName(projectName); + project = workspace.getRoot().getProject(description.getName()); + if (!project.exists()) + project.create(description, null); + if (!project.isOpen()) + project.open(null); + + referencedProjects = new org.eclipse.core.resources.IProject[referencedProjectNames.length]; + for (int i = 0; i < referencedProjectNames.length; i++) + referencedProjects[i] = workspace.getRoot().getProject(referencedProjectNames[i]); + description = project.getDescription(); + description.setReferencedProjects(referencedProjects); + project.setDescription(description, null); + } +} + +void closeOtherProjects(ArrayList projectNames) { + workspace = org.eclipse.core.resources.ResourcesPlugin.getWorkspace(); + workspaceProjects = workspace.getRoot().getProjects(); + for (int j = 0; j < workspaceProjects.length; j++) + if (!projectNames.contains(workspaceProjects[j].getName()) && workspaceProjects[j].isOpen()) { + //System.out.println("startup.bsh: Closing " + workspaceProjects[j].getName()); + workspaceProjects[j].close(null); + } +} + +void importAndOpenProjectsWithDependencies(String projectDepsEnv) { + collectedProjectNames = new ArrayList(); + + if (projectDepsEnv != null && !projectDepsEnv.isEmpty()) { + // Split by ';' to get each project definition + String[] projectEntries = projectDepsEnv.split(";"); + + // Iterate over each project entry to parse the dependencies and call importAndOpenProject + for (int i = 0; i < projectEntries.length; i++) { + String entry = projectEntries[i].trim(); + if (entry.isEmpty()) + continue; + + // Split by ':' to separate the project name from its dependencies, and split up the dependencies + String[] parts = entry.split(":", 2); + String projectName = parts[0].trim(); + String dependencies = parts.length > 1 ? parts[1].trim() : ""; + String[] referencedProjectNames = dependencies.isEmpty() ? new String[0] : dependencies.split("[ ,]+"); + + // Collect the project name + collectedProjectNames.add(projectName); + + // Import / open project + if (!projectName.startsWith("omnetpp-")) + importAndOpenProject(projectName, referencedProjectNames); + } + } + + closeOtherProjects(collectedProjectNames); +} + +// Read the OPP_ENV_PROJECT_DEPS environment variable. +// The OPP_ENV_PROJECT_DEPS environment variable is formatted as a semicolon-separated list +// of project entries, where each entry consists of a project name optionally +// followed by a colon and a list of dependencies separated by spaces or commas +// (e.g., "proj1: dep1 dep2 dep3; proj2: dep4,dep5; proj3; proj4:dep6"). +String projectDepsEnv = System.getenv("OPP_ENV_PROJECT_DEPS"); +importAndOpenProjectsWithDependencies(projectDepsEnv); + +//System.out.println("startup.bsh: Completed");