Skip to content

Commit ba76acc

Browse files
committed
refactor(run): move script build to global cache and key by absolute path
- remove local .vix-scripts usage - use ~/.vix/cache/scripts as script workspace - key script cache by absolute path hash (fnv1a) - fix collision between scripts with same filename - honor --clean in run mode - add local hash helpers in RunScript.cpp
1 parent d0b0dff commit ba76acc

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

src/commands/run/RunScript.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,47 @@ namespace vix::commands::RunCommand::detail
421421
hint("If you meant a runtime arg, use `--run` (or repeatable --args).");
422422
}
423423

424+
unsigned long long fnv1a_64(const std::string &input)
425+
{
426+
constexpr unsigned long long offset = 14695981039346656037ULL;
427+
constexpr unsigned long long prime = 1099511628211ULL;
428+
429+
unsigned long long hash = offset;
430+
for (char c : input)
431+
{
432+
const unsigned char uc = static_cast<unsigned char>(c);
433+
hash ^= static_cast<unsigned long long>(uc);
434+
hash *= prime;
435+
}
436+
437+
return hash;
438+
}
439+
440+
std::string hex_u64(unsigned long long value)
441+
{
442+
static constexpr char digits[] = "0123456789abcdef";
443+
std::string out(16, '0');
444+
445+
for (int i = 15; i >= 0; --i)
446+
{
447+
out[static_cast<std::size_t>(i)] = digits[value & 0xF];
448+
value >>= 4ULL;
449+
}
450+
451+
return out;
452+
}
453+
424454
ScriptProjectState prepare_script_project_state(Options &opt)
425455
{
426456
ScriptProjectState state;
427-
state.script = opt.cppFile;
457+
state.script = fs::absolute(opt.cppFile).lexically_normal();
428458
state.exeName = state.script.stem().string();
429459
state.scriptsRoot = get_scripts_root();
430-
state.projectDir = state.scriptsRoot / state.exeName;
460+
461+
const std::string scriptCacheKey = hex_u64(
462+
fnv1a_64("script-cache:" + state.script.string()));
463+
464+
state.projectDir = state.scriptsRoot / scriptCacheKey;
431465
state.cmakeLists = state.projectDir / "CMakeLists.txt";
432466
state.buildDir = state.projectDir / "build-ninja";
433467
state.sigFile = state.projectDir / ".vix-config.sig";
@@ -874,6 +908,14 @@ namespace vix::commands::RunCommand::detail
874908
if (prepCode != 0)
875909
return prepCode;
876910

911+
if (o.clean)
912+
{
913+
std::error_code ec;
914+
fs::remove_all(state.buildDir, ec);
915+
fs::remove(state.sigFile, ec);
916+
state.needConfigure = true;
917+
}
918+
877919
const int code = configure_and_build_script(o, state);
878920
if (code != 0)
879921
return code;

src/commands/run/detail/ScriptCMake.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,10 @@ namespace vix::commands::RunCommand::detail
12921292

12931293
fs::path get_scripts_root()
12941294
{
1295-
return fs::current_path() / ".vix-scripts";
1295+
if (const auto home = home_dir(); home)
1296+
return fs::path(*home) / ".vix" / "cache" / "scripts";
1297+
1298+
return fs::temp_directory_path() / "vix" / "cache" / "scripts";
12961299
}
12971300

12981301
std::string make_script_cmakelists(

0 commit comments

Comments
 (0)