1 /**
2  * Copyright: Copyright (c) 2010-2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Aug 15, 2010
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module dvm.dvm.Options;
8 
9 import std.exception : assumeUnique;
10 import std.path : baseName;
11 import std.process : environment;
12 
13 import tango.sys.Environment;
14 import tango.sys.HomeFolder;
15 
16 import dvm.io.Path;
17 
18 enum Shell
19 {
20     invalid,
21     unkown,
22     bash,
23     zsh
24 }
25 
26 class Options
27 {
28     enum indentation = "    ";
29     enum numberOfIndentations = 1;
30     enum path = Path();
31 
32     private static Options instance_;
33 
34     bool verbose = false;
35     bool tango = false;
36     bool isDefault = false;
37 
38     bool force = false;
39     bool decline = false;
40     bool latest = false;
41     bool compileDebug = false;
42 
43     private Shell shell_;
44 
45     version (D_LP64)
46         bool is64bit = true;
47 
48     else
49         bool is64bit = false;
50 
51     version (OSX)
52         enum platform = "osx";
53 
54     else version (FreeBSD)
55         enum platform = "freebsd";
56 
57     else version (linux)
58         enum platform = "linux";
59 
60     else version (Windows)
61         enum platform = "windows";
62 
63     else
64         static assert (false, "Platform not supported");
65 
66 	private this () {}
67 
68 	static Options instance ()
69 	{
70 		if (instance_)
71 			return instance_;
72 
73 		return instance_ = new typeof(this);
74 	}
75 
76     Shell shell()
77     {
78         if (shell_ != Shell.invalid)
79             return shell_;
80 
81         if ("SHELL" in environment)
82         {
83             with(Shell) switch (environment["SHELL"].baseName)
84             {
85                 case "bash": return shell_ = bash;
86                 case "zsh": return shell_ = zsh;
87                 default: return shell_ = unkown;
88             }
89         }
90 
91         return shell_ = Shell.unkown;
92     }
93 }
94 
95 private struct Path
96 {
97     enum bin = "bin";
98     enum bin32 = "bin32";
99     enum bin64 = "bin64";
100     enum src = "src";
101     enum lib = "lib";
102     enum lib32 = "lib32";
103     enum lib64 = "lib64";
104     enum import_ = "import";
105     enum license = "license.txt";
106     enum readme = "README.TXT";
107     enum std = "std";
108     enum object_di = "object.di";
109 
110     version (Posix)
111     {
112         enum libExtension = ".a";
113         enum tangoLibName = "libtango";
114         enum pathSeparator = ":";
115         enum confName = "dmd.conf";
116         enum scriptExtension = "";
117         enum executableExtension = "";
118     }
119 
120     else
121     {
122         enum libExtension = ".lib";
123         enum tangoLibName = "tango";
124         enum pathSeparator = ";";
125         enum confName = "sc.ini";
126         enum scriptExtension = ".bat";
127         enum executableExtension = ".exe";
128     }
129 
130     private
131     {
132         string home_;
133         string dvm_;
134         string env_;
135         string compilers_;
136         string archives_;
137         string result_;
138         string tmp_;
139         string scripts_;
140         string binDir_;
141         string dvmScript_;
142         string dvmExecutable_;
143         string conf_;
144         string tangoZip_;
145         string tangoTmp_;
146         string tangoBob_;
147         string tangoLib_;
148         string tangoSrc_;
149         string tangoObject_;
150         string tangoVendor_;
151         string tangoUnarchived_;
152         string defaultEnv_;
153         string defaultBin_;
154 
155         version (Posix)
156         {
157             enum string dvmDir = ".dvm";
158             enum string dvmExecName = "dvm";
159         }
160 
161         else version (Windows)
162         {
163             enum string dvmDir = "dvm";
164             enum string dvmExecName = "_dvm";
165         }
166     }
167 
168     string home ()
169     {
170         if (home_.length > 0)
171             return home_;
172 
173         version (Posix)
174             return home_ = homeFolder.assumeUnique;
175 
176         version (Windows)
177             return home_ = standard(Environment.get("APPDATA"));
178     }
179 
180     string dvm ()
181     {
182         if (dvm_.length > 0)
183             return dvm_;
184 
185         return dvm_ = join(home, dvmDir).assumeUnique;
186     }
187 
188     string dvmExecutable ()
189     {
190         if (dvmExecutable_.length > 0)
191             return dvmExecutable_;
192 
193         return dvmExecutable_ = join(binDir, dvmExecName ~ executableExtension).assumeUnique;
194     }
195 
196     string dvmScript ()
197     {
198         if (dvmScript_.length > 0)
199             return dvmScript_;
200 
201         version (Posix)
202             auto dir = scripts;
203 
204         version (Windows)
205             auto dir = binDir;
206 
207         return dvmScript_ = join(dir, "dvm" ~ scriptExtension).assumeUnique;
208     }
209 
210     string env ()
211     {
212         if (env_.length > 0)
213             return env_;
214 
215         return env_ = join(dvm, "env").assumeUnique;
216     }
217 
218     string compilers ()
219     {
220         if (compilers_.length > 0)
221             return compilers_;
222 
223         return compilers_ = join(dvm, "compilers").assumeUnique;
224     }
225 
226     string archives ()
227     {
228         if (archives_.length > 0)
229             return archives_;
230 
231         return archives_ = join(dvm, "archives").assumeUnique;
232     }
233 
234     string result ()
235     {
236         if (result_.length > 0)
237             return result_;
238 
239         return result_ = join(tmp, "result" ~ scriptExtension).assumeUnique;
240     }
241 
242     string scripts ()
243     {
244         if (scripts_.length > 0)
245             return scripts_;
246 
247         return scripts_ = join(dvm, "scripts").assumeUnique;
248     }
249 
250     string binDir ()
251     {
252         if (binDir_.length > 0)
253             return binDir_;
254 
255         return binDir_ = join(dvm, "bin").assumeUnique;
256     }
257 
258     string tmp ()
259     {
260         if (tmp_.length > 0)
261             return tmp_;
262 
263         return tmp_ = join(dvm, "tmp").assumeUnique;
264     }
265 
266     string conf ()
267     {
268         if (conf_.length > 0)
269             return conf_;
270 
271         return conf_ = join(bin, confName).assumeUnique;
272     }
273 
274     string tangoZip ()
275     {
276         if (tangoZip_.length > 0)
277             return tangoZip_;
278 
279         return tangoZip_ = join(tmp, "tango.zip").assumeUnique;
280     }
281 
282     string tangoTmp ()
283     {
284         if (tangoTmp_.length > 0)
285             return tangoTmp_;
286 
287         return tangoTmp_ = join(tangoUnarchived, "trunk").assumeUnique;
288     }
289 
290     string tangoUnarchived ()
291     {
292         if (tangoUnarchived_.length > 0)
293             return tangoUnarchived_;
294 
295         return tangoUnarchived_ = join(tmp, "tango", "head").assumeUnique;
296     }
297 
298     string tangoBob ()
299     {
300         if (tangoBob_.length > 0)
301             return tangoBob_;
302 
303         auto suffix = Options.instance.is64bit ? "64" : "32";
304         auto path = join(tangoTmp, "build", "bin").assumeUnique;
305 
306         version (OSX)
307             path = join(path, "osx" ~ suffix).assumeUnique;
308 
309         else version (FreeBSD)
310             path = join(path, "freebsd" ~ suffix).assumeUnique;
311 
312         else version (linux)
313             path = join(path, "linux" ~ suffix).assumeUnique;
314 
315         else version (Windows)
316             path = join(path, "win" ~ suffix).assumeUnique;
317 
318         else
319             static assert(false, "Unhandled platform for installing Tango");
320 
321         return tangoBob_ = join(path, "bob" ~ executableExtension).assumeUnique;
322     }
323 
324     string tangoLib ()
325     {
326         if (tangoLib_.length > 0)
327             return tangoLib_;
328 
329         return tangoLib_ = join(tangoTmp, tangoLibName ~ libExtension).assumeUnique;
330     }
331 
332     string tangoSrc ()
333     {
334         if (tangoSrc_.length)
335             return tangoSrc_;
336 
337         return tangoSrc_ = join(tangoTmp, "tango").assumeUnique;
338     }
339 
340     string tangoObject ()
341     {
342         if (tangoObject_.length > 0)
343             return tangoObject_;
344 
345         return tangoObject_ = join(tangoTmp, object_di).assumeUnique;
346     }
347 
348     string tangoVendor ()
349     {
350         if (tangoVendor_.length > 0)
351             return tangoVendor_;
352 
353         return tangoVendor_ = join(tangoSrc, "core", "vendor", std).assumeUnique;
354     }
355 
356     string defaultEnv ()
357     {
358         if (defaultEnv_.length > 0)
359             return defaultEnv_;
360 
361         return defaultEnv_ = join(env, "default").assumeUnique;
362     }
363 
364     string defaultBin ()
365     {
366         if (defaultBin_.length > 0)
367             return defaultBin_;
368 
369         return defaultBin_ = join(binDir, "dvm-default-dc" ~ scriptExtension).assumeUnique;
370     }
371 }