1 /**
2    Creates (maybe) a default reggaefile for a dub project.
3 */
4 module reggae.dub.interop.reggaefile;
5 
6 
7 import reggae.from;
8 
9 
10 void maybeCreateReggaefile(T)(auto ref T output,
11                               in from!"reggae.options".Options options)
12 {
13     import std.file: exists;
14 
15     if(options.isDubProject && !options.reggaeFilePath.exists) {
16         createReggaefile(output, options);
17     }
18 }
19 
20 // default build for a dub project when there is no reggaefile
21 private void createReggaefile(T)(auto ref T output,
22                                  in from!"reggae.options".Options options)
23 {
24     import reggae.io: log;
25     import reggae.path: buildPath;
26     import std.stdio: File;
27     import std..string: replace;
28 
29     output.log("Creating reggaefile.d from dub information");
30     auto file = File(buildPath(options.projectPath, "reggaefile.d"), "w");
31 
32     static cleanup(in string str) {
33         return str.replace("\n        ", "\n");
34     }
35 
36     const text = options.dubConfig == ""
37         ? standardDubReggaefile
38         : reducedDubReggaefile;
39 
40     file.write(text.replace("\n    ", "\n"));
41 }
42 
43 
44 // This is the default reggaefile for dub projects if one is not found
45 private enum standardDubReggaefile = q{
46     import reggae;
47 
48     alias buildTarget = dubDefaultTarget!(); // dub build
49     alias testTarget = dubTestTarget!();     // dub test (=> ut[.exe])
50 
51     Target aliasTarget(string aliasName, alias target)() {
52         import std.algorithm: map;
53         // Using a leaf target with `$builddir/<raw output>` outputs as dependency
54         // yields the expected relative target names for Ninja/make.
55         return Target.phony(aliasName, "", Target(target.rawOutputs.map!(o => "$builddir/" ~ o), ""));
56     }
57 
58     // Add a `default` convenience alias for the `dub build` target.
59     // Especially useful for Ninja (`ninja default ut` to build default & test targets in parallel).
60     alias defaultTarget = aliasTarget!("default", buildTarget);
61 
62     version (Windows) {
63         // Windows: extra `ut` convenience alias for `ut.exe`
64         alias utTarget = aliasTarget!("ut", testTarget);
65         mixin build!(buildTarget, optional!testTarget, optional!defaultTarget, optional!utTarget);
66     } else {
67         mixin build!(buildTarget, optional!testTarget, optional!defaultTarget);
68     }
69 };
70 
71 
72 // This is the default reggaefile if one is not found when --dub-config is used. This speeds
73 // up running reggae
74 private enum reducedDubReggaefile = q{
75     import reggae;
76 
77     alias buildTarget = dubDefaultTarget!(); // dub build
78 
79     Target aliasTarget(string aliasName, alias target)() {
80         import std.algorithm: map;
81         // Using a leaf target with `$builddir/<raw output>` outputs as dependency
82         // yields the expected relative target names for Ninja/make.
83         return Target.phony(aliasName, "", Target(target.rawOutputs.map!(o => "$builddir/" ~ o), ""));
84     }
85 
86     // Add a `default` convenience alias for the `dub build` target.
87     alias defaultTarget = aliasTarget!("default", buildTarget);
88 
89     mixin build!(buildTarget, optional!defaultTarget);
90 };