forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_out_files.dart
108 lines (97 loc) · 2.88 KB
/
opt_out_files.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env dart
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
final commentLine = RegExp(r'^///?');
final scriptLine = RegExp(r'^#\!');
final languageMarker = RegExp(r"^\s*//\s*@dart\s*=");
void main(List<String> args) {
if (args.isEmpty) {
print('Mark files passed on the command line or under a directory');
print(' passed on the command line as opted out of null safety. Does');
print(' not mark files under directories containing a pubspec.yaml file');
print(' unless the file is specified explicitly in the argument list.');
print(' Ignores files not ending in ".dart".');
print('Usage: opt_files_out.dart <files or directories>');
return;
}
for (var name in args) {
switch (FileSystemEntity.typeSync(name)) {
case FileSystemEntityType.directory:
markDirectory(Directory(name));
break;
case FileSystemEntityType.file:
markFile(File(name));
break;
default:
print("Ignoring unknown object $name");
break;
}
}
}
bool isPubSpec(FileSystemEntity entity) =>
entity is File && entity.path.endsWith("pubspec.yaml");
void markEntity(FileSystemEntity entity) {
if (entity is File) {
markFile(entity);
} else if (entity is Directory) {
markDirectory(entity);
} else {
print("Ignoring unknown object ${entity.path}");
}
}
void markDirectory(Directory dir) {
var children = dir.listSync();
if (children.any(isPubSpec)) {
print("Skipping files under ${dir.path}");
return;
}
for (var child in children) {
markEntity(child);
}
}
void markFile(File file) {
if (!file.path.endsWith(".dart")) {
return;
}
List<String> lines;
try {
lines = file.readAsLinesSync();
} catch (e) {
print("Failed to read file ${file.path}: $e");
return;
}
if (lines.any((line) => line.startsWith(languageMarker))) {
print("Skipping already marked file ${file.path}");
return;
}
var marked = markContents(lines);
try {
file.writeAsStringSync(marked);
} catch (e) {
print("Failed to write file ${file.path}: $e");
return;
}
print("Marked ${file.path}");
}
String markContents(List<String> lines) {
var buffer = StringBuffer();
var marked = false;
for (var line in lines) {
// If the file has not yet been marked, and we have reached the
// first non-comment line, insert an opt out marker.
if (!marked &&
(!commentLine.hasMatch(line)) &&
(!scriptLine.hasMatch(line))) {
buffer.write('\n// @dart = 2.9\n');
marked = true;
}
buffer.write('$line\n');
}
// In case of empty file, or file of all comments (who does that!?).
if (!marked) {
buffer.write('\n// @dart = 2.9\n');
}
return buffer.toString();
}