summaryrefslogtreecommitdiffhomepage
path: root/yscan.cpp
blob: 8dad6bf8a560c72c39ce8be1079c6f74eada2617 (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
#include "yscan.h"

#include "file.h"

#include <filesystem>
#include <iostream>
#include <string>

#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::string sources;
  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 += "  <source>" + p.string() + "</source>\n";
    } else {
     sources += "  <source>" + p.string() + "</source>\n";
    }
   }
  }

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

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