OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library descriptor.tar; | 5 library descriptor.tar; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 import 'package:scheduled_test/scheduled_test.dart'; | 10 import 'package:scheduled_test/scheduled_test.dart'; |
11 import 'package:scheduled_test/descriptor.dart'; | 11 import 'package:scheduled_test/descriptor.dart'; |
12 | 12 |
13 import '../../lib/src/io.dart'; | 13 import '../../lib/src/io.dart'; |
14 | 14 |
15 /// Describes a tar file and its contents. | 15 /// Describes a tar file and its contents. |
16 class TarFileDescriptor extends DirectoryDescriptor | 16 class TarFileDescriptor extends DirectoryDescriptor implements |
17 implements ReadableDescriptor { | 17 ReadableDescriptor { |
18 TarFileDescriptor(String name, List<Descriptor> contents) | 18 TarFileDescriptor(String name, List<Descriptor> contents) |
19 : super(name, contents); | 19 : super(name, contents); |
20 | 20 |
21 /// Creates the files and directories within this tar file, then archives | 21 /// Creates the files and directories within this tar file, then archives |
22 /// them, compresses them, and saves the result to [parentDir]. | 22 /// them, compresses them, and saves the result to [parentDir]. |
23 Future<String> create([String parent]) => schedule(() { | 23 Future<String> create([String parent]) => schedule(() { |
24 if (parent == null) parent = defaultRoot; | 24 if (parent == null) parent = defaultRoot; |
25 return withTempDir((tempDir) { | 25 return withTempDir((tempDir) { |
26 return Future.wait(contents.map((entry) { | 26 return Future.wait(contents.map((entry) { |
27 return entry.create(tempDir); | 27 return entry.create(tempDir); |
28 })).then((_) { | 28 })).then((_) { |
29 var createdContents = listDir(tempDir, | 29 var createdContents = |
30 recursive: true, | 30 listDir(tempDir, recursive: true, includeHidden: true); |
31 includeHidden: true); | |
32 return createTarGz(createdContents, baseDir: tempDir).toBytes(); | 31 return createTarGz(createdContents, baseDir: tempDir).toBytes(); |
33 }).then((bytes) { | 32 }).then((bytes) { |
34 var file = path.join(parent, name); | 33 var file = path.join(parent, name); |
35 writeBinaryFile(file, bytes); | 34 writeBinaryFile(file, bytes); |
36 return file; | 35 return file; |
37 }); | 36 }); |
38 }); | 37 }); |
39 }, 'creating tar file:\n${describe()}'); | 38 }, 'creating tar file:\n${describe()}'); |
40 | 39 |
41 /// Validates that the `.tar.gz` file at [path] contains the expected | 40 /// Validates that the `.tar.gz` file at [path] contains the expected |
42 /// contents. | 41 /// contents. |
43 Future validate([String parent]) { | 42 Future validate([String parent]) { |
44 throw new UnimplementedError("TODO(nweiz): implement this"); | 43 throw new UnimplementedError("TODO(nweiz): implement this"); |
45 } | 44 } |
46 | 45 |
47 Stream<List<int>> read() { | 46 Stream<List<int>> read() { |
48 return new Stream<List<int>>.fromFuture(withTempDir((tempDir) { | 47 return new Stream<List<int>>.fromFuture(withTempDir((tempDir) { |
49 return create(tempDir).then((_) => | 48 return create( |
50 readBinaryFile(path.join(tempDir, name))); | 49 tempDir).then((_) => readBinaryFile(path.join(tempDir, name))); |
51 })); | 50 })); |
52 } | 51 } |
53 } | 52 } |
OLD | NEW |