diff --git a/source/dub/commandline.d b/source/dub/commandline.d index 04c2e34..0b37efb 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -316,6 +316,7 @@ class InitCommand : Command { private{ string m_buildType = "minimal"; + PackageFormat m_format = PackageFormat.sdl; } this() { @@ -336,6 +337,10 @@ "vibe.d - minimal HTTP server based on vibe.d", "deimos - skeleton for C header bindings", ]); + args.getopt("f|format", &m_format, [ + "Sets the format to use for the package description file. Possible values:" + " sdl, json" + ]); } override int execute(Dub dub, string[] free_args, string[] app_args) @@ -358,7 +363,7 @@ logInfo("Deprecated use of init type. Use --type=[vibe.d | deimos | minimal] in future."); } } - dub.createEmptyPackage(Path(dir), free_args, m_buildType); + dub.createEmptyPackage(Path(dir), free_args, m_buildType, m_format); return 0; } } diff --git a/source/dub/dub.d b/source/dub/dub.d index f3b5848..3d8286c 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -641,7 +641,7 @@ m_packageManager.removeSearchPath(makeAbsolute(path), system ? LocalPackageType.system : LocalPackageType.user); } - void createEmptyPackage(Path path, string[] deps, string type) + void createEmptyPackage(Path path, string[] deps, string type, PackageFormat format = PackageFormat.sdl) { if (!path.absolute) path = m_rootPath ~ path; path.normalize(); @@ -660,13 +660,13 @@ } } if(notFound.length > 1){ - throw new Exception(format("Couldn't find packages: %-(%s, %).", notFound)); + throw new Exception(.format("Couldn't find packages: %-(%s, %).", notFound)); } else if(notFound.length == 1){ - throw new Exception(format("Couldn't find package: %-(%s, %).", notFound)); + throw new Exception(.format("Couldn't find package: %-(%s, %).", notFound)); } - initPackage(path, depVers, type); + initPackage(path, depVers, type, format); //Act smug to the user. logInfo("Successfully created an empty project in '%s'.", path.toNativeString()); diff --git a/source/dub/init.d b/source/dub/init.d index 40a5cd7..1e09c0c 100644 --- a/source/dub/init.d +++ b/source/dub/init.d @@ -1,7 +1,7 @@ /** Empty package initialization code. - Copyright: © 2013 rejectedsoftware e.K. + Copyright: © 2013-2015 rejectedsoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ @@ -9,7 +9,7 @@ import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; -import dub.package_ : packageInfoFiles, defaultPackageFilename; +import dub.package_ : PackageFormat, packageInfoFiles, defaultPackageFilename; import std.datetime; import std.exception; @@ -19,7 +19,7 @@ import std.string; -void initPackage(Path root_path, string[string] deps, string type) +void initPackage(Path root_path, string[string] deps, string type, PackageFormat format) { void enforceDoesNotExist(string filename) { enforce(!existsFile(root_path ~ filename), "The target directory already contains a '"~filename~"' file. Aborting."); @@ -41,16 +41,16 @@ switch (type) { default: throw new Exception("Unknown package init type: "~type); - case "minimal": initMinimalPackage(root_path, deps); break; - case "vibe.d": initVibeDPackage(root_path, deps); break; - case "deimos": initDeimosPackage(root_path, deps); break; + case "minimal": initMinimalPackage(root_path, deps, format); break; + case "vibe.d": initVibeDPackage(root_path, deps, format); break; + case "deimos": initDeimosPackage(root_path, deps, format); break; } writeGitignore(root_path); } -void initMinimalPackage(Path root_path, string[string] deps) +void initMinimalPackage(Path root_path, string[string] deps, PackageFormat format) { - writePackageJson(root_path, "A minimal D application.", deps); + writePackageDescription(format, root_path, "A minimal D application.", deps); createDirectory(root_path ~ "source"); write((root_path ~ "source/app.d").toNativeString(), q{import std.stdio; @@ -62,13 +62,13 @@ }); } -void initVibeDPackage(Path root_path, string[string] deps) +void initVibeDPackage(Path root_path, string[string] deps, PackageFormat format) { if("vibe-d" !in deps) deps["vibe-d"] = "~>0.7.19"; - writePackageJson(root_path, "A simple vibe.d server application.", - deps, ["versions": `["VibeDefaultMain"]`]); + writePackageDescription(format, root_path, "A simple vibe.d server application.", + deps, ["versions": ["VibeDefaultMain"]]); createDirectory(root_path ~ "source"); createDirectory(root_path ~ "views"); createDirectory(root_path ~ "public"); @@ -92,16 +92,30 @@ }); } -void initDeimosPackage(Path root_path, string[string] deps) +void initDeimosPackage(Path root_path, string[string] deps, PackageFormat format) { auto name = root_path.head.toString().toLower(); - writePackageJson(root_path, "Deimos Bindings for "~name~".", - deps, ["targetType": `"sourceLibrary"`, "importPaths": `["."]`]); + writePackageDescription(format, root_path, "Deimos Bindings for "~name~".", + deps, ["importPaths": ["."]], ["targetType": "sourceLibrary"]); createDirectory(root_path ~ "C"); createDirectory(root_path ~ "deimos"); } -void writePackageJson(Path root_path, string description, string[string] dependencies = null, string[string] addFields = null) +void writePackageDescription(PackageFormat format, Path root_path, string description, string[string] dependencies = null, string[][string] array_fields = null, string[string] string_fields = null) +{ + final switch (format) { + case PackageFormat.json: + foreach (f, v; array_fields) string_fields[f] = .format("[%(%s, %)]", v); + writePackageJSON(root_path, description, dependencies, string_fields); + break; + case PackageFormat.sdl: + foreach (f, v; array_fields) string_fields[f] = .format("%(%s %)", v); + writePackageSDL(root_path, description, dependencies, string_fields); + break; + } +} + +private void writePackageJSON(Path root_path, string description, string[string] dependencies = null, string[string] raw_fields = null) { import std.algorithm : map; @@ -111,7 +125,7 @@ version (Windows) username = environment.get("USERNAME", "Peter Parker"); else username = environment.get("USER", "Peter Parker"); - auto fil = openFile(root_path ~ defaultPackageFilename, FileMode.Append); + auto fil = openFile(root_path ~ "dub.json", FileMode.Append); scope(exit) fil.close(); fil.formattedWrite("{\n\t\"name\": \"%s\",\n", root_path.head.toString().toLower()); @@ -121,10 +135,33 @@ fil.formattedWrite("\t\"dependencies\": {"); fil.formattedWrite("%(\n\t\t%s: %s,%)", dependencies); fil.formattedWrite("\n\t}"); - fil.formattedWrite("%-(,\n\t\"%s\": %s%)", addFields); + fil.formattedWrite("%-(,\n\t\"%s\": %s%)", raw_fields); fil.write("\n}\n"); } +private void writePackageSDL(Path root_path, string description, string[string] dependencies = null, string[string] raw_fields = null) +{ + import std.algorithm : map; + + assert(!root_path.empty); + + string username; + version (Windows) username = environment.get("USERNAME", "Peter Parker"); + else username = environment.get("USER", "Peter Parker"); + + auto fil = openFile(root_path ~ "dub.sdl", FileMode.Append); + scope(exit) fil.close(); + + fil.formattedWrite("name \"%s\"\n", root_path.head.toString().toLower()); + fil.formattedWrite("description \"%s\"\n", description); + fil.formattedWrite("copyright \"Copyright © %s, %s\"\n", Clock.currTime().year, username); + fil.formattedWrite("authors \"%s\"\n", username); + foreach (d, v; dependencies) + fil.formattedWrite("dependency \"%s\" version=\"%s\"\n", d, v); + foreach (f, v; raw_fields) + fil.formattedWrite("%s %s\n", f, v); +} + void writeGitignore(Path root_path) { write((root_path ~ ".gitignore").toNativeString(), diff --git a/test/0-init-fail-json.sh b/test/0-init-fail-json.sh new file mode 100755 index 0000000..1a00eb8 --- /dev/null +++ b/test/0-init-fail-json.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +packname="0-init-fail-pack" +deps="logger PACKAGE_DONT_EXIST" # would be very unlucky if it does exist... + +$DUB init $packname $deps -f json + +function cleanup { + rm -rf $packname +} + +if [ -e $packname/dub.json ]; then # package is there, it should have failed + cleanup + exit 1 +fi +exit 0 diff --git a/test/0-init-fail.sh b/test/0-init-fail.sh index 11fadf4..f8cc21f 100755 --- a/test/0-init-fail.sh +++ b/test/0-init-fail.sh @@ -9,7 +9,7 @@ rm -rf $packname } -if [ -e $packname/dub.json ]; then # package is there, it should have failed +if [ -e $packname/dub.sdl ]; then # package is there, it should have failed cleanup exit 1 fi diff --git a/test/0-init-multi-json.sh b/test/0-init-multi-json.sh new file mode 100755 index 0000000..bc16fd8 --- /dev/null +++ b/test/0-init-multi-json.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +packname="0-init-multi-pack" +deps="openssl logger" +type="vibe.d" + +$DUB init $packname $deps --type=$type -f json + +function cleanup { + rm -rf $packname +} + +if [ ! -e $packname/dub.json ]; then # it failed, exit 1 + exit 1 +else # check if resulting dub.json has all dependancies in tow + deps="$deps vibe-d"; + IFS=" " read -a arr <<< "$deps" + for ele in "${arr[@]}" + do + if [ `grep -c "$ele" $packname/dub.json` -ne 1 ]; then #something went wrong + echo "$ele not in $packname/dub.json" + cleanup + exit 1 + fi + done + cleanup + exit 0 + +fi diff --git a/test/0-init-multi.sh b/test/0-init-multi.sh index 54b151e..6f2096f 100755 --- a/test/0-init-multi.sh +++ b/test/0-init-multi.sh @@ -10,15 +10,15 @@ rm -rf $packname } -if [ ! -e $packname/dub.json ]; then # it failed, exit 1 +if [ ! -e $packname/dub.sdl ]; then # it failed, exit 1 exit 1 -else # check if resulting dub.json has all dependancies in tow +else # check if resulting dub.sdl has all dependancies in tow deps="$deps vibe-d"; IFS=" " read -a arr <<< "$deps" for ele in "${arr[@]}" do - if [ `grep -c "$ele" $packname/dub.json` -ne 1 ]; then #something went wrong - echo "$ele not in $packname/dub.json" + if [ `grep -c "$ele" $packname/dub.sdl` -ne 1 ]; then #something went wrong + echo "$ele not in $packname/dub.sdl" cleanup exit 1 fi diff --git a/test/0-init-simple-json.sh b/test/0-init-simple-json.sh new file mode 100755 index 0000000..80a07ff --- /dev/null +++ b/test/0-init-simple-json.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +packname="0-init-simple-pack" + +$DUB init $packname -f json + +function cleanup { + rm -rf $packname +} + +if [ ! -e $packname/dub.json ]; then # it failed + cleanup + exit 1 +fi +cleanup +exit 0 diff --git a/test/0-init-simple.sh b/test/0-init-simple.sh index b5f9227..baeaea0 100755 --- a/test/0-init-simple.sh +++ b/test/0-init-simple.sh @@ -8,7 +8,7 @@ rm -rf $packname } -if [ ! -e $packname/dub.json ]; then # it failed +if [ ! -e $packname/dub.sdl ]; then # it failed cleanup exit 1 fi