diff options
author | Li Jin <dragon-fly@qq.com> | 2022-11-17 10:04:54 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-11-17 10:04:54 +0800 |
commit | 8abf668c0b031c0aa81f186745eaf154aa036c8a (patch) | |
tree | 5afa94cbeac138ced6b856b23a47481aede06fb9 | |
parent | 4cff61e72d04a249643e36a03b62f5560f269f83 (diff) | |
download | yuescript-8abf668c0b031c0aa81f186745eaf154aa036c8a.tar.gz yuescript-8abf668c0b031c0aa81f186745eaf154aa036c8a.tar.bz2 yuescript-8abf668c0b031c0aa81f186745eaf154aa036c8a.zip |
add -t support for -w option.
-rw-r--r-- | src/yue.cpp | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/yue.cpp b/src/yue.cpp index d6f1bc1..fa2baa5 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
@@ -105,11 +105,12 @@ static void pushLuaminify(lua_State* L) { | |||
105 | } | 105 | } |
106 | #endif // YUE_COMPILER_ONLY | 106 | #endif // YUE_COMPILER_ONLY |
107 | 107 | ||
108 | fs::path getTargetFile(const fs::path& srcFile) { | 108 | fs::path getTargetFile(const fs::path& file, const fs::path& workPath, const fs::path& targetPath) { |
109 | auto srcFile = fs::absolute(file); | ||
109 | auto ext = srcFile.extension().string(); | 110 | auto ext = srcFile.extension().string(); |
110 | for (auto& ch : ext) ch = std::tolower(ch); | 111 | for (auto& ch : ext) ch = std::tolower(ch); |
111 | if (!ext.empty() && ext.substr(1) == yue::extension) { | 112 | if (!ext.empty() && ext.substr(1) == yue::extension) { |
112 | auto targetFile = srcFile; | 113 | auto targetFile = targetPath / srcFile.lexically_relative(workPath); |
113 | targetFile.replace_extension("lua"s); | 114 | targetFile.replace_extension("lua"s); |
114 | if (fs::exists(targetFile)) { | 115 | if (fs::exists(targetFile)) { |
115 | return targetFile; | 116 | return targetFile; |
@@ -118,12 +119,13 @@ fs::path getTargetFile(const fs::path& srcFile) { | |||
118 | return fs::path(); | 119 | return fs::path(); |
119 | } | 120 | } |
120 | 121 | ||
121 | fs::path getTargetFileDirty(const fs::path& srcFile) { | 122 | fs::path getTargetFileDirty(const fs::path& file, const fs::path& workPath, const fs::path& targetPath) { |
122 | if (!fs::exists(srcFile)) return fs::path(); | 123 | if (!fs::exists(file)) return fs::path(); |
124 | auto srcFile = fs::absolute(file); | ||
123 | auto ext = srcFile.extension().string(); | 125 | auto ext = srcFile.extension().string(); |
124 | for (auto& ch : ext) ch = std::tolower(ch); | 126 | for (auto& ch : ext) ch = std::tolower(ch); |
125 | if (!fs::is_directory(srcFile) && !ext.empty() && ext.substr(1) == yue::extension) { | 127 | if (!fs::is_directory(srcFile) && !ext.empty() && ext.substr(1) == yue::extension) { |
126 | auto targetFile = srcFile; | 128 | auto targetFile = targetPath / srcFile.lexically_relative(workPath); |
127 | targetFile.replace_extension("lua"s); | 129 | targetFile.replace_extension("lua"s); |
128 | if (fs::exists(targetFile)) { | 130 | if (fs::exists(targetFile)) { |
129 | auto time = fs::last_write_time(targetFile); | 131 | auto time = fs::last_write_time(targetFile); |
@@ -138,8 +140,9 @@ fs::path getTargetFileDirty(const fs::path& srcFile) { | |||
138 | return fs::path(); | 140 | return fs::path(); |
139 | } | 141 | } |
140 | 142 | ||
141 | static std::string compileFile(const fs::path& srcFile, yue::YueConfig conf, const std::string& workPath) { | 143 | static std::string compileFile(const fs::path& file, yue::YueConfig conf, const fs::path& workPath, const fs::path& targetPath) { |
142 | auto targetFile = getTargetFileDirty(srcFile); | 144 | auto srcFile = fs::absolute(file); |
145 | auto targetFile = getTargetFileDirty(srcFile, workPath, targetPath); | ||
143 | if (targetFile.empty()) return std::string(); | 146 | if (targetFile.empty()) return std::string(); |
144 | std::ifstream input(srcFile, std::ios::in); | 147 | std::ifstream input(srcFile, std::ios::in); |
145 | if (input) { | 148 | if (input) { |
@@ -155,9 +158,9 @@ static std::string compileFile(const fs::path& srcFile, yue::YueConfig conf, con | |||
155 | auto it = conf.options.find("path"); | 158 | auto it = conf.options.find("path"); |
156 | if (it != conf.options.end()) { | 159 | if (it != conf.options.end()) { |
157 | it->second += ';'; | 160 | it->second += ';'; |
158 | it->second += (fs::path(workPath) / "?.lua"sv).string(); | 161 | it->second += (workPath / "?.lua"sv).string(); |
159 | } else { | 162 | } else { |
160 | conf.options["path"] = (fs::path(workPath) / "?.lua"sv).string(); | 163 | conf.options["path"] = (workPath / "?.lua"sv).string(); |
161 | } | 164 | } |
162 | } | 165 | } |
163 | auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); | 166 | auto result = yue::YueCompiler{YUE_ARGS}.compile(s, conf); |
@@ -200,16 +203,15 @@ public: | |||
200 | void handleFileAction(efsw::WatchID, const std::string& dir, const std::string& filename, efsw::Action action, std::string) override { | 203 | void handleFileAction(efsw::WatchID, const std::string& dir, const std::string& filename, efsw::Action action, std::string) override { |
201 | switch (action) { | 204 | switch (action) { |
202 | case efsw::Actions::Add: | 205 | case efsw::Actions::Add: |
203 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath); !res.empty()) { | 206 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath); !res.empty()) { |
204 | std::cout << res; | 207 | std::cout << res; |
205 | } | 208 | } |
206 | break; | 209 | break; |
207 | case efsw::Actions::Delete: { | 210 | case efsw::Actions::Delete: { |
208 | auto srcFile = fs::path(dir) / filename; | 211 | auto targetFile = getTargetFile(fs::path(dir) / filename, workPath, targetPath); |
209 | auto targetFile = getTargetFile(srcFile); | ||
210 | if (!targetFile.empty()) { | 212 | if (!targetFile.empty()) { |
211 | fs::remove(targetFile); | 213 | fs::remove(targetFile); |
212 | auto moduleFile = targetFile.lexically_relative(workPath); | 214 | auto moduleFile = targetFile.lexically_relative(targetPath); |
213 | if (moduleFile.empty()) { | 215 | if (moduleFile.empty()) { |
214 | moduleFile = targetFile; | 216 | moduleFile = targetFile; |
215 | } | 217 | } |
@@ -218,7 +220,7 @@ public: | |||
218 | break; | 220 | break; |
219 | } | 221 | } |
220 | case efsw::Actions::Modified: | 222 | case efsw::Actions::Modified: |
221 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath); !res.empty()) { | 223 | if (auto res = compileFile(fs::path(dir) / filename, config, workPath, targetPath); !res.empty()) { |
222 | std::cout << res; | 224 | std::cout << res; |
223 | } | 225 | } |
224 | break; | 226 | break; |
@@ -229,7 +231,8 @@ public: | |||
229 | } | 231 | } |
230 | } | 232 | } |
231 | yue::YueConfig config; | 233 | yue::YueConfig config; |
232 | std::string workPath; | 234 | fs::path workPath; |
235 | fs::path targetPath; | ||
233 | }; | 236 | }; |
234 | 237 | ||
235 | int main(int narg, const char** args) { | 238 | int main(int narg, const char** args) { |
@@ -591,15 +594,15 @@ int main(int narg, const char** args) { | |||
591 | return 1; | 594 | return 1; |
592 | } | 595 | } |
593 | if (watchFiles) { | 596 | if (watchFiles) { |
597 | auto fullWorkPath = fs::absolute(fs::path(workPath)).string(); | ||
598 | auto fullTargetPath = fullWorkPath; | ||
594 | if (!targetPath.empty()) { | 599 | if (!targetPath.empty()) { |
595 | std::cout << "Error: -t can not be used with watching mode\n"sv; | 600 | fullTargetPath = fs::absolute(fs::path(targetPath)).string(); |
596 | return 1; | ||
597 | } | 601 | } |
598 | auto fullWorkPath = fs::absolute(fs::path(workPath)).string(); | ||
599 | std::list<std::future<std::string>> results; | 602 | std::list<std::future<std::string>> results; |
600 | for (const auto& file : files) { | 603 | for (const auto& file : files) { |
601 | auto task = std::async(std::launch::async, [=]() { | 604 | auto task = std::async(std::launch::async, [=]() { |
602 | return compileFile(fs::absolute(file.first), config, fullWorkPath); | 605 | return compileFile(fs::absolute(file.first), config, fullWorkPath, fullTargetPath); |
603 | }); | 606 | }); |
604 | results.push_back(std::move(task)); | 607 | results.push_back(std::move(task)); |
605 | } | 608 | } |
@@ -613,6 +616,7 @@ int main(int narg, const char** args) { | |||
613 | UpdateListener listener{}; | 616 | UpdateListener listener{}; |
614 | listener.config = config; | 617 | listener.config = config; |
615 | listener.workPath = fullWorkPath; | 618 | listener.workPath = fullWorkPath; |
619 | listener.targetPath = fullTargetPath; | ||
616 | fileWatcher.addWatch(workPath, &listener, true); | 620 | fileWatcher.addWatch(workPath, &listener, true); |
617 | fileWatcher.watch(); | 621 | fileWatcher.watch(); |
618 | while (true) { | 622 | while (true) { |