Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://github.com/BaussProjects/ProjectXV4/
- // easyd source - compile this with dmd ...
- import std.stdio;
- import std.file;
- alias fwrite = std.file.write;
- import std.array : replace, split, join;
- import std.algorithm : canFind, endsWith, startsWith, stripLeft;
- void main(string[] args) {
- // removes the compile folder ...
- foreach (string name; dirEntries("cmpl", SpanMode.depth)) {
- remove(name);
- }
- void removeDirEntries(string dir) {
- foreach (string name; dirEntries(dir, SpanMode.breadth)) {
- if (isDir(name))
- removeDirEntries(name);
- remove(name);
- }
- }
- // removes the dsrc folder ...
- foreach (string name; dirEntries("dsrc", SpanMode.breadth)) {
- if (isDir(name))
- removeDirEntries(name);
- else
- remove(name);
- }
- string[] cmd = ["dmd"];
- string[] excludes;
- string[] extensions;
- string buildPath;
- bool makeDSource = false;
- bool useReports = false;
- string makeFixedPath(string p) {
- string opath = split(thisExePath(), "\\easyd")[0];
- return replace(p, opath, "");
- }
- auto text = readText(args[1]);
- text = replace(text, "\r", "");
- foreach (line; split(text, "\n")) {
- if (canFind(line, "=") && !startsWith(line, "//")) {
- auto data = split(line, "=");
- switch (data[0]) {
- case "cmd": {
- cmd ~= split(data[1], " ");
- break;
- }
- case "exclude": {
- excludes = split(data[1], ",");
- break;
- }
- case "build": {
- buildPath = data[1];
- break;
- }
- case "out": {
- buildPath = "-of" ~ buildPath ~ data[1];
- break;
- }
- case "extensions": {
- extensions ~= split(data[1], ",");
- break;
- }
- case "report": {
- if (data[1] == "true")
- useReports = true;
- break;
- }
- case "dsource": {
- if(data[1] == "true")
- makeDSource = true;
- break;
- }
- default: {
- writefln("Invalid setting. '%s' is not a valid setting.", data[0]);
- readln();
- return;
- }
- }
- }
- }
- foreach (string f; dirEntries("src", SpanMode.depth)) {
- if (!canFind(f, "."))
- continue;
- auto extension = split(f, ".")[1];
- if (canFind(extensions, extension)) {
- bool canAdd = true;
- foreach (exclude; excludes) {
- bool splitCanFind(string s1, string s2) {
- scope auto sf = split(s2, "\\");
- foreach (rf; sf) {
- scope auto rff = split (rf, ".");
- foreach (ef; rff) {
- if (ef == s1) {
- return true;
- }
- }
- }
- return false;
- }
- if (splitCanFind(exclude, f)) {
- canAdd = false;
- break;
- }
- }
- if (canAdd) {
- import std.path : baseName;
- if(extension == "d") {
- fwrite("cmpl\\" ~ baseName(f), compile(f, useReports));
- if (makeDSource) {
- string ff = makeFixedPath(f);
- auto fsp = split(ff, "\\");
- fsp = fsp[0 .. $-1];
- string fd = "dsrc\\" ~ join(fsp, "\\");
- if (!exists(fd))
- mkdir(fd);
- fwrite("dsrc\\" ~ ff, compile(f, false));
- }
- }
- else
- copy(f, "cmpl\\" ~ baseName(f));
- }
- }
- }
- foreach (string f; dirEntries("cmpl", SpanMode.depth)) {
- cmd ~= f;
- }
- if (exists(buildPath))
- remove(buildPath);
- cmd ~= buildPath;
- import std.process;
- scope auto logFile = File("errors.log", "w");
- auto dmdPid = spawnProcess(cmd, stdin, stdout, logFile);
- if (wait(dmdPid) != 0) {
- writeln("Failed to compile! View errors.log for more information.");
- import core.thread;
- Thread.sleep(dur!("msecs")(2000));
- }
- fwrite("cmd.txt", join(cmd, " "));
- }
- string compile(string dfile, bool useReports) {
- string[] src;
- auto text = readText(dfile);
- text = replace(text, "\r", "");
- int reportCount = 0;
- bool reportMode = false;
- foreach (sline; split(text, "\n")) {
- string line = stripLeft(sline, ' ');
- line = stripLeft(line, '\t');
- if (line == "report<") {
- if (!useReports)
- continue;
- reportCount = 0;
- reportMode = true;
- src ~= "import std.stdio;";
- }
- else if (line.length > 3 && (line[0 .. 3] == "__r") || line == "__r") {
- if (!useReports)
- continue;
- if (!reportMode) {
- src ~= line;
- continue;
- }
- import std.conv : to;
- string nick = "#" ~ to!string(reportCount);
- if (line != "__r")
- nick = split(line, "__r")[1];
- reportCount++;
- src ~= "writefln(\"%s report executed!\", \"" ~ nick ~ "\");";
- }
- else if (line.length > 3 && (line[0 .. 3] == "__v")) {
- if (!useReports)
- continue;
- if (!reportMode) {
- src ~= line;
- continue;
- }
- auto vars = split(line, "__v")[1];
- string varOut = "writefln(\"";
- foreach (var; split(vars, ","))
- varOut ~= var ~ " is '%s' ";
- varOut.length -= 1;
- varOut ~= "\", " ~ vars ~ ");";
- src ~= varOut;
- }
- else if (line == ">;") {
- if (!useReports)
- continue;
- if (!reportMode) {
- src ~= line;
- continue;
- }
- reportMode = false;
- }
- else {
- src ~= sline;
- }
- }
- return join(src, "\r\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement