1 module dflexe;
2 ///come from the old 'dflexe.d'
3 import std.stdio;
4 import std.path;
5 import std.stream;
6 import dfl.application;
7 import std.conv;
8 import std.string;
9 private import dfl.internal.winapi, dfl.internal.utf;//dfl.all, 
10 
11 alias dfl.internal.winapi.ShellExecuteA ShellExecuteA;
12 alias dfl.environment.Environment Environment;
13 
14 
15 private extern(Windows)
16 {
17 	DWORD GetLogicalDriveStringsA(DWORD nBufferLength,LPSTR lpBuffer);
18 	UINT GetDriveTypeA(LPCTSTR lpRootPathName);
19 	DWORD GetShortPathNameA(LPCSTR lpszLongPath, LPSTR lpszShortPath, DWORD cchBuffer);
20 
21 
22 enum: UINT
23 {
24 	DRIVE_FIXED = 3,
25 }
26 	alias DWORD function(LPCWSTR lpszLongPath, LPWSTR lpszShortPath, DWORD cchBuffer) GetShortPathNameWProc;
27 }
28 
29 
30 enum Flags: DWORD
31 {
32 	NONE = 0,
33 
34 	INSTALLED = 1, // Everything is setup.
35 }
36 
37 
38 string importdir,startpath, basepath;
39 string dmdpath, dmdpath_windows = "\0";
40 string strDebug="-debug";
41 string strWindow="SUBSYSTEM:WINDOWS";
42 string strDFLlib="dfl_debug.lib";
43 string strDFile="";
44 void main(string[] args)
45 {
46 	startpath = getshortpath(Application.startupPath);
47 	if(args.length <2 || (args.length == 2 && (args[1]=="-h" || args[1]=="-help")))
48 	{
49 		ShowUsage();
50 		 return;
51 	}
52 	 
53 	buildExe(args);
54 }
55 
56 void buildExe(string[] args)
57 {
58 	string batfilepath = std.path.buildPath(startpath, "exe.bat");
59 
60 	scope batf = new BufferedFile(batfilepath, FileMode.OutNew);
61 	string c;
62 	int i;
63 	foreach(arg;args)
64 	{
65 		c = toLower(arg);
66 		i = c.indexOf('-');
67 		if(i != -1)
68 		{
69 			c=c[i+1 .. c.length];
70 		}
71 		switch(c)
72     	{
73     		case "debug":
74 				strDFLlib = "dfl_debug.lib";
75 				break;
76 			case "release":
77 				strDFLlib = "dfl.lib";
78 				strDebug ="-"~c.idup;
79 			break;
80 			case "gui","windows","winexe":
81 				strWindow ="SUBSYSTEM:WINDOWS";
82 				break;
83     		case "console","con","exe":
84     			strWindow ="su:console:4";
85     			break;
86     		default:
87 				if(c.indexOf(".d") !=-1)
88 				{
89 					strDFile ~=" ";
90 					strDFile~=arg;
91 				}
92     		break;
93     	}
94 	}
95  
96     if(strDFile == "")
97 	{
98 		writeln("Please input *.d file");
99 		return;
100 	}
101 	string strwinLib = " ole32.lib oleAut32.lib gdi32.lib Comctl32.lib Comdlg32.lib advapi32.lib uuid.lib ws2_32.lib"; 
102     string buildstr ="dmd -de -w -property -X -I$(DMDInstallDir)windows\\import "~strDFLlib~strwinLib~" -L/"~strWindow~" "~strDFile~" "~strDebug~"\r\n";// 
103 	batf.writeString(buildstr);
104 	batf.writeString("\r\n");
105 	batf.close();
106 
107 	 std.process.system(batfilepath);
108 
109 	//std.file.remove(batfilepath);
110 
111 }
112 
113 string getshortpath(string fn)
114 {
115 	if(dfl.internal.utf.useUnicode)
116 	{
117 		version(STATIC_UNICODE)
118 		{
119 			alias GetShortPathNameW proc;
120 		}
121 		else
122 		{
123 			const string NAME = "GetShortPathNameW";
124 			static GetShortPathNameWProc proc = null;
125 
126 			if(!proc)
127 			{
128 				proc = cast(GetShortPathNameWProc)GetProcAddress(GetModuleHandleA("kernel32.dll"), NAME.ptr);
129 				if(!proc)
130 					throw new Exception("GetShortPathNameW not found");
131 			}
132 		}
133 
134 		DWORD len;
135 		wchar[MAX_PATH] s;
136 		len = proc(dfl.internal.utf.toUnicodez(fn), s.ptr, s.length);
137 		return to!string(s[0..len]);
138 	}
139 	else
140 	{
141 		DWORD len;
142 		char[MAX_PATH] s;
143 		len = GetShortPathNameA(dfl.internal.utf.toAnsiz(fn), s.ptr, s.length);
144 		return to!string(s[0..len]);
145 	}
146 } 
147 
148 void ShowUsage()
149 {
150 	writeln("\ndflexe written by FrankLIKE,and study by Christopher E. Miller\n");
151 	writeln("Usage:\n"
152 		"   dflexe [<switches...>] <files...>\n\n"
153 		~" for example: dflexe app.d \n\n");
154 	writeln("Switches:\n"
155 		"   -release   Build files's Release version(Default version is 'debug').\n"~
156 		"   -con	   output to Console(Default 'windows').\n");
157 		
158 }
159