Skip to content

Commit d682154

Browse files
Refactoring pipeline and command line options handling/feedback
1 parent 9bd9265 commit d682154

File tree

6 files changed

+117
-129
lines changed

6 files changed

+117
-129
lines changed

Include/runcpp2/Data/CmdOptions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace runcpp2
1616
SCRIPT_TEMPLATE,
1717
WATCH,
1818
BUILD,
19+
OUTPUT,
1920
VERSION,
2021
LOG_LEVEL,
2122
CONFIG_FILE,

Include/runcpp2/PipelineSteps.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,6 @@ namespace runcpp2
103103
const std::unordered_map<CmdOptions, std::string>& currentOptions,
104104
int& returnStatus);
105105

106-
PipelineResult
107-
HandleBuildOutput( const std::vector<ghc::filesystem::path>& targets,
108-
const std::vector<std::string>& filesToCopyPaths,
109-
const Data::ScriptInfo& scriptInfo,
110-
const Data::Profile& profile,
111-
const std::string& buildOutputDir,
112-
const std::unordered_map<CmdOptions, std::string>& currentOptions);
113-
114106
PipelineResult GetBuiltTargetPaths( const ghc::filesystem::path& buildDir,
115107
const std::string& scriptName,
116108
const Data::Profile& profile,

Include/runcpp2/runcpp2.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ namespace runcpp2
1414
struct OptionInfo
1515
{
1616
CmdOptions Option;
17-
bool HasValue;
17+
bool ValueExists;
1818
std::string Value;
1919

20-
OptionInfo(CmdOptions option, bool hasValue = false, const std::string& value = "")
21-
: Option(option), HasValue(hasValue), Value(value) {}
20+
inline OptionInfo( CmdOptions option,
21+
bool valueExists = false,
22+
const std::string& value = "") : Option(option),
23+
ValueExists(valueExists),
24+
Value(value)
25+
{}
2226
};
2327

2428
void GetDefaultScriptInfo(std::string& scriptInfo);

Src/runcpp2/PipelineSteps.cpp

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,7 @@ runcpp2::ProcessDependencies( Data::ScriptInfo& scriptInfo,
791791
}
792792

793793
if(currentOptions.count(CmdOptions::RESET_DEPENDENCIES) > 0)
794-
{
795-
ssLOG_LINE("Removed script dependencies");
796794
return PipelineResult::SUCCESS;
797-
}
798795

799796
if(!SetupDependenciesIfNeeded( profile,
800797
buildDir,
@@ -979,50 +976,6 @@ runcpp2::RunCompiledOutput( const ghc::filesystem::path& target,
979976
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
980977
}
981978

982-
runcpp2::PipelineResult
983-
runcpp2::HandleBuildOutput( const std::vector<ghc::filesystem::path>& targets,
984-
const std::vector<std::string>& filesToCopyPaths,
985-
const Data::ScriptInfo& scriptInfo,
986-
const Data::Profile& profile,
987-
const std::string& buildOutputDir,
988-
const std::unordered_map<CmdOptions, std::string>& currentOptions)
989-
{
990-
ssLOG_FUNC_INFO();
991-
INTERNAL_RUNCPP2_SAFE_START();
992-
993-
if(targets.empty())
994-
{
995-
ssLOG_WARNING("No target files found");
996-
return PipelineResult::SUCCESS;
997-
}
998-
999-
//Copy all output files and dependency files
1000-
std::vector<std::string> filesToCopy = filesToCopyPaths;
1001-
for(const ghc::filesystem::path& target : targets)
1002-
filesToCopy.push_back(target.string());
1003-
1004-
std::vector<std::string> copiedPaths;
1005-
if(!CopyFiles(buildOutputDir, filesToCopy, copiedPaths))
1006-
{
1007-
ssLOG_ERROR("Failed to copy binaries before running the script");
1008-
return PipelineResult::UNEXPECTED_FAILURE;
1009-
}
1010-
1011-
//Run PostBuild commands after successful compilation
1012-
PipelineResult result = HandlePostBuild(scriptInfo, profile, buildOutputDir);
1013-
if(result != PipelineResult::SUCCESS)
1014-
return result;
1015-
1016-
//Don't output anything here if we are just watching
1017-
if(currentOptions.count(CmdOptions::WATCH) > 0)
1018-
return PipelineResult::SUCCESS;
1019-
1020-
ssLOG_BASE("Build completed. Files copied to " << buildOutputDir);
1021-
return PipelineResult::SUCCESS;
1022-
1023-
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
1024-
}
1025-
1026979
runcpp2::PipelineResult
1027980
runcpp2::GetBuiltTargetPaths( const ghc::filesystem::path& buildDir,
1028981
const std::string& scriptName,

Src/runcpp2/main.cpp

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,21 @@ int ParseArgs( const std::unordered_map<std::string, runcpp2::OptionInfo>& long
4747
continue;
4848
}
4949

50-
//Checking for options
50+
//Matched long or short options
5151
if(longOptionsMap.count(currentArg) || shortOptionsMap.count(currentArg))
5252
{
5353
currentArgIndex = i;
5454
ssLOG_DEBUG("currentArgIndex: " << currentArgIndex);
5555
ssLOG_DEBUG("argv: " << argv[i]);
56-
5756
const runcpp2::OptionInfo& currentInfo = longOptionsMap.count(currentArg) ?
5857
longOptionsMap.at(currentArg) :
5958
shortOptionsMap.at(currentArg);
60-
61-
if(currentInfo.HasValue)
62-
{
59+
if(currentInfo.ValueExists)
6360
optionForCapturingValue = currentInfo.Option;
64-
continue;
65-
}
6661
else
67-
{
6862
outOptions[currentInfo.Option] = "";
69-
continue;
70-
}
63+
64+
continue;
7165
}
7266
else if(!currentArg.empty() && currentArg[0] == '-')
7367
{
@@ -174,11 +168,11 @@ int main(int argc, char* argv[])
174168
int currentArgIndex = 0;
175169
std::unordered_map<runcpp2::CmdOptions, std::string> currentOptions;
176170
{
177-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 17, "Update this");
171+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 19, "Update this");
178172
std::unordered_map<std::string, runcpp2::OptionInfo> longOptionsMap =
179173
{
180174
{
181-
"--reset-cache",
175+
"--rebuild",
182176
runcpp2::OptionInfo(runcpp2::CmdOptions::RESET_CACHE, false)
183177
},
184178
{
@@ -217,6 +211,10 @@ int main(int argc, char* argv[])
217211
"--build",
218212
runcpp2::OptionInfo(runcpp2::CmdOptions::BUILD, false)
219213
},
214+
{
215+
"--output",
216+
runcpp2::OptionInfo(runcpp2::CmdOptions::OUTPUT, true)
217+
},
220218
{
221219
"--version",
222220
runcpp2::OptionInfo(runcpp2::CmdOptions::VERSION, false)
@@ -234,19 +232,23 @@ int main(int argc, char* argv[])
234232
runcpp2::OptionInfo(runcpp2::CmdOptions::CLEANUP, false)
235233
},
236234
{
237-
"--build-source-only",
235+
"--source-only",
238236
runcpp2::OptionInfo(runcpp2::CmdOptions::BUILD_SOURCE_ONLY, false)
239237
},
240238
{
241239
"--jobs",
242240
runcpp2::OptionInfo(runcpp2::CmdOptions::THREADS, true)
243241
},
242+
{
243+
"--tutorial",
244+
runcpp2::OptionInfo(runcpp2::CmdOptions::TUTORIAL, false)
245+
},
244246
};
245247

246-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 17, "Update this");
248+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 19, "Update this");
247249
std::unordered_map<std::string, const runcpp2::OptionInfo&> shortOptionsMap =
248250
{
249-
{"-rc", longOptionsMap.at("--reset-cache")},
251+
{"-rb", longOptionsMap.at("--rebuild")},
250252
{"-ru", longOptionsMap.at("--reset-user-config")},
251253
{"-e", longOptionsMap.at("--executable")},
252254
{"-h", longOptionsMap.at("--help")},
@@ -256,18 +258,19 @@ int main(int argc, char* argv[])
256258
{"-t", longOptionsMap.at("--create-script-template")},
257259
{"-w", longOptionsMap.at("--watch")},
258260
{"-b", longOptionsMap.at("--build")},
261+
{"-o", longOptionsMap.at("--output")},
259262
{"-v", longOptionsMap.at("--version")},
260263
{"-c", longOptionsMap.at("--config")},
261264
{"-cu", longOptionsMap.at("--cleanup")},
262-
{"-s", longOptionsMap.at("--build-source-only")},
265+
{"-s", longOptionsMap.at("--source-only")},
263266
{"-j", longOptionsMap.at("--jobs")},
264267
};
265268

266269
currentArgIndex = ParseArgs(longOptionsMap, shortOptionsMap, currentOptions, argc, argv);
267270

268271
if(currentArgIndex == -1)
269272
{
270-
ssLOG_ERROR("Invalid option");
273+
ssLOG_ERROR("Failed to parse arguments. See --help for details");
271274
return -1;
272275
}
273276

@@ -277,31 +280,31 @@ int main(int argc, char* argv[])
277280
//Help message
278281
if(currentOptions.count(runcpp2::CmdOptions::HELP))
279282
{
280-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 17, "Update this");
283+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 19, "Update this");
281284
ssLOG_BASE("Usage: runcpp2 [options] [input_file]");
282285
ssLOG_BASE("Options:");
283286
ssLOG_BASE(" Run/Build:");
284-
ssLOG_BASE(" -b, --[b]uild Build the script and copy output files to the working directory");
287+
ssLOG_BASE(" -b, --[b]uild Build the script but don't run it");
288+
ssLOG_BASE(" -o, --[o]utput <Output Dir> Output files to the directory specified, must be used with --build");
285289
ssLOG_BASE(" -w, --[w]atch Watch script changes and output any compiling errors");
286290
ssLOG_BASE(" -l, --[l]ocal Build in the current working directory under .runcpp2 directory");
287291
ssLOG_BASE(" -e, --[e]xecutable Runs as executable instead of shared library");
288292
ssLOG_BASE(" -c, --[c]onfig <file> Use specified config file instead of default");
289293
ssLOG_BASE(" -t, --create-script-[t]emplate <file> Creates/prepend runcpp2 script info template");
290-
ssLOG_BASE(" -s, --build-[s]ource-only (Re)Builds source files only without building dependencies.");
294+
ssLOG_BASE(" -s, --[s]ource-only (Re)Builds source files only without building dependencies.");
291295
ssLOG_BASE(" The previous built binaries will be used for dependencies.");
292296
ssLOG_BASE(" Requires dependencies to be built already.");
293297
ssLOG_BASE(" -j, --[j]obs Maximum number of threads running. Defaults to 8");
294298
ssLOG_BASE(" Reset/Cleanup:");
295-
ssLOG_BASE(" -rc, --[r]eset-[c]ache Deletes compiled source files cache only");
299+
ssLOG_BASE(" -rb, --[r]e[b]uild Deletes compiled source files cache and rebuild");
296300
ssLOG_BASE(" -ru, --[r]eset-[u]ser-config Replace current user config with the default one");
297301
ssLOG_BASE(" -rd, --[r]eset-[d]ependencies <names> Reset dependencies (comma-separated names, or \"all\" for all)");
298302
ssLOG_BASE(" -cu, --[c]lean[u]p Run cleanup commands and remove build directory");
299303
ssLOG_BASE(" Settings:");
300304
ssLOG_BASE(" -sc, --[s]how-[c]onfig-path Show where runcpp2 is reading the config from");
301305
ssLOG_BASE(" -v, --[v]ersion Show the version of runcpp2");
302306
ssLOG_BASE(" -h, --[h]elp Show this help message");
303-
ssLOG_BASE(" --log-level <level> Sets the log level (Normal, Info, Debug) for runcpp2.");
304-
307+
ssLOG_BASE(" --log-level <level> Sets the log level (Normal, Info, Debug) for runcpp2");
305308
return 0;
306309
}
307310

@@ -327,6 +330,7 @@ int main(int argc, char* argv[])
327330
}
328331

329332
ssLOG_FUNC_INFO();
333+
INTERNAL_RUNCPP2_SAFE_START();
330334

331335
//Show user config path
332336
if(currentOptions.count(runcpp2::CmdOptions::SHOW_USER_CONFIG))
@@ -351,7 +355,7 @@ int main(int argc, char* argv[])
351355
ssLOG_ERROR("Failed reset user config");
352356
return -1;
353357
}
354-
ssLOG_LINE("User config reset successful");
358+
ssLOG_BASE("User config reset successful");
355359
return 0;
356360
}
357361

@@ -363,7 +367,7 @@ int main(int argc, char* argv[])
363367
return -1;
364368
else
365369
{
366-
ssLOG_LINE("Script template generated");
370+
ssLOG_BASE("Script template generated");
367371
return 0;
368372
}
369373
}
@@ -388,7 +392,7 @@ int main(int argc, char* argv[])
388392
std::vector<std::string> scriptArgs;
389393
if(currentArgIndex >= argc)
390394
{
391-
ssLOG_ERROR("An input file is required");
395+
ssLOG_ERROR("An input file is required. See --help for details");
392396
return 1;
393397
}
394398

@@ -412,6 +416,13 @@ int main(int argc, char* argv[])
412416
return -1;
413417
}
414418

419+
if( currentOptions.count(runcpp2::CmdOptions::OUTPUT) > 0 &&
420+
currentOptions.count(runcpp2::CmdOptions::BUILD) == 0)
421+
{
422+
ssLOG_ERROR("--build option must be supplied when specifying output directory");
423+
return -1;
424+
}
425+
415426
runcpp2::Data::ScriptInfo parsedScriptInfo;
416427

417428
if(currentOptions.count(runcpp2::CmdOptions::WATCH))
@@ -515,18 +526,24 @@ int main(int argc, char* argv[])
515526
}
516527

517528
std::this_thread::sleep_for(std::chrono::seconds(5));
518-
}
519-
}
529+
} //while(true)
530+
} //if(currentOptions.count(runcpp2::CmdOptions::WATCH))
520531

521532
int result = 0;
522533

523534
std::string outputDir;
524-
if(currentOptions.count(runcpp2::CmdOptions::BUILD) > 0)
525-
outputDir = ghc::filesystem::current_path().string();
535+
if( currentOptions.count(runcpp2::CmdOptions::BUILD) > 0 &&
536+
currentOptions.count(runcpp2::CmdOptions::OUTPUT) > 0)
537+
{
538+
auto buildOutputDir = ghc::filesystem::path(currentOptions.at(runcpp2::CmdOptions::OUTPUT));
539+
if(buildOutputDir.is_absolute())
540+
outputDir = buildOutputDir.string();
541+
else
542+
outputDir = (ghc::filesystem::current_path() / buildOutputDir).string();
543+
}
526544

527545
ghc::filesystem::file_time_type finalSourceWriteTime;
528546
ghc::filesystem::file_time_type finalIncludeWriteTime;
529-
530547
if(runcpp2::StartPipeline( script,
531548
profiles,
532549
preferredProfile,
@@ -542,9 +559,27 @@ int main(int argc, char* argv[])
542559
return -1;
543560
}
544561

562+
std::vector<std::string> actions;
563+
if(currentOptions.count(runcpp2::CmdOptions::RESET_CACHE) > 0)
564+
actions.push_back("Rebuild");
545565
if(currentOptions.count(runcpp2::CmdOptions::CLEANUP) > 0)
546-
ssLOG_LINE("Cleanup successful");
566+
actions.push_back("Cleanup");
567+
if(currentOptions.count(runcpp2::CmdOptions::RESET_DEPENDENCIES) > 0)
568+
actions.push_back("Dependencies Reset");
569+
if(currentOptions.count(runcpp2::CmdOptions::BUILD) > 0)
570+
actions.push_back("Build");
547571

572+
std::string action;
573+
if(!actions.empty())
574+
{
575+
action = actions.front();
576+
for(int i = 1; i < actions.size(); ++i)
577+
action += ", " + actions[i];
578+
579+
ssLOG_BASE(action << " success");
580+
}
548581
return result;
582+
583+
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(-1);
549584
}
550585

0 commit comments

Comments
 (0)