Skip to content

Commit 61e1aa2

Browse files
committed
feat(run,check): add --local-cache for script mode and unify cache behavior
2 parents 3c6f594 + 5c95d2b commit 61e1aa2

7 files changed

Lines changed: 65 additions & 7 deletions

File tree

include/vix/cli/commands/check/CheckDetail.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ namespace vix::commands::CheckCommand::detail
7878
/// Absolute path of the .cpp file to validate.
7979
fs::path cppFile;
8080

81+
/// Use local .vix-scripts instead of global ~/.vix/cache/scripts.
82+
bool localCache = false;
83+
8184
/// Enable AddressSanitizer + UBSan.
8285
bool enableSanitizers = false;
8386

include/vix/cli/commands/run/RunDetail.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ namespace vix::commands::RunCommand::detail
130130

131131
bool warnedVixFlagAfterDoubleDash = false;
132132
std::string warnedArg;
133+
134+
bool localCache = false;
133135
};
134136

135137
/**
@@ -353,7 +355,7 @@ namespace vix::commands::RunCommand::detail
353355
/**
354356
* @brief Return the root directory used for generated script projects.
355357
*/
356-
fs::path get_scripts_root();
358+
fs::path get_scripts_root(bool localCache);
357359

358360
/**
359361
* @brief Detect whether a .cpp script uses the Vix runtime.

src/commands/RunCommand.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ namespace vix::commands::RunCommand
12961296
out << " --ubsan Enable UBSan only\n";
12971297
out << " --with-sqlite Enable SQLite support for script mode\n";
12981298
out << " --with-mysql Enable MySQL support for script mode\n\n";
1299+
out << " --local-cache Use local .vix-scripts instead of global cache\n";
12991300

13001301
out << "Documentation:\n";
13011302
out << " --docs Enable auto docs (sets VIX_DOCS=1)\n";

src/commands/check/CheckScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace vix::commands::CheckCommand::detail
182182
const bool enableUbsanOnly = opt.enableUbsanOnly;
183183
const std::string exeName = script.stem().string();
184184

185-
fs::path scriptsRoot = run::get_scripts_root();
185+
fs::path scriptsRoot = run::get_scripts_root(opt.localCache);
186186
std::error_code ec;
187187
fs::create_directories(scriptsRoot, ec);
188188
if (ec)

src/commands/run/RunFlow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ namespace vix::commands::RunCommand::detail
439439
{
440440
opt.verbose = true;
441441
}
442+
else if (a == "--local-cache")
443+
{
444+
opt.localCache = true;
445+
}
442446
else if (a == "--log-level" || a == "--loglevel")
443447
{
444448
opt.logLevel = take_value(args, i, a, opt);

src/commands/run/RunScript.cpp

Lines changed: 45 additions & 3 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();
429-
state.scriptsRoot = get_scripts_root();
430-
state.projectDir = state.scriptsRoot / state.exeName;
459+
state.scriptsRoot = get_scripts_root(opt.localCache);
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,9 +1290,15 @@ namespace vix::commands::RunCommand::detail
12901290
return false;
12911291
}
12921292

1293-
fs::path get_scripts_root()
1293+
fs::path get_scripts_root(bool localCache)
12941294
{
1295-
return fs::current_path() / ".vix-scripts";
1295+
if (localCache)
1296+
return fs::current_path() / ".vix-scripts";
1297+
1298+
if (const auto home = home_dir(); home)
1299+
return fs::path(*home) / ".vix" / "cache" / "scripts";
1300+
1301+
return fs::temp_directory_path() / "vix" / "cache" / "scripts";
12961302
}
12971303

12981304
std::string make_script_cmakelists(

0 commit comments

Comments
 (0)