| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:pathos/path.dart' as path; | 10 import 'package:pathos/path.dart' as path; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 "new", "null", "return", "super", "switch", "this", "throw", "true", "try", | 21 "new", "null", "return", "super", "switch", "this", "throw", "true", "try", |
| 22 "var", "void", "while", "with" | 22 "var", "void", "while", "with" |
| 23 ]; | 23 ]; |
| 24 | 24 |
| 25 /// A validator that validates the name of the package and its libraries. | 25 /// A validator that validates the name of the package and its libraries. |
| 26 class NameValidator extends Validator { | 26 class NameValidator extends Validator { |
| 27 NameValidator(Entrypoint entrypoint) | 27 NameValidator(Entrypoint entrypoint) |
| 28 : super(entrypoint); | 28 : super(entrypoint); |
| 29 | 29 |
| 30 Future validate() { | 30 Future validate() { |
| 31 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"', | 31 return new Future.of(() { |
| 32 isPackage: true); | 32 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"', |
| 33 isPackage: true); |
| 33 | 34 |
| 34 return _libraries.then((libraries) { | 35 var libraries = _libraries; |
| 35 for (var library in libraries) { | 36 for (var library in libraries) { |
| 36 var libName = path.basenameWithoutExtension(library); | 37 var libName = path.basenameWithoutExtension(library); |
| 37 _checkName(libName, 'The name of "$library", "$libName",', | 38 _checkName(libName, 'The name of "$library", "$libName",', |
| 38 isPackage: false); | 39 isPackage: false); |
| 39 } | 40 } |
| 40 | 41 |
| 41 if (libraries.length == 1) { | 42 if (libraries.length == 1) { |
| 42 var libName = path.basenameWithoutExtension(libraries[0]); | 43 var libName = path.basenameWithoutExtension(libraries[0]); |
| 43 if (libName == entrypoint.root.name) return; | 44 if (libName == entrypoint.root.name) return; |
| 44 warnings.add('The name of "${libraries[0]}", "$libName", should match ' | 45 warnings.add('The name of "${libraries[0]}", "$libName", should match ' |
| 45 'the name of the package, "${entrypoint.root.name}".\n' | 46 'the name of the package, "${entrypoint.root.name}".\n' |
| 46 'This helps users know what library to import.'); | 47 'This helps users know what library to import.'); |
| 47 } | 48 } |
| 48 }); | 49 }); |
| 49 } | 50 } |
| 50 | 51 |
| 51 /// Returns a list of all libraries in the current package as paths relative | 52 /// Returns a list of all libraries in the current package as paths relative |
| 52 /// to the package's root directory. | 53 /// to the package's root directory. |
| 53 Future<List<String>> get _libraries { | 54 List<String> get _libraries { |
| 54 var libDir = path.join(entrypoint.root.dir, "lib"); | 55 var libDir = path.join(entrypoint.root.dir, "lib"); |
| 55 return defer(() { | 56 if (!dirExists(libDir)) return []; |
| 56 if (!dirExists(libDir)) return []; | 57 return listDir(libDir, recursive: true) |
| 57 return listDir(libDir, recursive: true); | 58 .map((file) => path.relative(file, from: path.dirname(libDir))) |
| 58 }).then((files) { | 59 .where((file) => !path.split(file).contains("src") && |
| 59 return files | 60 path.extension(file) == '.dart') |
| 60 .map((file) => path.relative(file, from: path.dirname(libDir))) | 61 .toList(); |
| 61 .where((file) => !path.split(file).contains("src") && | |
| 62 path.extension(file) == '.dart') | |
| 63 .toList(); | |
| 64 }); | |
| 65 } | 62 } |
| 66 | 63 |
| 67 void _checkName(String name, String description, {bool isPackage}) { | 64 void _checkName(String name, String description, {bool isPackage}) { |
| 68 // Packages names are more stringent than libraries. | 65 // Packages names are more stringent than libraries. |
| 69 var messages = isPackage ? errors : warnings; | 66 var messages = isPackage ? errors : warnings; |
| 70 | 67 |
| 71 if (name == "") { | 68 if (name == "") { |
| 72 errors.add("$description may not be empty."); | 69 errors.add("$description may not be empty."); |
| 73 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 70 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
| 74 messages.add("$description may only contain letters, numbers, and " | 71 messages.add("$description may only contain letters, numbers, and " |
| (...skipping 18 matching lines...) Expand all Loading... |
| 93 builder | 90 builder |
| 94 ..write(source.substring(lastMatchEnd, match.start + 1)) | 91 ..write(source.substring(lastMatchEnd, match.start + 1)) |
| 95 ..write("_") | 92 ..write("_") |
| 96 ..write(match.group(1).toLowerCase()); | 93 ..write(match.group(1).toLowerCase()); |
| 97 lastMatchEnd = match.end; | 94 lastMatchEnd = match.end; |
| 98 } | 95 } |
| 99 builder.write(source.substring(lastMatchEnd)); | 96 builder.write(source.substring(lastMatchEnd)); |
| 100 return builder.toString().toLowerCase(); | 97 return builder.toString().toLowerCase(); |
| 101 } | 98 } |
| 102 } | 99 } |
| OLD | NEW |