diff --git a/changelog/addcommand.dd b/changelog/addcommand.dd new file mode 100644 index 0000000..58e5b15 --- /dev/null +++ b/changelog/addcommand.dd @@ -0,0 +1,11 @@ +Add Command + +The `add` command adds a dependency to the dub.json/dub.sdl file. + +Running `dub add vibe-d` is equivalent to manually adding the following line to dub.sdl (X.Y.Z represents the latest version): +dependency "vibe-d" version="~>X.Y.Z" + +Or the following in dub.json: +"dependencies": { + "vibe-d": "~>X.Y.Z" +} diff --git a/source/dub/commandline.d b/source/dub/commandline.d index bd947f4..875fe06 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -57,6 +57,7 @@ CommandGroup("Package management", new FetchCommand, new InstallCommand, + new AddCommand, new RemoveCommand, new UninstallCommand, new UpgradeCommand, @@ -1125,9 +1126,52 @@ /******************************************************************************/ -/* FETCH / REMOVE / UPGRADE */ +/* FETCH / ADD / REMOVE / UPGRADE */ /******************************************************************************/ +class AddCommand : Command { + this() + { + this.name = "add"; + this.argumentsPattern = ""; + this.description = "Adds dependencies to the package file."; + this.helpText = [ + "Adds as dependencies.", + "", + "Running \"dub add \" is the same as adding to the \"dependencies\" section in dub.json/dub.sdl." + ]; + } + + override void prepare(scope CommandArgs args) {} + + override int execute(Dub dub, string[] free_args, string[] app_args) + { + import dub.recipe.io : readPackageRecipe, writePackageRecipe; + import dub.internal.vibecompat.core.file : existsFile; + enforceUsage(free_args.length != 0, "Expected one or more arguments."); + enforceUsage(app_args.length == 0, "Unexpected application arguments."); + + string filetype = existsFile(dub.rootPath ~ "dub.json") ? "json" : "sdl"; + foreach (depname; free_args) { + try { + auto ver = dub.getLatestVersion(depname); + auto dep = ver.isBranch ? Dependency(ver) : Dependency("~>" ~ ver.toString()); + auto pkg = readPackageRecipe(dub.rootPath ~ ("dub." ~ filetype)); + + pkg.buildSettings.dependencies[depname] = dep; + writePackageRecipe(dub.rootPath ~ ("dub." ~ filetype), pkg); + + logInfo("Added dependency %s %s", depname, dep.versionSpec); + } catch (Exception e) { + logError("Could not find package '%s'.", depname); + logDebug("Full error: %s", e.toString().sanitize); + } + } + + return 0; + } +} + class UpgradeCommand : Command { private { bool m_prerelease = false; @@ -1214,9 +1258,9 @@ this.argumentsPattern = ""; this.description = "Manually retrieves and caches a package"; this.helpText = [ - "Note: Use the \"dependencies\" field in the package description file (e.g. dub.json) if you just want to use a certain package as a dependency, you don't have to explicitly fetch packages.", + "Note: Use \"dub add \" if you just want to use a certain package as a dependency, you don't have to explicitly fetch packages.", "", - "Explicit retrieval/removal of packages is only needed when you want to put packages to a place where several applications can share these. If you just have an dependency to a package, just add it to your dub.json, dub will do the rest for you.", + "Explicit retrieval/removal of packages is only needed when you want to put packages in a place where several applications can share them. If you just have a dependency to add, use the `add` command. Dub will do the rest for you.", "", "Without specified options, placement/removal will default to a user wide shared location.", "", diff --git a/test/issue1574-addcommand.sh b/test/issue1574-addcommand.sh new file mode 100755 index 0000000..e84e1ac --- /dev/null +++ b/test/issue1574-addcommand.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +. $(dirname "${BASH_SOURCE[0]}")/common.sh + +tempDir="issue1574-addcommand" + +function cleanup { + cd .. + rm -rf $tempDir +} +trap cleanup EXIT + + +$DUB init -n $tempDir +cd $tempDir + +echo "import mir.math.common; void main(){}" > source/app.d + +$DUB add mir-core + +#if dub fails to compile, that means that the "import mir.math.common" did not work +if ! $DUB build; then + die "Add command failed" +fi