1 module reggae.dependencies;
2 
3 import std.regex;
4 import std.range;
5 
6 
7 string[] dependenciesFromFile(R)(R lines) if(isInputRange!R) {
8     if(lines.empty) return [];
9     import std.algorithm: map, filter, find;
10     import std.string: strip, split;
11     return lines
12         .map!(a => a.replace(`\`, ``).strip)
13         .join(" ")
14         .find(":")
15         .split(" ")
16         .filter!(a => a != "")
17         .array[1..$];
18 }
19 
20 
21 @safe:
22 
23 
24 /**
25  * Given the output of compiling a file, return
26  * the list of D files to compile to link the executable
27  * Includes all dependencies, not just source files to
28  * compile.
29  */
30 string[] dMainDependencies(in string output) {
31     string[] dependencies = dMainDepSrcs(output);
32     auto fileReg = regex(`^file +([^\t]+)\t+\((.+)\)$`);
33     foreach(line; output.split("\n")) {
34         auto fileMatch = line.matchFirst(fileReg);
35         if(fileMatch) dependencies ~= fileMatch.captures[2];
36     }
37 
38     return dependencies;
39 }
40 
41 
42 
43 /**
44  * Given the output of compiling a file, return
45  * the list of D files to compile to link the executable.
46  * Only includes source files to compile
47  */
48 string[] dMainDepSrcs(in string output) {
49     string[] dependencies;
50     auto importReg = regex(`^import +([^\t]+)[\t\s]+\((.+)\)$`);
51     auto stdlibReg = regex(`^(std\.|core\.|etc\.|object$)`);
52     foreach(line; output.split("\n")) {
53         auto importMatch = line.matchFirst(importReg);
54         if(importMatch) {
55             auto stdlibMatch = importMatch.captures[1].matchFirst(stdlibReg);
56             if(!stdlibMatch) {
57                 dependencies ~= importMatch.captures[2];
58             }
59         }
60     }
61 
62     return dependencies;
63 }
64 
65 
66 string[] dependenciesToFile(in string objFile, in string[] deps) pure nothrow {
67     import std.array;
68     return [objFile ~ ": \\",
69             deps.join(" "),
70         ];
71 }