Skip to content
This repository has been archived by the owner on Jan 20, 2018. It is now read-only.

Commit

Permalink
order by relative url, and other code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemac53 committed Jan 14, 2015
1 parent ed0be89 commit 2e33a7d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
25 changes: 17 additions & 8 deletions lib/src/mirror_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library static_init.mirror_loader;
import 'dart:async';
import 'dart:collection' show Queue;
import 'dart:mirrors';
import 'package:path/path.dart' as path;
import 'package:static_init/static_init.dart';

// Crawls a library and all its dependencies for `StaticInitializer`
Expand Down Expand Up @@ -84,11 +85,19 @@ class StaticInitializationCrawler {

Iterable<LibraryDependencyMirror>
_sortedLibraryDependencies(LibraryMirror lib) =>
lib.libraryDependencies
..sort((a, b) => _targetLibraryUri(a).compareTo(_targetLibraryUri(b)));
new List.from(lib.libraryDependencies)
..sort((a, b) =>
_relativeLibraryUri(a).compareTo(_relativeLibraryUri(b)));

String _relativeLibraryUri(LibraryDependencyMirror lib) {
if (lib.targetLibrary.uri.scheme == 'file'
&& lib.sourceLibrary.uri.scheme == 'file') {
return path.relative(lib.targetLibrary.uri.path,
from: path.dirname(lib.sourceLibrary.uri.path));
}
return lib.targetLibrary.uri.toString();
}

String _targetLibraryUri(LibraryDependencyMirror lib) =>
lib.targetLibrary.uri.toString();

Iterable<DeclarationMirror> _sortedLibraryDeclarations(LibraryMirror lib) =>
lib.declarations.values
Expand Down Expand Up @@ -136,6 +145,7 @@ class StaticInitializationCrawler {
annotatedValue = declaration.reflectedType;
} else if (declaration is MethodMirror) {
if (declaration.owner is! LibraryMirror) {
// TODO(jakemac): Support static class methods.
throw _TOP_LEVEL_FUNCTIONS_ONLY;
}
annotatedValue = (declaration.owner as ObjectMirror)
Expand All @@ -150,17 +160,16 @@ class StaticInitializationCrawler {
;
}

// Filter function that returns true only if `meta` is a `StaticInitializer`,
// it passes the `typeFilter` or `customFilter` if they exist, and it has not
// yet been seen.
bool _filterMetadata(DeclarationMirror declaration, InstanceMirror meta) {
// We only care about StaticInitializer annotations
if (meta.reflectee is! StaticInitializer) return false;
// Respect the typeFilter
if (typeFilter != null &&
!typeFilter.any((t) => meta.reflectee.runtimeType == t)) {
return false;
}
// Respect the customFilter
if (customFilter != null && !customFilter(meta.reflectee)) return false;
// Filter out already seen annotations
if (!_annotationsFound.containsKey(declaration)) {
_annotationsFound[declaration] = new Set<InstanceMirror>();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/static_initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ part of static_init;
/// @Print('hello world!')
/// class Foo {}
///
/// Call run() from your main and this will print 'Foo says `hello world!`'
/// Call [run] from your main and this will print 'Foo says `hello world!`'
///
abstract class StaticInitializer<T> {
dynamic initialize(T target);
Expand Down
5 changes: 2 additions & 3 deletions lib/static_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import 'dart:async';
part 'src/init_method.dart';
part 'src/static_initializer.dart';

/// Top level function which crawls the dependency graph and runs initializers.
/// If `typeFilter` is supplied then only those types of annotations will be
/// parsed.
/// Executes initializers in every library discovered by crawling Dart imports,
/// exports, and part directives.
Future run({List<Type> typeFilter, InitializerFilter customFilter}) =>
new StaticInitializationCrawler(typeFilter, customFilter).run();
2 changes: 1 addition & 1 deletion test/static_init_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
@initializeTracker
library static_init.static_init_test;

import 'bar.dart';
import 'foo.dart';
import 'bar.dart';
import 'initialize_tracker.dart';
import 'package:static_init/static_init.dart';
import 'package:unittest/unittest.dart';
Expand Down

0 comments on commit 2e33a7d

Please sign in to comment.