| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #pragma once
- #include <iostream>
- #include <sstream>
- #include <string>
- #include "types.h"
- namespace build {
- class command_builder {
- private:
- indent_t indent_;
- std::stringstream buffer_;
- public:
- command_builder(std::string const & cmd, int indent = 0);
- template <typename T> command_builder && operator<<(T const & value) &&;
- template <typename T> command_builder & operator<<(T const & value) &;
- template <typename T>
- command_builder & operator<<(std::vector<T> const & value) &;
- int execute() const;
- private:
- template <typename T> void write(T const & value) { buffer_ << value; }
- void write(version const & vers);
- void write(source_file const & file);
- void write(output_file const & file);
- };
- command_builder::command_builder(std::string const & cmd, int indent)
- : indent_{indent}, buffer_() {
- buffer_ << cmd;
- }
- template <typename T>
- command_builder && command_builder::operator<<(T const & value) && {
- return std::move((*this) << value);
- }
- template <typename T>
- command_builder & command_builder::operator<<(T const & value) & {
- buffer_ << ' ';
- write(value);
- return *this;
- }
- template <typename T>
- command_builder &
- command_builder::operator<<(std::vector<T> const & value) & {
- for (T const & v : value) {
- (*this) << v;
- }
- return *this;
- }
- void command_builder::write(version const & vers) {
- switch (vers) {
- case version::cxx11:
- buffer_ << ' ' << "-std=c++11";
- break;
- case version::cxx14:
- buffer_ << ' ' << "-std=c++14";
- break;
- case version::cxx17:
- buffer_ << ' ' << "-std=c++17";
- break;
- case none:
- break;
- }
- }
- void command_builder::write(source_file const & file) {
- buffer_ << " -c " << file.get();
- }
- void command_builder::write(output_file const & file) {
- buffer_ << " -o " << file.get();
- }
- std::ostream & operator<<(std::ostream & os, indent_t const & value) {
- for (int i = 0; i < value.value; ++i) {
- os << " ";
- }
- return os;
- }
- int command_builder::execute() const {
- std::string cmdstr = buffer_.str();
- // TODO(samjaffe): Not sure this belongs here, so much as command_builder
- // should take a formatter object (including things like color config)
- std::cout << indent_ << cmdstr << std::endl;
- return std::system(cmdstr.c_str());
- }
- }
|