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