1 /** 2 Empty package initialization code. 3 4 Copyright: © 2013 rejectedsoftware e.K. 5 License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. 6 Authors: Sönke Ludwig 7 */ 8 module dub.init; 9 10 import dub.internal.vibecompat.core.file; 11 import dub.internal.vibecompat.core.log; 12 import dub.package_ : PackageJsonFilename; 13 14 import std.datetime; 15 import std.file; 16 import std.format; 17 import std.process; 18 import std.string; 19 20 21 void initPackage(Path root_path, string type) 22 { 23 switch (type) { 24 default: throw new Exception("Unknown package init type: "~type); 25 case "minimal": initMinimalPackage(root_path); break; 26 case "vibe.d": initVibeDPackage(root_path); break; 27 } 28 } 29 30 void initMinimalPackage(Path root_path) 31 { 32 writePackageJson(root_path, "A minimal D application.", null); 33 createDirectory(root_path ~ "source"); 34 write((root_path ~ "source/app.d").toNativeString(), 35 q{import std.stdio; 36 37 void main() 38 { 39 writeln("Edit source/app.d to start your project."); 40 } 41 }); 42 } 43 44 void initVibeDPackage(Path root_path) 45 { 46 writePackageJson(root_path, "A simple vibe.d server application.", ["vibe-d": ">=0.7.17"], ["VibeDefaultMain"]); 47 createDirectory(root_path ~ "source"); 48 createDirectory(root_path ~ "views"); 49 createDirectory(root_path ~ "public"); 50 write((root_path ~ "source/app.d").toNativeString(), 51 q{import vibe.d; 52 53 shared static this() 54 { 55 auto settings = new HTTPServerSettings; 56 settings.port = 8080; 57 settings.bindAddresses = ["::1", "127.0.0.1"]; 58 listenHTTP(settings, &hello); 59 60 logInfo("Please open http://127.0.0.1:8080/ in your browser."); 61 } 62 63 void hello(HTTPServerRequest req, HTTPServerResponse res) 64 { 65 res.writeBody("Hello, World!"); 66 } 67 }); 68 } 69 70 void writePackageJson(Path root_path, string description, string[string] dependencies, string[] versions = null) 71 { 72 import std.algorithm : map; 73 74 assert(!root_path.empty); 75 76 string username; 77 version (Windows) username = environment.get("USERNAME", "Peter Parker"); 78 else username = environment.get("USER", "Peter Parker"); 79 80 auto fil = openFile(root_path ~ PackageJsonFilename, FileMode.Append); 81 scope(exit) fil.close(); 82 83 fil.formattedWrite("{\n\t\"name\": \"%s\",\n", root_path.head.toString().toLower()); 84 fil.formattedWrite("\t\"description\": \"%s\",\n", description); 85 fil.formattedWrite("\t\"copyright\": \"Copyright © %s, %s\",\n", Clock.currTime().year, username); 86 fil.formattedWrite("\t\"authors\": [\"%s\"],\n", username); 87 fil.formattedWrite("\t\"dependencies\": {"); 88 bool first = true; 89 foreach (dep, ver; dependencies) { 90 if (first) first = false; 91 else fil.write(","); 92 fil.formattedWrite("\n\t\t\"%s\": \"%s\"", dep, ver); 93 } 94 fil.formattedWrite("\n\t}"); 95 if (versions.length) fil.formattedWrite(",\n\t\"versions\": [%s]", versions.map!(v => `"`~v~`"`).join(", ")); 96 fil.write("\n}\n"); 97 }