diff options
Diffstat (limited to '')
| -rw-r--r-- | src/yue.cpp | 51 | 
1 files changed, 50 insertions, 1 deletions
| diff --git a/src/yue.cpp b/src/yue.cpp index 4dec0e4..dc42ff5 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
| @@ -143,7 +143,12 @@ fs::path getTargetFileDirty(const fs::path& file, const fs::path& workPath, cons | |||
| 143 | } | 143 | } | 
| 144 | 144 | ||
| 145 | #ifndef YUE_NO_WATCHER | 145 | #ifndef YUE_NO_WATCHER | 
| 146 | |||
| 147 | #ifndef YUE_COMPILER_ONLY | ||
| 148 | static std::string compileFile(const fs::path& file, yue::YueConfig conf, const fs::path& workPath, const fs::path& targetPath, bool minify, bool rewrite) { | ||
| 149 | #else | ||
| 146 | static std::string compileFile(const fs::path& file, yue::YueConfig conf, const fs::path& workPath, const fs::path& targetPath) { | 150 | static std::string compileFile(const fs::path& file, yue::YueConfig conf, const fs::path& workPath, const fs::path& targetPath) { | 
| 151 | #endif // YUE_COMPILER_ONLY | ||
| 147 | auto srcFile = fs::absolute(file); | 152 | auto srcFile = fs::absolute(file); | 
| 148 | auto targetFile = getTargetFileDirty(srcFile, workPath, targetPath); | 153 | auto targetFile = getTargetFileDirty(srcFile, workPath, targetPath); | 
| 149 | if (targetFile.empty()) return std::string(); | 154 | if (targetFile.empty()) return std::string(); | 
| @@ -184,6 +189,26 @@ static std::string compileFile(const fs::path& file, yue::YueConfig conf, const | |||
| 184 | std::ofstream output(targetFile, std::ios::trunc | std::ios::out); | 189 | std::ofstream output(targetFile, std::ios::trunc | std::ios::out); | 
| 185 | if (output) { | 190 | if (output) { | 
| 186 | const auto& codes = result.codes; | 191 | const auto& codes = result.codes; | 
| 192 | #ifndef YUE_COMPILER_ONLY | ||
| 193 | if (minify || rewrite) { | ||
| 194 | lua_State* L = luaL_newstate(); | ||
| 195 | DEFER(lua_close(L)); | ||
| 196 | luaL_openlibs(L); | ||
| 197 | pushLuaminify(L); | ||
| 198 | lua_getfield(L, -1, rewrite ? "FormatYue" : "FormatMini"); | ||
| 199 | lua_pushlstring(L, codes.c_str(), codes.size()); | ||
| 200 | if (lua_pcall(L, 1, 1, 0) != 0) { | ||
| 201 | std::string err = lua_tostring(L, -1); | ||
| 202 | return (rewrite ? "Failed to rewrite: "s : "Failed to minify: "s) + modulePath.string() + '\n' + err + '\n'; | ||
| 203 | } else { | ||
| 204 | size_t size = 0; | ||
| 205 | const char* transformedCodes = lua_tolstring(L, -1, &size); | ||
| 206 | output.write(transformedCodes, size); | ||
| 207 | output.close(); | ||
| 208 | return (rewrite ? "Rewritten built "s : "Minified built "s) + modulePath.string() + '\n'; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | #endif // YUE_COMPILER_ONLY | ||
| 187 | if (conf.reserveLineNumber) { | 212 | if (conf.reserveLineNumber) { | 
| 188 | auto head = "-- [yue]: "s + modulePath.string() + '\n'; | 213 | auto head = "-- [yue]: "s + modulePath.string() + '\n'; | 
| 189 | output.write(head.c_str(), head.size()); | 214 | output.write(head.c_str(), head.size()); | 
| @@ -206,7 +231,11 @@ public: | |||
| 206 | void handleFileAction(efsw::WatchID, const std::string& dir, const std::string& filename, efsw::Action action, std::string) override { | 231 | void handleFileAction(efsw::WatchID, const std::string& dir, const std::string& filename, efsw::Action action, std::string) override { | 
| 207 | switch (action) { | 232 | switch (action) { | 
| 208 | case efsw::Actions::Add: | 233 | case efsw::Actions::Add: | 
| 234 | #ifndef YUE_COMPILER_ONLY | ||
| 235 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath, minify, rewrite); !res.empty()) { | ||
| 236 | #else // YUE_COMPILER_ONLY | ||
| 209 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath); !res.empty()) { | 237 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath); !res.empty()) { | 
| 238 | #endif // YUE_COMPILER_ONLY | ||
| 210 | std::cout << res; | 239 | std::cout << res; | 
| 211 | } | 240 | } | 
| 212 | break; | 241 | break; | 
| @@ -223,7 +252,11 @@ public: | |||
| 223 | break; | 252 | break; | 
| 224 | } | 253 | } | 
| 225 | case efsw::Actions::Modified: | 254 | case efsw::Actions::Modified: | 
| 255 | #ifndef YUE_COMPILER_ONLY | ||
| 256 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath, minify, rewrite); !res.empty()) { | ||
| 257 | #else // YUE_COMPILER_ONLY | ||
| 226 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath); !res.empty()) { | 258 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath); !res.empty()) { | 
| 259 | #endif // YUE_COMPILER_ONLY | ||
| 227 | std::cout << res; | 260 | std::cout << res; | 
| 228 | } | 261 | } | 
| 229 | break; | 262 | break; | 
| @@ -236,6 +269,10 @@ public: | |||
| 236 | yue::YueConfig config; | 269 | yue::YueConfig config; | 
| 237 | fs::path workPath; | 270 | fs::path workPath; | 
| 238 | fs::path targetPath; | 271 | fs::path targetPath; | 
| 272 | #ifndef YUE_COMPILER_ONLY | ||
| 273 | bool rewrite = false; | ||
| 274 | bool minify = false; | ||
| 275 | #endif // YUE_COMPILER_ONLY | ||
| 239 | }; | 276 | }; | 
| 240 | #endif // YUE_NO_WATCHER | 277 | #endif // YUE_NO_WATCHER | 
| 241 | 278 | ||
| @@ -631,7 +668,11 @@ int main(int narg, const char** args) { | |||
| 631 | std::list<std::future<std::string>> results; | 668 | std::list<std::future<std::string>> results; | 
| 632 | for (const auto& file : files) { | 669 | for (const auto& file : files) { | 
| 633 | auto task = std::async(std::launch::async, [=]() { | 670 | auto task = std::async(std::launch::async, [=]() { | 
| 671 | #ifndef YUE_COMPILER_ONLY | ||
| 672 | return compileFile(fs::absolute(file.first), config, fullWorkPath, fullTargetPath, minify, rewrite); | ||
| 673 | #else | ||
| 634 | return compileFile(fs::absolute(file.first), config, fullWorkPath, fullTargetPath); | 674 | return compileFile(fs::absolute(file.first), config, fullWorkPath, fullTargetPath); | 
| 675 | #endif // YUE_COMPILER_ONLY | ||
| 635 | }); | 676 | }); | 
| 636 | results.push_back(std::move(task)); | 677 | results.push_back(std::move(task)); | 
| 637 | } | 678 | } | 
| @@ -646,6 +687,14 @@ int main(int narg, const char** args) { | |||
| 646 | listener.config = config; | 687 | listener.config = config; | 
| 647 | listener.workPath = fullWorkPath; | 688 | listener.workPath = fullWorkPath; | 
| 648 | listener.targetPath = fullTargetPath; | 689 | listener.targetPath = fullTargetPath; | 
| 690 | #ifndef YUE_COMPILER_ONLY | ||
| 691 | listener.minify = minify; | ||
| 692 | if (minify) { | ||
| 693 | listener.rewrite = false; | ||
| 694 | } else { | ||
| 695 | listener.rewrite = rewrite; | ||
| 696 | } | ||
| 697 | #endif // YUE_COMPILER_ONLY | ||
| 649 | fileWatcher.addWatch(workPath, &listener, true); | 698 | fileWatcher.addWatch(workPath, &listener, true); | 
| 650 | fileWatcher.watch(); | 699 | fileWatcher.watch(); | 
| 651 | while (true) { | 700 | while (true) { | 
| @@ -807,7 +856,7 @@ int main(int narg, const char** args) { | |||
| 807 | std::ofstream output(file, std::ios::trunc | std::ios::out); | 856 | std::ofstream output(file, std::ios::trunc | std::ios::out); | 
| 808 | output.write(transformedCodes, size); | 857 | output.write(transformedCodes, size); | 
| 809 | output.close(); | 858 | output.close(); | 
| 810 | std::cout << (rewrite ? "Rewrited built "sv : "Minified built "sv) << file << '\n'; | 859 | std::cout << (rewrite ? "Rewritten built "sv : "Minified built "sv) << file << '\n'; | 
| 811 | } else { | 860 | } else { | 
| 812 | std::cout << transformedCodes << '\n'; | 861 | std::cout << transformedCodes << '\n'; | 
| 813 | } | 862 | } | 
