Skip to content

doxide.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Driver.hpp"

void write_file(const std::string& contents,
    const std::filesystem::path& dst) {
  if (dst.has_parent_path()) {
    std::filesystem::create_directories(dst.parent_path());
  }
  std::fstream out(dst, std::ios::out);
  if (!out.is_open()) {
    throw std::runtime_error("could not write file " + dst.string());
  }
  out << contents;
}

void write_file_prompt(const std::string& contents,
    const std::filesystem::path& dst) {
  if (dst.has_parent_path()) {
    std::filesystem::create_directories(dst.parent_path());
  }
  if (std::filesystem::exists(dst)) {
    std::cout << dst.string() << " already exists, overwrite? [y/N] ";
    std::string ans;
    std::getline(std::cin, ans);
    if (ans.length() > 0 && (ans[0] == 'y' || ans[0] == 'Y')) {
      write_file(contents, dst);
    }
  } else {
    write_file(contents, dst);
  }
}

std::string gulp(const std::filesystem::path& src) {
  std::string contents;
  std::ifstream in(src);
  if (!in.is_open()) {
    throw std::runtime_error("could not read file " + src.string());
  }
  char buffer[8192];
  while (in.read(buffer, sizeof(buffer))) {
    contents.append(buffer, sizeof(buffer));
  }
  contents.append(buffer, in.gcount());
  return contents;
}

int main(int argc, char** argv) {
  Driver driver;
  CLI::App app{"Modern documentation for modern C++.\n"};
  app.get_formatter()->column_width(30);
  app.add_option("--title",
      driver.title,
      "Main page title.");
  app.add_option("--description",
      driver.description,
      "Main page description.");
  app.add_option("--output", driver.output,
      "Output directory.");
  app.add_option("--coverage", driver.coverage,
      "Code coverage file (.gcov or .json).");
  app.add_subcommand("init",
      "Initialize configuration files.")->
      fallthrough()->
      callback([&]() { driver.init(); });
  app.add_subcommand("build",
      "Build documentation in output directory.")->
      fallthrough()->
      callback([&]() { driver.build(); });
  app.add_subcommand("clean",
      "Clean output directory.")->
      fallthrough()->
      callback([&]() { driver.clean(); });
  app.add_subcommand("cover",
      "Output code coverage data to stdout in JSON format.")->
      fallthrough()->
      callback([&]() { driver.cover(); });
  app.require_subcommand(1);
  CLI11_PARSE(app, argc, argv);
}