Newer
Older
dub_jkp / source / app.d
  1. /**
  2. The entry point to dub
  3.  
  4. Copyright: © 2012 Matthias Dondorff
  5. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  6. Authors: Matthias Dondorff, Sönke Ludwig
  7. */
  8. module app;
  9.  
  10. import dub.compilers.compiler;
  11. import dub.dependency;
  12. import dub.dub;
  13. import dub.generators.generator;
  14. import dub.internal.std.process;
  15. import dub.internal.vibecompat.core.file;
  16. import dub.internal.vibecompat.core.log;
  17. import dub.internal.vibecompat.inet.url;
  18. import dub.package_;
  19. import dub.packagesupplier;
  20. import dub.project;
  21. import dub.version_;
  22.  
  23. import std.algorithm;
  24. import std.array;
  25. import std.conv;
  26. import std.encoding;
  27. import std.exception;
  28. import std.file;
  29. import std.getopt;
  30.  
  31.  
  32. int main(string[] args)
  33. {
  34. string cmd;
  35.  
  36. version(Windows){
  37. // rdmd uses $TEMP to compute a temporary path. since cygwin substitutes backslashes
  38. // with slashes, this causes OPTLINK to fail (it thinks path segments are options)
  39. // we substitute the other way around here to fix this.
  40. environment["TEMP"] = environment["TEMP"].replace("/", "\\");
  41. }
  42.  
  43. try {
  44. // parse general options
  45. bool verbose, vverbose, quiet, vquiet;
  46. bool help, nodeps, annotate;
  47. LogLevel loglevel = LogLevel.info;
  48. string build_type, build_config;
  49. string compiler_name = "dmd";
  50. string arch;
  51. bool rdmd = false;
  52. bool print_platform, print_builds, print_configs;
  53. bool install_system = false, install_local = false;
  54. string install_version;
  55. string[] registry_urls;
  56. string[] debug_versions;
  57. getopt(args,
  58. "v|verbose", &verbose,
  59. "vverbose", &vverbose,
  60. "q|quiet", &quiet,
  61. "vquiet", &vquiet,
  62. "h|help", &help, // obsolete
  63. "nodeps", &nodeps,
  64. "annotate", &annotate,
  65. "build", &build_type,
  66. "compiler", &compiler_name,
  67. "arch", &arch,
  68. "rdmd", &rdmd,
  69. "config", &build_config,
  70. "debug", &debug_versions,
  71. "print-builds", &print_builds,
  72. "print-configs", &print_configs,
  73. "print-platform", &print_platform,
  74. "system", &install_system,
  75. "local", &install_local,
  76. "version", &install_version,
  77. "registry", &registry_urls
  78. );
  79.  
  80. if( vverbose ) loglevel = LogLevel.debug_;
  81. else if( verbose ) loglevel = LogLevel.diagnostic;
  82. else if( vquiet ) loglevel = LogLevel.none;
  83. else if( quiet ) loglevel = LogLevel.warn;
  84. setLogLevel(loglevel);
  85.  
  86. // extract the command
  87. if( args.length > 1 && !args[1].startsWith("-") ){
  88. cmd = args[1];
  89. args = args[0] ~ args[2 .. $];
  90. } else cmd = "run";
  91.  
  92. // contrary to the documentation, getopt does not remove --
  93. if( args.length >= 2 && args[1] == "--" ) args = args[0] ~ args[2 .. $];
  94.  
  95. // display help if requested (obsolete)
  96. if( help ){
  97. showHelp(cmd);
  98. return 0;
  99. }
  100.  
  101. BuildSettings build_settings;
  102. auto compiler = getCompiler(compiler_name);
  103. auto build_platform = compiler.determinePlatform(build_settings, compiler_name, arch);
  104. build_settings.addDFlags(debug_versions.map!(v => "-debug="~v).array);
  105.  
  106. if( print_platform ){
  107. logInfo("Build platform:");
  108. logInfo(" Compiler: %s", build_platform.compiler);
  109. logInfo(" System: %s", build_platform.platform.join(" "));
  110. logInfo(" Architecture: %s", build_platform.architecture.join(" "));
  111. logInfo("");
  112. }
  113.  
  114. Dub dub = new Dub(registry_urls.map!(url => cast(PackageSupplier)new RegistryPackageSupplier(Url(url))).array);
  115. string def_config;
  116.  
  117. bool loadCwdPackage()
  118. {
  119. if( !existsFile("package.json") && !existsFile("source/app.d") ){
  120. logInfo("");
  121. logInfo("Neither package.json, nor source/app.d was found in the current directory.");
  122. logInfo("Please run dub from the root directory of an existing package, or create a new");
  123. logInfo("package using \"dub init <name>\".");
  124. logInfo("");
  125. showHelp(null);
  126. return false;
  127. }
  128.  
  129. dub.loadPackageFromCwd();
  130.  
  131. def_config = dub.getDefaultConfiguration(build_platform);
  132. if( !build_config.length ) build_config = def_config;
  133.  
  134. return true;
  135. }
  136.  
  137. // handle the command
  138. switch( cmd ){
  139. default:
  140. enforce(false, "Command is unknown: " ~ cmd);
  141. assert(false);
  142. case "help":
  143. if(args.length >= 2) cmd = args[1];
  144. showHelp(cmd);
  145. return 0;
  146. case "init":
  147. string dir;
  148. if( args.length >= 2 ) dir = args[1];
  149. dub.createEmptyPackage(Path(dir));
  150. return 0;
  151. case "upgrade":
  152. dub.loadPackageFromCwd();
  153. logInfo("Upgrading project in %s", dub.projectPath.toNativeString());
  154. logDiagnostic("dub initialized");
  155. dub.update(UpdateOptions.Upgrade | (annotate ? UpdateOptions.JustAnnotate : UpdateOptions.None));
  156. return 0;
  157. case "install":
  158. enforce(args.length >= 2, "Missing package name.");
  159. auto location = InstallLocation.userWide;
  160. auto name = args[1];
  161. enforce(!install_local || !install_system, "Cannot install locally and system wide at the same time.");
  162. if( install_local ) location = InstallLocation.local;
  163. else if( install_system ) location = InstallLocation.systemWide;
  164. if( install_version.length ) dub.install(name, Dependency(install_version), location);
  165. else {
  166. try dub.install(name, Dependency(">=0.0.0"), location);
  167. catch(Exception e){
  168. logInfo("Installing a release version failed: %s", e.msg);
  169. logInfo("Retry with ~master...");
  170. dub.install(name, Dependency("~master"), location);
  171. }
  172. }
  173. break;
  174. case "uninstall":
  175. enforce(args.length >= 2, "Missing package name.");
  176. auto location = InstallLocation.userWide;
  177. auto package_id = args[1];
  178. enforce(!install_local || !install_system, "Cannot install locally and system wide at the same time.");
  179. if( install_local ) location = InstallLocation.local;
  180. else if( install_system ) location = InstallLocation.systemWide;
  181. try dub.uninstall(package_id, install_version, location);
  182. catch logError("Please specify a individual version or use the wildcard identifier '%s' (without quotes).", Dub.UninstallVersionWildcard);
  183. break;
  184. case "add-local":
  185. enforce(args.length >= 3, "Missing arguments.");
  186. dub.addLocalPackage(args[1], args[2], install_system);
  187. break;
  188. case "remove-local":
  189. enforce(args.length >= 2, "Missing path to package.");
  190. dub.removeLocalPackage(args[1], install_system);
  191. break;
  192. case "add-path":
  193. enforce(args.length >= 2, "Missing search path.");
  194. dub.addSearchPath(args[1], install_system);
  195. break;
  196. case "remove-path":
  197. enforce(args.length >= 2, "Missing search path.");
  198. dub.removeSearchPath(args[1], install_system);
  199. break;
  200. case "list-installed":
  201. logInfo("Installed packages:");
  202. foreach (p; dub.packageManager.getPackageIterator())
  203. logInfo(" %s %s: %s", p.name, p.ver, p.path.toNativeString());
  204. logInfo("");
  205. break;
  206. case "run":
  207. case "build":
  208. case "generate":
  209. if (!loadCwdPackage()) return 1;
  210.  
  211. string generator;
  212. if( cmd == "run" || cmd == "build" ) generator = rdmd ? "rdmd" : "build";
  213. else {
  214. if( args.length >= 2 ) generator = args[1];
  215. if(generator.empty) {
  216. logInfo("Usage: dub generate <generator_name>");
  217. return 1;
  218. }
  219. }
  220.  
  221. if( print_builds ){
  222. logInfo("Available build types:");
  223. foreach( tp; ["debug", "release", "unittest", "profile"] )
  224. logInfo(" %s", tp);
  225. logInfo("");
  226. }
  227.  
  228. if( print_configs ){
  229. logInfo("Available configurations:");
  230. foreach( tp; dub.configurations )
  231. logInfo(" %s%s", tp, tp == def_config ? " [default]" : null);
  232. logInfo("");
  233. }
  234.  
  235. if( !nodeps ){
  236. logInfo("Checking dependencies in '%s'", dub.projectPath.toNativeString());
  237. logDiagnostic("dub initialized");
  238. dub.update(annotate ? UpdateOptions.JustAnnotate : UpdateOptions.None);
  239. }
  240.  
  241. enforce(build_config.length == 0 || dub.configurations.canFind(build_config), "Unknown build configuration: "~build_config);
  242.  
  243. if (build_type.length == 0) {
  244. if (environment.get("DFLAGS")) build_type = "$DFLAGS";
  245. else build_type = "debug";
  246. }
  247.  
  248. GeneratorSettings gensettings;
  249. gensettings.platform = build_platform;
  250. gensettings.config = build_config;
  251. gensettings.buildType = build_type;
  252. gensettings.compiler = compiler;
  253. gensettings.compilerBinary = compiler_name;
  254. gensettings.buildSettings = build_settings;
  255. gensettings.run = cmd == "run";
  256. gensettings.runArgs = args[1 .. $];
  257.  
  258. logDiagnostic("Generating using %s", generator);
  259. dub.generateProject(generator, gensettings);
  260. if( build_type == "ddox" ) dub.runDdox();
  261. break;
  262. case "describe":
  263. if (!loadCwdPackage()) return 1;
  264. dub.describeProject(build_platform, build_config);
  265. break;
  266. }
  267.  
  268. return 0;
  269. }
  270. catch(Throwable e)
  271. {
  272. logError("Error: %s\n", e.msg);
  273. logDiagnostic("Full exception: %s", sanitize(e.toString()));
  274. logInfo("Run 'dub help' for usage information.");
  275. return 1;
  276. }
  277. }
  278.  
  279. private void showHelp(string command)
  280. {
  281. if(command == "uninstall" || command == "install") {
  282. logInfo(
  283. `Usage: dub <install|uninstall> <package> [<options>]
  284.  
  285. Note: use dependencies (package.json) if you want to add a dependency, you
  286. don't have to fiddle with installation stuff.
  287.  
  288. (Un)Installation of packages is only needed when you want to put packages to a
  289. place where several applications can share these. If you just have an
  290. dependency to a package, just add it to your package.json, dub will do the rest
  291. for you.
  292.  
  293. Without specified options, (un)installation will default to a user wide shared
  294. location.
  295.  
  296. Complete applications can be installed and run easily by e.g.
  297. dub install vibelog --local
  298. cd vibelog
  299. dub
  300. This will grab all needed dependencies and compile and run the application.
  301.  
  302. Install options:
  303. --version Use the specified version/branch instead of the latest
  304. For the uninstall command, this may be a wildcard
  305. string: "*", which will remove all packages from the
  306. specified location.
  307. --system Install system wide instead of user local
  308. --local Install as in a sub folder of the current directory
  309. Note that system and local cannot be mixed.
  310. `);
  311. return;
  312. }
  313.  
  314. // No specific help, show general help.
  315. logInfo(
  316. `Usage: dub [<command>] [<options...>] [-- <application arguments...>]
  317.  
  318. Manages the DUB project in the current directory. "--" can be used to separate
  319. DUB options from options passed to the application. If the command is omitted,
  320. dub will default to "run".
  321.  
  322. Available commands:
  323. help Prints this help screen
  324. init [<directory>] Initializes an empy project in the specified directory
  325. run Compiles and runs the application (default command)
  326. build Just compiles the application in the project directory
  327. upgrade Forces an upgrade of all dependencies
  328. install <name> Manually installs a package. See 'dub help install'.
  329. uninstall <name> Uninstalls a package. See 'dub help uninstall'.
  330. add-local <dir> <version>
  331. Adds a local package directory (e.g. a git repository)
  332. remove-local <dir> Removes a local package directory
  333. add-path <dir> Adds a default package search path
  334. remove-path <dir> Removes a package search path
  335. list-installed Prints a list of all installed packages
  336. generate <name> Generates project files using the specified generator:
  337. visuald, visuald-combined, mono-d, build, rdmd
  338. describe Prints a JSON description of the project and its
  339. dependencies
  340.  
  341. General options:
  342. --annotate Do not execute dependency installations, just print
  343. -v --verbose Also output debug messages
  344. --vverbose Also output trace messages (produces a lot of output)
  345. -q --quiet Only output warnings and errors
  346. --vquiet No output
  347. --registry=URL Search the given DUB registry URL first when resolving
  348. dependencies. Can be specified multiple times.
  349.  
  350. Build/run options:
  351. --build=NAME Specifies the type of build to perform. Note that
  352. setting the DFLAGS environment variable will override
  353. the build type with custom flags.
  354. Possible names:
  355. debug (default), plain, release, unittest, profile,
  356. docs, ddox, cov, unittest-cov and custom types
  357. --config=NAME Builds the specified configuration. Configurations can
  358. be defined in package.json
  359. --compiler=NAME Specifies the compiler binary to use. Arbitrary pre-
  360. and suffixes to the identifiers below are recognized
  361. (e.g. ldc2 or dmd-2.063) and matched to the proper
  362. compiler type:
  363. dmd (default), gcc, ldc, gdmd, ldmd
  364. --arch=NAME Force a different architecture (e.g. x86 or x86_64)
  365. --nodeps Do not check dependencies for 'run' or 'build'
  366. --print-builds Prints the list of available build types
  367. --print-configs Prints the list of available configurations
  368. --print-platform Prints the identifiers for the current build platform
  369. as used for the build fields in package.json
  370. --rdmd Use rdmd instead of directly invoking the compiler
  371. --debug=NAME Define the specified debug version identifier when
  372. builing - can be used multiple times
  373.  
  374. Install options:
  375. --version Use the specified version/branch instead of the latest
  376. --system Install system wide instead of user local
  377. --local Install as in a sub folder of the current directory
  378.  
  379. `);
  380. logInfo("DUB version %s", dubVersion);
  381. }