Skip to content

Commit

Permalink
Merge pull request #3 from AlexVincent525/master
Browse files Browse the repository at this point in the history
Correct typos and formatted codes.
  • Loading branch information
zmtzawqlp committed Nov 12, 2019
2 parents 6c8ed41 + e346c64 commit 002fcaa
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 156 deletions.
30 changes: 15 additions & 15 deletions README.md
Expand Up @@ -101,11 +101,10 @@ add dart bin into to your `$PATH`.

#### Execute command

you can cd to your project and exectue command.
`ff_annotation_route`
you can cd to your project and execute command. `ff_annotation_route`

or you can exectue command with your project path
`ff_annotation_route path=`
or you can execute command with your project path `ff_annotation_route
path=`

#### Command Parameter

Expand All @@ -120,10 +119,10 @@ use as parameter=xxx, and use space to split them.

### Main.dart

- if you exectue command with mode=1, FFNavigatorObserver/FFRouteSettings will generate in xxx_route_helper.dart
- if you execute command with mode=1, FFNavigatorObserver/FFRouteSettings will generate in xxx_route_helper.dart
they help you to track page or change status bar state.

- if you exectue command with mode=1, FFTransparentPageRoute will generate in xxx_route_helper.dart
- if you execute command with mode=1, FFTransparentPageRoute will generate in xxx_route_helper.dart
it helps you to push a transparent page route.

```dart
Expand Down Expand Up @@ -209,13 +208,14 @@ Widget build(BuildContext context) {

and arguments should be Map<String,dynamic>
```dart
Navigator.pushNamed(context, "fluttercandies://picswiper",
arguments: {
"index": index,
"pics": listSourceRepository
.map<PicSwiperItem>(
(f) => PicSwiperItem(f.imageUrl, des: f.title))
.toList(),
});
Navigator.pushNamed(
context,
"fluttercandies://picswiper",
arguments: {
"index": index,
"pics": listSourceRepository
.map<PicSwiperItem>((f) => PicSwiperItem(f.imageUrl, des: f.title))
.toList(),
},
);
```
23 changes: 13 additions & 10 deletions lib/ff_annotation_route.dart
Expand Up @@ -5,15 +5,17 @@ export 'src/ff_route.dart';
import 'src/package_graph.dart';
import 'src/route_generator.dart';

void generate(List<PackageNode> annotationPackages,
{bool generateRouteNames = false,
int mode = 0,
bool routeSettingsNoArguments = false,
bool rootAnnotationRouteEnable = true}) {
void generate(
List<PackageNode> annotationPackages, {
bool generateRouteNames = false,
int mode = 0,
bool routeSettingsNoArguments = false,
bool rootAnnotationRouteEnable = true,
}) {
RouteGenerator root;
List<RouteGenerator> nodes = List<RouteGenerator>();
for (var annotationPackage in annotationPackages) {
var routeGenerator = RouteGenerator(annotationPackage);
for (final annotationPackage in annotationPackages) {
final routeGenerator = RouteGenerator(annotationPackage);
if (routeGenerator.isRoot) {
root = routeGenerator;
} else {
Expand All @@ -34,7 +36,8 @@ void generate(List<PackageNode> annotationPackages,
generateRouteNames: generateRouteNames,
);
root?.generateHelperFile(
nodes: nodes,
routeSettingsNoArguments: routeSettingsNoArguments,
mode: mode);
nodes: nodes,
routeSettingsNoArguments: routeSettingsNoArguments,
mode: mode,
);
}
63 changes: 39 additions & 24 deletions lib/src/ast.dart
Expand Up @@ -27,37 +27,46 @@ import 'package:path/path.dart' as pathos;
///
/// If [parseFunctionBodies] is [false] then only function signatures will be
/// parsed.
CompilationUnit parseCompilationUnit(String contents,
{String name,
bool suppressErrors = false,
bool parseFunctionBodies = true,
FeatureSet featureSet}) {
CompilationUnit parseCompilationUnit(
String contents, {
String name,
bool suppressErrors = false,
bool parseFunctionBodies = true,
FeatureSet featureSet,
}) {
featureSet ??= FeatureSet.fromEnableFlags([]);
Source source = StringSource(contents, name);
return _parseSource(contents, source, featureSet,
suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies);
}

CompilationUnit parseDartFile(String path,
{bool suppressErrors = false,
bool parseFunctionBodies = true,
FeatureSet featureSet}) {
CompilationUnit parseDartFile(
String path, {
bool suppressErrors = false,
bool parseFunctionBodies = true,
FeatureSet featureSet,
}) {
featureSet ??= FeatureSet.fromEnableFlags([]);
String contents = io.File(path).readAsStringSync();
var sourceFactory =
final contents = io.File(path).readAsStringSync();
final sourceFactory =
SourceFactory([ResourceUriResolver(PhysicalResourceProvider.INSTANCE)]);

var absolutePath = pathos.absolute(path);
var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString());
final absolutePath = pathos.absolute(path);
final source = sourceFactory.forUri(pathos.toUri(absolutePath).toString());
if (source == null) {
throw ArgumentError("Can't get source for path $path");
}
if (!source.exists()) {
throw ArgumentError("Source $source doesn't exist");
}

return _parseSource(contents, source, featureSet,
suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies);
return _parseSource(
contents,
source,
featureSet,
suppressErrors: suppressErrors,
parseFunctionBodies: parseFunctionBodies,
);
}

/// Parses a Dart file into an AST.
Expand All @@ -68,19 +77,25 @@ CompilationUnit parseDartFile(String path,
/// If [parseFunctionBodies] is [false] then only function signatures will be
/// parsed.
CompilationUnit _parseSource(
String contents, Source source, FeatureSet featureSet,
{bool suppressErrors = false, bool parseFunctionBodies = true}) {
var reader = CharSequenceReader(contents);
var errorCollector = _ErrorCollector();
var scanner = Scanner(source, reader, errorCollector)
String contents,
Source source,
FeatureSet featureSet, {
bool suppressErrors = false,
bool parseFunctionBodies = true,
}) {
final reader = CharSequenceReader(contents);
final errorCollector = _ErrorCollector();
final scanner = Scanner(source, reader, errorCollector)
..configureFeatures(featureSet);
var token = scanner.tokenize();
var parser = Parser(source, errorCollector, featureSet: featureSet)
final token = scanner.tokenize();
final parser = Parser(source, errorCollector, featureSet: featureSet)
..parseFunctionBodies = parseFunctionBodies;
var unit = parser.parseCompilationUnit(token)
final unit = parser.parseCompilationUnit(token)
..lineInfo = LineInfo(scanner.lineStarts);

if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group;
if (errorCollector.hasErrors && !suppressErrors) {
throw errorCollector.group;
}

return unit;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/src/ff_route.dart
@@ -1,6 +1,5 @@
/// annotation to generate route
///
///
/// Annotation to generate route
class FFRoute {
/// The name of the route (e.g., "/settings").
///
Expand Down
1 change: 1 addition & 0 deletions lib/src/file_info.dart
Expand Up @@ -4,5 +4,6 @@ class FileInfo {
final String packageName;
final String export;
final List<RouteInfo> routes = List<RouteInfo>();

FileInfo({this.packageName, this.export});
}
69 changes: 37 additions & 32 deletions lib/src/package_graph.dart
Expand Up @@ -38,15 +38,15 @@ class PackageGraph {
factory PackageGraph.fromRoot(PackageNode root) {
final allPackages = <String, PackageNode>{root.name: root};

void addDeps(PackageNode package) {
for (var dep in package.dependencies) {
void addDependency(PackageNode package) {
for (final dep in package.dependencies) {
if (allPackages.containsKey(dep.name)) continue;
allPackages[dep.name] = dep;
addDeps(dep);
addDependency(dep);
}
}

addDeps(root);
addDependency(root);

return PackageGraph._(root, allPackages);
}
Expand All @@ -71,18 +71,24 @@ class PackageGraph {

final nodes = <String, PackageNode>{};
final rootNode = PackageNode(
rootPackageName, packagePath, DependencyType.path,
isRoot: true);
rootPackageName,
packagePath,
DependencyType.path,
isRoot: true,
);
nodes[rootPackageName] = rootNode;
for (final packageName in packageLocations.keys) {
if (packageName == rootPackageName) continue;
nodes[packageName] = PackageNode(packageName,
packageLocations[packageName], dependencyTypes[packageName],
isRoot: false);
nodes[packageName] = PackageNode(
packageName,
packageLocations[packageName],
dependencyTypes[packageName],
isRoot: false,
);
}

rootNode.dependencies
.addAll(_depsFromYaml(rootPubspec, isRoot: true).map((n) => nodes[n]));
.addAll(_dependenciesFromYaml(rootPubspec, isRoot: true).map((n) => nodes[n]));

final packageDependencies = _parsePackageDependencies(packageLocations);
for (final packageName in packageDependencies.keys) {
Expand All @@ -102,8 +108,8 @@ class PackageGraph {

@override
String toString() {
var buffer = StringBuffer();
for (var package in allPackages.values) {
StringBuffer buffer = StringBuffer();
for (final package in allPackages.values) {
buffer.writeln('$package');
}
return buffer.toString();
Expand All @@ -116,15 +122,13 @@ class PackageNode {
final String name;

/// The type of dependency being used to pull in this package.
///
/// May be `null`.
final DependencyType dependencyType;

/// All the packages that this package directly depends on.
final List<PackageNode> dependencies = [];

/// The absolute path of the current version of this package.
///
/// Paths are platform dependent.
final String path;

Expand All @@ -151,23 +155,23 @@ class PackageNode {
/// Parse the `.packages` file and return a Map from package name to the file
/// location for that package.
Map<String, String> _parsePackageLocations(String rootPackagePath) {
var packagesFile = File(p.join(rootPackagePath, '.packages'));
final packagesFile = File(p.join(rootPackagePath, '.packages'));
if (!packagesFile.existsSync()) {
throw StateError('Unable to generate package graph, no `.packages` found. '
'This program must be ran from the root directory of your package.');
}
var packageLocations = <String, String>{};
Map<String, String> packageLocations = <String, String>{};
for (final line in packagesFile.readAsLinesSync().skip(1)) {
var firstColon = line.indexOf(':');
var name = line.substring(0, firstColon);
final firstColon = line.indexOf(':');
final name = line.substring(0, firstColon);
assert(line.endsWith('lib/'));
// Start after package_name:, and strip out trailing 'lib/'.
var uriString = line.substring(firstColon + 1, line.length - 4);
String uriString = line.substring(firstColon + 1, line.length - 4);
// Strip the trailing slash, if present.
if (uriString.endsWith('/')) {
uriString = uriString.substring(0, uriString.length - 1);
}
var uri = Uri.tryParse(uriString) ?? Uri.file(uriString);
Uri uri = Uri.tryParse(uriString) ?? Uri.file(uriString);
if (!uri.isAbsolute) {
uri = p.toUri(p.join(rootPackagePath, uri.path));
}
Expand Down Expand Up @@ -207,7 +211,7 @@ DependencyType _dependencyTypeFromSource(String source) {
return DependencyType.hosted;
case 'path':
return DependencyType.path;
case 'sdk': // Until Flutter supports another type, assum same as path.
case 'sdk': // Until Flutter supports another type, assume same as path.
return DependencyType.sdk;
}
throw ArgumentError('Unable to determine dependency type:\n$source');
Expand All @@ -221,28 +225,29 @@ Map<String, Set<String>> _parsePackageDependencies(
final dependencies = <String, Set<String>>{};
for (final packageName in packageLocations.keys) {
final pubspec = pubspecForPath(packageLocations[packageName]);
dependencies[packageName] = _depsFromYaml(pubspec);
dependencies[packageName] = _dependenciesFromYaml(pubspec);
}
return dependencies;
}

/// Gets the deps from a yaml file, taking into account dependency_overrides.
Set<String> _depsFromYaml(YamlMap yaml, {bool isRoot = false}) {
var deps = Set<String>()..addAll(_stringKeys(yaml['dependencies'] as Map));
//if (isRoot) {
deps..addAll(_stringKeys(yaml['dev_dependencies'] as Map));
deps..addAll(_stringKeys(yaml['dependency_overrides'] as Map));
//}
return deps;
/// Gets the dependencies from a yaml file, taking into account
/// dependency_overrides.
Set<String> _dependenciesFromYaml(YamlMap yaml, {bool isRoot = false}) {
final dependencies = Set<String>()..addAll(_stringKeys(yaml['dependencies'] as Map));
// if (isRoot) {
dependencies..addAll(_stringKeys(yaml['dev_dependencies'] as Map));
dependencies..addAll(_stringKeys(yaml['dependency_overrides'] as Map));
// }
return dependencies;
}

Iterable<String> _stringKeys(Map m) =>
m == null ? const [] : m.keys.cast<String>();

/// Should point to the top level directory for the package.
YamlMap pubspecForPath(String absolutePath) {
var pubspecPath = p.join(absolutePath, 'pubspec.yaml');
var pubspec = File(pubspecPath);
final pubspecPath = p.join(absolutePath, 'pubspec.yaml');
final pubspec = File(pubspecPath);
if (!pubspec.existsSync()) {
throw StateError(
'Unable to generate package graph, no `$pubspecPath` found.');
Expand Down

0 comments on commit 002fcaa

Please sign in to comment.