Skip to content

Commit 3219d9f

Browse files
committed
Ware importation in CLI
* added CLI command to clone ware from hub or direct git source * fixed CMake pre-test command CREATE_DIRECTORY * added a test in Process to check if working directory exists (references #1260)
1 parent 5f56ece commit 3219d9f

File tree

11 files changed

+178
-23
lines changed

11 files changed

+178
-23
lines changed

cmake/OpenFLUIDTestScript.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ FUNCTION(EXECUTE_COMMAND CMD)
8080
IF(IS_DIRECTORY ${ARGV1})
8181
MESSAGE(FATAL_ERROR "CHECK_DIRECTORY_NOT_EXISTING: directory ${ARGV1} found")
8282
ENDIF()
83+
ELSEIF(${CMD} STREQUAL "CREATE_DIRECTORY")
84+
FILE(MAKE_DIRECTORY ${ARGV1})
8385
ELSEIF(${CMD} STREQUAL "REMOVE_FILE")
8486
IF(EXISTS ${ARGV1})
8587
FILE(REMOVE ${ARGV1})
@@ -133,7 +135,7 @@ FUNCTION(PARSE_COMMANDS)
133135
${ELEM} STREQUAL "COPY_DIRECTORY" OR
134136
${ELEM} STREQUAL "EMPTY_DIRECTORY" OR
135137
${ELEM} STREQUAL "CREATE_FILE" OR
136-
${ELEM} STREQUAL "CREATE_DIR" OR
138+
${ELEM} STREQUAL "CREATE_DIRECTORY" OR
137139
${ELEM} STREQUAL "COMPARE_FILES" OR
138140
${ELEM} STREQUAL "COMPARE_DIRECTORIES")
139141
SET(CURRENT_CMD "${ELEM}")

src/apps/openfluid/WareTasks.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,58 @@ int WareTasks::processCreate() const
215215
// =====================================================================
216216

217217

218+
int WareTasks::processImport() const
219+
{
220+
const auto ParentPath = (m_Cmd.getOptionValue("parent-path").empty() ? openfluid::tools::Filesystem::currentPath() :
221+
m_Cmd.getOptionValue("parent-path"));
222+
223+
std::string ID = m_Cmd.getOptionValue("id");
224+
std::string SourceType = "hub";
225+
std::string SourceURL = "";
226+
227+
if (m_Cmd.isOptionActive("git"))
228+
{
229+
SourceType = "git";
230+
SourceURL = m_Cmd.getOptionValue("git");
231+
if (m_Cmd.isOptionActive("id"))
232+
{
233+
ID = m_Cmd.getOptionValue("id");
234+
if (!openfluid::tools::isValidWareID(ID))
235+
{
236+
return error("invalid ware ID");
237+
}
238+
}
239+
}
240+
else if (m_Cmd.isOptionActive("hub"))
241+
{
242+
if (!m_Cmd.isOptionActive("id"))
243+
{
244+
return error("missing ware ID");
245+
}
246+
ID = m_Cmd.getOptionValue("id");
247+
248+
if (!openfluid::tools::isValidWareID(ID))
249+
{
250+
return error("invalid ware ID");
251+
}
252+
if (!m_Cmd.isOptionActive("type"))
253+
{
254+
return error("missing ware type");
255+
}
256+
SourceURL = m_Cmd.getOptionValue("hub");
257+
}
258+
else
259+
{
260+
return error("remote URL missing: --git=<url> or --hub[=<url>] must be provided");
261+
}
262+
return openfluid::waresdev::cloneWare(SourceURL, SourceType, ParentPath, ID, m_Cmd.getOptionValue("type"));
263+
}
264+
265+
266+
// =====================================================================
267+
// =====================================================================
268+
269+
218270
int WareTasks::processConfigure() const
219271
{
220272
if (!openfluid::utils::CMakeProxy::isAvailable())
@@ -747,6 +799,10 @@ int WareTasks::process() const
747799
{
748800
return processCreate();
749801
}
802+
else if (m_Cmd.getName() == "import-ware")
803+
{
804+
return processImport();
805+
}
750806
else if (m_Cmd.getName() == "check")
751807
{
752808
return processCheck();

src/apps/openfluid/WareTasks.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class WareTasks : public TasksBase
5353

5454
int processCreate() const;
5555

56+
int processImport() const;
57+
5658
int processConfigure() const;
5759

5860
int processBuild() const;

src/apps/openfluid/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@ int main(int argc, char **argv)
213213

214214
// ---
215215

216+
auto ImportWareCmd = openfluid::utils::CommandLineCommand("import-ware","Import ware sources from remote git or hub");
217+
ImportWareCmd.addOptions({{"parent-path","p","parent path where to create the ware sources",true},
218+
{"git","","git URL"},
219+
{"hub","","hub URL"},
220+
{"id","i","ID of the ware sources to create"},
221+
{"type","t","type of the ware sources to create (simulator|observer|builderext), required "
222+
"when importing from hub"}});
223+
Parser.addCommand(ImportWareCmd, &WareSection);
224+
225+
// ---
226+
216227
auto CheckCmd = openfluid::utils::CommandLineCommand("check","Checks ware sources for potential issues");
217228
CheckCmd.addOptions({{"src-path","s","path to the ware sources (required)",true},
218229
{"ignore","i","ignore checks (comma separated list)"},
@@ -375,6 +386,7 @@ int main(int argc, char **argv)
375386
return ReportTasks(Parser).process();
376387
}
377388
else if (ActiveCmdStr == "create-ware" ||
389+
ActiveCmdStr == "import-ware" ||
378390
ActiveCmdStr == "check" ||
379391
ActiveCmdStr == "migrate-ware" ||
380392
ActiveCmdStr == "docalyze" ||

src/apps/openfluid/tests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,20 @@ SET_PROPERTY(TEST cmdline-openfluid-CreateWareSimulatorFromExisting APPEND PROPE
411411
###########################################################################
412412

413413

414+
OPENFLUID_ADD_TEST(NAME cmdline-openfluid-ImportSimulator
415+
COMMAND "${OFBUILD_DIST_BIN_DIR}/${OPENFLUID_CMD_APP}"
416+
"import-ware"
417+
"-p" "${OFBUILD_TMP_DIR}/cmdline/create-ware/sim"
418+
"--id=tests.cmdline.sim.ported"
419+
"--git=${OFBUILD_TESTS_GITHELPER_URL}"
420+
PRE_TEST REMOVE_DIRECTORY "${OFBUILD_TMP_DIR}/cmdline/create-ware/sim/tests.cmdline.sim.ported"
421+
PRE_TEST CREATE_DIRECTORY "${OFBUILD_TMP_DIR}/cmdline/create-ware/sim"
422+
POST_TEST CHECK_FILE_EXIST "${OFBUILD_TMP_DIR}/cmdline/create-ware/sim/tests.cmdline.sim.ported/master.txt")
423+
424+
425+
###########################################################################
426+
427+
414428
OPENFLUID_ADD_TEST(NAME cmdline-openfluid-CreateWareObserver
415429
COMMAND "${OFBUILD_DIST_BIN_DIR}/${OPENFLUID_CMD_APP}"
416430
"create-ware" "--type=observer" "--id=tests.cmdline.obs"

src/openfluid/tools/IDHelpers.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#include <string>
4848

4949
#include <openfluid/core/TypeDefs.hpp>
50-
//#include <openfluid/ware/WareSignature.hpp>
5150
#include <openfluid/ware/TypeDefs.hpp>
5251
#include <openfluid/dllexport.hpp>
5352
#include <openfluid/tools/VarHelpers.hpp>

src/openfluid/utils/GitProxy.cpp

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ bool GitProxy::isPathGitRepo(const std::string& Path)
138138
// =====================================================================
139139

140140

141+
int callRemoteProcess(openfluid::utils::Process::Command Cmd, std::string Context)
142+
{
143+
openfluid::utils::Process P(Cmd);
144+
P.run();
145+
if (P.getExitCode() == 0)
146+
{
147+
openfluid::utils::log::debug("Git", Context+" OK");
148+
}
149+
else
150+
{
151+
openfluid::utils::log::debug("Git", Context+" out: "+openfluid::tools::join(P.stdOutLines(), "\n"));
152+
openfluid::utils::log::error("Git", Context+" err: "+openfluid::tools::join(P.stdErrLines(), "\n"));
153+
}
154+
return P.getExitCode();
155+
}
156+
157+
158+
// =====================================================================
159+
// =====================================================================
160+
161+
162+
int GitProxy::clone(const std::string& Path, const std::string& URL, const std::string& LocalName)
163+
{
164+
openfluid::utils::Process::Command Cmd{
165+
.Program = m_ExecutablePath,
166+
.Args = {"clone", "--recurse-submodules", URL, LocalName},
167+
.WorkDir = Path
168+
};
169+
return callRemoteProcess(Cmd, "Git clone "+URL+" at "+Path+" as "+LocalName);
170+
}
171+
172+
173+
// =====================================================================
174+
// =====================================================================
175+
176+
141177
const std::string GitProxy::getCurrentBranchName(const std::string& Path)
142178
{
143179
if (!canGetBranch())
@@ -171,27 +207,6 @@ const std::string GitProxy::getCurrentBranchName(const std::string& Path)
171207
// =====================================================================
172208

173209

174-
int callRemoteProcess(openfluid::utils::Process::Command Cmd, std::string Context)
175-
{
176-
openfluid::utils::Process P(Cmd);
177-
P.run();
178-
if (P.getExitCode() == 0)
179-
{
180-
openfluid::utils::log::debug("Git", Context+" OK");
181-
}
182-
else
183-
{
184-
openfluid::utils::log::debug("Git", Context+" out: "+openfluid::tools::join(P.stdOutLines(), "\n"));
185-
openfluid::utils::log::error("Git", Context+" err: "+openfluid::tools::join(P.stdErrLines(), "\n"));
186-
}
187-
return P.getExitCode();
188-
}
189-
190-
191-
// =====================================================================
192-
// =====================================================================
193-
194-
195210
int GitProxy::setRemote(const std::string RepoPath, const std::string RemoteUrl)
196211
{
197212
if(isPathGitRepo(RepoPath))

src/openfluid/utils/GitProxy.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class OPENFLUID_API GitProxy : public ProgramProxy<GitProxy>
8080
return (openfluid::tools::compareVersions(m_Version, "2.21") >= 0);
8181
}
8282

83+
int clone(const std::string& Path, const std::string& URL, const std::string& LocalName="");
84+
8385
static const std::string getCurrentBranchName(const std::string& Path);
8486

8587
static int setRemote(const std::string RepoPath, const std::string RemoteUrl);

src/openfluid/utils/Process.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ bool Process::run()
170170
WorkDir = m_Cmd.WorkDir;
171171
}
172172

173+
if (!openfluid::tools::Path(WorkDir).isDirectory())
174+
{
175+
openfluid::utils::log::error("Process", "Working directory does not exist: "+WorkDir);
176+
return false;
177+
}
178+
173179
boost::process::child BPC(boost::process::exe = m_Cmd.Program,
174180
boost::process::args = m_Cmd.Args,
175181
boost::process::start_dir = WorkDir,

src/openfluid/waresdev/WareSrcHelpers.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <algorithm>
4444

4545
#include <openfluid/tools/Filesystem.hpp>
46+
#include <openfluid/tools/SettingsBackend.hpp>
4647
#include <openfluid/waresdev/WareSrcEnquirer.hpp>
4748
#include <openfluid/waresdev/WareSrcHelpers.hpp>
4849
#include <openfluid/config.hpp>
@@ -119,4 +120,33 @@ std::map<std::string,std::string> initializeConfigureVariables()
119120
}
120121

121122

123+
// =====================================================================
124+
// =====================================================================
125+
126+
127+
int OPENFLUID_API cloneWare(const std::string& SourceURL, const std::string& SourceType, const std::string& ParentPath,
128+
const std::string& WareID, const std::string& WareType)
129+
{
130+
std::string GitURL;
131+
if (SourceType=="hub")
132+
{
133+
std::string HubURL = SourceURL;
134+
if (HubURL.length() == 0)
135+
{
136+
// try to deduce hub info from openfluid config (only working with fluidhub-like URLs)
137+
openfluid::base::Environment::init();
138+
const auto Settings = openfluid::tools::SettingsBackend(openfluid::base::Environment::getSettingsFile());
139+
HubURL = Settings.getValue("/waresdev/ui/import/hub/url").get<std::string>();
140+
}
141+
GitURL = HubURL.substr(0,HubURL.length()-5)+"/git-service/wares/"+WareType+"s/"+WareID;
142+
}
143+
else
144+
{
145+
GitURL = SourceURL;
146+
}
147+
openfluid::utils::GitProxy Git;
148+
return Git.clone(ParentPath, GitURL, WareID);
149+
}
150+
151+
122152
} } // namespaces

0 commit comments

Comments
 (0)