Skip to content

Commit

Permalink
[vcpkg] Rewriting CmdLineBuilder/Command (3/n) (#15673)
Browse files Browse the repository at this point in the history
Rename CmdLineBuilder to Command, since it's no longer a builder but an actual data type
  • Loading branch information
strega-nil authored Jan 16, 2021
1 parent a2cc2b1 commit b60f003
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 179 deletions.
60 changes: 30 additions & 30 deletions toolsrc/include/vcpkg/base/system.process.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ namespace vcpkg::System
std::string s;
};

struct CmdLineBuilder
struct Command
{
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) &
Command() = default;
explicit Command(const fs::path& p) { path_arg(p); }
explicit Command(StringView s) { string_arg(s); }
explicit Command(const std::string& s) { string_arg(s); }
explicit Command(const char* s) { string_arg({s, ::strlen(s)}); }

Command& path_arg(const fs::path& p) & { return string_arg(fs::u8string(p)); }
Command& string_arg(StringView s) &;
Command& raw_arg(StringView s) &
{
buf.push_back(' ');
buf.append(s.data(), s.size());
return *this;
}

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)); }
Command&& path_arg(const fs::path& p) && { return std::move(path_arg(p)); }
Command&& string_arg(StringView s) && { return std::move(string_arg(s)); };
Command&& raw_arg(StringView s) && { return std::move(raw_arg(s)); }

std::string&& extract() && { return std::move(buf); }
StringView command_line() const { return buf; }
Expand All @@ -51,17 +51,17 @@ namespace vcpkg::System
std::string buf;
};

struct CmdLineBuilderMapLess
struct CommandLess
{
bool operator()(const CmdLineBuilder& lhs, const CmdLineBuilder& rhs) const
bool operator()(const Command& lhs, const Command& rhs) const
{
return lhs.command_line() < rhs.command_line();
}
};

CmdLineBuilder make_basic_cmake_cmd(const fs::path& cmake_tool_path,
const fs::path& cmake_script,
const std::vector<CMakeVariable>& pass_variables);
Command make_basic_cmake_cmd(const fs::path& cmake_tool_path,
const fs::path& cmake_script,
const std::vector<CMakeVariable>& pass_variables);

fs::path get_exe_path_of_current_process();

Expand All @@ -87,48 +87,48 @@ namespace vcpkg::System
const fs::path& working_directory;
};

int cmd_execute(const CmdLineBuilder& cmd_line, InWorkingDirectory wd, const Environment& env = {});
inline int cmd_execute(const CmdLineBuilder& cmd_line, const Environment& env = {})
int cmd_execute(const Command& cmd_line, InWorkingDirectory wd, const Environment& env = {});
inline int cmd_execute(const Command& cmd_line, const Environment& env = {})
{
return cmd_execute(cmd_line, InWorkingDirectory{fs::path()}, env);
}

int cmd_execute_clean(const CmdLineBuilder& cmd_line, InWorkingDirectory wd);
inline int cmd_execute_clean(const CmdLineBuilder& cmd_line)
int cmd_execute_clean(const Command& cmd_line, InWorkingDirectory wd);
inline int cmd_execute_clean(const Command& cmd_line)
{
return cmd_execute_clean(cmd_line, InWorkingDirectory{fs::path()});
}

#if defined(_WIN32)
Environment cmd_execute_modify_env(const CmdLineBuilder& cmd_line, const Environment& env = {});
Environment cmd_execute_modify_env(const Command& cmd_line, const Environment& env = {});

void cmd_execute_background(const CmdLineBuilder& cmd_line);
void cmd_execute_background(const Command& cmd_line);
#endif

ExitCodeAndOutput cmd_execute_and_capture_output(const CmdLineBuilder& cmd_line,
ExitCodeAndOutput cmd_execute_and_capture_output(const Command& cmd_line,
InWorkingDirectory wd,
const Environment& env = {});
inline ExitCodeAndOutput cmd_execute_and_capture_output(const CmdLineBuilder& cmd_line, const Environment& env = {})
inline ExitCodeAndOutput cmd_execute_and_capture_output(const Command& cmd_line, const Environment& env = {})
{
return cmd_execute_and_capture_output(cmd_line, InWorkingDirectory{fs::path()}, env);
}

int cmd_execute_and_stream_lines(const CmdLineBuilder& cmd_line,
int cmd_execute_and_stream_lines(const Command& cmd_line,
InWorkingDirectory wd,
std::function<void(StringView)> per_line_cb,
const Environment& env = {});
inline int cmd_execute_and_stream_lines(const CmdLineBuilder& cmd_line,
inline int cmd_execute_and_stream_lines(const Command& 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 CmdLineBuilder& cmd_line,
int cmd_execute_and_stream_data(const Command& cmd_line,
InWorkingDirectory wd,
std::function<void(StringView)> data_cb,
const Environment& env = {});
inline int cmd_execute_and_stream_data(const CmdLineBuilder& cmd_line,
inline int cmd_execute_and_stream_data(const Command& cmd_line,
std::function<void(StringView)> data_cb,
const Environment& env = {})
{
Expand Down
4 changes: 2 additions & 2 deletions toolsrc/include/vcpkg/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ namespace vcpkg::Build
const VcpkgPaths& m_paths;
};

System::CmdLineBuilder make_build_env_cmd(const PreBuildInfo& pre_build_info, const Toolset& toolset);
System::Command make_build_env_cmd(const PreBuildInfo& pre_build_info, const Toolset& toolset);

struct ExtendedBuildResult
{
Expand Down Expand Up @@ -369,7 +369,7 @@ namespace vcpkg::Build
struct EnvMapEntry
{
std::unordered_map<std::string, std::string> env_map;
Cache<System::CmdLineBuilder, System::Environment, System::CmdLineBuilderMapLess> cmd_cache;
Cache<System::Command, System::Environment, System::CommandLess> cmd_cache;
};

Cache<std::vector<std::string>, EnvMapEntry> envs;
Expand Down
6 changes: 3 additions & 3 deletions toolsrc/include/vcpkg/buildenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace vcpkg
{
System::CmdLineBuilder make_cmake_cmd(const VcpkgPaths& paths,
const fs::path& cmake_script,
std::vector<System::CMakeVariable>&& pass_variables);
System::Command make_cmake_cmd(const VcpkgPaths& paths,
const fs::path& cmake_script,
std::vector<System::CMakeVariable>&& pass_variables);
}
4 changes: 2 additions & 2 deletions toolsrc/src/vcpkg-test/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ TEST_CASE ("guess_visual_studio_prompt", "[system]")

TEST_CASE ("cmdlinebuilder", "[system]")
{
using vcpkg::System::CmdLineBuilder;
using vcpkg::System::Command;

CmdLineBuilder cmd;
Command cmd;
cmd.path_arg(fs::u8path("relative/path.exe"));
cmd.string_arg("abc");
cmd.string_arg("hello world!");
Expand Down
8 changes: 4 additions & 4 deletions toolsrc/src/vcpkg/archives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace vcpkg::Archives
const std::string nugetid = match[1];
const std::string version = match[2];

const auto code_and_output = System::cmd_execute_and_capture_output(System::CmdLineBuilder{nuget_exe}
const auto code_and_output = System::cmd_execute_and_capture_output(System::Command{nuget_exe}
.string_arg("install")
.string_arg(nugetid)
.string_arg("-Version")
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace vcpkg::Archives
recursion_limiter_sevenzip = true;
const auto seven_zip = paths.get_tool_exe(Tools::SEVEN_ZIP);
const auto code_and_output = System::cmd_execute_and_capture_output(
System::CmdLineBuilder{seven_zip}
System::Command{seven_zip}
.string_arg("x")
.path_arg(archive)
.string_arg(Strings::format("-o%s", fs::u8string(to_path_partial)))
Expand All @@ -89,13 +89,13 @@ namespace vcpkg::Archives
#else
if (ext == ".gz" && ext.extension() != ".tar")
{
const auto code = System::cmd_execute(System::CmdLineBuilder{"tar"}.string_arg("xzf").path_arg(archive),
const auto code = System::cmd_execute(System::Command{"tar"}.string_arg("xzf").path_arg(archive),
System::InWorkingDirectory{to_path_partial});
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "tar failed while extracting %s", fs::u8string(archive));
}
else if (ext == ".zip")
{
const auto code = System::cmd_execute(System::CmdLineBuilder{"unzip"}.string_arg("-qqo").path_arg(archive),
const auto code = System::cmd_execute(System::Command{"unzip"}.string_arg("-qqo").path_arg(archive),
System::InWorkingDirectory{to_path_partial});
Checks::check_exit(VCPKG_LINE_INFO, code == 0, "unzip failed while extracting %s", fs::u8string(archive));
}
Expand Down
8 changes: 4 additions & 4 deletions toolsrc/src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace vcpkg::Downloads
{
static constexpr StringLiteral guid_marker = "8a1db05f-a65d-419b-aa72-037fb4d0672e";

System::CmdLineBuilder cmd;
System::Command cmd;
cmd.string_arg("curl")
.string_arg("--head")
.string_arg("--location")
Expand Down Expand Up @@ -260,7 +260,7 @@ namespace vcpkg::Downloads
{
static constexpr StringLiteral guid_marker = "8a1db05f-a65d-419b-aa72-037fb4d0672e";

System::CmdLineBuilder cmd;
System::Command cmd;
cmd.string_arg("curl")
.string_arg("--location")
.string_arg("-w")
Expand Down Expand Up @@ -298,7 +298,7 @@ namespace vcpkg::Downloads
{
static constexpr StringLiteral guid_marker = "9a1db05f-a65d-419b-aa72-037fb4d0672e";

System::CmdLineBuilder cmd;
System::Command cmd;
cmd.string_arg("curl").string_arg("-X").string_arg("PUT");
cmd.string_arg("-w").string_arg(Strings::concat("\\n", guid_marker, "%{http_code}"));
cmd.string_arg(url);
Expand Down Expand Up @@ -443,7 +443,7 @@ namespace vcpkg::Downloads
}
}
#endif
System::CmdLineBuilder cmd;
System::Command cmd;
cmd.string_arg("curl")
.string_arg("--fail")
.string_arg("-L")
Expand Down
28 changes: 14 additions & 14 deletions toolsrc/src/vcpkg/base/system.process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ namespace vcpkg
}
System::CMakeVariable::CMakeVariable(std::string var) : s(std::move(var)) { }

System::CmdLineBuilder System::make_basic_cmake_cmd(const fs::path& cmake_tool_path,
const fs::path& cmake_script,
const std::vector<CMakeVariable>& pass_variables)
System::Command System::make_basic_cmake_cmd(const fs::path& cmake_tool_path,
const fs::path& cmake_script,
const std::vector<CMakeVariable>& pass_variables)
{
System::CmdLineBuilder cmd{cmake_tool_path};
System::Command cmd{cmake_tool_path};
for (auto&& var : pass_variables)
{
cmd.string_arg(var.s);
Expand All @@ -197,7 +197,7 @@ namespace vcpkg
return cmd;
}

System::CmdLineBuilder& System::CmdLineBuilder::string_arg(StringView s) &
System::Command& System::Command::string_arg(StringView s) &
{
if (!buf.empty()) buf.push_back(' ');
if (Strings::find_first_of(s, " \t\n\r\"\\,;&`^|'") != s.end())
Expand Down Expand Up @@ -383,7 +383,7 @@ namespace vcpkg
return clean_env;
}

int System::cmd_execute_clean(const CmdLineBuilder& cmd_line, InWorkingDirectory wd)
int System::cmd_execute_clean(const Command& cmd_line, InWorkingDirectory wd)
{
return cmd_execute(cmd_line, wd, get_clean_environment());
}
Expand Down Expand Up @@ -564,7 +564,7 @@ namespace vcpkg
#endif

#if defined(_WIN32)
void System::cmd_execute_background(const CmdLineBuilder& cmd_line)
void System::cmd_execute_background(const Command& cmd_line)
{
auto timer = Chrono::ElapsedTimer::create_started();

Expand All @@ -581,7 +581,7 @@ namespace vcpkg
Debug::print("cmd_execute_background() took ", static_cast<int>(timer.microseconds()), " us\n");
}

Environment System::cmd_execute_modify_env(const CmdLineBuilder& cmd_line, const Environment& env)
Environment System::cmd_execute_modify_env(const Command& cmd_line, const Environment& env)
{
static StringLiteral magic_string = "cdARN4xjKueKScMy9C6H";

Expand Down Expand Up @@ -623,7 +623,7 @@ namespace vcpkg
}
#endif

int System::cmd_execute(const CmdLineBuilder& cmd_line, System::InWorkingDirectory wd, const Environment& env)
int System::cmd_execute(const Command& cmd_line, System::InWorkingDirectory wd, const Environment& env)
{
auto timer = Chrono::ElapsedTimer::create_started();
#if defined(_WIN32)
Expand Down Expand Up @@ -651,7 +651,7 @@ namespace vcpkg
}
else
{
real_command_line = System::CmdLineBuilder("cd")
real_command_line = System::Command("cd")
.path_arg(wd.working_directory)
.raw_arg("&&")
.raw_arg(cmd_line.command_line())
Expand All @@ -667,7 +667,7 @@ namespace vcpkg
return exit_code;
}

int System::cmd_execute_and_stream_lines(const CmdLineBuilder& cmd_line,
int System::cmd_execute_and_stream_lines(const Command& cmd_line,
System::InWorkingDirectory wd,
std::function<void(StringView)> per_line_cb,
const Environment& env)
Expand Down Expand Up @@ -696,7 +696,7 @@ namespace vcpkg
return rc;
}

int System::cmd_execute_and_stream_data(const CmdLineBuilder& cmd_line,
int System::cmd_execute_and_stream_data(const Command& cmd_line,
System::InWorkingDirectory wd,
std::function<void(StringView)> data_cb,
const Environment& env)
Expand Down Expand Up @@ -724,7 +724,7 @@ namespace vcpkg
}
else
{
actual_cmd_line = System::CmdLineBuilder("cd")
actual_cmd_line = System::Command("cd")
.path_arg(wd.working_directory)
.raw_arg("&&")
.raw_arg(cmd_line.command_line())
Expand Down Expand Up @@ -763,7 +763,7 @@ namespace vcpkg
return exit_code;
}

ExitCodeAndOutput System::cmd_execute_and_capture_output(const CmdLineBuilder& cmd_line,
ExitCodeAndOutput System::cmd_execute_and_capture_output(const Command& cmd_line,
System::InWorkingDirectory wd,
const Environment& env)
{
Expand Down
Loading

0 comments on commit b60f003

Please sign in to comment.