Advertisement
Guest User

ProjectXV4 - easyd source

a guest
Jan 16th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.96 KB | None | 0 0
  1. // https://github.com/BaussProjects/ProjectXV4/
  2. // easyd source - compile this with dmd ...
  3. import std.stdio;
  4. import std.file;
  5. alias fwrite = std.file.write;
  6. import std.array : replace, split, join;
  7. import std.algorithm : canFind, endsWith, startsWith, stripLeft;
  8.  
  9. void main(string[] args) {
  10.     // removes the compile folder ...
  11.     foreach (string name; dirEntries("cmpl", SpanMode.depth)) {
  12.         remove(name);
  13.     }
  14.  
  15.     void removeDirEntries(string dir) {
  16.         foreach (string name; dirEntries(dir, SpanMode.breadth)) {
  17.             if (isDir(name))
  18.                 removeDirEntries(name);
  19.             remove(name);
  20.         }
  21.     }
  22.     // removes the dsrc folder ...
  23.     foreach (string name; dirEntries("dsrc", SpanMode.breadth)) {
  24.         if (isDir(name))
  25.             removeDirEntries(name);
  26.         else
  27.             remove(name);
  28.     }
  29.    
  30.     string[] cmd = ["dmd"];
  31.     string[] excludes;
  32.     string[] extensions;
  33.     string buildPath;
  34.     bool makeDSource = false;
  35.     bool useReports = false;
  36.    
  37.     string makeFixedPath(string p) {
  38.         string opath = split(thisExePath(), "\\easyd")[0];
  39.         return replace(p, opath, "");
  40.     }
  41.    
  42.     auto text = readText(args[1]);
  43.     text = replace(text, "\r", "");
  44.     foreach (line; split(text, "\n")) {
  45.         if (canFind(line, "=") && !startsWith(line, "//")) {
  46.             auto data = split(line, "=");
  47.             switch (data[0]) {
  48.                 case "cmd": {
  49.                     cmd ~= split(data[1], " ");
  50.                     break;
  51.                 }
  52.                
  53.                 case "exclude": {
  54.                     excludes = split(data[1], ",");
  55.                     break;
  56.                 }
  57.                
  58.                 case "build": {
  59.                     buildPath = data[1];
  60.                     break;
  61.                 }
  62.                
  63.                 case "out": {
  64.                     buildPath = "-of" ~ buildPath ~ data[1];
  65.                     break;
  66.                 }
  67.                
  68.                 case "extensions": {
  69.                     extensions ~= split(data[1], ",");
  70.                     break;
  71.                 }
  72.                
  73.                 case "report": {
  74.                     if (data[1] == "true")
  75.                         useReports = true;
  76.                     break;
  77.                 }
  78.                
  79.                 case "dsource": {
  80.                     if(data[1] == "true")
  81.                         makeDSource = true;
  82.                     break;
  83.                 }
  84.                
  85.                 default: {
  86.                     writefln("Invalid setting. '%s' is not a valid setting.", data[0]);
  87.                     readln();
  88.                     return;
  89.                 }
  90.             }
  91.         }
  92.     }
  93.    
  94.     foreach (string f; dirEntries("src", SpanMode.depth)) {
  95.         if (!canFind(f, "."))
  96.             continue;
  97.            
  98.         auto extension = split(f, ".")[1];
  99.         if (canFind(extensions, extension)) {
  100.             bool canAdd = true;
  101.             foreach (exclude; excludes) {
  102.                 bool splitCanFind(string s1, string s2) {
  103.                     scope auto sf = split(s2, "\\");
  104.                     foreach (rf; sf) {
  105.                         scope auto rff = split (rf, ".");
  106.                         foreach (ef; rff) {
  107.                             if (ef == s1) {
  108.                                 return true;
  109.                             }
  110.                         }
  111.                     }
  112.                     return false;
  113.                 }
  114.                 if (splitCanFind(exclude, f)) {
  115.                     canAdd = false;
  116.                     break;
  117.                 }
  118.             }
  119.             if (canAdd) {
  120.                 import std.path : baseName;
  121.                
  122.                 if(extension == "d") {
  123.                     fwrite("cmpl\\" ~ baseName(f), compile(f, useReports));
  124.                    
  125.                     if (makeDSource) {
  126.                         string ff = makeFixedPath(f);
  127.                         auto fsp = split(ff, "\\");
  128.                         fsp = fsp[0 .. $-1];
  129.                         string fd = "dsrc\\" ~ join(fsp, "\\");
  130.                         if (!exists(fd))
  131.                             mkdir(fd);
  132.                         fwrite("dsrc\\" ~ ff, compile(f, false));
  133.                     }  
  134.                 }
  135.                 else
  136.                     copy(f, "cmpl\\" ~ baseName(f));
  137.             }
  138.         }
  139.     }
  140.    
  141.     foreach (string f; dirEntries("cmpl", SpanMode.depth)) {
  142.         cmd ~= f;
  143.     }
  144.    
  145.     if (exists(buildPath))
  146.         remove(buildPath);
  147.     cmd ~= buildPath;
  148.    
  149.     import std.process;
  150.     scope auto logFile = File("errors.log", "w");
  151.     auto dmdPid = spawnProcess(cmd, stdin, stdout, logFile);
  152.     if (wait(dmdPid) != 0) {
  153.         writeln("Failed to compile! View errors.log for more information.");
  154.         import core.thread;
  155.         Thread.sleep(dur!("msecs")(2000));
  156.     }
  157.     fwrite("cmd.txt", join(cmd, " "));
  158. }
  159.  
  160. string compile(string dfile, bool useReports) {
  161.     string[] src;
  162.     auto text = readText(dfile);
  163.     text = replace(text, "\r", "");
  164.     int reportCount = 0;
  165.     bool reportMode = false;
  166.     foreach (sline; split(text, "\n")) {
  167.         string line = stripLeft(sline, ' ');
  168.         line = stripLeft(line, '\t');
  169.        
  170.         if (line == "report<") {
  171.             if (!useReports)
  172.                 continue;
  173.             reportCount = 0;
  174.             reportMode = true;
  175.             src ~= "import std.stdio;";
  176.         }
  177.         else if (line.length > 3 && (line[0 .. 3] == "__r") || line == "__r") {
  178.             if (!useReports)
  179.                 continue;
  180.             if (!reportMode) {
  181.                 src ~= line;
  182.                 continue;
  183.             }
  184.             import std.conv : to;
  185.             string nick = "#" ~ to!string(reportCount);
  186.             if (line != "__r")
  187.                 nick = split(line, "__r")[1];
  188.             reportCount++;
  189.             src ~= "writefln(\"%s report executed!\", \"" ~ nick ~ "\");";
  190.         }
  191.         else if (line.length > 3 && (line[0 .. 3] == "__v")) {
  192.             if (!useReports)
  193.                 continue;
  194.             if (!reportMode) {
  195.                 src ~= line;
  196.                 continue;
  197.             }
  198.             auto vars = split(line, "__v")[1];
  199.             string varOut = "writefln(\"";
  200.             foreach (var; split(vars, ","))
  201.                 varOut ~= var ~ " is '%s' ";
  202.             varOut.length -= 1;
  203.             varOut ~= "\", " ~ vars ~ ");";
  204.             src ~= varOut;
  205.         }
  206.         else if (line == ">;") {
  207.             if (!useReports)
  208.                 continue;
  209.             if (!reportMode) {
  210.                 src ~= line;
  211.                 continue;
  212.             }
  213.             reportMode = false;
  214.         }
  215.         else {
  216.             src ~= sline;
  217.         }
  218.     }
  219.     return join(src, "\r\n");
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement