1 /++
2 $(H2 Scriptlike $(SCRIPTLIKE_VERSION))
3 
4 Wrappers for $(MODULE_STD_FILE) that add support for Scriptlike's
5 $(API_PATH_EXTR Path), command echoing and dry-run features.
6 
7 Copyright: Copyright (C) 2014-2015 Nick Sabalausky
8 License:   zlib/libpng
9 Authors:   Nick Sabalausky
10 +/
11 module scriptlike.file.wrappers;
12 
13 import std.algorithm;
14 import std.conv;
15 import std.datetime;
16 import std.string;
17 import std.traits;
18 import std.typecons;
19 
20 static import std.file;
21 public import std.file : FileException, SpanMode,
22 	attrIsDir, attrIsFile, attrIsSymlink;
23 static import std.path;
24 
25 import scriptlike.core;
26 import scriptlike.path.extras;
27 
28 /// Alias of same-named function from $(MODULE_STD_FILE)
29 alias read       = std.file.read;
30 alias readText() = std.file.readText; ///ditto
31 alias write      = std.file.write;    ///ditto
32 alias append     = std.file.append;   ///ditto
33 alias rename     = std.file.rename;   ///ditto
34 alias remove     = std.file.remove;   ///ditto
35 alias getSize    = std.file.getSize;  ///ditto
36 alias getTimes   = std.file.getTimes; ///ditto
37 
38 version(ddoc_scriptlike_d) alias getTimesWin = std.file.getTimesWin; ///ditto
39 else version(Windows)      alias getTimesWin = std.file.getTimesWin; ///ditto
40 
41 alias setTimes          = std.file.setTimes;          ///ditto
42 alias timeLastModified  = std.file.timeLastModified;  ///ditto
43 alias exists            = std.file.exists;            ///ditto
44 alias getAttributes     = std.file.getAttributes;     ///ditto
45 alias getLinkAttributes = std.file.getLinkAttributes; ///ditto
46 alias isDir             = std.file.isDir;             ///ditto
47 alias isFile            = std.file.isFile;            ///ditto
48 alias isSymlink()       = std.file.isSymlink;         ///ditto
49 alias chdir             = std.file.chdir;             ///ditto
50 alias mkdir             = std.file.mkdir;             ///ditto
51 alias mkdirRecurse      = std.file.mkdirRecurse;      ///ditto
52 alias rmdir             = std.file.rmdir;             ///ditto
53 
54 version(ddoc_scriptlike_d) alias symlink() = std.file.symlink; ///ditto
55 else version(Posix)        alias symlink() = std.file.symlink; ///ditto
56 
57 version(ddoc_scriptlike_d) alias readLink() = std.file.readLink; ///ditto
58 else version(Posix)        alias readLink() = std.file.readLink; ///ditto
59 
60 alias copy         = std.file.copy;         ///ditto
61 alias rmdirRecurse = std.file.rmdirRecurse; ///ditto
62 alias dirEntries   = std.file.dirEntries;   ///ditto
63 alias slurp        = std.file.slurp;        ///ditto
64 
65 /// Like $(FULL_STD_FILE read), but supports Path and command echoing.
66 void[] read(in Path name, size_t upTo = size_t.max)
67 {
68 	yapFunc(name);
69 	return std.file.read(name.toRawString(), upTo);
70 }
71 
72 /// Like $(FULL_STD_FILE readTXXXXXXXext), but supports Path and command echoing.
73 S readText(S = string)(in Path name)
74 {
75 	yapFunc(name);
76 	return std.file.readText(name.toRawString());
77 }
78 
79 /// Like $(FULL_STD_FILE write), but supports Path, command echoing and dryrun.
80 void write(in Path name, const void[] buffer)
81 {
82 	write(name.toRawString(), buffer);
83 }
84 
85 ///ditto
86 void write(in string name, const void[] buffer)
87 {
88 	yapFunc(name.escapeShellArg());
89 	
90 	if(!scriptlikeDryRun)
91 		std.file.write(name, buffer);
92 }
93 
94 /// Like $(FULL_STD_FILE append), but supports Path, command echoing and dryrun.
95 void append(in Path name, in void[] buffer)
96 {
97 	append(name.toRawString(), buffer);
98 }
99 
100 ///ditto
101 void append(in string name, in void[] buffer)
102 {
103 	yapFunc(name.escapeShellArg());
104 
105 	if(!scriptlikeDryRun)
106 		std.file.append(name, buffer);
107 }
108 
109 /// Like $(FULL_STD_FILE rename), but supports Path, command echoing and dryrun.
110 void rename(in Path from, in Path to)
111 {
112 	rename(from.toRawString(), to.toRawString());
113 }
114 
115 ///ditto
116 void rename(in string from, in Path to)
117 {
118 	rename(from, to.toRawString());
119 }
120 
121 ///ditto
122 void rename(in Path from, in string to)
123 {
124 	rename(from.toRawString(), to);
125 }
126 
127 ///ditto
128 void rename(in string from, in string to)
129 {
130 	yapFunc(from.escapeShellArg(), " -> ", to.escapeShellArg());
131 
132 	if(!scriptlikeDryRun)
133 		std.file.rename(from, to);
134 }
135 
136 /// Like $(FULL_STD_FILE remove), but supports Path, command echoing and dryrun.
137 void remove(in Path name)
138 {
139 	remove(name.toRawString());
140 }
141 
142 ///ditto
143 void remove(in string name)
144 {
145 	yapFunc(name.escapeShellArg());
146 
147 	if(!scriptlikeDryRun)
148 		std.file.remove(name);
149 }
150 
151 /// Like $(FULL_STD_FILE getSize), but supports Path and command echoing.
152 ulong getSize(in Path name)
153 {
154 	yapFunc(name);
155 	return std.file.getSize(name.toRawString());
156 }
157 
158 /// Like $(FULL_STD_FILE getTimes), but supports Path and command echoing.
159 void getTimes(in Path name,
160 	out SysTime accessTime,
161 	out SysTime modificationTime)
162 {
163 	yapFunc(name);
164 	std.file.getTimes(name.toRawString(), accessTime, modificationTime);
165 }
166 
167 version(ddoc_scriptlike_d)
168 {
169 	/// Windows-only. Like $(FULL_STD_FILE getTimesWin), but supports Path and command echoing.
170 	void getTimesWin(in Path name,
171 		out SysTime fileCreationTime,
172 		out SysTime fileAccessTime,
173 		out SysTime fileModificationTime);
174 }
175 else version(Windows) void getTimesWin(in Path name,
176 	out SysTime fileCreationTime,
177 	out SysTime fileAccessTime,
178 	out SysTime fileModificationTime)
179 {
180 	yapFunc(name);
181 	std.file.getTimesWin(name.toRawString(), fileCreationTime, fileAccessTime, fileModificationTime);
182 }
183 
184 /// Like $(FULL_STD_FILE setTimes), but supports Path, command echoing and dryrun.
185 void setTimes(in Path name,
186 	SysTime accessTime,
187 	SysTime modificationTime)
188 {
189 	setTimes(name.toRawString(), accessTime, modificationTime);
190 }
191 
192 ///ditto
193 void setTimes(in string name,
194 	SysTime accessTime,
195 	SysTime modificationTime)
196 {
197 	yapFunc(name.escapeShellArg(),
198 		"Accessed ", accessTime, "; Modified ", modificationTime);
199 
200 	if(!scriptlikeDryRun)
201 		std.file.setTimes(name, accessTime, modificationTime);
202 }
203 
204 /// Like $(FULL_STD_FILE timeLastModified), but supports Path and command echoing.
205 SysTime timeLastModified(in Path name)
206 {
207 	yapFunc(name);
208 	return std.file.timeLastModified(name.toRawString());
209 }
210 
211 /// Like $(FULL_STD_FILE timeLastModified), but supports Path and command echoing.
212 SysTime timeLastModified(in Path name, SysTime returnIfMissing)
213 {
214 	yapFunc(name);
215 	return std.file.timeLastModified(name.toRawString(), returnIfMissing);
216 }
217 
218 /// Like $(FULL_STD_FILE exists), but supports Path and command echoing.
219 bool exists(in Path name) @trusted
220 {
221 	yapFunc(name);
222 	return std.file.exists(name.toRawString());
223 }
224 
225 /// Like $(FULL_STD_FILE getAttributes), but supports Path and command echoing.
226 uint getAttributes(in Path name)
227 {
228 	yapFunc(name);
229 	return std.file.getAttributes(name.toRawString());
230 }
231 
232 /// Like $(FULL_STD_FILE getLinkAttributes), but supports Path and command echoing.
233 uint getLinkAttributes(in Path name)
234 {
235 	yapFunc(name);
236 	return std.file.getLinkAttributes(name.toRawString());
237 }
238 
239 /// Like $(FULL_STD_FILE isDir), but supports Path and command echoing.
240 @property bool isDir(in Path name)
241 {
242 	yapFunc(name);
243 	return std.file.isDir(name.toRawString());
244 }
245 
246 /// Like $(FULL_STD_FILE isFile), but supports Path and command echoing.
247 @property bool isFile(in Path name)
248 {
249 	yapFunc(name);
250 	return std.file.isFile(name.toRawString());
251 }
252 
253 /// Like $(FULL_STD_FILE isSymlink), but supports Path and command echoing.
254 @property bool isSymlink(Path name)
255 {
256 	yapFunc(name);
257 	return std.file.isSymlink(name.toRawString());
258 }
259 
260 /// Like $(FULL_STD_FILE getcwd), but returns a Path.
261 Path getcwd()
262 {
263 	return Path( std.file.getcwd() );
264 }
265 
266 /// Like $(FULL_STD_FILE chdir), but supports Path and command echoing.
267 void chdir(in Path pathname)
268 {
269 	chdir(pathname.toRawString());
270 }
271 
272 /// Like $(FULL_STD_FILE chdir), but supports Path and command echoing.
273 void chdir(in string pathname)
274 {
275 	yapFunc(pathname.escapeShellArg());
276 	std.file.chdir(pathname);
277 }
278 
279 /// Like $(FULL_STD_FILE mkdir), but supports Path, command echoing and dryrun.
280 void mkdir(in Path pathname)
281 {
282 	mkdir(pathname.toRawString());
283 }
284 
285 ///ditto
286 void mkdir(in string pathname)
287 {
288 	yapFunc(pathname.escapeShellArg());
289 
290 	if(!scriptlikeDryRun)
291 		std.file.mkdir(pathname);
292 }
293 
294 /// Like $(FULL_STD_FILE mkdirRecurse), but supports Path, command echoing and dryrun.
295 void mkdirRecurse(in Path pathname)
296 {
297 	mkdirRecurse(pathname.toRawString());
298 }
299 
300 ///ditto
301 void mkdirRecurse(in string pathname)
302 {
303 	yapFunc(pathname.escapeShellArg());
304 
305 	if(!scriptlikeDryRun)
306 		std.file.mkdirRecurse(pathname);
307 }
308 
309 /// Like $(FULL_STD_FILE rmdir), but supports Path, command echoing and dryrun.
310 void rmdir(in Path pathname)
311 {
312 	rmdir(pathname.toRawString());
313 }
314 
315 ///ditto
316 void rmdir(in string pathname)
317 {
318 	yapFunc(pathname.escapeShellArg());
319 
320 	if(!scriptlikeDryRun)
321 		std.file.rmdir(pathname);
322 }
323 
324 version(ddoc_scriptlike_d)
325 {
326 	/// Posix-only. Like $(FULL_STD_FILE symlink), but supports Path and command echoing.
327 	void symlink(Path original, Path link);
328 
329 	///ditto
330 	void symlink(string original, Path link);
331 
332 	///ditto
333 	void symlink(Path original, string link);
334 
335 	///ditto
336 	void symlink(string original, string link);
337 
338 	/// Posix-only. Like $(FULL_STD_FILE readLink), but supports Path and command echoing.
339 	Path readLink(Path link);
340 }
341 else version(Posix)
342 {
343 	void symlink(Path original, Path link)
344 	{
345 		symlink(original.toRawString(), link.toRawString());
346 	}
347 
348 	void symlink(string original, Path link)
349 	{
350 		symlink(original, link.toRawString());
351 	}
352 
353 	void symlink(Path original, string link)
354 	{
355 		symlink(original.toRawString(), link);
356 	}
357 
358 	void symlink(string original, string link)
359 	{
360 		yapFunc("[original] ", original.escapeShellArg(), " : [symlink] ", link.escapeShellArg());
361 
362 		if(!scriptlikeDryRun)
363 			std.file.symlink(original, link);
364 	}
365 
366 	Path readLink(Path link)
367 	{
368 		yapFunc(link);
369 		return Path( std.file.readLink(link.toRawString()) );
370 	}
371 }
372 
373 /// Like $(FULL_STD_FILE copy), but supports Path, command echoing and dryrun.
374 void copy(in Path from, in Path to)
375 {
376 	copy(from.toRawString(), to.toRawString());
377 }
378 
379 ///ditto
380 void copy(in string from, in Path to)
381 {
382 	copy(from, to.toRawString());
383 }
384 
385 ///ditto
386 void copy(in Path from, in string to)
387 {
388 	copy(from.toRawString(), to);
389 }
390 
391 ///ditto
392 void copy(in string from, in string to)
393 {
394 	yapFunc(from.escapeShellArg(), " -> ", to.escapeShellArg());
395 
396 	if(!scriptlikeDryRun)
397 		std.file.copy(from, to);
398 }
399 
400 /// Like $(FULL_STD_FILE rmdirRecurse), but supports Path, command echoing and dryrun.
401 void rmdirRecurse(in Path pathname)
402 {
403 	rmdirRecurse(pathname.toRawString());
404 }
405 
406 ///ditto
407 void rmdirRecurse(in string pathname)
408 {
409 	yapFunc(pathname.escapeShellArg());
410 
411 	if(!scriptlikeDryRun)
412 		std.file.rmdirRecurse(pathname);
413 }
414 
415 /// Like $(FULL_STD_FILE dirEntries), but supports Path and command echoing.
416 auto dirEntries(Path path, SpanMode mode, bool followSymlink = true)
417 {
418 	yapFunc(path);
419 	return std.file.dirEntries(path.toRawString(), mode, followSymlink);
420 }
421 
422 /// Like $(FULL_STD_FILE dirEntries), but supports Path and command echoing.
423 auto dirEntries(Path path, string pattern, SpanMode mode,
424 	bool followSymlink = true)
425 {
426 	yapFunc(path);
427 	return std.file.dirEntries(path.toRawString(), pattern, mode, followSymlink);
428 }
429 
430 /// Like $(FULL_STD_FILE slurp), but supports Path and command echoing.
431 template slurp(Types...)
432 {
433 	auto slurp(Path filename, in string format)
434 	{
435 		yapFunc(filename);
436 		return std.file.slurp!Types(filename.toRawString(), format);
437 	}
438 }
439 
440 /// Like $(FULL_STD_FILE thisExePath), but supports Path and command echoing.
441 @trusted Path thisExePath()
442 {
443 	auto path = Path( std.file.thisExePath() );
444 	yapFunc(path);
445 	return path;
446 }
447 
448 /// Like $(FULL_STD_FILE tempDir), but supports Path and command echoing.
449 @trusted Path tempDir()
450 {
451 	auto path = Path( std.file.tempDir() );
452 	yapFunc(path);
453 	return path;
454 }