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