diff options
| author | nicole mazzuca <mazzucan@outlook.com> | 2021-01-12 09:26:42 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-12 09:26:42 -0800 |
| commit | 94b9db927c33e0e6ec358893f29ddbc49423cac0 (patch) | |
| tree | d434f475dd19cfee1b0100efd477ee95ecb200e0 /toolsrc/include | |
| parent | 331f87775cd53d6ab36545b0a1ba55e051b090f9 (diff) | |
| download | vcpkg-94b9db927c33e0e6ec358893f29ddbc49423cac0.tar.gz vcpkg-94b9db927c33e0e6ec358893f29ddbc49423cac0.zip | |
[vcpkg] Add a different way to use CommandBuilder, support InWorkingDirectory (#15583)
* [vcpkg] Add stuff to the system.process API
* CmdLineBuilder - add rvalue overloads
* Add InWorkingDirectory support
* remove `&&` outside of system.process.cpp
* minor CR
* move the non-InWorkingDirectory functions into the header as inline
* Update toolsrc/include/vcpkg/base/system.process.h
Co-authored-by: Billy O'Neal <bion@microsoft.com>
* fix CmdLineBuilder name
* moar bugfixin
* fix native() location
Co-authored-by: Billy O'Neal <bion@microsoft.com>
Diffstat (limited to 'toolsrc/include')
| -rw-r--r-- | toolsrc/include/vcpkg/base/system.process.h | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/toolsrc/include/vcpkg/base/system.process.h b/toolsrc/include/vcpkg/base/system.process.h index 3b60630bb..1c62fa285 100644 --- a/toolsrc/include/vcpkg/base/system.process.h +++ b/toolsrc/include/vcpkg/base/system.process.h @@ -26,17 +26,30 @@ namespace vcpkg::System struct CmdLineBuilder { - CmdLineBuilder& path_arg(const fs::path& p) { return string_arg(fs::u8string(p)); } - CmdLineBuilder& string_arg(StringView s); - CmdLineBuilder& ampersand() + CmdLineBuilder() = default; + explicit CmdLineBuilder(const fs::path& p) { path_arg(p); } + explicit CmdLineBuilder(StringView s) { string_arg(s); } + explicit CmdLineBuilder(const std::string& s) { string_arg(s); } + explicit CmdLineBuilder(const char* s) { string_arg({s, ::strlen(s)}); } + + CmdLineBuilder& path_arg(const fs::path& p) & { return string_arg(fs::u8string(p)); } + CmdLineBuilder& string_arg(StringView s) &; + CmdLineBuilder& raw_arg(StringView s) & { - buf.push_back('&'); - buf.push_back('&'); + buf.push_back(' '); + buf.append(s.data(), s.size()); return *this; } - std::string extract() noexcept { return std::move(buf); } - operator ZStringView() const { return buf; } + CmdLineBuilder&& path_arg(const fs::path& p) && { return std::move(path_arg(p)); } + CmdLineBuilder&& string_arg(StringView s) && { return std::move(string_arg(s)); }; + CmdLineBuilder&& raw_arg(StringView s) && { return std::move(raw_arg(s)); } + + std::string&& extract() && { return std::move(buf); } + operator StringView() noexcept { return buf; } + StringView command_line() const { return buf; } + + void clear() { buf.clear(); } private: std::string buf; @@ -61,24 +74,59 @@ namespace vcpkg::System Environment get_modified_clean_environment(const std::unordered_map<std::string, std::string>& extra_env, const std::string& prepend_to_path = {}); - int cmd_execute(const ZStringView cmd_line, const Environment& env = {}); - int cmd_execute_clean(const ZStringView cmd_line); + struct InWorkingDirectory + { + const fs::path& working_directory; + }; + + int cmd_execute(StringView cmd_line, InWorkingDirectory wd, const Environment& env = {}); + inline int cmd_execute(StringView cmd_line, const Environment& env = {}) + { + return cmd_execute(cmd_line, InWorkingDirectory{fs::path()}, env); + } + + int cmd_execute_clean(StringView cmd_line, InWorkingDirectory wd); + inline int cmd_execute_clean(StringView cmd_line) + { + return cmd_execute_clean(cmd_line, InWorkingDirectory{fs::path()}); + } #if defined(_WIN32) - Environment cmd_execute_modify_env(const ZStringView cmd_line, const Environment& env = {}); + Environment cmd_execute_modify_env(StringView cmd_line, const Environment& env = {}); void cmd_execute_background(const StringView cmd_line); #endif - ExitCodeAndOutput cmd_execute_and_capture_output(const ZStringView cmd_line, const Environment& env = {}); + ExitCodeAndOutput cmd_execute_and_capture_output(StringView cmd_line, + InWorkingDirectory wd, + const Environment& env = {}); + inline ExitCodeAndOutput cmd_execute_and_capture_output(StringView cmd_line, const Environment& env = {}) + { + return cmd_execute_and_capture_output(cmd_line, InWorkingDirectory{fs::path()}, env); + } - int cmd_execute_and_stream_lines(const ZStringView cmd_line, - std::function<void(const std::string&)> per_line_cb, + int cmd_execute_and_stream_lines(StringView cmd_line, + InWorkingDirectory wd, + std::function<void(StringView)> per_line_cb, const Environment& env = {}); + inline int cmd_execute_and_stream_lines(StringView cmd_line, + std::function<void(StringView)> per_line_cb, + const Environment& env = {}) + { + return cmd_execute_and_stream_lines(cmd_line, InWorkingDirectory{fs::path()}, std::move(per_line_cb), env); + } - int cmd_execute_and_stream_data(const ZStringView cmd_line, + int cmd_execute_and_stream_data(StringView cmd_line, + InWorkingDirectory wd, std::function<void(StringView)> data_cb, const Environment& env = {}); + inline int cmd_execute_and_stream_data(StringView cmd_line, + std::function<void(StringView)> data_cb, + const Environment& env = {}) + { + return cmd_execute_and_stream_data(cmd_line, InWorkingDirectory{fs::path()}, std::move(data_cb), env); + } + void register_console_ctrl_handler(); #if defined(_WIN32) void initialize_global_job_object(); |
