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