OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'package:status_file/status_file.dart'; | 7 import 'package:status_file/status_file.dart'; |
8 | 8 |
9 import 'io.dart'; | 9 import 'io.dart'; |
10 import 'log.dart'; | 10 import 'log.dart'; |
11 | 11 |
12 // TODO(rnystrom): This file is kind of a hack. The problem is that a parsed | 12 // TODO(rnystrom): This file is kind of a hack. The problem is that a parsed |
13 // StatusFile doesn't contain all of the original information in the source | 13 // StatusFile doesn't contain all of the original information in the source |
14 // text. Comments and whitespace are lost. That means a StatusFile itself | 14 // text. Comments and whitespace are lost. That means a StatusFile itself |
15 // couldn't be modified and saved back to disc. | 15 // couldn't be modified and saved back to disc. |
16 // | 16 // |
17 // Instead, this wraps a StatusFile and also lets you modify entire lines in | 17 // Instead, this wraps a StatusFile and also lets you modify entire lines in |
18 // it, including their comments. Ideally, StatusFile would include parsed | 18 // it, including their comments. Ideally, StatusFile would include parsed |
19 // comments and we would canonicalize other whitespace so that you could | 19 // comments and we would canonicalize other whitespace so that you could |
20 // modify a StatusFile object then save it back to disc. | 20 // modify a StatusFile object then save it back to disc. |
21 class EditableStatusFile { | 21 class EditableStatusFile { |
22 final String path; | 22 final String path; |
23 | 23 |
24 StatusFile _statusFile; | 24 StatusFile _statusFile; |
25 StatusFile get statusFile { | 25 StatusFile get statusFile { |
26 if (_statusFile == null) { | 26 if (_statusFile == null) { |
27 _statusFile = new StatusFile.read(path); | 27 if (new File(path).existsSync()) { |
28 _statusFile = new StatusFile.read(path); | |
29 } else { | |
30 _statusFile = new StatusFile(path); | |
31 } | |
28 } | 32 } |
29 | 33 |
30 return _statusFile; | 34 return _statusFile; |
31 } | 35 } |
32 | 36 |
33 List<String> _lines; | 37 List<String> _lines; |
34 | 38 |
35 EditableStatusFile(this.path); | 39 EditableStatusFile(this.path); |
36 | 40 |
37 /// Gets the line at the given one-based index. | 41 /// Gets the line at the given one-based index. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 } | 89 } |
86 | 90 |
87 _ensureLines(); | 91 _ensureLines(); |
88 _lines.add(""); | 92 _lines.add(""); |
89 | 93 |
90 _lines.add(header); | 94 _lines.add(header); |
91 _lines.addAll(entries); | 95 _lines.addAll(entries); |
92 _save(); | 96 _save(); |
93 } | 97 } |
94 | 98 |
95 void _ensureLines() { | 99 void _ensureLines() { |
jcollins
2017/08/04 16:55:56
optional: replace this and _lines with a getter fo
Bob Nystrom
2017/08/04 17:33:01
Good idea, done.
| |
96 if (_lines == null) { | 100 if (_lines == null) { |
97 _lines = new File(path).readAsLinesSync(); | 101 var file = new File(path); |
102 if (file.existsSync()) { | |
103 _lines = file.readAsLinesSync(); | |
104 } else { | |
105 _lines = [ | |
106 "# Copyright (c) 2017, the Dart project authors. Please see the " | |
107 "AUTHORS file", | |
108 "# for details. All rights reserved. Use of this source code is " | |
109 "governed by a", | |
110 "# BSD-style license that can be found in the LICENSE file." | |
111 ]; | |
112 } | |
98 } | 113 } |
99 } | 114 } |
100 | 115 |
101 void _save() { | 116 void _save() { |
117 if (dryRun) { | |
118 print("Save ${bold(path)}"); | |
119 return; | |
120 } | |
121 | |
102 new File(path).writeAsStringSync(_lines.join("\n") + "\n"); | 122 new File(path).writeAsStringSync(_lines.join("\n") + "\n"); |
103 | 123 |
104 // It needs to be reparsed now since the lines have changed. | 124 // It needs to be reparsed now since the lines have changed. |
105 // TODO(rnystrom): This is kind of hacky and slow, but it gets the job done. | 125 // TODO(rnystrom): This is kind of hacky and slow, but it gets the job done. |
106 _statusFile = null; | 126 _statusFile = null; |
107 _lines = null; | 127 _lines = null; |
108 } | 128 } |
109 } | 129 } |
OLD | NEW |