diff --git a/build-files.txt b/build-files.txt index d8f0be5..126b868 100644 --- a/build-files.txt +++ b/build-files.txt @@ -1,6 +1,7 @@ source/app.d source/dub/dependency.d source/dub/dub.d +source/dub/init.d source/dub/packagemanager.d source/dub/packagesupplier.d source/dub/package_.d diff --git a/source/app.d b/source/app.d index 65defb6..33045c7 100644 --- a/source/app.d +++ b/source/app.d @@ -182,8 +182,10 @@ return 0; case "init": string dir; - if( args.length >= 2 ) dir = args[1]; - dub.createEmptyPackage(Path(dir)); + string type = "minimal"; + if (args.length >= 2) dir = args[1]; + if (args.length >= 3) type = args[2]; + dub.createEmptyPackage(Path(dir), type); return 0; case "upgrade": dub.loadPackageFromCwd(); @@ -356,7 +358,7 @@ only within dub internal ecosystem. Generation of native system packages / installer may be added later. -Retrieval options: +Options: --version Use the specified version/branch instead of the latest For the remove command, this may be a wildcard string: "*", which will remove all packages from the @@ -378,12 +380,16 @@ Available commands: help Prints this help screen - init [] Initializes an empty project in the specified directory + init [ []] + Initializes an empty project of the specified type in + the given directory. By default, the current working + dirctory is used. Available types: + minimal (default), vibe.d run [] Builds and runs a package (default command) build [] Builds a package (uses the main package in the current working directory by default) upgrade Forces an upgrade of all dependencies - fetch Manually retrieves a package. See 'dub help fetch'. + fetch Manually retrieves a package. See 'dub help fetch'. remove Removes present package. See 'dub help remove'. add-local Adds a local package directory (e.g. a git repository) @@ -431,7 +437,7 @@ --debug=NAME Define the specified debug version identifier when building - can be used multiple times -Retrieval options: +Fetch/remove options: --version Use the specified version/branch instead of the latest --system Put package into system wide dub cache instead of user local one --local Put packahe to a sub folder of the current directory diff --git a/source/dub/dub.d b/source/dub/dub.d index 6ae7027..ba5f1ca 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -20,6 +20,7 @@ import dub.packagesupplier; import dub.project; import dub.generators.generator; +import dub.init; // todo: cleanup imports. @@ -337,7 +338,7 @@ m_packageManager.removeSearchPath(makeAbsolute(path), system ? LocalPackageType.system : LocalPackageType.user); } - void createEmptyPackage(Path path) + void createEmptyPackage(Path path, string type) { if( !path.absolute() ) path = m_rootPath ~ path; path.normalize(); @@ -357,40 +358,10 @@ throw new Exception("The current directory is not empty.\n"); } - //raw strings must be unindented. - immutable packageJson = -`{ - "name": "`~(path.empty ? "my-project" : path.head.toString().toLower())~`", - "description": "An example project skeleton", - "homepage": "http://example.org", - "copyright": "Copyright © 2000, Your Name", - "authors": [ - "Your Name" - ], - "dependencies": { - } -} -`; - immutable appFile = -`import std.stdio; - -void main() -{ - writeln("Edit source/app.d to start your project."); -} -`; - - //Create the common directories. - createDirectory(path ~ "source"); - createDirectory(path ~ "views"); - createDirectory(path ~ "public"); - - //Create the common files. - openFile(path ~ PackageJsonFilename, FileMode.Append).write(packageJson); - openFile(path ~ "source/app.d", FileMode.Append).write(appFile); + initPackage(path, type); //Act smug to the user. - logInfo("Successfully created an empty project in '"~path.toNativeString()~"'."); + logInfo("Successfully created an empty project in '%s'.", path.toNativeString()); } void runDdox() diff --git a/source/dub/init.d b/source/dub/init.d new file mode 100644 index 0000000..647790a --- /dev/null +++ b/source/dub/init.d @@ -0,0 +1,93 @@ +/** + Empty package initialization code. + + Copyright: © 2013 rejectedsoftware e.K. + License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. + Authors: Sönke Ludwig +*/ +module dub.init; + +import dub.internal.std.process; +import dub.internal.vibecompat.core.file; +import dub.internal.vibecompat.core.log; +import dub.package_ : PackageJsonFilename; + +import std.datetime; +import std.file; +import std.format; +import std.string; + + +void initPackage(Path root_path, string type) +{ + switch (type) { + default: throw new Exception("Unknown package init type: "~type); + case "minimal": initMinimalPackage(root_path); break; + case "vibe.d": initVibeDPackage(root_path); break; + } +} + +void initMinimalPackage(Path root_path) +{ + writePackageJson(root_path, "A minimal D application.", null); + createDirectory(root_path ~ "source"); + write((root_path ~ "source/app.d").toNativeString(), +q{import std.stdio; + +void main() +{ + writeln("Edit source/app.d to start your project."); +} +}); +} + +void initVibeDPackage(Path root_path) +{ + writePackageJson(root_path, "A simple vibe.d server application.", ["vibe-d": ">=0.7.17"]); + createDirectory(root_path ~ "source"); + createDirectory(root_path ~ "views"); + createDirectory(root_path ~ "public"); + write((root_path ~ "source/app.d").toNativeString(), +q{import vibe.d; + +shared static this() +{ + auto settings = new HTTPServerSettings; + settings.port = 8080; + settings.bindAddresses = ["::1", "127.0.0.1"]; + listenHTTP(settings, &hello); + + logInfo("Please open http://127.0.0.1:8080/ in your browser."); +} + +void hello(HTTPServerRequest req, HTTPServerResponse res) +{ + res.writeBody("Hello, World!"); +} +}); +} + +void writePackageJson(Path root_path, string description, string[string] dependencies) +{ + 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 ~ PackageJsonFilename, 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\": {"); + bool first = true; + foreach (dep, ver; dependencies) { + if (first) first = false; + else fil.write(","); + fil.formattedWrite("\n\t\t\"%s\": \"%s\"", dep, ver); + } + fil.formattedWrite("\n\t}\n}\n"); +} \ No newline at end of file diff --git a/source/dub/version_.d b/source/dub/version_.d index a298aca..8b20f7a 100644 --- a/source/dub/version_.d +++ b/source/dub/version_.d @@ -1 +1 @@ -module dub.version_; enum dubVersion = "v0.9.19"; +module dub.version_; enum dubVersion = "v0.9.19-25-g75aa3e4";