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_ : packageInfoFiles, defaultPackageFilename; 13 14 import std.datetime; 15 import std.exception; 16 import std.file; 17 import std.format; 18 import std.process; 19 import std..string; 20 21 22 void initPackage(Path root_path, string[string] deps, string type) 23 { 24 void enforceDoesNotExist(string filename) { 25 enforce(!existsFile(root_path ~ filename), "The target directory already contains a '"~filename~"' file. Aborting."); 26 } 27 28 //Check to see if a target directory needs to be created 29 if( !root_path.empty ){ 30 if( !existsFile(root_path) ) 31 createDirectory(root_path); 32 } 33 34 //Make sure we do not overwrite anything accidentally 35 foreach (fil; packageInfoFiles) 36 enforceDoesNotExist(fil.filename); 37 38 auto files = ["source/", "views/", "public/", "dub.json", ".gitignore"]; 39 foreach (fil; files) 40 enforceDoesNotExist(fil); 41 42 switch (type) { 43 default: throw new Exception("Unknown package init type: "~type); 44 case "minimal": initMinimalPackage(root_path, deps); break; 45 case "vibe.d": initVibeDPackage(root_path, deps); break; 46 case "deimos": initDeimosPackage(root_path, deps); break; 47 } 48 writeGitignore(root_path); 49 } 50 51 void initMinimalPackage(Path root_path, string[string] deps) 52 { 53 writePackageJson(root_path, "A minimal D application.", deps); 54 createDirectory(root_path ~ "source"); 55 write((root_path ~ "source/app.d").toNativeString(), 56 q{import std.stdio; 57 58 void main() 59 { 60 writeln("Edit source/app.d to start your project."); 61 } 62 }); 63 } 64 65 void initVibeDPackage(Path root_path, string[string] deps) 66 { 67 if("vibe-d" !in deps) 68 deps["vibe-d"] = "~>0.7.19"; 69 70 writePackageJson(root_path, "A simple vibe.d server application.", 71 deps, ["versions": `["VibeDefaultMain"]`]); 72 createDirectory(root_path ~ "source"); 73 createDirectory(root_path ~ "views"); 74 createDirectory(root_path ~ "public"); 75 write((root_path ~ "source/app.d").toNativeString(), 76 q{import vibe.d; 77 78 shared static this() 79 { 80 auto settings = new HTTPServerSettings; 81 settings.port = 8080; 82 settings.bindAddresses = ["::1", "127.0.0.1"]; 83 listenHTTP(settings, &hello); 84 85 logInfo("Please open http://127.0.0.1:8080/ in your browser."); 86 } 87 88 void hello(HTTPServerRequest req, HTTPServerResponse res) 89 { 90 res.writeBody("Hello, World!"); 91 } 92 }); 93 } 94 95 void initDeimosPackage(Path root_path, string[string] deps) 96 { 97 auto name = root_path.head.toString().toLower(); 98 writePackageJson(root_path, "Deimos Bindings for "~name~".", 99 deps, ["targetType": `"sourceLibrary"`, "importPaths": `["."]`]); 100 createDirectory(root_path ~ "C"); 101 createDirectory(root_path ~ "deimos"); 102 } 103 104 void writePackageJson(Path root_path, string description, string[string] dependencies = null, string[string] addFields = null) 105 { 106 import std.algorithm : map; 107 108 assert(!root_path.empty); 109 110 string username; 111 version (Windows) username = environment.get("USERNAME", "Peter Parker"); 112 else username = environment.get("USER", "Peter Parker"); 113 114 auto fil = openFile(root_path ~ defaultPackageFilename, FileMode.Append); 115 scope(exit) fil.close(); 116 117 fil.formattedWrite("{\n\t\"name\": \"%s\",\n", root_path.head.toString().toLower()); 118 fil.formattedWrite("\t\"description\": \"%s\",\n", description); 119 fil.formattedWrite("\t\"copyright\": \"Copyright © %s, %s\",\n", Clock.currTime().year, username); 120 fil.formattedWrite("\t\"authors\": [\"%s\"],\n", username); 121 fil.formattedWrite("\t\"dependencies\": {"); 122 fil.formattedWrite("%(\n\t\t%s: %s,%)", dependencies); 123 fil.formattedWrite("\n\t}"); 124 fil.formattedWrite("%-(,\n\t\"%s\": %s%)", addFields); 125 fil.write("\n}\n"); 126 } 127 128 void writeGitignore(Path root_path) 129 { 130 write((root_path ~ ".gitignore").toNativeString(), 131 ".dub\ndocs.json\n__dummy.html\n*.o\n*.obj\n"); 132 }