diff options
author | Li Jin <dragon-fly@qq.com> | 2022-07-13 14:46:55 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-07-13 14:46:55 +0800 |
commit | a1d341085eed96d567329a30f2cf57c95fe6f071 (patch) | |
tree | 37d359a02b99cf94fa52cfc43eb9c5c80ecfeb89 /src/yue.cpp | |
parent | aa194667c7cb7bb70f2a58477555d1419ae33eb0 (diff) | |
download | yuescript-a1d341085eed96d567329a30f2cf57c95fe6f071.tar.gz yuescript-a1d341085eed96d567329a30f2cf57c95fe6f071.tar.bz2 yuescript-a1d341085eed96d567329a30f2cf57c95fe6f071.zip |
auto add search path for cmd tools.
Diffstat (limited to 'src/yue.cpp')
-rw-r--r-- | src/yue.cpp | 20 |
1 files changed, 13 insertions, 7 deletions
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) { | |||
272 | bool dumpCompileTime = false; | 272 | bool dumpCompileTime = false; |
273 | std::string targetPath; | 273 | std::string targetPath; |
274 | std::string resultFile; | 274 | std::string resultFile; |
275 | std::string workPath; | ||
275 | std::list<std::pair<std::string,std::string>> files; | 276 | std::list<std::pair<std::string,std::string>> files; |
276 | for (int i = 1; i < narg; ++i) { | 277 | for (int i = 1; i < narg; ++i) { |
277 | std::string arg = args[i]; | 278 | std::string arg = args[i]; |
@@ -414,10 +415,11 @@ int main(int narg, const char** args) { | |||
414 | yue::Utils::trim(value); | 415 | yue::Utils::trim(value); |
415 | config.options[key] = value; | 416 | config.options[key] = value; |
416 | } else { | 417 | } else { |
417 | config.options[argStr] = '1'; | 418 | config.options[argStr] = ""; |
418 | } | 419 | } |
419 | } else { | 420 | } else { |
420 | if (fs::is_directory(arg)) { | 421 | if (fs::is_directory(arg)) { |
422 | workPath = arg; | ||
421 | for (auto item : fs::recursive_directory_iterator(arg)) { | 423 | for (auto item : fs::recursive_directory_iterator(arg)) { |
422 | if (!item.is_directory()) { | 424 | if (!item.is_directory()) { |
423 | auto ext = item.path().extension().string(); | 425 | auto ext = item.path().extension().string(); |
@@ -428,6 +430,7 @@ int main(int narg, const char** args) { | |||
428 | } | 430 | } |
429 | } | 431 | } |
430 | } else { | 432 | } else { |
433 | workPath = fs::path(arg).parent_path().string(); | ||
431 | files.emplace_back(arg, arg); | 434 | files.emplace_back(arg, arg); |
432 | } | 435 | } |
433 | } | 436 | } |
@@ -450,6 +453,9 @@ int main(int narg, const char** args) { | |||
450 | std::istreambuf_iterator<char>()); | 453 | std::istreambuf_iterator<char>()); |
451 | auto conf = config; | 454 | auto conf = config; |
452 | conf.module = file.first; | 455 | conf.module = file.first; |
456 | if (!workPath.empty()) { | ||
457 | conf.options["path"] = (fs::path(workPath) / "?.lua").string(); | ||
458 | } | ||
453 | if (dumpCompileTime) { | 459 | if (dumpCompileTime) { |
454 | auto start = std::chrono::high_resolution_clock::now(); | 460 | auto start = std::chrono::high_resolution_clock::now(); |
455 | auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); | 461 | auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); |
@@ -467,7 +473,7 @@ int main(int narg, const char** args) { | |||
467 | return std::tuple{0, file.first, buf.str()}; | 473 | return std::tuple{0, file.first, buf.str()}; |
468 | } else { | 474 | } else { |
469 | std::ostringstream buf; | 475 | std::ostringstream buf; |
470 | buf << "Fail to compile: "sv << file.first << ".\n"sv; | 476 | buf << "Failed to compile: "sv << file.first << ".\n"sv; |
471 | buf << result.error << '\n'; | 477 | buf << result.error << '\n'; |
472 | return std::tuple{1, file.first, buf.str()}; | 478 | return std::tuple{1, file.first, buf.str()}; |
473 | } | 479 | } |
@@ -511,17 +517,17 @@ int main(int narg, const char** args) { | |||
511 | output.write(codes.c_str(), codes.size()); | 517 | output.write(codes.c_str(), codes.size()); |
512 | return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; | 518 | return std::tuple{0, targetFile.string(), std::string("Built "sv) + file.first + '\n'}; |
513 | } else { | 519 | } else { |
514 | return std::tuple{1, std::string(), std::string("Fail to write file: "sv) + targetFile.string() + '\n'}; | 520 | return std::tuple{1, std::string(), std::string("Failed to write file: "sv) + targetFile.string() + '\n'}; |
515 | } | 521 | } |
516 | } | 522 | } |
517 | } else { | 523 | } else { |
518 | std::ostringstream buf; | 524 | std::ostringstream buf; |
519 | buf << "Fail to compile: "sv << file.first << ".\n"; | 525 | buf << "Failed to compile: "sv << file.first << ".\n"; |
520 | buf << result.error << '\n'; | 526 | buf << result.error << '\n'; |
521 | return std::tuple{1, std::string(), buf.str()}; | 527 | return std::tuple{1, std::string(), buf.str()}; |
522 | } | 528 | } |
523 | } else { | 529 | } else { |
524 | return std::tuple{1, std::string(), std::string("Fail to read file: "sv) + file.first + ".\n"}; | 530 | return std::tuple{1, std::string(), std::string("Failed to read file: "sv) + file.first + ".\n"}; |
525 | } | 531 | } |
526 | }); | 532 | }); |
527 | results.push_back(std::move(task)); | 533 | results.push_back(std::move(task)); |
@@ -568,7 +574,7 @@ int main(int narg, const char** args) { | |||
568 | if (lua_pcall(L, 1, 1, 0) != 0) { | 574 | if (lua_pcall(L, 1, 1, 0) != 0) { |
569 | ret = 2; | 575 | ret = 2; |
570 | std::string err = lua_tostring(L, -1); | 576 | std::string err = lua_tostring(L, -1); |
571 | errs.push_back(std::string("Fail to minify: "sv) + file + '\n' + err + '\n'); | 577 | errs.push_back(std::string("Failed to minify: "sv) + file + '\n' + err + '\n'); |
572 | } else { | 578 | } else { |
573 | size_t size = 0; | 579 | size_t size = 0; |
574 | const char* minifiedCodes = lua_tolstring(L, -1, &size); | 580 | const char* minifiedCodes = lua_tolstring(L, -1, &size); |
@@ -583,7 +589,7 @@ int main(int narg, const char** args) { | |||
583 | } | 589 | } |
584 | } else { | 590 | } else { |
585 | ret = 2; | 591 | ret = 2; |
586 | errs.push_back(std::string("Fail to minify: "sv) + file + '\n'); | 592 | errs.push_back(std::string("Failed to minify: "sv) + file + '\n'); |
587 | } | 593 | } |
588 | } else { | 594 | } else { |
589 | std::cout << msg; | 595 | std::cout << msg; |