1 ///	
2 // Written in the D programming language.
3 /**
4 This is a build tool,compile *.d to exe or lib,and help to build dgui,dfl2 gui (or other you like).
5 now default DC is dmd ,default platform is windows.
6 
7 If your DC is dmd, dco can start only 'dco.ini' config file. 
8 
9 Compiler dco.d :dmd dco.d -release,and dco ↓,
10 
11 Usage:
12 Config some info to 'dco.ini' file,';' or '#' means you can do as it.Then copy dco.ini to your PATH: such as dmd's config file:sc.ini.
13 And dco.exe can auto copy itsself to EnvPath,that also is  dmd.exe 's path: dmd2\window\bin.
14 After that,you can run the 'dco.exe'  anywhere.
15 If not found 'dco.ini',run: dco -ini,please.
16 
17 For example:
18 to get the debug version( -release to get another)
19 
20 build some *.d to lib or exe 			 : dco ↓
21 build some *.d to lib or exe for 64 bit	 : dco -m64 ↓
22 build  one app.d in many *.d    		 : dco app or  dco app.d
23 build for libs 	such as dfl,dgui         : dco  -lib
24 build for app.d use dgui,dfl2			 : dco  -gui
25 build app.d use dfl2 for console		 : dco  -con
26 build lib and copy to libs				 : dco -lib -copy
27 build by custom	and copy to libs         : dco -arg -addlib -lib -copy
28 
29 if your exe's file works on console,you should add '-con' or '-console'. 
30 
31 Copyright: Copyright FrankLIKE 2014-.
32 
33 License:   $(LGPL-3.0).
34 
35 Authors:   FrankLIKE
36 
37 Source: $(dco.d)
38 
39 Created Time:2014-10-27
40 Modify Time:2014-10-31~2016-08-12
41 */
42 module dco.operateFile;
43 /// dco 
44 import	std.stdio;
45 import	std.datetime;
46 import	std.process; 
47 import	std..string;
48 import	std.file;
49 import	std.path;
50 import	std.exception;
51 import  std.json;
52 import std.typetuple;
53 import std.algorithm;
54 
55 import dco.globalInfo;
56 import dco.helpInfo;
57 
58 
59 
60 
61 //targetTypes
62 enum targetTypes {exe,lib,staticLib,dynamicLib,sourceLib,none};
63 string[] targetExt = ["exe","lib","dll","a"];
64 
65  
66 
67 
68 void copyFile()
69 {
70 	string strcopy;
71 	if(!exists(strTargetFileName)) 
72 	{
73 	 	writeln(strTargetFileName," is not exists,stop copy.");
74 		return;
75 	}
76 
77 	if(strTargetFileName.indexOf("exe") != -1)
78 	{
79 		//copy(strTargetFileName,strDCEnv); //
80 		strcopy = "copy " ~ strTargetFileName~" " ~ strDCEnv;
81 	}
82 	else
83 	{ 
84 		string strDCLibPath = strDCEnv[0..(strDCEnv.length - "bin".length)].idup ~ "lib" ~ compileType; 
85 		//copy(strDCEnv,strDCLibPath);
86 		strcopy = "copy " ~ strTargetFileName ~ " " ~ strDCLibPath;
87 	}
88 	if(bDisplayCopyInfo)
89 	{
90 		writeln(strcopy);
91 	}
92 
93 	auto pid =  enforce(spawnShell(strcopy.dup()),"copyFile() error");
94 	if (wait(pid) != 0)
95 	{
96 		writeln("Copy failed.");
97 	}
98 }
99 
100 bool findFiles()
101 { 
102 	int i = 0;
103 	bool bPackage = false; 
104 	auto packages = dirEntries(".","{package.d,all.d}",SpanMode.depth);
105 	foreach(p; packages){i++;}
106 	bPackage = (i > 0);
107 
108 	auto dFiles = dirEntries(".","*.{d,di}",SpanMode.depth);
109 	int icount =0;
110     SysTime fileTime;
111     DirEntry rootDE ;
112 
113 	foreach(d; dFiles)
114 	{	 
115 	    if(!bAssignTarget)
116 	    {
117 			if(icount == 0)
118 			{
119 				strTargetName = d.name[(d.name.lastIndexOf(separator)+1) .. d.name.lastIndexOf(".")];
120 				strTargetName ~= "." ~ strTargetType; 
121 			}
122 		}
123 		if(icount == 0 )
124 		{
125 			ReadDFile(d,bPackage);
126 		}
127 
128 		if(d.toLower().indexOf("ignore") != -1) continue;
129 
130 		strDFile ~= " ";
131 		strDFile ~= d.name[2 ..$].idup;
132 
133 		//sourceLastUpdateTime 
134 		rootDE = DirEntry(d);
135         if(rootDE.timeLastModified > fileTime)
136         {
137         	fileTime = rootDE.timeLastModified;
138         } 
139         icount++;
140 	}
141     sourceLastUpdateTime = fileTime;
142 
143 	strDFile = strDFile.stripRight().idup;
144 
145 	if(icount <= 0)  
146 	{
147 		writeln("Not found any *.d files in current folder.If there is a 'source' or 'src' folder,dco will find the '*.d' from there.");
148 		return false;
149 	}
150 	bCopy = (strDFile.indexOf("dco.d") != -1) ? true : false;
151 	return true;
152 }
153 
154 void getTargetInfo()
155 { 
156 	string root_path = getcwd();
157     string strPath;
158 	auto dFiles = dirEntries(root_path,strTargetName ~ ".{lib,exe,dll,a}",SpanMode.shallow);
159 	int i =0;
160 	foreach(d;dFiles)
161 	{
162 		i++;
163 		strTargetFileName =d;
164 		strTargetType = d.name[d.name.lastIndexOf(".")+1..$];
165 		break;
166 
167 	}
168 	if(i == 0)
169 	{
170 		if(strTargetName.indexOf("." ~ strTargetType) == -1)
171 		{
172 			strTargetName = strTargetName ~ "." ~ strTargetType;
173 		}
174 		strTargetFileName = root_path ~ separator ~ strTargetName;
175 	}
176 	if(!findStr(strTargetFileName,targetExt))
177 	{
178 		writeln("don't known the targetType ",strTargetFileName);
179 	}
180 	return;
181 }
182 
183 bool findStr(string strIn,string[] strFind)
184 { 
185    return strFind.canFind!(a => strIn.canFind(a) != false);
186 }
187 
188 
189 void ReadDFile(string dFile,bool bPackage)
190 { 
191     if(bGetLocal) return;
192 	auto file = File(dFile); 
193 	scope(exit)  file.close();
194 	auto range = file.byLine();
195 	int icount = 0;
196     foreach (line; range)
197     {
198         if (!line.init && line.indexOf("import") != -1)
199         { 
200         	if(line.indexOf("dfl") != -1)
201         	{
202         		bBuildSpecialLib = bPackage;
203         		SpecialLib = "dfl";
204         		bUseSpecialLib = !bPackage;
205 
206 				if(bUseSpecialLib) 
207 					strTargetLflags = strWindows;
208 				else
209 					strTargetLflags = strConsole;
210 				break;
211 			}
212 			else if(line.indexOf("dgui") != -1)
213 			{
214 				strArgs = strAddArgsdfl = " -g -de -w -X ";
215 				SpecialLib = "dgui";
216 				bBuildSpecialLib = bPackage;
217 				break;
218 			}
219         }
220         else if(line.indexOf("WinMain") != -1)
221         {
222 			strTargetLflags = strWindows;
223 			break;
224         }
225         icount++;
226         if(icount >100) break;
227     }
228 }
229 
230 
231 void readInJson()
232 {
233 	if(!exists("dub.json") && !exists("package.json") ) return;
234 }