Skip to content

Commit

Permalink
Rename util::shell to util::cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Dec 5, 2020
1 parent 9a5070c commit 7bfa963
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 163 deletions.
7 changes: 3 additions & 4 deletions include/poac/core/builder/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
#include <optional>
#include <numeric>

#include <optional>
#include <poac/core/builder/absorb.hpp>
#include <poac/io/path.hpp>
#include <poac/util/shell.hpp>
#include <string>
#include <vector>

namespace poac::core::builder {
namespace options {
Expand Down
7 changes: 3 additions & 4 deletions include/poac/core/builder/depends.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#define POAC_CORE_BUILDER_DEPENDS_HPP

#include <iostream>
#include <string>
#include <vector>
#include <optional>

#include <poac/util/shell.hpp>
#include <poac/util/misc.hpp>
#include <poac/util/shell.hpp>
#include <string>
#include <vector>

namespace poac::core::builder::depends {
template <typename Opts>
Expand Down
15 changes: 7 additions & 8 deletions include/poac/core/builder/standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

#include <cstdint>
#include <filesystem>
#include <stdexcept>
#include <string>
#include <string_view>
#include <regex>

#include <poac/core/except.hpp>
#include <poac/io/path.hpp>
#include <poac/util/cfg.hpp>
#include <poac/util/semver/semver.hpp>
#include <poac/util/shell.hpp>
#include <poac/util/cfg.hpp>
#include <regex>
#include <stdexcept>
#include <string>
#include <string_view>

namespace poac::core::builder::standard {
inline std::string version_prefix(const bool& enable_gnu) noexcept {
Expand All @@ -23,7 +22,7 @@ namespace poac::core::builder::standard {

std::string get_compiler_version(const std::string& compiler) {
if (util::_shell::has_command(compiler)) {
if (const auto res = util::shell(compiler + " --version").stderr_to_stdout().exec()) {
if (const auto res = util::cmd(compiler + " --version").stderr_to_stdout().exec()) {
const std::regex SEARCH_VERSION("^" + ANY + "(" + semver::MAIN_VERSION + ")" + ANY + "$");
std::smatch match;
if (std::regex_match(*res, match, SEARCH_VERSION)) {
Expand Down Expand Up @@ -189,7 +188,7 @@ namespace poac::core::builder::standard {
else if (cmd == "g++" || cmd == "clang++") {
# ifdef __APPLE__
const std::string compiler(cmd);
if (const auto res = util::shell(compiler + " --version").stderr_to_stdout().exec()) {
if (const auto res = util::cmd(compiler + " --version").stderr_to_stdout().exec()) {
const std::regex SEARCH("^" + ANY + "(Apple LLVM)" + ANY + "$");
std::smatch match;
if (std::regex_match(*res, match, SEARCH)) {
Expand Down
3 changes: 1 addition & 2 deletions include/poac/io/tar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

#include <filesystem>
#include <iostream>
#include <poac/util/shell.hpp>
#include <string>
#include <vector>

#include <poac/util/shell.hpp>

namespace poac::io::tar {
bool extract(const std::filesystem::path& filename, const std::string& options = "") {
const std::string cmd = "tar xf " + filename.string() + " " + options;
Expand Down
18 changes: 8 additions & 10 deletions include/poac/opts/install.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#ifndef POAC_OPTS_INSTALL_HPP
#define POAC_OPTS_INSTALL_HPP

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <filesystem>
#include <fstream>
#include <future>
#include <iostream>
#include <vector>
#include <string>
#include <string_view>
#include <fstream>
#include <map>
#include <regex>
#include <optional>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <poac/io.hpp>
#include <poac/core/except.hpp>
#include <poac/core/resolver/resolve.hpp>
#include <poac/io.hpp>
#include <poac/util/shell.hpp>
#include <regex>
#include <string>
#include <string_view>
#include <vector>

namespace poac::opts::install {
inline const clap::subcommand cli =
Expand Down
100 changes: 53 additions & 47 deletions include/poac/util/shell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@
#include <cstdlib>

namespace poac::util {
class shell {
class cmd {
public:
std::string string() const {
return cmd;
return cmd_;
}

shell() : cmd() {}
explicit shell(const std::string& c) : cmd(c) {}
cmd() : cmd_() {}
explicit cmd(const std::string& c) : cmd_(c) {}

shell& env(const std::string& name, const std::string& value) {
cmd.insert(0, name + "=" + value + " ");
cmd& env(const std::string& name, const std::string& value) {
cmd_.insert(0, name + "=" + value + " ");
return *this;
}
shell& stderr_to_stdout() {
cmd += " 2>&1";
cmd& stderr_to_stdout() {
cmd_ += " 2>&1";
return *this;
}
shell& to_dev_null() {
cmd += " >/dev/null";
cmd& to_dev_null() {
cmd_ += " >/dev/null";
return *this;
}
shell& dump_stdout() {
cmd += " 1>/dev/null";
cmd& dump_stdout() {
cmd_ += " 1>/dev/null";
return *this;
}
shell& dump_stderr() {
cmd += " 2>/dev/null";
cmd& dump_stderr() {
cmd_ += " 2>/dev/null";
return *this;
}

Expand All @@ -50,7 +50,7 @@ namespace poac::util {
#ifdef _WIN32
if (FILE* pipe = _popen(cmd.c_str(), "r")) {
#else
if (FILE* pipe = popen(cmd.c_str(), "r")) {
if (FILE* pipe = popen(cmd_.c_str(), "r")) {
#endif
while (std::fgets(buffer.data(), 128, pipe) != nullptr)
result += buffer.data();
Expand All @@ -72,76 +72,82 @@ namespace poac::util {
bool exec_ignore() const {
// EXIT_SUCCESS -> 0 -> false -> true
// EXIT_FAILURE -> 1 -> true -> false
return !static_cast<bool>(std::system(cmd.c_str()));
return !static_cast<bool>(std::system(cmd_.c_str()));
}

friend std::ostream&
operator<<(std::ostream& os, const shell& c) {
return (os << c.cmd);
operator<<(std::ostream& os, const cmd& c) {
return (os << c.cmd_);
}

bool operator==(const shell& rhs) const {
return this->cmd == rhs.cmd;
bool operator==(const cmd& rhs) const {
return this->cmd_ == rhs.cmd_;
}
bool operator==(const std::string& rhs) const {
return this->cmd == rhs;
return this->cmd_ == rhs;
}

shell operator&&(const shell& rhs) const {
return shell(this->cmd + " && " + rhs.cmd);
cmd
operator&&(const cmd& rhs) const {
return cmd(this->cmd_ + " && " + rhs.cmd_);
}
shell operator&&(const std::string& rhs) const {
return shell(this->cmd + " && " + rhs);
cmd
operator&&(const std::string& rhs) const {
return cmd(this->cmd_ + " && " + rhs);
}

shell& operator&=(const shell& rhs) {
this->cmd += " && " + rhs.cmd;
cmd& operator&=(const cmd& rhs) {
this->cmd_ += " && " + rhs.cmd_;
return *this;
}
shell& operator&=(const std::string& rhs) {
this->cmd += " && " + rhs;
cmd& operator&=(const std::string& rhs) {
this->cmd_ += " && " + rhs;
return *this;
}

shell operator||(const shell& rhs) const {
return shell(this->cmd + " || " + rhs.cmd);
cmd
operator||(const cmd& rhs) const {
return cmd(this->cmd_ + " || " + rhs.cmd_);
}
shell operator||(const std::string& rhs) const {
return shell(this->cmd + " || " + rhs);
cmd
operator||(const std::string& rhs) const {
return cmd(this->cmd_ + " || " + rhs);
}

shell& operator|=(const shell& rhs) {
this->cmd += " || " + rhs.cmd;
cmd& operator|=(const cmd& rhs) {
this->cmd_ += " || " + rhs.cmd_;
return *this;
}
shell& operator|=(const std::string& rhs) {
this->cmd += " || " + rhs;
cmd& operator|=(const std::string& rhs) {
this->cmd_ += " || " + rhs;
return *this;
}

shell operator+(const shell& rhs) const { // TODO: "; "でなくても良いのか
return shell(this->cmd + " " + rhs.cmd);
cmd
operator+(const cmd& rhs) const { // TODO: "; "でなくても良いのか
return cmd(this->cmd_ + " " + rhs.cmd_);
}
shell operator+(const std::string& rhs) const {
return shell(this->cmd + " " + rhs);
cmd
operator+(const std::string& rhs) const {
return cmd(this->cmd_ + " " + rhs);
}

shell& operator+=(const shell& rhs) {
this->cmd += " " + rhs.cmd;
cmd& operator+=(const cmd& rhs) {
this->cmd_ += " " + rhs.cmd_;
return *this;
}
shell& operator+=(const std::string& rhs) {
this->cmd += " " + rhs;
cmd& operator+=(const std::string& rhs) {
this->cmd_ += " " + rhs;
return *this;
}

private:
std::string cmd;
std::string cmd_;
};

namespace _shell {
bool has_command(const std::string& c) {
return shell("type " + c + " >/dev/null 2>&1").exec().has_value();
return cmd("type " + c + " >/dev/null 2>&1").exec().has_value();
}
}
} // end namespace
Expand Down
5 changes: 2 additions & 3 deletions include/poac/util/vcs.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#ifndef POAC_UTIL_VCS_HPP
#define POAC_UTIL_VCS_HPP

#include <string>

#include <poac/util/shell.hpp>
#include <poac/util/git2-cpp/git2.hpp>
#include <poac/util/shell.hpp>
#include <string>

namespace poac::util::vcs {
namespace git_repo {
Expand Down
Loading

0 comments on commit 7bfa963

Please sign in to comment.