-
Notifications
You must be signed in to change notification settings - Fork 171
/
io.dart
137 lines (115 loc) · 4.02 KB
/
io.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2015, 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.
library linter.src.io;
import 'dart:io';
import 'package:glob/glob.dart';
import 'package:linter/src/pub.dart';
import 'package:linter/src/util.dart';
import 'package:path/path.dart' as p;
final dartMatcher = new Glob('**.dart');
/// Shared IO sink for standard error reporting.
/// Visible for testing
IOSink errorSink = stderr;
/// Shared IO sink for standard out reporting.
/// Visible for testing
IOSink outSink = stdout;
/// Cached project package.
String _projectPackageName;
/// Cached project root.
String _projectRoot;
/// Collect all lintable files, recursively, under this [path] root, ignoring
/// links.
Iterable<File> collectFiles(String path) {
List<File> files = [];
var file = new File(path);
if (file.existsSync()) {
files.add(file);
} else {
var directory = new Directory(path);
if (directory.existsSync()) {
for (var entry
in directory.listSync(recursive: true, followLinks: false)) {
var relative = p.relative(entry.path, from: directory.path);
if (isLintable(entry) && !isInHiddenDir(relative)) {
files.add(entry);
}
}
}
}
return files;
}
/// Calculates the project package name based on a pubspec found relative to
/// the given path. In case a pubspec is not found, default to the project
/// root directory name.
/// Note that the name will be calculated just once. After the first call,
/// the argument is ignored and the cached result returned.
String getProjectPackageName(String path) {
if (_projectPackageName == null) {
_projectPackageName = _calculateProjectPackageName(path);
}
return _projectPackageName;
}
/// Calculates the absolute path to the project root based on the given path
/// (which is assumed to be a file path in the project).
/// Note that the root will be calculated just once. After the first call,
/// the argument is ignored and the cached result returned.
String getProjectRoot(String path) {
if (_projectRoot == null) {
_projectRoot = _calculateProjectRoot(path);
}
return _projectRoot;
}
/// Returns `true` if this [entry] is a Dart file.
bool isDartFile(FileSystemEntity entry) => isDartFileName(entry.path);
/// Returns `true` if this relative path is a hidden directory.
bool isInHiddenDir(String relative) =>
p.split(relative).any((part) => part.startsWith("."));
/// Returns `true` if this relative path is a hidden directory.
bool isLintable(FileSystemEntity file) =>
file is File && (isDartFile(file) || isPubspecFile(file));
/// Returns `true` if this [entry] is a pubspec file.
bool isPubspecFile(FileSystemEntity entry) =>
isPubspecFileName(p.basename(entry.path));
/// Synchronously read the contents of the file at the given [path] as a string.
String readFile(String path) => new File(path).readAsStringSync();
String _calculateProjectPackageName(String path) {
var pubspec = _findPubspecFileRelativeTo(path);
if (pubspec != null) {
var spec =
new Pubspec.parse(pubspec.readAsStringSync(), sourceUrl: pubspec.path);
var nameEntry = spec.name;
if (nameEntry != null) {
var value = nameEntry.value.text;
if (value != null) {
return value;
}
}
}
// Fall back
return p.basename(Directory.current.path);
}
String _calculateProjectRoot(String path) {
var pubspec = _findPubspecFileRelativeTo(path);
var dir = pubspec != null ? pubspec.parent : Directory.current;
return dir.absolute.path;
}
File _findPubspecFileRelativeTo(String path) {
var file = new File(path);
if (file.existsSync()) {
if (isPubspecFile(file)) {
return file;
}
} else {
var directory = new Directory(path);
if (directory.existsSync()) {
for (var file
in directory.listSync(recursive: true, followLinks: false)) {
if (isPubspecFile(file)) {
return file;
}
}
}
}
return null;
}