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. /*if( add_dependency ){
  169. dub.loadPackageFromCwd();
  170. dub.addDependency(...);
  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. GeneratorSettings gensettings;
  244. gensettings.platform = build_platform;
  245. gensettings.config = build_config;
  246. gensettings.buildType = build_type;
  247. gensettings.compiler = compiler;
  248. gensettings.compilerBinary = compiler_name;
  249. gensettings.buildSettings = build_settings;
  250. gensettings.run = cmd == "run";
  251. gensettings.runArgs = args[1 .. $];
  252.  
  253. logDiagnostic("Generating using %s", generator);
  254. dub.generateProject(generator, gensettings);
  255. if( build_type == "ddox" ) dub.runDdox();
  256. break;
  257. case "describe":
  258. if (!loadCwdPackage()) return 1;
  259. dub.describeProject(build_platform, build_config);
  260. break;
  261. }
  262.  
  263. return 0;
  264. }
  265. catch(Throwable e)
  266. {
  267. logError("Error: %s\n", e.msg);
  268. logDiagnostic("Full exception: %s", sanitize(e.toString()));
  269. logInfo("Run 'dub help' for usage information.");
  270. return 1;
  271. }
  272. }
  273.  
  274. private void showHelp(string command)
  275. {
  276. if(command == "uninstall" || command == "install") {
  277. logInfo(
  278. `Usage: dub <install|uninstall> <package> [<options>]
  279.  
  280. Note: use dependencies (package.json) if you want to add a dependency, you
  281. don't have to fiddle with installation stuff.
  282.  
  283. (Un)Installation of packages is only needed when you want to put packages to a
  284. place where several applications can share these. If you just have an
  285. dependency to a package, just add it to your package.json, dub will do the rest
  286. for you.
  287.  
  288. Without specified options, (un)installation will default to a user wide shared
  289. location.
  290.  
  291. Complete applications can be installed and run easily by e.g.
  292. dub install vibelog --local
  293. cd vibelog
  294. dub
  295. This will grab all needed dependencies and compile and run the application.
  296.  
  297. Install options:
  298. --version Use the specified version/branch instead of the latest
  299. For the uninstall command, this may be a wildcard
  300. string: "*", which will remove all packages from the
  301. specified location.
  302. --system Install system wide instead of user local
  303. --local Install as in a sub folder of the current directory
  304. Note that system and local cannot be mixed.
  305. `);
  306. return;
  307. }
  308.  
  309. // No specific help, show general help.
  310. logInfo(
  311. `Usage: dub [<command>] [<vibe options...>] [-- <application options...>]
  312.  
  313. Manages the DUB project in the current directory. "--" can be used to separate
  314. DUB options from options passed to the application. If the command is omitted,
  315. dub will default to "run".
  316.  
  317. Available commands:
  318. help Prints this help screen
  319. init [<directory>] Initializes an empy project in the specified directory
  320. run Compiles and runs the application (default command)
  321. build Just compiles the application in the project directory
  322. upgrade Forces an upgrade of all dependencies
  323. install <name> Manually installs a package. See 'dub help install'.
  324. uninstall <name> Uninstalls a package. See 'dub help uninstall'.
  325. add-local <dir> <version>
  326. Adds a local package directory (e.g. a git repository)
  327. remove-local <dir> Removes a local package directory
  328. list-installed Prints a list of all installed packages
  329. generate <name> Generates project files using the specified generator:
  330. visuald, mono-d, build, rdmd
  331. describe Prints a JSON description of the project and its
  332. dependencies
  333.  
  334. General options:
  335. --annotate Do not execute dependency installations, just print
  336. -v --verbose Also output debug messages
  337. --vverbose Also output trace messages (produces a lot of output)
  338. -q --quiet Only output warnings and errors
  339. --vquiet No output
  340.  
  341. Build/run options:
  342. --build=NAME Specifies the type of build to perform. Note that
  343. setting the DFLAGS environment variable will override
  344. the build type with custom flags.
  345. Possible names:
  346. debug (default), plain, release, unittest, profile,
  347. docs, ddox, cov, unittest-cov
  348. --config=NAME Builds the specified configuration. Configurations can
  349. be defined in package.json
  350. --compiler=NAME Uses one of the supported compilers:
  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. }