diff --git a/source/dub/init.d b/source/dub/init.d index f710d56..aff77c4 100644 --- a/source/dub/init.d +++ b/source/dub/init.d @@ -10,6 +10,8 @@ import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.core.log; import dub.package_ : PackageFormat, packageInfoFiles, defaultPackageFilename; +import dub.recipe.packagerecipe; +import dub.dependency; import std.datetime; import std.exception; @@ -18,13 +20,27 @@ import std.process; import std.string; - -void initPackage(Path root_path, string[string] deps, string type, PackageFormat format) +void initPackage(Path root_path, string[string] deps, string type, PackageFormat format, scope void delegate(ref PackageRecipe) recipe_callback = null) { + import std.conv : to; + import dub.recipe.io : writePackageRecipe; + void enforceDoesNotExist(string filename) { enforce(!existsFile(root_path ~ filename), "The target directory already contains a '"~filename~"' file. Aborting."); } + string username = getUserName(); + + PackageRecipe p; + p.name = root_path.head.toString().toLower(); + p.authors ~= username; + p.license = "proprietary"; + p.copyright = .format("Copyright © %s, %s", Clock.currTime().year, username); + foreach (pack, v; deps) { + import std.ascii : isDigit; + p.buildSettings.dependencies[pack] = Dependency(v); + } + //Check to see if a target directory needs to be created if( !root_path.empty ){ if( !existsFile(root_path) ) @@ -41,16 +57,19 @@ switch (type) { default: throw new Exception("Unknown package init type: "~type); - 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; + case "minimal": initMinimalPackage(root_path, p); break; + case "vibe.d": initVibeDPackage(root_path, p); break; + case "deimos": initDeimosPackage(root_path, p); break; } + + if (recipe_callback) recipe_callback(p); + writePackageRecipe(root_path ~ ("dub."~format.to!string), p); writeGitignore(root_path); } -void initMinimalPackage(Path root_path, string[string] deps, PackageFormat format) +private void initMinimalPackage(Path root_path, ref PackageRecipe p) { - writePackageDescription(format, root_path, "A minimal D application.", deps); + p.description = "A minimal D application."; createDirectory(root_path ~ "source"); write((root_path ~ "source/app.d").toNativeString(), q{import std.stdio; @@ -62,13 +81,13 @@ }); } -void initVibeDPackage(Path root_path, string[string] deps, PackageFormat format) +private void initVibeDPackage(Path root_path, ref PackageRecipe p) { - if("vibe-d" !in deps) - deps["vibe-d"] = "~>0.7.23"; + if("vibe-d" !in p.dependencies) + p.buildSettings.dependencies["vibe-d"] = Dependency("~>0.7.26"); + p.description = "A simple vibe.d server application."; + p.buildSettings.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,70 +111,18 @@ }); } -void initDeimosPackage(Path root_path, string[string] deps, PackageFormat format) +private void initDeimosPackage(Path root_path, ref PackageRecipe p) { + import dub.compilers.buildsettings : TargetType; + auto name = root_path.head.toString().toLower(); - writePackageDescription(format, root_path, "Deimos Bindings for "~name~".", - deps, ["importPaths": ["."]], ["targetType": "sourceLibrary"]); + p.description = format("Deimos Bindings for "~p.name~"."); + p.buildSettings.importPaths[""] ~= "."; + p.buildSettings.targetType = TargetType.sourceLibrary; createDirectory(root_path ~ "C"); createDirectory(root_path ~ "deimos"); } -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; - - assert(!root_path.empty); - - auto username = getUserName(); - 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()); - fil.formattedWrite("\t\"description\": \"%s\",\n", description); - fil.formattedWrite("\t\"copyright\": \"Copyright © %s, %s\",\n", Clock.currTime().year, username); - fil.formattedWrite("\t\"authors\": [\"%s\"],\n", username); - fil.formattedWrite("\t\"dependencies\": {"); - fil.formattedWrite("%(\n\t\t%s: %s,%)", dependencies); - fil.formattedWrite("\n\t}"); - 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); - - auto username = getUserName(); - 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(),