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_ : packageInfoFilenames, 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 type)
23 {
24 	//Check to see if a target directory needs to be created
25 	if( !root_path.empty ){
26 		if( !existsFile(root_path) )
27 			createDirectory(root_path);
28 	} 
29 
30 	//Make sure we do not overwrite anything accidentally
31 	auto files = packageInfoFilenames ~ ["source/", "views/", "public/"];
32 	foreach (fil; files)
33 		enforce(!existsFile(root_path ~ fil), "The target directory already contains a '"~fil~"' file. Aborting.");
34 
35 	switch (type) {
36 		default: throw new Exception("Unknown package init type: "~type);
37 		case "minimal": initMinimalPackage(root_path); break;
38 		case "vibe.d": initVibeDPackage(root_path); break;
39 	}
40 }
41 
42 void initMinimalPackage(Path root_path)
43 {
44 	writePackageJson(root_path, "A minimal D application.", null);
45 	createDirectory(root_path ~ "source");
46 	write((root_path ~ "source/app.d").toNativeString(), 
47 q{import std.stdio;
48 
49 void main()
50 {
51 	writeln("Edit source/app.d to start your project.");
52 }
53 });
54 }
55 
56 void initVibeDPackage(Path root_path)
57 {
58 	writePackageJson(root_path, "A simple vibe.d server application.", ["vibe-d": ">=0.7.17"], ["VibeDefaultMain"]);
59 	createDirectory(root_path ~ "source");
60 	createDirectory(root_path ~ "views");
61 	createDirectory(root_path ~ "public");
62 	write((root_path ~ "source/app.d").toNativeString(), 
63 q{import vibe.d;
64 
65 shared static this()
66 {
67 	auto settings = new HTTPServerSettings;
68 	settings.port = 8080;
69 	settings.bindAddresses = ["::1", "127.0.0.1"];
70 	listenHTTP(settings, &hello);
71 
72 	logInfo("Please open http://127.0.0.1:8080/ in your browser.");
73 }
74 
75 void hello(HTTPServerRequest req, HTTPServerResponse res)
76 {
77 	res.writeBody("Hello, World!");
78 }
79 });
80 }
81 
82 void writePackageJson(Path root_path, string description, string[string] dependencies, string[] versions = null)
83 {
84 	import std.algorithm : map;
85 
86 	assert(!root_path.empty);
87 
88 	string username;
89 	version (Windows) username = environment.get("USERNAME", "Peter Parker");
90 	else username = environment.get("USER", "Peter Parker");
91 
92 	auto fil = openFile(root_path ~ defaultPackageFilename(), FileMode.Append);
93 	scope(exit) fil.close();
94 
95 	fil.formattedWrite("{\n\t\"name\": \"%s\",\n", root_path.head.toString().toLower());
96 	fil.formattedWrite("\t\"description\": \"%s\",\n", description);
97 	fil.formattedWrite("\t\"copyright\": \"Copyright © %s, %s\",\n", Clock.currTime().year, username);
98 	fil.formattedWrite("\t\"authors\": [\"%s\"],\n", username);
99 	fil.formattedWrite("\t\"dependencies\": {");
100 	bool first = true;
101 	foreach (dep, ver; dependencies) {
102 		if (first) first = false;
103 		else fil.write(",");
104 		fil.formattedWrite("\n\t\t\"%s\": \"%s\"", dep, ver);
105 	}
106 	fil.formattedWrite("\n\t}");
107 	if (versions.length) fil.formattedWrite(",\n\t\"versions\": [%s]", versions.map!(v => `"`~v~`"`).join(", "));
108 	fil.write("\n}\n");
109 }