diff --git a/changelog/custom-dub-init-type.dd b/changelog/custom-dub-init-type.dd new file mode 100644 index 0000000..62f0418 --- /dev/null +++ b/changelog/custom-dub-init-type.dd @@ -0,0 +1,10 @@ +Support of custom dub init type + +Command dub init now supports custom dub packages for argument `-t`. +$(CONSOLE +> dub init -n myPackage --format sdl -t custom-dub-init-dubpackage -- --foo=bar +) + +Dub init will be invoked like before. The package `custom-dub-init-dubpackage` +contains a sub package `init` which will be invoked afterwards to create a custom +package skeleton. Additional arguments could be passed e.g. `-- --foo=bar`. diff --git a/source/dub/commandline.d b/source/dub/commandline.d index c210f4e..8039645 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -489,6 +489,7 @@ this.helpText = [ "Initializes an empty package of the specified type in the given directory. By default, the current working directory is used." ]; + this.acceptsAppArgs = true; } override void prepare(scope CommandArgs args) @@ -499,6 +500,7 @@ "minimal - simple \"hello world\" project (default)", "vibe.d - minimal HTTP server based on vibe.d", "deimos - skeleton for C header bindings", + "custom - custom project provided by dub package", ]); args.getopt("f|format", &m_format, [ "Sets the format to use for the package description file. Possible values:", @@ -510,7 +512,6 @@ override int execute(Dub dub, string[] free_args, string[] app_args) { string dir; - enforceUsage(app_args.empty, "Unexpected application arguments."); if (free_args.length) { dir = free_args[0]; @@ -565,18 +566,11 @@ } } - //TODO: Remove this block in next version - // Checks if argument uses current method of specifying project type. - if (free_args.length) + if (!["vibe.d", "deimos", "minimal"].canFind(m_templateType)) { - if (["vibe.d", "deimos", "minimal"].canFind(free_args[0])) - { - m_templateType = free_args[0]; - free_args = free_args[1 .. $]; - logInfo("Deprecated use of init type. Use --type=[vibe.d | deimos | minimal] in future."); - } + free_args ~= m_templateType; } - dub.createEmptyPackage(NativePath(dir), free_args, m_templateType, m_format, &depCallback); + dub.createEmptyPackage(NativePath(dir), free_args, m_templateType, m_format, &depCallback, app_args); logInfo("Package successfully created in %s", dir.length ? dir : "."); return 0; diff --git a/source/dub/dub.d b/source/dub/dub.d index dff7a97..0f81545 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -1168,7 +1168,8 @@ */ void createEmptyPackage(NativePath path, string[] deps, string type, PackageFormat format = PackageFormat.sdl, - scope void delegate(ref PackageRecipe, ref PackageFormat) recipe_callback = null) + scope void delegate(ref PackageRecipe, ref PackageFormat) recipe_callback = null, + string[] app_args = []) { if (!path.absolute) path = m_rootPath ~ path; path.normalize(); @@ -1196,10 +1197,40 @@ initPackage(path, depVers, type, format, recipe_callback); + if (!["vibe.d", "deimos", "minimal"].canFind(type)) { + runCustomInitialization(path, type, app_args); + } + //Act smug to the user. logInfo("Successfully created an empty project in '%s'.", path.toNativeString()); } + private void runCustomInitialization(NativePath path, string type, string[] runArgs) + { + string packageName = type; + auto template_pack = m_packageManager.getBestPackage(packageName, ">=0.0.0"); + if (!template_pack) template_pack = m_packageManager.getBestPackage(packageName, "~master"); + if (!template_pack) { + logInfo("%s is not present, getting and storing it user wide", packageName); + template_pack = fetch(packageName, Dependency(">=0.0.0"), defaultPlacementLocation, FetchOptions.none); + } + + Package initSubPackage = m_packageManager.getSubPackage(template_pack, "init", false); + auto template_dub = new Dub(null, m_packageSuppliers); + template_dub.loadPackage(initSubPackage); + auto compiler_binary = this.defaultCompiler; + + GeneratorSettings settings; + settings.config = "application"; + settings.compiler = getCompiler(compiler_binary); + settings.platform = settings.compiler.determinePlatform(settings.buildSettings, compiler_binary, m_defaultArchitecture); + settings.buildType = "debug"; + settings.run = true; + settings.runArgs = runArgs; + initSubPackage.recipe.buildSettings.workingDirectory = path.toNativeString(); + template_dub.generateProject("build", settings); + } + /** Converts the package recipe of the loaded root package to the given format. Params: diff --git a/source/dub/init.d b/source/dub/init.d index c5e4598..98c98c4 100644 --- a/source/dub/init.d +++ b/source/dub/init.d @@ -82,7 +82,7 @@ } switch (type) { - default: throw new Exception("Unknown package init type: "~type); + default: break; case "minimal": initMinimalPackage(root_path, p, &processRecipe); break; case "vibe.d": initVibeDPackage(root_path, p, &processRecipe); break; case "deimos": initDeimosPackage(root_path, p, &processRecipe); break; diff --git a/test/issue1651-custom-dub-init-type.sh b/test/issue1651-custom-dub-init-type.sh new file mode 100755 index 0000000..5c48313 --- /dev/null +++ b/test/issue1651-custom-dub-init-type.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +. $(dirname "${BASH_SOURCE[0]}")/common.sh +DIR=$(dirname "${BASH_SOURCE[0]}") +packname="custom-dub-init-type-sample" + +$DUB remove custom-dub-init-dubpackage --non-interactive --version=* 2>/dev/null || true +$DUB init -n $packname --format sdl -t custom-dub-init-dubpackage --skip-registry=all --registry=file://"$DIR"/issue1651-custom-dub-init-type -- --foo=bar + +function cleanup { + rm -rf $packname +} + +if [ ! -e $packname/dub.sdl ]; then # it failed + cleanup + die $LINENO 'No dub.sdl file has been generated.' +fi + +cd $packname +if ! { ${DUB} 2>&1 || true; } | grep -cF 'foo=bar'; then + cd .. + cleanup + die $LINENO 'Custom init type.' +fi +cd .. +cleanup \ No newline at end of file diff --git a/test/issue1651-custom-dub-init-type/.no_build b/test/issue1651-custom-dub-init-type/.no_build new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/issue1651-custom-dub-init-type/.no_build diff --git a/test/issue1651-custom-dub-init-type/.no_run b/test/issue1651-custom-dub-init-type/.no_run new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/issue1651-custom-dub-init-type/.no_run diff --git a/test/issue1651-custom-dub-init-type/.no_test b/test/issue1651-custom-dub-init-type/.no_test new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/issue1651-custom-dub-init-type/.no_test diff --git a/test/issue1651-custom-dub-init-type/custom-dub-init-dubpackage-1.0.1.zip b/test/issue1651-custom-dub-init-type/custom-dub-init-dubpackage-1.0.1.zip new file mode 100644 index 0000000..45b8b38 --- /dev/null +++ b/test/issue1651-custom-dub-init-type/custom-dub-init-dubpackage-1.0.1.zip Binary files differ