OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 name_validator; | 5 library name_validator; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import '../entrypoint.dart'; | 9 import '../entrypoint.dart'; |
10 import '../io.dart'; | 10 import '../io.dart'; |
(...skipping 13 matching lines...) Expand all Loading... | |
24 : super(entrypoint); | 24 : super(entrypoint); |
25 | 25 |
26 Future validate() { | 26 Future validate() { |
27 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); | 27 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); |
28 | 28 |
29 var libDir = join(entrypoint.root.dir, "lib"); | 29 var libDir = join(entrypoint.root.dir, "lib"); |
30 return dirExists(libDir).chain((libDirExists) { | 30 return dirExists(libDir).chain((libDirExists) { |
31 if (!libDirExists) return new Future.immediate([]); | 31 if (!libDirExists) return new Future.immediate([]); |
32 return listDir(libDir, recursive: true); | 32 return listDir(libDir, recursive: true); |
33 }).transform((files) { | 33 }).transform((files) { |
34 for (var file in files) { | 34 var libraries = files.map((file) => relativeTo(file, dirname(libDir))) |
35 file = relativeTo(file, libDir); | 35 .filter((file) { |
36 if (splitPath(file).contains("src")) continue; | 36 return !splitPath(file).contains("src") && |
37 if (path.extension(file) != '.dart') continue; | 37 path.extension(file) == '.dart'; |
38 var libName = path.basenameWithoutExtension(file); | 38 }); |
Bob Nystrom
2012/12/13 19:52:32
How about splitting this out into a separate funct
| |
39 _checkName(libName, 'The name of "$file", "$libName",'); | 39 |
40 for (var library in libraries) { | |
41 var libName = path.basenameWithoutExtension(library); | |
42 _checkName(libName, 'The name of "$library", "$libName",'); | |
43 } | |
44 | |
45 if (libraries.length == 1) { | |
46 var libName = path.basenameWithoutExtension(libraries[0]); | |
47 if (libName == entrypoint.root.name) return; | |
48 warnings.add('The name of "$libraries[0]", "$libName", should match ' | |
49 'the name of the package, "${entrypoint.root.name}".\n' | |
50 'This helps users know what library to import.'); | |
40 } | 51 } |
41 }); | 52 }); |
42 } | 53 } |
43 | 54 |
44 void _checkName(String name, String description) { | 55 void _checkName(String name, String description) { |
45 if (name == "") { | 56 if (name == "") { |
46 errors.add("$description may not be empty."); | 57 errors.add("$description may not be empty."); |
47 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 58 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
48 errors.add("$description may only contain letters, numbers, and " | 59 errors.add("$description may only contain letters, numbers, and " |
49 "underscores.\n" | 60 "underscores.\n" |
(...skipping 17 matching lines...) Expand all Loading... | |
67 builder | 78 builder |
68 ..add(source.substring(lastMatchEnd, match.start + 1)) | 79 ..add(source.substring(lastMatchEnd, match.start + 1)) |
69 ..add("_") | 80 ..add("_") |
70 ..add(match.group(1).toLowerCase()); | 81 ..add(match.group(1).toLowerCase()); |
71 lastMatchEnd = match.end; | 82 lastMatchEnd = match.end; |
72 } | 83 } |
73 builder.add(source.substring(lastMatchEnd)); | 84 builder.add(source.substring(lastMatchEnd)); |
74 return builder.toString().toLowerCase(); | 85 return builder.toString().toLowerCase(); |
75 } | 86 } |
76 } | 87 } |
OLD | NEW |