-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileHandler.dart
51 lines (45 loc) · 1.65 KB
/
fileHandler.dart
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
import 'dart:io';
import 'package:path/path.dart' as path;
class FileHandler {
static void writeChangedFile(String filePath, String newFileContent) {
var distFilePath = 'dist' + Platform.pathSeparator + filePath;
File(distFilePath).createSync(recursive: true);
var distFile = File(distFilePath);
distFile.writeAsStringSync(newFileContent);
}
static void copyDirectory(
Directory source, Directory destination, bool overWriteFiles) =>
source.listSync(recursive: false).forEach((var entity) {
if (entity is Directory) {
var newDirectory = Directory(
path.join(destination.absolute.path, path.basename(entity.path)));
newDirectory.createSync();
copyDirectory(entity.absolute, newDirectory, overWriteFiles);
} else if (entity is File) {
var targetPath =
path.join(destination.path, path.basename(entity.path));
if (overWriteFiles || !File(targetPath).existsSync()) {
entity.copySync(targetPath);
}
}
});
//when given a file path, the relative file path to current working
//directory is returned.
static String getRelativeFilePath(filePath) {
var currentDirPath = Directory.current.path;
return filePath
.toString()
.replaceFirst(currentDirPath + Platform.pathSeparator, '');
}
static String getTempDirPath(relativeTempDirPath) {
return Directory.systemTemp.path +
Platform.pathSeparator +
relativeTempDirPath;
}
static void deleteDirectory(dirPath) {
var dir = Directory(dirPath);
if (dir.existsSync()) {
dir.deleteSync(recursive: true);
}
}
}