Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://github.com/BaussProjects/ProjectXV4/
- // Compile 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);
- }
- rmdirRecurse("dsrc");
- mkdir("dsrc");
- string[] cmd = ["dmd"];
- string[] cmd2;
- 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;
- }
- }
- }
- }
- string[] dmd_cmd = cmd[1 .. $];
- 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") {
- int c = 0;
- import std.algorithm : count;
- import std.conv : to;
- c = count(dirEntries("cmpl", SpanMode.shallow));
- if (baseName(f) != "package.d") {
- fwrite("cmpl\\" ~ to!string(c) ~ "_" ~ baseName(f) ~ ".d", compile(f, useReports));
- }
- else {
- fwrite("cmpl\\package.d", compile(f, useReports));
- }
- if (makeDSource) {
- string ff = makeFixedPath(f);
- dmd_cmd ~= "dsrc\\" ~ ff;
- auto fsp = split(ff, "\\");
- fsp = fsp[0 .. $-1];
- string fd = "dsrc\\" ~ join(fsp, "\\");
- if (!exists(fd)) {
- mkdirRecurse(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;
- dmd_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(dmd_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 if (line.length > 6 && (line[0 .. 7] == "aswitch")) {
- auto spl = split(line, " ");
- string swArg = spl[1];
- string basedir = spl[2];
- string cl = spl[3];
- string params = spl[4];
- auto calls = findMatchingCalls("src\\" ~ basedir, cl, params);
- foreach (imp; calls[0]) {
- src ~= imp;
- }
- foreach (call; calls[1]) {
- src ~= call;
- }
- }
- else {
- src ~= sline;
- }
- }
- return join(src, "\r\n");
- }
- auto findMatchingCalls(string basedir, string call, string params) {
- string[][] results;
- string[] _imports;
- string[] _cases;
- foreach (string name; dirEntries(basedir, SpanMode.depth)) {
- if (endsWith(name, ".d")) {
- auto text = readText(name);
- text = replace(text, "\r", "");
- string[] res;
- string nextImport;
- foreach (line; split(text, "\n")) {
- if (startsWith(line, "module")) {
- nextImport = replace(line, "module", "import");
- }
- else if (canFind(line, call ~ "(")) {
- //writefln("'%s' '%s' %s", call, canFind(line, call ~ "("), line);
- //readln();
- auto spl = split(line, call ~ "(");
- auto packetId = split(spl[1], ")")[0];
- auto packetCall = split(line, "(");
- string pcall = split(packetCall[$-2], " ")[$-1];
- import std.string : format;
- _cases ~= format("case %s: %s(%s); break;", packetId, pcall, params);
- _imports ~= nextImport;
- break;
- }
- }
- }
- }
- results = [_imports, _cases];
- return results;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement