1 // Hello!
2 module driver.sdfmt;
3 
4 int main(string[] args) {
5 	bool dbg = false;
6 	bool inPlace = false;
7 
8 	import std.getopt;
9 	try {
10 		auto help_info = getopt(
11 			// sdfmt off
12 			args, std.getopt.config.caseSensitive,
13 			"debug", "Enable debug",          &dbg,
14 			"i",     "Format files in place", &inPlace,
15 			// sdfmt on
16 		);
17 
18 		if (help_info.helpWanted || args.length == 1) {
19 			import std.stdio;
20 			writeln("The Snazzy D Compiler - Code Formatter");
21 			writeln("Usage: sdfmt [options] file.d");
22 			writeln("Options:");
23 
24 			foreach (option; help_info.options) {
25 				writefln("  %-16s %s",
26 				         // bug : optShort is empty if there is no long version
27 				         option.optShort.length
28 					         ? option.optShort
29 					         : (option.optLong.length == 3)
30 						         ? option.optLong[1 .. $]
31 						         : option.optLong,
32 				         option.help);
33 			}
34 
35 			return 0;
36 		}
37 	} catch (GetOptException ex) {
38 		import std.stdio;
39 		writefln("%s", ex.msg);
40 		writeln("Please use -h to get a list of valid options.");
41 		return 1;
42 	}
43 
44 	auto files = args[1 .. $];
45 
46 	import source.context;
47 	auto context = new Context();
48 
49 	foreach (filename; files) {
50 		import format.config;
51 		Config conf;
52 
53 		import config.build;
54 		conf.buildLocalConfig("sdfmt", context, filename);
55 
56 		import source.location;
57 		auto base = context.registerFile(Location.init, filename, "");
58 
59 		import format.parser;
60 		auto chunks = Parser(base, context).parse();
61 
62 		if (dbg) {
63 			import std.stdio;
64 			writeln(chunks);
65 		}
66 
67 		import format.writer;
68 		auto o = chunks.write(conf);
69 
70 		if (inPlace) {
71 			// TODO: Only write if the file changed.
72 			import std.file;
73 			filename.write(o);
74 		} else {
75 			import std.stdio;
76 			write(o);
77 		}
78 	}
79 
80 	return 0;
81 }