Skip to content

Commit 1ddb5a7

Browse files
committed
Refactoring
1 parent e990af4 commit 1ddb5a7

File tree

9 files changed

+349
-307
lines changed

9 files changed

+349
-307
lines changed

.gitignore

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
.dub
2-
docs.json
3-
__dummy.html
4-
*.o
5-
*.obj
6-
__test__*__
7-
*.html
8-
*.dwt
9-
dwttool
102
dub.selections.json
113
bin

dub.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"copyright": "Copyright © 2017, Martin Muehlbauer",
88
"license": "MIT",
99
"dependencies": {
10-
"handy-httpd": "~>5.6.0"
10+
"handy-httpd": "~>5.6.2"
1111
},
1212
"targetPath": "bin"
1313
}

source/app.d

Lines changed: 5 additions & 298 deletions
Original file line numberDiff line numberDiff line change
@@ -1,301 +1,8 @@
1-
import core.exception;
2-
import core.stdc.stdlib;
3-
import handy_httpd;
4-
import handy_httpd.handlers.file_resolving_handler;
5-
import std.algorithm;
6-
import std.conv;
7-
import std.file;
8-
import std.format;
9-
import std.path;
10-
import std.range;
11-
import std.regex;
12-
import std.stdio;
1+
// Main entry point of the application
132

3+
import dwttool.cli;
144

15-
private
16-
{
17-
// Version
18-
enum ver = "0.2.0";
19-
20-
// Errors
21-
enum invalidArgumentError = "ERROR: No or invalid argument(s) given. Enter 'dwttool help' for help.";
22-
enum invalidAmountError = "ERROR: Invalid amount of arguments given. Enter 'dwttool help' for help.";
23-
enum invalidTypeError = "ERROR: %s is not a valid port number.";
24-
enum missingTemplateError = "ERROR: Could not find template '%s'.";
25-
enum createProjectError = "ERROR: %s already exists. Try another name.";
26-
enum createFileError = "ERROR: Could not create file %s from template %s.";
27-
enum readFileError = "ERROR: Could not read file %s.";
28-
enum readTemplateError = "ERROR: Could not read template %s.";
29-
30-
// Standard server port
31-
ushort port = 4343;
32-
}
33-
34-
// Do not indent!
35-
private enum exampleTemplate = "
36-
<!DOCTYPE html>
37-
<html>
38-
39-
<head>
40-
<!-- #BeginEditable \"head\" -->
41-
<!-- #EndEditable -->
42-
</head>
43-
44-
<body>
45-
<!-- #BeginEditable \"content\" -->
46-
<!-- #EndEditable -->
47-
</body>
48-
49-
</html>";
50-
51-
52-
private void main(string[] args)
53-
{
54-
// CLI handling
55-
try
56-
{
57-
switch (args[1])
58-
{
59-
case "create":
60-
switch (args[2])
61-
{
62-
case "page":
63-
if (args.length == 5)
64-
{
65-
createPage(args[3], args[4]);
66-
break;
67-
}
68-
else
69-
{
70-
writeln(invalidAmountError);
71-
break;
72-
}
73-
case "project":
74-
if (args.length == 4)
75-
{
76-
createProject(args[3]);
77-
break;
78-
}
79-
else
80-
{
81-
writeln(invalidAmountError);
82-
break;
83-
}
84-
default: writeln(invalidArgumentError);
85-
}
86-
break;
87-
case "update":
88-
switch (args[2])
89-
{
90-
case "page":
91-
if (args.length == 5)
92-
{
93-
updatePage(args[3], args[4]);
94-
break;
95-
}
96-
else
97-
{
98-
writeln(invalidAmountError);
99-
break;
100-
}
101-
case "project":
102-
if (args.length == 4)
103-
{
104-
updateProject(args[3]);
105-
break;
106-
}
107-
else
108-
{
109-
writeln(invalidAmountError);
110-
break;
111-
}
112-
default: writeln(invalidArgumentError);
113-
}
114-
break;
115-
case "serve":
116-
if (args.length == 2)
117-
{
118-
serveProject(port);
119-
break;
120-
}
121-
if (args.length == 3)
122-
{
123-
try
124-
{
125-
port = to!ushort(args[2]);
126-
serveProject(port);
127-
break;
128-
}
129-
catch (ConvException e)
130-
{
131-
writeln(format(invalidTypeError, args[2]));
132-
break;
133-
}
134-
}
135-
else
136-
{
137-
writeln(invalidAmountError);
138-
break;
139-
}
140-
case "version":
141-
if (args.length == 2)
142-
{
143-
getVersion;
144-
break;
145-
}
146-
else
147-
{
148-
writeln(invalidAmountError);
149-
break;
150-
}
151-
case "help":
152-
if (args.length == 2)
153-
{
154-
showHelp;
155-
break;
156-
}
157-
else
158-
{
159-
writeln(invalidAmountError);
160-
break;
161-
}
162-
default: writeln(invalidArgumentError);
163-
}
164-
}
165-
catch (RangeError e)
166-
{
167-
writeln(invalidArgumentError);
168-
}
169-
}
170-
171-
private void createProject(string projectName)
172-
{
173-
writeln("Creating new project...");
174-
175-
if (!projectName.exists)
176-
{
177-
mkdir(projectName);
178-
// Create an example template inside of the new project folder
179-
string templatePath = buildPath(projectName, "master.dwt");
180-
std.file.write(templatePath, exampleTemplate);
181-
}
182-
else
183-
{
184-
writeln(format(createProjectError, projectName));
185-
exitDwttool;
186-
}
187-
}
188-
189-
private void updateProject(string templateName)
190-
{
191-
writeln("Updating project...");
192-
193-
if (templateName.exists)
194-
{
195-
foreach (DirEntry entry; dirEntries(".", SpanMode.breadth))
196-
{
197-
if (endsWith(entry.name, ".html") && entry.isFile)
198-
{
199-
updatePage(entry.name, templateName);
200-
}
201-
}
202-
}
203-
else
204-
{
205-
writeln(format(missingTemplateError, templateName));
206-
exitDwttool;
207-
}
208-
}
209-
210-
private void createPage(string fileName, string templateName)
211-
{
212-
writeln("Creating new page...");
213-
214-
try
215-
{
216-
// Determine if a directory has to be made
217-
string pathHierarchy = fileName.dropBack(baseName(fileName).length+1);
218-
if (!pathHierarchy.empty && !pathHierarchy.exists)
219-
{
220-
mkdirRecurse(pathHierarchy);
221-
}
222-
// A new HTML file is just a copy of a template
223-
copy(templateName, fileName);
224-
}
225-
catch (FileException ex)
226-
{
227-
writeln(format(createFileError, fileName, templateName));
228-
}
229-
}
230-
231-
private void updatePage(string fileName, string templateName)
232-
{
233-
writeln("Updating page...");
234-
235-
string fromFile;
236-
try
237-
{
238-
fromFile = readText(fileName);
239-
}
240-
catch (FileException ex)
241-
{
242-
writeln(format(readFileError, fileName));
243-
exitDwttool;
244-
}
245-
string fromTemplate;
246-
try
247-
{
248-
fromTemplate = readText(templateName);
249-
}
250-
catch (FileException ex)
251-
{
252-
writeln(format(readTemplateError, templateName));
253-
exitDwttool;
254-
}
255-
256-
// Get editable regions from file
257-
auto matchings = matchAll(fromFile, regex(`<!-- #BeginEditable "(.*?)" -->(.*?)<!-- #EndEditable -->`, "s"));
258-
// Insert editable regions into template
259-
foreach (Captures!string c; matchings)
260-
{
261-
string r = format(`<!-- #BeginEditable "%s" -->(.*?)<!-- #EndEditable -->`, c[1]);
262-
fromTemplate = replaceAll(fromTemplate, regex(r, "s"), c[0]);
263-
}
264-
265-
std.file.write(fileName, fromTemplate);
266-
}
267-
268-
private void serveProject(ushort port)
269-
{
270-
writeln("Starting server...");
271-
writeln("Press Ctrl+C to quit.");
272-
ServerConfig cfg = ServerConfig.defaultValues();
273-
cfg.port = port;
274-
auto handler = new FileResolvingHandler(".");
275-
new HttpServer(handler, cfg).start();
276-
}
277-
278-
private void showHelp()
279-
{
280-
writeln("dwttool - A tool to manage websites based on Dynamic Web Templates");
281-
writeln("");
282-
writeln("Usage:");
283-
writeln(" dwttool create project <project> Create new dwttool project");
284-
writeln(" dwttool create page <page> <template> Create new dwttool page");
285-
writeln(" dwttool update project <template> Update dwttool project");
286-
writeln(" dwttool update page <page> <template> Update dwttool page");
287-
writeln(" dwttool serve [<port>] Serve dwttool project. Standard port is 4343");
288-
writeln(" dwttool version Get dwttool version");
289-
writeln(" dwttool help Read this text");
290-
}
291-
292-
private void exitDwttool()
293-
{
294-
writeln("Exiting...");
295-
exit(0);
296-
}
297-
298-
private void getVersion()
299-
{
300-
writeln(ver);
5+
void main(string[] args)
6+
{
7+
handleCli(args);
3018
}

0 commit comments

Comments
 (0)