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