-
Notifications
You must be signed in to change notification settings - Fork 11
/
Use.d
164 lines (125 loc) · 4.03 KB
/
Use.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Copyright: Copyright (c) 2010-2011 Jacob Carlborg. All rights reserved.
* Authors: Jacob Carlborg
* Version: Initial created: Nov 8, 2010
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
*/
module dvm.commands.Use;
import std.exception : assumeUnique;
import std.format : format;
import dvm.util.Util;
import tango.core.Exception;
import dvm.dvm.Options;
import dvm.dvm.ShellScript;
import dvm.dvm.Wrapper;
import dvm.commands.Command;
import dvm.io.Path;
version (Windows) import DvmRegistry = dvm.util.DvmRegistry;
version (Windows) import dvm.util.Windows;
class Use : Command
{
private
{
string envPath_;
Wrapper wrapper;
}
this ()
{
super("use", "Setup current shell to use a specific D compiler version.");
}
override void execute ()
{
loadEnvironment;
installWrapper;
version (Posix)
setPermissions;
version (Windows)
updateRegistry;
}
private:
void loadEnvironment ()
{
auto shellScript = createShellScript;
auto current = options.isDefault ? "default" : "current";
verbose(format(`Installing "%s" as the %s D compiler`, args.first, current));
writeShellScript(shellScript, options.path.result);
version (Posix)
if (options.isDefault)
{
verbose("Installing environment: ", options.path.defaultEnv);
copy(options.path.result, options.path.defaultEnv);
}
}
void installWrapper ()
{
wrapper.target = wrapperTarget(args.first);
wrapper.path = join(options.path.dvm, options.path.bin, "dvm-current-dc" ~ options.path.scriptExtension).assumeUnique;
verbose("Installing wrapper: " ~ wrapper.path);
if (exists(wrapper.path))
dvm.io.Path.remove(wrapper.path);
wrapper.write;
if (options.isDefault)
{
verbose("Installing wrapper: ", options.path.defaultBin);
copy(wrapper.path, options.path.defaultBin);
}
}
version (Windows)
void updateRegistry ()
{
if (options.isDefault)
{
auto dmdDir = join(options.path.compilers, "dmd-" ~ args.first, options.platform, options.path.bin);
DvmRegistry.updateEnvironment(options.path.binDir, dmdDir);
DvmRegistry.checkSystemPath();
broadcastSettingChange("Environment");
}
}
version (Posix)
void setPermissions ()
{
verbose("Setting permissions:");
setPermission(wrapper.path, "+x");
if (options.isDefault)
setPermission(options.path.defaultBin, "+x");
}
version (Posix)
void setPermission (string path, string mode)
{
verbose(options.indentation, "mode: ", mode);
verbose(options.indentation, "file: ", path);
permission(path, mode);
}
ShellScript createShellScript ()
{
verbose("Creating shell script");
auto sh = new ShellScript;
sh.echoOff;
sh.source(Sh.quote(envPath));
return sh;
}
void writeShellScript (ShellScript sh, string path)
{
validatePath(envPath);
sh.path = path;
if (!exists(options.path.tmp))
createFolder(options.path.tmp);
sh.write;
}
string envPath ()
{
if (envPath_.length > 0)
return envPath_;
return envPath_ = native(join(options.path.env, "dmd-" ~ args.first ~ options.path.scriptExtension)).assumeUnique;
}
string wrapperTarget (string compilerVersion)
{
auto basePath = join(options.path.compilers, "dmd-" ~ compilerVersion);
auto executable = "dmd" ~ options.path.executableExtension;
auto path = join(basePath, options.platform, options.path.bin, executable);
return exists(path) ? path : join(basePath, options.path.bin, executable);
}
}
template UseImpl ()
{
}