1 /**
2 Copyright: Auburn Sounds 2015-2018.
3 License:   All Rights Reserved.
4 */
5 module utils;
6 
7 import std.process;
8 import std..string;
9 import std.file;
10 import std.path;
11 
12 import colorize;
13 
14 string white(string s) @property
15 {
16     return s.color(fg.light_white);
17 }
18 
19 string grey(string s) @property
20 {
21     return s.color(fg.white);
22 }
23 
24 string cyan(string s) @property
25 {
26     return s.color(fg.light_cyan);
27 }
28 
29 string green(string s) @property
30 {
31     return s.color(fg.light_green);
32 }
33 
34 string yellow(string s) @property
35 {
36     return s.color(fg.light_yellow);
37 }
38 
39 string red(string s) @property
40 {
41     return s.color(fg.light_red);
42 }
43 
44 string magenta(string s) @property
45 {
46     return s.color(fg.light_magenta);
47 }
48 
49 void info(string msg)
50 {
51     cwritefln("info: %s".white, msg);
52 }
53 
54 void warning(string msg)
55 {
56     cwritefln("warning: %s".yellow, msg);
57 }
58 
59 void error(string msg)
60 {
61     cwritefln("error: %s".red, msg);
62 }
63 
64 class ExternalProgramErrored : Exception
65 {
66     public
67     {
68         @safe pure nothrow this(int errorCode,
69                                 string message,
70                                 string file =__FILE__,
71                                 size_t line = __LINE__,
72                                 Throwable next = null)
73         {
74             super(message, file, line, next);
75             this.errorCode = errorCode;
76         }
77 
78         int errorCode;
79     }
80 }
81 
82 
83 void safeCommand(string cmd)
84 {
85     cwritefln("$ %s".cyan, cmd);
86     auto pid = spawnShell(cmd);
87     auto errorCode = wait(pid);
88     if (errorCode != 0)
89         throw new ExternalProgramErrored(errorCode, format("Command '%s' returned %s", cmd, errorCode));
90 }
91 
92 // Currently this only escapes spaces...
93 string escapeShellArgument(string arg)
94 {
95     version(Windows)
96     {
97         return `"` ~ arg ~ `"`;
98     }
99     else
100         return arg.replace(" ", "\\ ");
101 }
102 
103 /// Recursive directory copy.
104 /// https://forum.dlang.org/post/n7hc17$19jg$1@digitalmars.com
105 /// Returns: number of copied files
106 int copyRecurse(string from, string to)
107 {
108   //  from = absolutePath(from);
109   //  to = absolutePath(to);
110 
111     if (isDir(from))
112     {
113         mkdirRecurse(to);
114 
115         auto entries = dirEntries(from, SpanMode.shallow);
116         int result = 0;
117         foreach (entry; entries)
118         {
119             auto dst = buildPath(to, entry.name[from.length + 1 .. $]);
120             result += copyRecurse(entry.name, dst);
121         }
122         return result;
123     }
124     else
125     {
126         std.file.copy(from, to);
127         return 1;
128     }
129 }
130