Ver código fonte

refactor: move to source files

Sam Jaffe 2 meses atrás
pai
commit
20179434ea
5 arquivos alterados com 126 adições e 119 exclusões
  1. 2 2
      build.cxx
  2. 69 0
      command_builder.cxx
  3. 0 67
      command_builder.h
  4. 53 0
      project.cxx
  5. 2 50
      project.h

+ 2 - 2
build.cxx

@@ -1,9 +1,9 @@
 #include "build.h"
 
 int main(int argc, char const * const argv[]) {
-  auto p = build::project(argc, argv);
+  build::project p{argc, argv};
   p.set_name("make-build")
       .set_version(build::version::cxx17)
-      .set_source_file("main.cxx");
+      .add_source_file("main.cxx");
   return p.generate();
 }

+ 69 - 0
command_builder.cxx

@@ -0,0 +1,69 @@
+#include "command_builder.h"
+
+namespace build {
+  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());
+  }
+
+}

+ 0 - 67
command_builder.h

@@ -7,7 +7,6 @@
 #include "types.h"
 
 namespace build {
-
   class command_builder {
   private:
     indent_t indent_;
@@ -29,70 +28,4 @@ namespace build {
     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());
-  }
-
 }

+ 53 - 0
project.cxx

@@ -0,0 +1,53 @@
+#include "project.h"
+
+namespace build {
+  // TODO(samjaffe): Are there actually any arguments that belong here?
+  project::project(int argc, char const * const * const argv) {}
+
+  project & project::set_version(version vers) {
+    vers_ = vers;
+    return *this;
+  }
+
+  project & project::set_name(std::string const & name) {
+    name_ = name;
+    return *this;
+  }
+
+  project & project::add_source_file(fs::path const & file) {
+    if (!fs::is_regular_file(file)) {
+      throw std::logic_error(file.string() + " is not a regular file");
+    }
+    source_files_.emplace_back(file);
+    return *this;
+  }
+
+  std::pair<std::vector<object_file>, int> project::generate_objects() const {
+    fs::path obj_dir(".obj");
+    fs::create_directory(obj_dir);
+    std::vector<object_file> objects;
+    for (auto const & file : source_files_) {
+      output_file const outfile =
+          obj_dir / file.get().filename().replace_extension(".o");
+      auto cmd = command_builder(CXX, 1) << vers_ << file << outfile;
+      if (int rc = cmd.execute()) { return {{}, rc}; }
+      objects.emplace_back(outfile);
+    }
+    return {objects, 0};
+  }
+
+  int project::generate() const {
+    std::cout << "Generating Project: " << name_ << std::endl;
+    // Step 1: Compile Object files
+    auto [objects, rc] = generate_objects();
+    if (rc) { return rc; }
+
+    // Step 2: Generate Executable
+    fs::path bin_dir(".bin");
+    fs::create_directory(bin_dir);
+    output_file const outfile = bin_dir / name_;
+    auto cmd = command_builder(CXX, 1) << vers_ << outfile << objects;
+    return cmd.execute();
+  }
+
+}

+ 2 - 50
project.h

@@ -3,6 +3,7 @@
 // Because Apple refuses to update libc++ in a timely manner
 // In exchange, this in C++11 compatible...
 #include <string>
+#include <utility>
 #include <vector>
 
 #include "command_builder.h"
@@ -23,7 +24,7 @@ namespace build {
 
     project & set_version(version vers);
     project & set_name(std::string const & name);
-    project & set_source_file(fs::path const & file);
+    project & add_source_file(fs::path const & file);
 
     int generate() const;
 
@@ -31,53 +32,4 @@ namespace build {
     std::pair<std::vector<object_file>, int> generate_objects() const;
   };
 
-  // TODO(samjaffe): Are there actually any arguments that belong here?
-  project::project(int argc, char const * const * const argv) {}
-
-  project & project::set_version(version vers) {
-    vers_ = vers;
-    return *this;
-  }
-
-  project & project::set_name(std::string const & name) {
-    name_ = name;
-    return *this;
-  }
-
-  project & project::set_source_file(fs::path const & file) {
-    if (!fs::is_regular_file(file)) {
-      throw std::logic_error(file.string() + " is not a regular file");
-    }
-    source_files_.emplace_back(file);
-    return *this;
-  }
-
-  std::pair<std::vector<object_file>, int> project::generate_objects() const {
-    fs::path obj_dir(".obj");
-    fs::create_directory(obj_dir);
-    std::vector<object_file> objects;
-    for (auto const & file : source_files_) {
-      output_file const outfile =
-          obj_dir / file.get().filename().replace_extension(".o");
-      auto cmd = command_builder(CXX, 1) << vers_ << file << outfile;
-      if (int rc = cmd.execute()) { return {{}, rc}; }
-      objects.emplace_back(outfile);
-    }
-    return {objects, 0};
-  }
-
-  int project::generate() const {
-    std::cout << "Generating Project: " << name_ << std::endl;
-    // Step 1: Compile Object files
-    auto [objects, rc] = generate_objects();
-    if (rc) { return rc; }
-
-    // Step 2: Generate Executable
-    fs::path bin_dir(".bin");
-    fs::create_directory(bin_dir);
-    output_file const outfile = bin_dir / name_;
-    auto cmd = command_builder(CXX, 1) << vers_ << outfile << objects;
-    return cmd.execute();
-  }
-
 }