-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFile.hx
549 lines (449 loc) · 16.8 KB
/
File.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package hx.files;
import hx.concurrent.ConcurrentException;
import hx.strings.internal.Either2;
#if (sys || macro)
import sys.io.FileInput;
import sys.io.FileOutput;
#end
#if (sys || macro || nodejs)
import sys.FileSystem;
#end
using hx.strings.Strings;
/**
* Represents a regular file.
*/
class File {
/**
* This method does not check if the path actually exists and if it currently points to a directory or a file
*
* <pre><code>
* >>> File.of(null) throws "[path] must not be null"
* </code></pre>
*
* @param trimWhiteSpaces controls if leading/trailing whitespaces of path elements shall be removed automatically
*/
public static function of(path:Either2<String, Path>, trimWhiteSpaces = true):File {
if (path == null)
throw "[path] must not be null";
return switch(path.value) {
case a(str): new File(Path.of(str, trimWhiteSpaces));
case b(obj): new File(obj);
}
}
public final path:Path;
inline //
private function new(path:Path) {
this.path = path;
}
#if (filesystem_support || macro)
function assertValidPath(mustExist = true) {
if (path.filename.isEmpty())
throw "[path.filename] must not be empty!";
if (path.exists()) {
if (!path.isFile()) throw '[path] "$path" exists but is not a file!';
} else {
if (mustExist) throw '[path] "$path" does not exist!';
}
}
#if (sys || macro)
public function openInput(binary = true):FileInput
return sys.io.File.read(toString(), binary);
public function openOutput(mode:FileWriteMode, binary = true):FileOutput
return switch(mode) {
case REPLACE: sys.io.File.write(toString(), binary);
case UPDATE: sys.io.File.update(toString(), binary);
case APPEND: sys.io.File.append(toString(), binary);
}
#end
/**
* <pre><code>
* >>> File.of("target/test.txt").writeString("HEY!", true) throws nothing
* >>> File.of("target/test.txt").appendString("HO!") throws nothing
* >>> File.of("target/test.txt").readAsString().indexOf("HEY!") > -1 == true
* >>> File.of("target/test.txt").delete() == true
* >>> File.of("" ).appendString("") throws "[path.filename] must not be empty!"
* </code></pre>
*/
public function appendString(content:Null<String>):Void {
assertValidPath(false);
if (content == null)
return;
#if (sys || macro)
var ex:Null<ConcurrentException> = null;
var out = sys.io.File.append(path.toString());
try {
out.writeString(content);
} catch (e:Dynamic) {
ex = ConcurrentException.capture(e);
}
out.close();
if (ex != null)
ex.rethrow();
#elseif nodejs
js.node.Fs.appendFileSync(path.toString(), content);
#elseif phantomjs
js.phantomjs.FileSystem.write(path.toString(), content, "a");
#else
throw "Operation not supported on current target.";
#end
}
/**
* Delete the file.
*
* <pre><code>
* >>> File.of("target/test.txt").writeString("HEY!", true) throws nothing
* >>> File.of("target/test.txt").delete() == true
* >>> File.of("target" ).delete() throws '[path] "target" exists but is not a file!'
* >>> File.of("" ).delete() == false
* <code></pre>
*
* @return false if path does not exist
*
* @throws if path is not a file
*/
public function delete():Bool {
if (!path.exists())
return false;
assertValidPath();
#if (sys || macro || nodejs)
FileSystem.deleteFile(path.toString());
return true;
#elseif phantomjs
js.phantomjs.FileSystem.remove(path.toString());
return true;
#else
throw "Operation not supported on current target.";
#end
}
/**
* <pre><code>
* >>> File.of("CHANGELOG.md").readAsBytes().length > 0
* >>> File.of("nonexistent" ).readAsBytes() == null
* >>> File.of("" ).readAsBytes() == null
* >>> File.of("." ).readAsBytes() throws '[path] "." exists but is not a file!'
* >>> File.of("src" ).readAsBytes() throws '[path] "src" exists but is not a file!'
* </code></pre>
*
* @return null in case the file does not exist
*
* @throws if path is not a file
*/
public function readAsBytes():Null<haxe.io.Bytes> {
if (!path.exists())
return null;
assertValidPath();
#if (sys || macro || nodejs)
return sys.io.File.getBytes(path.toString());
#elseif phantomjs
return haxe.io.Bytes.ofString(js.phantomjs.FileSystem.read(path.toString()));
#else
throw "Operation not supported on current target.";
#end
}
/**
* <pre><code>
* >>> File.of("CHANGELOG.md").readAsString().indexOf("Initial release") > -1 == true
* >>> File.of("nonexistent" ).readAsString() == null
* >>> File.of("" ).readAsString() == null
* >>> File.of("." ).readAsString() throws '[path] "." exists but is not a file!'
* >>> File.of("src" ).readAsString() throws '[path] "src" exists but is not a file!'
* </code></pre>
*
* @param defaultValue string to be returned in case the file does not exist
*
* @throws if path is not a file
*/
public function readAsString(?defaultValue:String):Null<String> {
if (!path.exists())
return defaultValue;
assertValidPath();
#if (sys || macro || nodejs)
return sys.io.File.getContent(path.toString());
#elseif phantomjs
return js.phantomjs.FileSystem.read(path.toString());
#else
throw "Operation not supported on current target.";
#end
}
/**
* Copies the given file.
*
* <pre><code>
* >>> File.of("target/foo.txt").writeString("HEY!") throws nothing
* >>> File.of("target/foo.txt").copyTo("target/bar.txt") throws nothing
* >>> File.of("target/foo.txt").copyTo("target/bar.txt") throws '[newPath] "target' + Path.of("").dirSep + 'bar.txt" already exists!'
* >>> File.of("target/foo.txt").copyTo("target/bar.txt", [OVERWRITE]) throws nothing
* >>> File.of("target/foo.txt").path.exists() == true
* >>> File.of("target/bar.txt").path.exists() == true
* >>> File.of("target/foo.txt").delete() == true
* >>> File.of("target/bar.txt").delete() == true
*
* >>> File.of("README.md" ).copyTo(null) throws "[newPath] must not be null!"
* >>> File.of("README.md" ).copyTo("") throws "[newPath] must not be empty!"
* >>> File.of("nonexistent").copyTo("") throws '[path] "nonexistent" does not exist!'
* >>> File.of("" ).copyTo("") throws "[path.filename] must not be empty!"
* </code></pre>
*
* @return a File instance pointing to the copy target
*/
public function copyTo(newPath:Either2<String, Path>, ?options:Array<FileCopyOption>):File {
if (newPath == null)
throw "[newPath] must not be null!";
assertValidPath();
var trimWhiteSpaces = true;
var overwrite = false;
if (options != null) for (o in options) {
switch(o) {
case OVERWRITE: overwrite = true;
case NO_WHITESPACE_TRIMMING: trimWhiteSpaces = false;
}
}
final targetPath:Path = switch(newPath.value) {
case a(str): Path.of(str, trimWhiteSpaces);
case b(obj): obj;
}
if (targetPath.filename == "")
throw "[newPath] must not be empty!";
if (path.getAbsolutePath() == targetPath.getAbsolutePath())
return this;
final targetFile = targetPath.toFile();
if (targetPath.exists()) {
if (!overwrite)
throw '[newPath] "$targetPath" already exists!';
if (!targetPath.isFile())
throw '[newPath] "$targetPath" already exists and is not a file!';
targetFile.delete();
}
#if (sys || macro || nodejs)
sys.io.File.copy(path.toString(), targetPath.toString());
#elseif phantomjs
js.phantomjs.FileSystem.copy(path.toString(), targetPath.toString());
#else
throw "Operation not supported on current target.";
#end
return targetFile;
}
/**
* Moves the given file.
*
* <pre><code>
* >>> File.of("target/foo.txt").writeString("HEY!") throws nothing
* >>> File.of("target/foo.txt").moveTo("target/bar.txt") throws nothing
* >>> File.of("target/bar.txt").moveTo("target/bar.txt") throws nothing
* >>> File.of("target/foo.txt").path.exists() == false
* >>> File.of("target/bar.txt").path.exists() == true
*
* >>> File.of("target/bar.txt").moveTo(null) throws "[newPath] must not be null!"
* >>> File.of("target/bar.txt").moveTo("") throws "[newPath] must not be empty!"
* >>> File.of("target/bar.txt").delete() == true
* >>> File.of("" ).moveTo("") throws "[path.filename] must not be empty!"
* </code></pre>
*
* @return a File instance pointing to the new location
*/
public function moveTo(newPath:Either2<String, Path>, ?options:Array<FileMoveOption>):File {
if (newPath == null)
throw "[newPath] must not be null!";
assertValidPath();
var trimWhiteSpaces = true;
var overwrite = false;
if (options != null) for (o in options) {
switch(o) {
case OVERWRITE: overwrite = true;
case NO_WHITESPACE_TRIMMING: trimWhiteSpaces = false;
}
}
final targetPath:Path = switch(newPath.value) {
case a(str): Path.of(str, trimWhiteSpaces);
case b(obj): obj;
}
if (targetPath.filename == "")
throw "[newPath] must not be empty!";
final targetFile = targetPath.toFile();
if (targetPath.exists()) {
if (path.getAbsolutePath() == targetPath.getAbsolutePath())
return this;
if (!overwrite)
throw '[newPath] "$targetPath" already exists!';
if (targetPath.isDirectory())
throw '[newPath] "$targetPath" already exists and is a directory!';
targetFile.delete();
}
#if (sys || macro || nodejs)
FileSystem.rename(path.toString(), targetPath.toString());
#elseif phantomjs
js.phantomjs.FileSystem.move(path.toString(), targetPath.toString());
#else
throw "Operation not supported on current target.";
#end
return targetFile;
}
/**
* Renames the given file within it's current parent directory.
*
* <pre><code>
* >>> File.of("target/foo.txt").writeString("HEY!") throws nothing
* >>> File.of("target/foo.txt").renameTo("bar.txt") throws nothing
* >>> File.of("target/foo.txt").path.exists() == false
* >>> File.of("target/bar.txt").path.exists() == true
* >>> File.of("target/bar.txt").delete() == true
*
* >>> File.of("target/foo.txt").renameTo("target/bar.txt") throws '[newFileName] "target/bar.txt" must not contain directory separators!'
* >>> File.of("target/foo.txt").renameTo("") throws "[newFileName] must not be null or empty!"
* >>> File.of("" ).renameTo("") throws "[newFileName] must not be null or empty!"
* </code></pre>
*
* @param newDirName the new directory name (NOT the full path!)
*
* @return a File instance pointing to the new location
*/
public function renameTo(newFileName:String, ?options:Array<FileRenameOption>):File {
if (newFileName.isEmpty())
throw "[newFileName] must not be null or empty!";
if (newFileName.containsAny([Path.UnixPath.DIR_SEP, Path.WindowsPath.DIR_SEP]))
throw '[newFileName] "$newFileName" must not contain directory separators!';
var opts:Null<Array<FileMoveOption>> = null;
if (options != null) for (o in options) {
switch(o) {
case OVERWRITE: opts = [OVERWRITE];
}
}
if (path.parent == null)
return moveTo(newFileName, opts);
@:nullSafety(Off)
return moveTo(path.parent.join(newFileName), opts);
}
/**
* @return size in bytes
*/
public function size():Int {
if (!path.exists())
throw '[path] "$path" doesn\'t exists!';
#if (sys || macro || nodejs)
final stat = sys.FileSystem.stat(path.toString());
return stat.size;
#elseif phantomjs
return js.phantomjs.FileSystem.size(path.toString());
#else
throw "Operation not supported on current target.";
#end
}
/**
* Creates the file if it does not exist yet or updates the modification timestamp.
*
* <pre><code>
* >>> File.of("target/touch.txt").path.exists() == false
* >>> File.of("target/touch.txt").touch() throws nothing
* >>> File.of("target/touch.txt").path.exists() == true
* >>> File.of("target/touch.txt").delete() throws nothing
* </code></pre>
*/
public function touch():Void {
assertValidPath(false);
#if (sys || macro || nodejs)
if (path.exists()) {
if (hx.files.internal.OS.isWindows) {
Sys.command('copy /b "${path.toString()}"+,,'); // https://superuser.com/a/764721
} else {
Sys.command('touch "${path.toString()}"');
}
} else {
writeString("", false);
}
#elseif phantomjs
js.phantomjs.FileSystem.touch(path.toString());
#else
throw "Operation not supported on current target.";
#end
}
public function writeBytes(content:haxe.io.Bytes, overwrite = true):Void {
assertValidPath(false);
if (path.exists() && !overwrite)
throw '[path] "$path" already exists!';
#if (sys || macro || nodejs)
sys.io.File.saveBytes(path.toString(), content);
#elseif phantomjs
js.phantomjs.FileSystem.write(path.toString(), content.toString(), "w");
#else
throw "Operation not supported on current target.";
#end
}
/**
* <pre><code>
* >>> File.of("target/test.txt").writeString("HEY!") throws nothing
* >>> File.of("target/test.txt").readAsString().indexOf("HEY!") > -1 == true
* >>> File.of("target/test.txt").writeString("HEY!", false) throws '[path] "target' + Path.of("").dirSep + 'test.txt" already exists!'
* >>> File.of("target/test.txt").delete() throws nothing
* >>> File.of("" ).writeString("") throws "[path.filename] must not be empty!"
* </code></pre>
*/
public function writeString(content:String, overwrite = true):Void {
assertValidPath(false);
if (path.exists() && !overwrite)
throw '[path] "$path" already exists!';
if (content == null)
return;
#if (sys || macro || nodejs)
#if (flash || openfl)
//before writing a string, make sure path's parent exists.
//if it doesn't, it needs to be created to write the content for OpenFL and AIR/Flash.
var dir:String = haxe.io.Path.directory(path.toString());
if (!FileSystem.exists(dir))
FileSystem.createDirectory(dir);
var output = sys.io.File.write(path.toString(), false);
output.writeString(content);
output.close();
#else
//on other targets, this check is unnecessary.
sys.io.File.saveContent(path.toString(), content);
#end
#elseif phantomjs
js.phantomjs.FileSystem.write(path.toString(), content, "w");
#else
throw "Operation not supported on current target.";
#end
}
#end // filesystem_support
/**
* @return the file's path
*/
inline //
public function toString():String
return path.toString();
}
enum FileWriteMode {
REPLACE;
UPDATE;
APPEND;
}
enum FileRenameOption {
/**
* if a file already existing at `newPath` it will be deleted automatically
*/
OVERWRITE;
}
enum FileCopyOption {
/**
* if a file already existing at `newPath` it will be deleted automatically
*/
OVERWRITE;
/**
* If `newPath` is a string do not automatically remove leading/trailing whitespaces of path elements
*/
NO_WHITESPACE_TRIMMING;
}
enum FileMoveOption {
/**
* if a file already existing at `newPath` it will be deleted automatically
*/
OVERWRITE;
/**
* If `newPath` is a string do not automatically remove leading/trailing whitespaces of path elements
*/
NO_WHITESPACE_TRIMMING;
}