Skip to content

Commit 46fec18

Browse files
Extracting RunProfileCommands
1 parent 824848d commit 46fec18

File tree

2 files changed

+66
-87
lines changed

2 files changed

+66
-87
lines changed

Include/runcpp2/runcpp2.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ namespace runcpp2
6363

6464
void SetLogLevel(const std::string& logLevel);
6565

66+
PipelineResult RunProfileCommands( const Data::ProfilesCommands* commands,
67+
const Data::Profile& profile,
68+
const std::string& workingDir,
69+
const std::string& commandType);
70+
6671
PipelineResult ValidateInputs( const std::string& scriptPath,
6772
const std::vector<Data::Profile>& profiles,
6873
ghc::filesystem::path& outAbsoluteScriptPath,
@@ -118,6 +123,10 @@ namespace runcpp2
118123
const Data::Profile& profile,
119124
const ghc::filesystem::path& buildDir);
120125

126+
PipelineResult HandlePostBuild( const Data::ScriptInfo& scriptInfo,
127+
const Data::Profile& profile,
128+
const ghc::filesystem::path& buildDir);
129+
121130
PipelineResult
122131
RunCompiledOutput( const ghc::filesystem::path& target,
123132
const ghc::filesystem::path& absoluteScriptPath,

Src/runcpp2/runcpp2.cpp

Lines changed: 57 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -505,39 +505,6 @@ namespace
505505

506506
return true;
507507
}
508-
509-
bool RunPostBuildCommands( const runcpp2::Data::ScriptInfo& scriptInfo,
510-
const runcpp2::Data::Profile& profile,
511-
const std::string& outputDir)
512-
{
513-
const runcpp2::Data::ProfilesCommands* postBuildCommands =
514-
runcpp2::GetValueFromPlatformMap(scriptInfo.PostBuild);
515-
516-
if(postBuildCommands != nullptr)
517-
{
518-
const std::vector<std::string>* commands =
519-
runcpp2::GetValueFromProfileMap(profile, postBuildCommands->CommandSteps);
520-
if(commands != nullptr)
521-
{
522-
for(const std::string& cmd : *commands)
523-
{
524-
std::string output;
525-
int returnCode = 0;
526-
if(!runcpp2::RunCommandAndGetOutput(cmd, output, returnCode, outputDir))
527-
{
528-
ssLOG_ERROR("PostBuild command failed: " << cmd <<
529-
" with return code " << returnCode);
530-
ssLOG_ERROR("Output: \n" << output);
531-
return false;
532-
}
533-
534-
ssLOG_INFO("PostBuild command ran: \n" << cmd);
535-
ssLOG_INFO("PostBuild command output: \n" << output);
536-
}
537-
}
538-
}
539-
return true;
540-
}
541508
}
542509

543510
void runcpp2::GetDefaultScriptInfo(std::string& scriptInfo)
@@ -559,6 +526,39 @@ void runcpp2::SetLogLevel(const std::string& logLevel)
559526
ssLOG_ERROR("Invalid log level: " << logLevel);
560527
}
561528

529+
runcpp2::PipelineResult
530+
runcpp2::RunProfileCommands(const Data::ProfilesCommands* commands,
531+
const Data::Profile& profile,
532+
const std::string& workingDir,
533+
const std::string& commandType)
534+
{
535+
if(commands != nullptr)
536+
{
537+
const std::vector<std::string>* commandSteps =
538+
runcpp2::GetValueFromProfileMap(profile, commands->CommandSteps);
539+
540+
if(commandSteps != nullptr)
541+
{
542+
for(const std::string& cmd : *commandSteps)
543+
{
544+
std::string output;
545+
int returnCode = 0;
546+
if(!runcpp2::RunCommandAndGetOutput(cmd, output, returnCode, workingDir))
547+
{
548+
ssLOG_ERROR(commandType << " command failed: " << cmd <<
549+
" with return code " << returnCode);
550+
ssLOG_ERROR("Output: \n" << output);
551+
return PipelineResult::UNEXPECTED_FAILURE;
552+
}
553+
554+
ssLOG_INFO(commandType << " command ran: \n" << cmd);
555+
ssLOG_INFO(commandType << " command output: \n" << output);
556+
}
557+
}
558+
}
559+
return PipelineResult::SUCCESS;
560+
}
561+
562562
runcpp2::PipelineResult runcpp2::ValidateInputs(const std::string& scriptPath,
563563
const std::vector<Data::Profile>& profiles,
564564
ghc::filesystem::path& outAbsoluteScriptPath,
@@ -786,26 +786,12 @@ runcpp2::CheckScriptInfoChanges(const ghc::filesystem::path& buildDir,
786786

787787
if(setupCommands != nullptr)
788788
{
789-
const std::vector<std::string>* commands =
790-
runcpp2::GetValueFromProfileMap(profile, setupCommands->CommandSteps);
791-
if(commands != nullptr)
792-
{
793-
for(const std::string& cmd : *commands)
794-
{
795-
std::string output;
796-
int returnCode = 0;
797-
if(!runcpp2::RunCommandAndGetOutput(cmd, output, returnCode, scriptDirectory))
798-
{
799-
ssLOG_ERROR("Setup command failed: " << cmd <<
800-
" with return code " << returnCode);
801-
ssLOG_ERROR("Output: \n" << output);
802-
return PipelineResult::UNEXPECTED_FAILURE;
803-
}
804-
805-
ssLOG_INFO("Setup command ran: \n" << cmd);
806-
ssLOG_INFO("Setup command output: \n" << output);
807-
}
808-
}
789+
PipelineResult result = RunProfileCommands( setupCommands,
790+
profile,
791+
scriptDirectory.string(),
792+
"Setup");
793+
if(result != PipelineResult::SUCCESS)
794+
return result;
809795
}
810796
}
811797

@@ -1067,41 +1053,24 @@ void runcpp2::SeparateDependencyFiles( const Data::FilesTypesInfo& filesTypes,
10671053
ssLOG_INFO(" " << outFilesToCopyPaths[i]);
10681054
}
10691055

1070-
runcpp2::PipelineResult runcpp2::HandlePreBuild( const Data::ScriptInfo& scriptInfo,
1071-
const Data::Profile& profile,
1072-
const ghc::filesystem::path& buildDir)
1056+
runcpp2::PipelineResult runcpp2::HandlePreBuild(const Data::ScriptInfo& scriptInfo,
1057+
const Data::Profile& profile,
1058+
const ghc::filesystem::path& buildDir)
10731059
{
10741060
const Data::ProfilesCommands* preBuildCommands =
10751061
runcpp2::GetValueFromPlatformMap(scriptInfo.PreBuild);
10761062

1077-
if(preBuildCommands != nullptr)
1078-
{
1079-
const std::vector<std::string>* commands =
1080-
runcpp2::GetValueFromProfileMap(profile, preBuildCommands->CommandSteps);
1081-
if(commands != nullptr)
1082-
{
1083-
for(const std::string& cmd : *commands)
1084-
{
1085-
std::string output;
1086-
int returnCode = 0;
1087-
if(!runcpp2::RunCommandAndGetOutput(cmd,
1088-
output,
1089-
returnCode,
1090-
buildDir.string()))
1091-
{
1092-
ssLOG_ERROR("PreBuild command failed: " << cmd <<
1093-
" with return code " << returnCode);
1094-
ssLOG_ERROR("Output: \n" << output);
1095-
return PipelineResult::UNEXPECTED_FAILURE;
1096-
}
1097-
1098-
ssLOG_INFO("PreBuild command ran: \n" << cmd);
1099-
ssLOG_INFO("PreBuild command output: \n" << output);
1100-
}
1101-
}
1102-
}
1063+
return RunProfileCommands(preBuildCommands, profile, buildDir.string(), "PreBuild");
1064+
}
1065+
1066+
runcpp2::PipelineResult runcpp2::HandlePostBuild( const Data::ScriptInfo& scriptInfo,
1067+
const Data::Profile& profile,
1068+
const ghc::filesystem::path& buildDir)
1069+
{
1070+
const Data::ProfilesCommands* postBuildCommands =
1071+
GetValueFromPlatformMap(scriptInfo.PostBuild);
11031072

1104-
return PipelineResult::SUCCESS;
1073+
return RunProfileCommands(postBuildCommands, profile, buildDir.string(), "PostBuild");
11051074
}
11061075

11071076
runcpp2::PipelineResult
@@ -1164,8 +1133,9 @@ runcpp2::HandleBuildOutput( const ghc::filesystem::path& target,
11641133
}
11651134

11661135
//Run PostBuild commands after successful compilation
1167-
if(!RunPostBuildCommands(scriptInfo, profile, buildOutputDir))
1168-
return PipelineResult::UNEXPECTED_FAILURE;
1136+
PipelineResult result = HandlePostBuild(scriptInfo, profile, buildOutputDir);
1137+
if(result != PipelineResult::SUCCESS)
1138+
return result;
11691139

11701140
//Don't output anything here if we are just watching
11711141
if(currentOptions.count(CmdOptions::WATCH) > 0)
@@ -1483,7 +1453,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
14831453
}
14841454

14851455
//Run PostBuild commands after successful compilation
1486-
if(!RunPostBuildCommands(scriptInfo, profiles.at(profileIndex), buildDir.string()))
1456+
result = HandlePostBuild(scriptInfo, profiles.at(profileIndex), buildDir.string());
1457+
if(result != PipelineResult::SUCCESS)
14871458
return PipelineResult::UNEXPECTED_FAILURE;
14881459

14891460
//Don't run if we are just watching
@@ -1550,4 +1521,3 @@ std::string runcpp2::PipelineResultToString(PipelineResult result)
15501521
return "UNKNOWN_PIPELINE_RESULT";
15511522
}
15521523
}
1553-

0 commit comments

Comments
 (0)