module ybud;
/*
Author: yidabu, D programming language, China
http://www.d-programming-language-china.org DMD: 1.018
date: 20070708
*/
import std.regexp;
import std.file;
import std.utf;
import std.path;
import std.string;
import win32.shellapi : ShellExecuteW;
import win32.winuser;
import ydb.ini;
import dfl.application; //for startupPath;
pragma(lib,"ydb.lib");
pragma(lib,"dfl_debug.lib");
pragma(lib,"shell32.lib");
int main(string[] args)
{
int result;
string ybudpath; //ybud.exe fullpath
string ybuddir; //ybud.exe dir
string filedir; //D:\d
string filename; //D:\d\test.d
string fileBaseName; //test.d
string fileShortName; //test
string batcontent; //
string buildflags; //
string rc; //test.rc
string res; //test.res
string run; //test.bat
string def; //test.def
string batpath; //test_ybud.bat
string src; //=std.file.read(test.d)
string inipath; //...\ybud.ini
string rcc; //...\rcc.exe
string upx; //...\\upx.exe
string copyto; //...\down\...
try
{
assert(args.length > 2, "argument number must larger than 2");
filename = args[1];
assert(std.file.exists(filename), "not exists " ~ filename);
buildflags = args[2];
if (args[2] == "debug")
buildflags = "-debug -w -O -unittest -version=IE4";
else if (args[2] == "release")
buildflags = "-release -w -O -release -version=IE4";
else
assert(0, "args[2] must -debug or -release");
ybudpath = Application.executablePath();
ybuddir = std.path.getDirName(ybudpath) ~ r"\";
filedir = std.path.getDirName(filename) ~ r"\";
fileShortName = std.path.getBaseName(filename);
fileShortName = fileShortName[0..$-2]; //test
fileBaseName = std.path.getBaseName(filename); //test.d
rc = fileShortName ~ ".rc"; //test.rc
res = fileShortName ~ ".res"; //test.res
run = fileShortName ~ ".bat"; //test.bat
batpath = fileShortName ~ "_ybud.bat"; //test_ybud.bat
def = fileShortName ~ ".def"; //test.def
inipath = ybuddir ~ "ybud.ini"; //..\ybud.ini
batcontent = "dmd " ~ fileBaseName;
Ini ini = new Ini(inipath);
if (ini["config"] is null ) ini.addSection("config");
if (ini["config"]["rcc"] is null) ini["config"]["rcc"] = "";
if (ini["config"]["upx"] is null) ini["config"]["upx"] = "";
if (ini["config"]["copyto"] is null) ini["config"]["copyto"] = "";
rcc = ini["config"]["rcc"];
upx = ini["config"]["upx"];
copyto = ini["config"]["copyto"];
if (!rcc || rcc.length < 2 || !std.file.exists(rcc)) rcc = null;
if (!upx || upx.length < 2 || !std.file.exists(upx)) upx = null;
if (!copyto || copyto.length < 2 || !std.file.exists(copyto)) copyto = null;
ini.save();
delete ini;
if (std.file.exists(rc) && rcc && !std.file.exists(res))
{
ShellExecuteW(null, toUTF16z("open"), toUTF16z(rcc), toUTF16z("-32 " ~ rc), toUTF16z(filedir), SW_HIDE);
debug printf("now create res: %.*s \n");
}
if (std.file.exists(run))
{
ShellExecuteW(null, toUTF16z("open"), toUTF16z(run), toUTF16z(null), toUTF16z(filedir), SW_SHOW);
debug printf("run %.*s \n", run);
return 0;
}
string[] files;
bool cb(DirEntry* de)
{
if (de.isdir)
std.file.listdir(de.name, &cb);
else
{
if (std.path.getExt(de.name) != "d") return true;
if (de.name == filename) return true;
batcontent ~=" " ~ std.string.replace(de.name, filedir,"");
}
return true;
}
std.file.listdir(filedir, &cb);
if (std.file.exists(res)) batcontent ~= " " ~ res;
if (std.file.exists(def)) batcontent ~= " " ~ def;
string pattern = r"////buildflags\s*=\s*(.+)\s*";
if (auto m = std.regexp.search(src, pattern, "i")) buildflags ~= m.match(1);
batcontent ~= " " ~ buildflags;
src =cast(string) std.file.read(filename);
pattern = r"////" ~ args[2] ~ r"((.|\s)+?)////";
if (auto m = std.regexp.search(src, pattern, "mgi"))
{
batcontent ~= m.match(1);
}
batcontent ~= "\r\npause\r\nerase *.obj *.map";
if (args[2] == "release" && upx)
{
batcontent ~= "\r\nerase " ~ fileShortName ~ "_u.exe";
batcontent ~= "\r\n" ~ upx ~ " --best -o" ~ fileShortName ~ "_u.exe " ~ fileShortName ~ ".exe" ;
}
if (args[2] == "release" && copyto)
{
string sexe = std.file.exists(fileShortName ~ "_u.exe") ? fileShortName ~ "_u.exe" : fileShortName ~ ".exe";
if (auto m = std.regexp.search(src, r"////copyto\s*=?\s*(.*)\s*", "i"))
{
string t = m.match(1);
debug printf("copyto %.*s \n", t);
if ( t.length >5 ) // std.file.exists(t) failed ?
{
copyto = m.match(1);
}
batcontent ~= "\r\ncopy " ~ sexe ~ " " ~ copyto ~ r"\" ~ fileShortName ~ ".exe";
debug printf("copyto %.*s \n",copyto);
}
}
std.file.write(batpath, batcontent);
assert(std.file.exists(batpath));
ShellExecuteW(null, toUTF16z("open"), toUTF16z(batpath), null, toUTF16z(filedir), SW_SHOW);
}
catch(Exception e)
{
printf("catch %.*s\n", e.msg);
result = 1;
}
return result;
}
//=main