From a1d341085eed96d567329a30f2cf57c95fe6f071 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 13 Jul 2022 14:46:55 +0800 Subject: auto add search path for cmd tools. --- src/yue.cpp | 20 +++++++++++++------- src/yuescript/yue_compiler.cpp | 18 ++++++++++++++---- src/yuescript/yue_compiler.h | 2 +- src/yuescript/yuescript.h | 18 +++++++++++------- 4 files changed, 39 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/yue.cpp b/src/yue.cpp index 96649c4..7b2ad0b 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -272,6 +272,7 @@ int main(int narg, const char** args) { bool dumpCompileTime = false; std::string targetPath; std::string resultFile; + std::string workPath; std::list> files; for (int i = 1; i < narg; ++i) { std::string arg = args[i]; @@ -414,10 +415,11 @@ int main(int narg, const char** args) { yue::Utils::trim(value); config.options[key] = value; } else { - config.options[argStr] = '1'; + config.options[argStr] = ""; } } else { if (fs::is_directory(arg)) { + workPath = arg; for (auto item : fs::recursive_directory_iterator(arg)) { if (!item.is_directory()) { auto ext = item.path().extension().string(); @@ -428,6 +430,7 @@ int main(int narg, const char** args) { } } } else { + workPath = fs::path(arg).parent_path().string(); files.emplace_back(arg, arg); } } @@ -450,6 +453,9 @@ int main(int narg, const char** args) { std::istreambuf_iterator()); auto conf = config; conf.module = file.first; + if (!workPath.empty()) { + conf.options["path"] = (fs::path(workPath) / "?.lua").string(); + } if (dumpCompileTime) { auto start = std::chrono::high_resolution_clock::now(); auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); @@ -467,7 +473,7 @@ int main(int narg, const char** args) { return std::tuple{0, file.first, buf.str()}; } else { std::ostringstream buf; - buf << "Fail to compile: "sv << file.first << ".\n"sv; + buf << "Failed to compile: "sv << file.first << ".\n"sv; buf << result.error << '\n'; return std::tuple{1, file.first, buf.str()}; } @@ -511,17 +517,17 @@ int main(int narg, const char** args) { output.write(codes.c_str(), codes.size()); return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; } else { - return std::tuple{1, std::string(), std::string("Fail to write file: "sv) + targetFile.string() + '\n'}; + return std::tuple{1, std::string(), std::string("Failed to write file: "sv) + targetFile.string() + '\n'}; } } } else { std::ostringstream buf; - buf << "Fail to compile: "sv << file.first << ".\n"; + buf << "Failed to compile: "sv << file.first << ".\n"; buf << result.error << '\n'; return std::tuple{1, std::string(), buf.str()}; } } else { - return std::tuple{1, std::string(), std::string("Fail to read file: "sv) + file.first + ".\n"}; + return std::tuple{1, std::string(), std::string("Failed to read file: "sv) + file.first + ".\n"}; } }); results.push_back(std::move(task)); @@ -568,7 +574,7 @@ int main(int narg, const char** args) { if (lua_pcall(L, 1, 1, 0) != 0) { ret = 2; std::string err = lua_tostring(L, -1); - errs.push_back(std::string("Fail to minify: "sv) + file + '\n' + err + '\n'); + errs.push_back(std::string("Failed to minify: "sv) + file + '\n' + err + '\n'); } else { size_t size = 0; const char* minifiedCodes = lua_tolstring(L, -1, &size); @@ -583,7 +589,7 @@ int main(int narg, const char** args) { } } else { ret = 2; - errs.push_back(std::string("Fail to minify: "sv) + file + '\n'); + errs.push_back(std::string("Failed to minify: "sv) + file + '\n'); } } else { std::cout << msg; diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c7ceeb4..c68746c 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -56,7 +56,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.13.2"sv; +const std::string_view version = "0.13.3"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -6887,13 +6887,23 @@ private: DEFER(lua_settop(L, top)); pushYue("find_modulepath"sv); // cur find_modulepath lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName - if (lua_pcall(L, 1, 1, 0) != 0) { + if (lua_pcall(L, 1, 2, 0) != 0) { std::string err = lua_tostring(L, -1); throw std::logic_error(_info.errorMessage("failed to resolve module path\n"s + err, x)); } - if (lua_isnil(L, -1) != 0) { - throw std::logic_error(_info.errorMessage("failed to find module '"s + moduleName + '\'', x)); + if (lua_isnil(L, -2) != 0) { + str_list files; + if (lua_istable(L, -1) != 0) { + int size = static_cast(lua_objlen(L, -1)); + for (int i = 0; i < size; i++) { + lua_rawgeti(L, -1, i + 1); + files.push_back("no file \""s + lua_tostring(L, -1) + "\""s); + lua_pop(L, 1); + } + } + throw std::logic_error(_info.errorMessage("module '"s + moduleName + "\' not found:\n\t"s + join(files, "\n\t"sv), x)); } + lua_pop(L, 1); std::string moduleFullName = lua_tostring(L, -1); lua_pop(L, 1); // cur if (!isModuleLoaded(moduleFullName)) { diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index 6f5170f..903cb3d 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -21,7 +21,7 @@ namespace yue { extern const std::string_view version; extern const std::string_view extension; -using Options = std::unordered_map; +using Options = std::unordered_map; struct YueConfig { bool lintGlobalVariable = false; diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index 1430758..2d2d660 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h @@ -59,13 +59,17 @@ local function find_modulepath(name) local name_path = name:match("[\\/]") and name or name:gsub("%.", dirsep) local file_exist, file_path local tried = {} - for path in package.path:gmatch("[^;]+") do - file_path = path:gsub("?", name_path):gsub("%.lua$", suffix) - file_exist = yue.file_exist(file_path) - if file_exist then - break - else - tried[#tried + 1] = file_path + local paths = {package.path, yue.options.path} + for i = 1, #paths do + local yue_path = paths[i] + for path in yue_path:gmatch("[^;]+") do + file_path = path:gsub("?", name_path):gsub("%.lua$", suffix) + file_exist = yue.file_exist(file_path) + if file_exist then + break + else + tried[#tried + 1] = file_path + end end end if file_exist then -- cgit v1.2.3-55-g6feb