Skip to content

Instantly share code, notes, and snippets.

@Swader
Created March 14, 2013 18:25
Show Gist options
  • Save Swader/5163867 to your computer and use it in GitHub Desktop.
Save Swader/5163867 to your computer and use it in GitHub Desktop.
Recursive folder copy in Dart: An updated Dart web_ui build script which recursively copies a folder and its contents to another location. Useful when building for a defined vhost (e.g. Apache with PHP back end for your Dart app) and don't want the build script to relativize your paths to go "one folder up" back from /out into /web to get CSS an…
import 'dart:io';
import 'package:web_ui/component_build.dart';
// Ref: http://www.dartlang.org/articles/dart-web-components/tools.html
main() {
build(new Options().arguments, ['web/index.html']);
//recursiveFolderCopySync('web/assets', 'web/out/assets');
}
bool isSymlink(String pathString) {
var path = new Path(pathString);
var parentPath = path.directoryPath;
var fullParentPath = new File.fromPath(parentPath).fullPathSync();
var expectedPath = new Path(fullParentPath).append(path.filename).toString();
var fullPath = new File.fromPath(path).fullPathSync();
return fullPath != expectedPath;
}
void recursiveFolderCopySync(String path1, String path2) {
Directory dir1 = new Directory(path1);
if (!dir1.existsSync()) {
throw new Exception(
'Source directory "${dir1.path}" does not exist, nothing to copy'
);
}
Directory dir2 = new Directory(path2);
if (!dir2.existsSync()) {
dir2.createSync(recursive: true);
}
dir1.listSync().forEach((element) {
if (!isSymlink(element.path)) {
Path elementPath = new Path(element.path);
String newPath = "${dir2.path}/${elementPath.filename}";
if (element is File) {
File newFile = new File(newPath);
newFile.writeAsBytesSync(element.readAsBytesSync());
} else if (element is Directory) {
recursiveFolderCopySync(element.path, newPath);
} else {
throw new Exception('File is neither File nor Directory. HOW?!');
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment