Skip to content

Commit 6c56cd7

Browse files
committed
initial commit
0 parents  commit 6c56cd7

24 files changed

+1106
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Files and directories created by pub
2+
.dart_tool/
3+
.packages
4+
# Remove the following pattern if you wish to check in your lock file
5+
pubspec.lock
6+
7+
# Conventional directory for build outputs
8+
build/
9+
10+
# Directory created by dartdoc
11+
doc/api/
12+
13+
*.iml
14+
.idea/

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version, created by Stagehand

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
A library for Dart developers.
2+
3+
Created from templates made available by Stagehand under a BSD-style
4+
[license](https://github.com/dart-lang/stagehand/blob/master/LICENSE).
5+
6+
## Usage
7+
8+
A simple usage example:
9+
10+
```dart
11+
import 'package:runtime/runtime.dart';
12+
13+
main() {
14+
var awesome = new Awesome();
15+
}
16+
```
17+
18+
## Features and bugs
19+
20+
Please file feature requests and bugs at the [issue tracker][tracker].
21+
22+
[tracker]: http://example.com/issues/replaceme

analysis_options.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Defines a default set of lint rules enforced for
2+
# projects at Google. For details and rationale,
3+
# see https://github.com/dart-lang/pedantic#enabled-lints.
4+
include: package:pedantic/analysis_options.yaml
5+
6+
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
7+
# Uncomment to specify additional rules.
8+
# linter:
9+
# rules:
10+
# - camel_case_types
11+
12+
analyzer:
13+
# exclude:
14+
# - path/to/excluded/files/**

lib/runtime.dart

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
library runtime;
2+
3+
import 'dart:io';
4+
5+
import 'package:runtime/src/compiler.dart';
6+
import 'package:runtime/src/mirror_context.dart';
7+
8+
export 'src/analyzer.dart';
9+
export 'package:runtime/src/context.dart';
10+
export 'src/build.dart';
11+
export 'src/compiler.dart';
12+
export 'src/file_system.dart';
13+
export 'src/generator.dart';
14+
export 'src/mirror_context.dart';
15+
16+
/// Compiler for the runtime package itself.
17+
///
18+
/// Removes dart:mirror from a replica of this package, and adds
19+
/// a generated runtime to the replica's pubspec.
20+
class RuntimePackageCompiler extends Compiler {
21+
@override
22+
Map<String, dynamic> compile(MirrorContext context) => {};
23+
24+
@override
25+
void deflectPackage(Directory destinationDirectory) {
26+
final libraryFile = File.fromUri(
27+
destinationDirectory.uri.resolve("lib/").resolve("runtime.dart"));
28+
libraryFile
29+
.writeAsStringSync("library runtime;\nexport 'src/context.dart';");
30+
31+
final contextFile = File.fromUri(destinationDirectory.uri
32+
.resolve("lib/")
33+
.resolve("src/")
34+
.resolve("context.dart"));
35+
final contextFileContents = contextFile.readAsStringSync().replaceFirst(
36+
"import 'package:runtime/src/mirror_context.dart' as context;",
37+
"import 'package:generated_runtime/generated_runtime.dart' as context;");
38+
contextFile.writeAsStringSync(contextFileContents);
39+
40+
final pubspecFile = File.fromUri(destinationDirectory.uri.resolve("pubspec.yaml"));
41+
final pubspecContents = pubspecFile.readAsStringSync().replaceFirst("\ndependencies:",
42+
"\ndependencies:\n generated_runtime:\n path: ../../generated_runtime/");
43+
pubspecFile.writeAsStringSync(pubspecContents);
44+
}
45+
}

lib/src/analyzer.dart

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
2+
import 'package:analyzer/dart/ast/ast.dart';
3+
import 'package:analyzer/file_system/physical_file_system.dart';
4+
5+
class CodeAnalyzer {
6+
CodeAnalyzer(this.uri) {
7+
if (!uri.isAbsolute) {
8+
throw ArgumentError("'uri' must be absolute for CodeAnalyzer");
9+
}
10+
11+
contexts = AnalysisContextCollection(includedPaths: [path]);
12+
if (contexts.contexts.isEmpty) {
13+
throw ArgumentError("no analysis context found for path '${path}'");
14+
}
15+
}
16+
17+
String get path {
18+
return _getPath(uri);
19+
}
20+
21+
final Uri uri;
22+
23+
AnalysisContextCollection contexts;
24+
25+
ClassDeclaration getClassFromFile(String className, Uri fileUri) {
26+
return _getFileAstRoot(fileUri)
27+
.declarations
28+
.whereType<ClassDeclaration>()
29+
.firstWhere((c) => c.name.name == className, orElse: () => null);
30+
}
31+
32+
List<ClassDeclaration> getSubclassesFromFile(
33+
String superclassName, Uri fileUri) {
34+
return _getFileAstRoot(fileUri)
35+
.declarations
36+
.whereType<ClassDeclaration>()
37+
.where((c) => c.extendsClause.superclass.name.name == superclassName)
38+
.toList();
39+
}
40+
41+
CompilationUnit _getFileAstRoot(Uri fileUri) {
42+
final path = _getPath(fileUri);
43+
44+
final unit = contexts.contextFor(path).currentSession.getParsedUnit(path);
45+
46+
if (unit.errors.isNotEmpty) {
47+
throw StateError(
48+
"Project file '${path}' could not be analysed for the following reasons:\n\t${unit.errors.join("\n\t")}");
49+
}
50+
51+
return unit.unit;
52+
}
53+
54+
static String _getPath(Uri inputUri) {
55+
return PhysicalResourceProvider.INSTANCE.pathContext.normalize(
56+
PhysicalResourceProvider.INSTANCE.pathContext.fromUri(inputUri));
57+
}
58+
}

0 commit comments

Comments
 (0)