@@ -16,7 +16,7 @@ Author: Sebastian Wilzbach
1616*/
1717import std.algorithm , std.array , std.ascii , std.conv , std.file , std.functional ,
1818 std.meta , std.path , std.range , std.string , std.typecons ;
19- import std.stdio : writeln, writefln ;
19+ import std.stdio ;
2020
2121struct Config
2222{
@@ -25,40 +25,55 @@ struct Config
2525}
2626Config config;
2727
28- int main (string [] args)
28+ auto findAndReplace (alias pred)(ref string [] args, string replacement)
29+ {
30+ auto pos = args.countUntil! pred;
31+ string ret;
32+ if (pos >= 0 )
33+ {
34+ ret = args[pos];
35+ args = args[0 .. pos].chain(replacement.only, args[pos.. $].dropOne).array;
36+ }
37+ return ret;
38+ }
39+
40+ int main (string [] rootArgs)
2941{
3042 import std.getopt ;
3143 auto helpInformation = getopt(
32- args ,
44+ rootArgs, std.getopt.config.passThrough ,
3345 " compiler" , " Compiler to use" , &config.dmdBinPath,
34- " o|output" , " Output file" , &config.outputFile,
3546 );
36-
37- assert (config.outputFile, " An output file is required." );
38- assert (args.length > 1 , " An input file is required." );
39-
4047 if (helpInformation.helpWanted)
4148 {
4249` DDoc wrapper
4350./ddoc <file>...
4451` .defaultGetoptPrinter(helpInformation.options);
4552 return 1 ;
4653 }
47-
48- import std.file : readText;
49- auto text = args[$ - 1 ].readText;
54+ auto args = rootArgs[1 .. $];
55+ auto inputFile = args.findAndReplace! (a => a.endsWith(" .dd" , " .d" ) > 0 )(" -" );
56+ assert (inputFile, " An input file (.d or .dd) must be provided" );
57+ auto text = inputFile.readText;
58+ // TODO:
59+ if (inputFile.endsWith(" package.d" ))
60+ return 0 ;
5061
5162 // transform and extend the ddoc page
5263 text = genHeader(text);
5364
54- return compile (text, args[ 1 .. $ - 1 ] );
65+ return compile (text, args);
5566}
5667
5768auto compile (R)(R buffer, string [] arguments)
5869{
5970 import std.process : pipeProcess, Redirect, wait;
60- auto args = [config.dmdBinPath, " -c" , " -Df" ~ config.outputFile, " -o-" ] ~ arguments;
61- args ~= " -" ;
71+ auto args = [config.dmdBinPath] ~ arguments;
72+ foreach (arg; [" -c" , " -o-" , " -" ])
73+ {
74+ if (! args.canFind(arg))
75+ args ~= arg;
76+ }
6277 auto pipes = pipeProcess(args, Redirect.stdin);
6378 pipes.stdin.write(buffer);
6479 pipes.stdin.close;
0 commit comments