Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: tools/migration/lib/src/editable_status_file.dart

Issue 2989033002: Migrate status file entries when migrating files. Yay! (Closed)
Patch Set: Remove old TODO. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/migration/bin/migrate_batch.dart ('k') | tools/migration/lib/src/fork.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:io';
6
7 import 'package:status_file/status_file.dart';
8
9 import 'io.dart';
10 import 'log.dart';
11
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
14 // text. Comments and whitespace are lost. That means a StatusFile itself
15 // couldn't be modified and saved back to disc.
16 //
17 // Instead, this wraps a StatusFile and also lets you modify entire lines in
18 // it, including their comments. Ideally, StatusFile would include parsed
19 // comments and we would canonicalize other whitespace so that you could
20 // modify a StatusFile object then save it back to disc.
21 class EditableStatusFile {
22 final String path;
23
24 StatusFile _statusFile;
25 StatusFile get statusFile {
26 if (_statusFile == null) {
27 _statusFile = new StatusFile.read(path);
28 }
29
30 return _statusFile;
31 }
32
33 List<String> _lines;
34
35 EditableStatusFile(this.path);
36
37 /// Gets the line at the given one-based index.
38 String lineAt(int line) {
39 _ensureLines();
40 return _lines[line - 1];
41 }
42
43 /// Delete the numbered [lines] from the file.
44 void delete(List<int> lines) {
45 if (lines.isEmpty) return;
46
47 if (dryRun) {
48 print("Delete lines ${lines.join(', ')} from $path.");
49 return;
50 }
51
52 _ensureLines();
53
54 var deleted = 0;
55 for (var line in lines) {
56 // Adjust index because previous lines have already been removed, shifting
57 // the subsequent line numbers.
58 _lines.removeAt(line - deleted);
59 deleted++;
60 }
61
62 _save();
63 }
64
65 /// Insert [entries] at [line] in the file.
66 void insert(int line, List<String> entries) {
67 if (dryRun) {
68 print("Insert ${bold(path)}, insert at line $line:");
69 entries.forEach(print);
70 return;
71 }
72
73 _ensureLines();
74 _lines.insertAll(line, entries);
75 _save();
76 }
77
78 /// Adds [header] followed by [entries] to the end of the file.
79 void append(String header, List<String> entries) {
80 if (dryRun) {
81 print("To ${bold(path)}, append:");
82 print(header);
83 entries.forEach(print);
84 return;
85 }
86
87 _ensureLines();
88 _lines.add("");
89
90 _lines.add(header);
91 _lines.addAll(entries);
92 _save();
93 }
94
95 void _ensureLines() {
96 if (_lines == null) {
97 _lines = new File(path).readAsLinesSync();
98 }
99 }
100
101 void _save() {
102 new File(path).writeAsStringSync(_lines.join("\n") + "\n");
103
104 // 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.
106 _statusFile = null;
107 _lines = null;
108 }
109 }
OLDNEW
« no previous file with comments | « tools/migration/bin/migrate_batch.dart ('k') | tools/migration/lib/src/fork.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698