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