1 #!/usr/bin/env dub 2 /+ dub.sdl: 3 dependency "dsh" version="~>1.5.1" 4 +/ 5 6 /** 7 * This script is used to build `dshs.d`, which is a single-file version of DSH 8 * that can be included in other D programs without the use of Dub. 9 */ 10 module tools.buildsingle; 11 12 import dsh; 13 import std.algorithm; 14 import std.array; 15 import std.string; 16 17 const MAIN_SOURCE = "../source/dsh.d"; 18 19 int main() { 20 string mainSourceText = readText(MAIN_SOURCE); 21 ptrdiff_t importLocation = indexOf(mainSourceText, "public import dshutils;"); 22 mainSourceText = mainSourceText.replaceAll("public import dshutils;", "").stripTests; 23 auto app = appender!string; 24 app ~= mainSourceText[0 .. importLocation]; 25 26 findFilesByExtension("../source/dshutils", ".d") 27 .filter!(s => !s.endsWith("package.d")) 28 .each!((s) { 29 string src = readText(s).strip.cleanSource.stripTests; 30 app ~= "// IMPORTED SOURCE: " ~ s ~ "\n"; 31 app ~= src ~ "\n"; 32 }); 33 34 app ~= mainSourceText[importLocation .. $]; 35 std.file.write("dshs.d", app[]); 36 print("Wrote single-file dsh to dshs.d."); 37 38 return 0; 39 } 40 41 private string cleanSource(string src) { 42 string moduleText = dsh.find(src, "module .*;"); 43 if (moduleText !is null) { 44 ptrdiff_t moduleIndex = indexOf(src, moduleText); 45 src = src[moduleIndex + moduleText.length .. $]; 46 } 47 return stripTests(src); 48 } 49 50 private string stripTests(string src) { 51 while (true) { 52 const marker = "unittest {"; 53 ptrdiff_t start = indexOf(src, marker); 54 if (start == -1) break; 55 ptrdiff_t end = indexOf(src, "\n}", start); 56 src = src[0 .. start] ~ src[end + 2 .. $]; 57 } 58 return src; 59 }