Skip to content

Commit 070ba01

Browse files
Merge branch 'dev'
2 parents f25475e + a122b52 commit 070ba01

File tree

3 files changed

+128
-11
lines changed

3 files changed

+128
-11
lines changed

Include/runcpp2/runcpp2.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ namespace runcpp2
2525

2626
void SetLogLevel(const std::string& logLevel);
2727

28+
PipelineResult GetLatestSourceWriteTime(const std::string& scriptPath,
29+
const std::vector<Data::Profile>& profiles,
30+
const std::string& configPreferredProfile,
31+
const Data::ScriptInfo& scriptInfo,
32+
int64_t& outWriteTime);
33+
2834
PipelineResult StartPipeline( const std::string& scriptPath,
2935
const std::vector<Data::Profile>& profiles,
3036
const std::string& configPreferredProfile,

Src/runcpp2/main.cpp

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ int main(int argc, char* argv[])
344344
ssLOG_ERROR("Failed reset user config");
345345
return -1;
346346
}
347-
ssLOG_BASE("User config reset successful");
347+
ssLOG_LINE("User config reset successful");
348348
return 0;
349349
}
350350

@@ -355,7 +355,7 @@ int main(int argc, char* argv[])
355355
return -1;
356356
else
357357
{
358-
ssLOG_BASE("Script template generated");
358+
ssLOG_LINE("Script template generated");
359359
return 0;
360360
}
361361
}
@@ -415,12 +415,37 @@ int main(int argc, char* argv[])
415415
return -1;
416416
}
417417

418-
ghc::filesystem::file_time_type lastScriptWriteTime {};
418+
int64_t lastSourceWriteTime = 0;
419+
runcpp2::Data::ScriptInfo* lastParsedScriptInfo = nullptr;
420+
419421
while(true)
420422
{
421-
runcpp2::Data::ScriptInfo* lastParsedScriptInfo = nullptr;
423+
bool needsRunning = false;
424+
int64_t currentWriteTime = 0;
422425

423-
if(ghc::filesystem::last_write_time(script, e) > lastScriptWriteTime)
426+
runcpp2::PipelineResult result =
427+
runcpp2::GetLatestSourceWriteTime( script,
428+
profiles,
429+
preferredProfile,
430+
lastParsedScriptInfo ?
431+
*lastParsedScriptInfo :
432+
runcpp2::Data::ScriptInfo(),
433+
currentWriteTime);
434+
435+
if(result != runcpp2::PipelineResult::SUCCESS)
436+
{
437+
ssLOG_ERROR("Failed to get latest source write time");
438+
return -1;
439+
}
440+
441+
if(currentWriteTime > lastSourceWriteTime)
442+
{
443+
ssLOG_INFO("Source files have changed");
444+
needsRunning = true;
445+
lastSourceWriteTime = currentWriteTime;
446+
}
447+
448+
if(needsRunning)
424449
{
425450
int result = 0;
426451

@@ -453,16 +478,36 @@ int main(int argc, char* argv[])
453478
case runcpp2::PipelineResult::INVALID_PROFILE:
454479
case runcpp2::PipelineResult::RUN_SCRIPT_FAILED:
455480
case runcpp2::PipelineResult::INVALID_OPTION:
456-
ssLOG_BASE("Watching...");
481+
ssLOG_LINE("Watching...");
457482
break;
458483
case runcpp2::PipelineResult::SUCCESS:
459-
ssLOG_BASE("No error. Watching...");
484+
ssLOG_LINE("No error. Watching...");
460485
break;
461486
}
487+
488+
//Upate the timestamp if we managed to parse the script info for the first time
489+
if( !lastParsedScriptInfo &&
490+
pipelineResult != runcpp2::PipelineResult::INVALID_SCRIPT_INFO)
491+
{
492+
runcpp2::PipelineResult result =
493+
runcpp2::GetLatestSourceWriteTime( script,
494+
profiles,
495+
preferredProfile,
496+
parsedScriptInfo,
497+
currentWriteTime);
498+
499+
if(result != runcpp2::PipelineResult::SUCCESS)
500+
{
501+
ssLOG_ERROR("Failed to get latest source write time");
502+
return -1;
503+
}
504+
505+
lastSourceWriteTime = currentWriteTime;
506+
}
507+
508+
lastParsedScriptInfo = &parsedScriptInfo;
462509
}
463510

464-
lastScriptWriteTime = ghc::filesystem::last_write_time(script, e);
465-
lastParsedScriptInfo = &parsedScriptInfo;
466511
std::this_thread::sleep_for(std::chrono::seconds(5));
467512
}
468513
}
@@ -487,7 +532,7 @@ int main(int argc, char* argv[])
487532
}
488533

489534
if(currentOptions.count(runcpp2::CmdOptions::CLEANUP) > 0)
490-
ssLOG_BASE("Cleanup successful");
535+
ssLOG_LINE("Cleanup successful");
491536

492537
return result;
493538
}

Src/runcpp2/runcpp2.cpp

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,70 @@ void runcpp2::SetLogLevel(const std::string& logLevel)
238238
ssLOG_ERROR("Invalid log level: " << logLevel);
239239
}
240240

241+
runcpp2::PipelineResult
242+
runcpp2::GetLatestSourceWriteTime( const std::string& scriptPath,
243+
const std::vector<Data::Profile>& profiles,
244+
const std::string& configPreferredProfile,
245+
const Data::ScriptInfo& scriptInfo,
246+
int64_t& outWriteTime)
247+
{
248+
INTERNAL_RUNCPP2_SAFE_START();
249+
ssLOG_FUNC_DEBUG();
250+
251+
//Validate inputs and get paths
252+
ghc::filesystem::path absoluteScriptPath;
253+
ghc::filesystem::path scriptDirectory;
254+
std::string scriptName;
255+
256+
PipelineResult result = ValidateInputs( scriptPath,
257+
profiles,
258+
absoluteScriptPath,
259+
scriptDirectory,
260+
scriptName);
261+
if(result != PipelineResult::SUCCESS)
262+
return result;
263+
264+
std::error_code e;
265+
if(!ghc::filesystem::exists(absoluteScriptPath, e))
266+
{
267+
ssLOG_ERROR("Script path " << absoluteScriptPath << " doesn't exist");
268+
return PipelineResult::INVALID_SCRIPT_PATH;
269+
}
270+
271+
outWriteTime = std::chrono::duration_cast<std::chrono::seconds>(
272+
ghc::filesystem::last_write_time(absoluteScriptPath, e).time_since_epoch()).count();
273+
274+
std::vector<ghc::filesystem::path> sourceFiles;
275+
if(GatherSourceFiles( absoluteScriptPath,
276+
scriptInfo,
277+
profiles.at(GetPreferredProfileIndex( scriptPath,
278+
scriptInfo,
279+
profiles,
280+
configPreferredProfile)),
281+
sourceFiles))
282+
{
283+
for(const ghc::filesystem::path& sourcePath : sourceFiles)
284+
{
285+
if(ghc::filesystem::exists(sourcePath, e))
286+
{
287+
ghc::filesystem::file_time_type currentWriteTime =
288+
ghc::filesystem::last_write_time(sourcePath, e);
289+
290+
using namespace std::chrono;
291+
int64_t currentWriteTimeCount =
292+
duration_cast<seconds>(currentWriteTime.time_since_epoch()).count();
293+
294+
if(currentWriteTimeCount > outWriteTime)
295+
outWriteTime = currentWriteTimeCount;
296+
}
297+
}
298+
}
299+
300+
return PipelineResult::SUCCESS;
301+
302+
INTERNAL_RUNCPP2_SAFE_CATCH_RETURN(PipelineResult::UNEXPECTED_FAILURE);
303+
}
304+
241305
runcpp2::PipelineResult
242306
runcpp2::StartPipeline( const std::string& scriptPath,
243307
const std::vector<Data::Profile>& profiles,
@@ -349,7 +413,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
349413
if(result != PipelineResult::SUCCESS)
350414
return result;
351415

352-
if(recompileNeeded || !changedDependencies.empty() || relinkNeeded)
416+
if(!lastScriptInfo || recompileNeeded || !changedDependencies.empty() || relinkNeeded)
353417
outScriptInfo = scriptInfo;
354418

355419
std::vector<std::string> gatheredBinariesPaths;
@@ -473,6 +537,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
473537
{
474538
return PipelineResult::COMPILE_LINK_FAILED;
475539
}
540+
541+
return PipelineResult::SUCCESS;
476542
}
477543
else if(!CompileAndLinkScript( buildDir,
478544
absoluteScriptPath,

0 commit comments

Comments
 (0)