-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
init.d
169 lines (140 loc) · 5.47 KB
/
init.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
Empty package initialization code.
Copyright: © 2013-2015 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.vibecompat.core.file;
import dub.internal.vibecompat.core.log;
import dub.package_ : PackageFormat, packageInfoFiles, defaultPackageFilename;
import std.datetime;
import std.exception;
import std.file;
import std.format;
import std.process;
import std.string;
void initPackage(Path root_path, string[string] deps, string type, PackageFormat format)
{
void enforceDoesNotExist(string filename) {
enforce(!existsFile(root_path ~ filename), "The target directory already contains a '"~filename~"' file. Aborting.");
}
//Check to see if a target directory needs to be created
if( !root_path.empty ){
if( !existsFile(root_path) )
createDirectory(root_path);
}
//Make sure we do not overwrite anything accidentally
foreach (fil; packageInfoFiles)
enforceDoesNotExist(fil.filename);
auto files = ["source/", "views/", "public/", "dub.json", ".gitignore"];
foreach (fil; files)
enforceDoesNotExist(fil);
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;
}
writeGitignore(root_path);
}
void initMinimalPackage(Path root_path, string[string] deps, PackageFormat format)
{
writePackageDescription(format, root_path, "A minimal D application.", deps);
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, string[string] deps, PackageFormat format)
{
if("vibe-d" !in deps)
deps["vibe-d"] = "~>0.7.23";
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");
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 initDeimosPackage(Path root_path, string[string] deps, PackageFormat format)
{
auto name = root_path.head.toString().toLower();
writePackageDescription(format, root_path, "Deimos Bindings for "~name~".",
deps, ["importPaths": ["."]], ["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);
string username;
version (Windows) username = environment.get("USERNAME", "Peter Parker");
else username = environment.get("USER", "Peter Parker");
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);
string username;
version (Windows) username = environment.get("USERNAME", "Peter Parker");
else username = environment.get("USER", "Peter Parker");
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(),
".dub\ndocs.json\n__dummy.html\n*.o\n*.obj\n");
}