Skip to content

Commit 96faa6b

Browse files
Merge pull request #12 from Neko-Box-Coder/ResetOnScriptInfoChange
Reset cache on script info change
2 parents ccbdef5 + c5545fb commit 96faa6b

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

DefaultYAMLs/DefaultScriptInfo.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@
8181
# # Properties for searching the library binary for the profile
8282
# # You can also use "All" if all compilers use the same values
8383
# "g++":
84-
# # The library names to be searched for when linking against the script
84+
# # The library names to be searched for when linking against the script.
85+
# # Binaries with linkable extension that contains one of the names will be linked
8586
# SearchLibraryNames: ["MyLibrary"]
8687
#
87-
# # (Optional) The library names to be excluded from being searched
88+
# # (Optional) The library names to be excluded from being searched.
89+
# # Works the same as SearchLibraryNames but will NOT be linked instead
8890
# ExcludeLibraryNames: []
8991
#
9092
# # The path (relative to the dependency folder) to be searched for the dependency binaries
9193
# SearchDirectories: ["./build"]
9294
#
93-
# # (Optional) Additional link options for this dependency for each platform
95+
# # (Optional) Additional link flags for this dependency for each platform
9496
# AdditionalLinkOptions:
9597
# All: []
9698
#

Include/runcpp2/runcpp2.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace runcpp2
5858
const std::string& configPreferredProfile,
5959
const std::unordered_map<CmdOptions, std::string> currentOptions,
6060
const std::vector<std::string>& runArgs,
61+
const Data::ScriptInfo* lastScriptInfo,
62+
Data::ScriptInfo& outScriptInfo,
6163
int& returnStatus);
6264
}
6365

Src/runcpp2/main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ int main(int argc, char* argv[])
321321
}
322322
}
323323

324+
runcpp2::Data::ScriptInfo parsedScriptInfo;
325+
324326
if(currentOptions.count(runcpp2::CmdOptions::WATCH))
325327
{
326328
std::error_code e;
@@ -331,19 +333,24 @@ int main(int argc, char* argv[])
331333
}
332334

333335
ghc::filesystem::file_time_type lastScriptWriteTime {};
334-
335336
while(true)
336337
{
338+
runcpp2::Data::ScriptInfo* lastParsedScriptInfo = nullptr;
339+
337340
if(ghc::filesystem::last_write_time(script, e) > lastScriptWriteTime)
338341
{
339342
int result = 0;
340343

344+
345+
341346
runcpp2::PipelineResult pipelineResult =
342347
runcpp2::StartPipeline( script,
343348
profiles,
344349
preferredProfile,
345350
currentOptions,
346351
scriptArgs,
352+
lastParsedScriptInfo,
353+
parsedScriptInfo,
347354
result);
348355

349356
static_assert(static_cast<int>(runcpp2::PipelineResult::COUNT) == 12, "Update this");
@@ -366,10 +373,11 @@ int main(int argc, char* argv[])
366373
case runcpp2::PipelineResult::RUN_SCRIPT_FAILED:
367374
break;
368375
}
369-
ssLOG_BASE("Watching...");
376+
ssLOG_BASE("No error. Watching...");
370377
}
371378

372379
lastScriptWriteTime = ghc::filesystem::last_write_time(script, e);
380+
lastParsedScriptInfo = &parsedScriptInfo;
373381
std::this_thread::sleep_for(std::chrono::seconds(5));
374382
}
375383
}
@@ -381,6 +389,8 @@ int main(int argc, char* argv[])
381389
preferredProfile,
382390
currentOptions,
383391
scriptArgs,
392+
nullptr,
393+
parsedScriptInfo,
384394
result) != runcpp2::PipelineResult::SUCCESS)
385395
{
386396
return -1;

Src/runcpp2/runcpp2.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
461461
const std::string& configPreferredProfile,
462462
const std::unordered_map<CmdOptions, std::string> currentOptions,
463463
const std::vector<std::string>& runArgs,
464+
const Data::ScriptInfo* lastScriptInfo,
465+
Data::ScriptInfo& outScriptInfo,
464466
int& returnStatus)
465467
{
466468
INTERNAL_RUNCPP2_SAFE_START();
@@ -619,6 +621,42 @@ runcpp2::StartPipeline( const std::string& scriptPath,
619621
return PipelineResult::INVALID_BUILD_DIR;
620622
}
621623
}
624+
625+
//Check if script info has changed if provided
626+
bool scriptInfoChanged = true;
627+
{
628+
ghc::filesystem::path lastScriptInfoFilePath = buildDir / "LastScriptInfo.yaml";
629+
630+
//Compare script info in memory
631+
if(lastScriptInfo != nullptr)
632+
scriptInfoChanged = lastScriptInfo->ToString("") != scriptInfo.ToString("");
633+
//Compare script info in disk
634+
else
635+
{
636+
if(ghc::filesystem::exists(lastScriptInfoFilePath, e))
637+
{
638+
ssLOG_DEBUG("Last script info file exists: " << lastScriptInfoFilePath);
639+
std::ifstream lastScriptInfoFile;
640+
lastScriptInfoFile.open(lastScriptInfoFilePath);
641+
std::stringstream lastScriptInfoBuffer;
642+
lastScriptInfoBuffer << lastScriptInfoFile.rdbuf();
643+
scriptInfoChanged = lastScriptInfoBuffer.str() != scriptInfo.ToString("");
644+
}
645+
}
646+
647+
std::ofstream writeOutputFile(lastScriptInfoFilePath);
648+
if(!writeOutputFile)
649+
{
650+
ssLOG_ERROR("Failed to open file: " << lastScriptInfoFilePath);
651+
//TODO: Maybee add a pipeline result for this?
652+
return PipelineResult::INVALID_BUILD_DIR;
653+
}
654+
655+
writeOutputFile << scriptInfo.ToString("");
656+
657+
//Pass the current script info out
658+
outScriptInfo = scriptInfo;
659+
}
622660

623661
profileIndex = GetPreferredProfileIndex(absoluteScriptPath,
624662
scriptInfo,
@@ -656,7 +694,8 @@ runcpp2::StartPipeline( const std::string& scriptPath,
656694
}
657695

658696
if( currentOptions.count(CmdOptions::RESET_CACHE) > 0 ||
659-
currentOptions.count(CmdOptions::REMOVE_DEPENDENCIES) > 0)
697+
currentOptions.count(CmdOptions::REMOVE_DEPENDENCIES) > 0 ||
698+
scriptInfoChanged)
660699
{
661700
if(!CleanupDependencies(profiles.at(profileIndex),
662701
scriptInfo,
@@ -726,7 +765,7 @@ runcpp2::StartPipeline( const std::string& scriptPath,
726765
std::vector<ghc::filesystem::path> cachedObjectsFiles;
727766
ghc::filesystem::file_time_type finalObjectWriteTime;
728767

729-
if(currentOptions.count(runcpp2::CmdOptions::RESET_CACHE) > 0)
768+
if(currentOptions.count(runcpp2::CmdOptions::RESET_CACHE) > 0 || scriptInfoChanged)
730769
sourceHasCache = std::vector<bool>(sourceFiles.size(), false);
731770
else if(!HasCompiledCache( sourceFiles,
732771
buildDir,
@@ -782,8 +821,6 @@ runcpp2::StartPipeline( const std::string& scriptPath,
782821
return PipelineResult::COMPILE_LINK_FAILED;
783822
}
784823
}
785-
786-
//TODO: Write last compiled configuration to build directory for cache
787824
}
788825

789826
//We are only compiling when watching changes

TODO.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
TODO:
2-
- Ability to add multiple cpp files
32
- Group object files in folders to avoid name collision
43

5-
64
- Add ability to add defines for cross-compiler
75
- Ability to compile runcpp2 as single cpp
86

0 commit comments

Comments
 (0)