summaryrefslogtreecommitdiffhomepage
path: root/yscan.cpp
blob: 138b67f3633cb4034c8bb27a19a9d8102068b333 (plain)
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
#include "yscan.h"

#include "file.h"

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

#include <fmt/format.h>

#include <libreichwein/file.h>

namespace fs = std::filesystem;
using namespace std::string_literals;

int yscan(int argc, char* argv[])
{
 try {
  std::string name {fs::current_path().stem().string()};

  std::vector<std::string> sources;
  std::vector<std::string> test_sources;
  for (const auto& i: fs::directory_iterator(".")) {
   fs::path p{i.path().filename()};
   if (is_compile_unit_source_by_extension(p)) {
    if (p.string().substr(0, 4) == "test") {
     test_sources.push_back(p.string());
    } else {
     sources.push_back(p.string());
    }
   }
  }

  std::sort(sources.begin(), sources.end());
  std::sort(test_sources.begin(), test_sources.end());

  std::string sources_string{std::accumulate(sources.begin(), sources.end(), std::string{},
                                             [](const std::string& sum, const std::string& i){return sum + "  <source>" + i + "</source>\n";})};
  std::string test_sources_string{std::accumulate(test_sources.begin(), test_sources.end(), std::string{},
                                                  [](const std::string& sum, const std::string& i){return sum + "  <source>" + i + "</source>\n";})};

  if (!test_sources_string.empty()) {
   test_sources_string = fmt::format(R"( <test>
  <name>{}</name>
{} </test>
)", "test-"s + name, test_sources_string);
  }

  std::string contents {R"(<ymake>
 <build>
  <name>{}</name>
{} </build>
{}</ymake>)"};
  contents = fmt::format(contents, name, sources_string, test_sources_string);
  std::cout << contents << std::endl;
 } catch (const std::exception& ex) {
  std::cerr << "yscan: " << ex.what() << std::endl;
  return 1;
 }
 return 0;
}