Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: pkg/docgen/lib/src/models/library.dart

Issue 242363004: pkg/docgen: moved model classes into minilibs (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cl nits Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 library docgen.models.library;
6
7 import 'dart:io';
8
9 import 'package:markdown/markdown.dart' as markdown;
10
11 import '../exports/source_mirrors.dart';
12 import '../exports/mirrors_util.dart' as dart2js_util;
13
14 import '../library_helpers.dart';
15 import '../package_helpers.dart';
16
17 import 'class.dart';
18 import 'dummy_mirror.dart';
19 import 'indexable.dart';
20 import 'method.dart';
21 import 'model_helpers.dart';
22 import 'typedef.dart';
23 import 'variable.dart';
24
25 /// A class containing contents of a Dart library.
26 class Library extends Indexable {
27 final Map<String, Class> classes = {};
28 final Map<String, Typedef> typedefs = {};
29 final Map<String, Class> errors = {};
30
31 /// Top-level variables in the library.
32 Map<String, Variable> variables;
33
34 /// Top-level functions in the library.
35 Map<String, Method> functions;
36
37 String packageName = '';
38 bool _hasBeenCheckedForPackage = false;
39 String packageIntro;
40
41 Library get owningLibrary => this;
42
43 /// Returns the [Library] for the given [mirror] if it has already been
44 /// created, else creates it.
45 factory Library(LibraryMirror mirror) {
46 var library = getDocgenObject(mirror);
47 if (library is DummyMirror) {
48 library = new Library._(mirror);
49 }
50 return library;
51 }
52
53 Library._(LibraryMirror libraryMirror) : super(libraryMirror) {
54 var exported = calcExportedItems(libraryMirror);
55 var exportedClasses = addAll(exported['classes'],
56 dart2js_util.typesOf(libraryMirror.declarations));
57 updateLibraryPackage(mirror);
58 exportedClasses.forEach((String mirrorName, TypeMirror mirror) {
59 if (mirror is TypedefMirror) {
60 // This is actually a Dart2jsTypedefMirror, and it does define value,
61 // but we don't have visibility to that type.
62 if (includePrivateMembers || !mirror.isPrivate) {
63 typedefs[dart2js_util.nameOf(mirror)] = new Typedef(mirror, this);
64 }
65 } else if (mirror is ClassMirror) {
66 var clazz = new Class(mirror, this);
67
68 if (clazz.isError()) {
69 errors[dart2js_util.nameOf(mirror)] = clazz;
70 } else {
71 classes[dart2js_util.nameOf(mirror)] = clazz;
72 }
73 } else {
74 throw new ArgumentError(
75 '${dart2js_util.nameOf(mirror)} - no class type match. ');
76 }
77 });
78 this.functions = createMethods(addAll(exported['methods'],
79 libraryMirror.declarations.values.where(
80 (mirror) => mirror is MethodMirror)).values, this);
81 this.variables = createVariables(addAll(exported['variables'],
82 dart2js_util.variablesOf(libraryMirror.declarations)).values, this);
83 }
84
85 /// Look for the specified name starting with the current member, and
86 /// progressively working outward to the current library scope.
87 String findElementInScope(String name) {
88 var lookupFunc = determineLookupFunc(name);
89 var libraryScope = lookupFunc(mirror, name);
90 if (libraryScope != null) {
91 var result = getDocgenObject(libraryScope, this);
92 if (result is DummyMirror) return packagePrefix + result.docName;
93 return result.packagePrefix + result.docName;
94 }
95 return super.findElementInScope(name);
96 }
97
98 String getMdnComment() => '';
99
100 /// For a library's [mirror], determine the name of the package (if any) we
101 /// believe it came from (because of its file URI).
102 ///
103 /// If no package could be determined, we return an empty string.
104 void updateLibraryPackage(LibraryMirror mirror) {
105 if (mirror == null) return;
106 if (_hasBeenCheckedForPackage) return;
107 _hasBeenCheckedForPackage = true;
108 if (mirror.uri.scheme != 'file') return;
109 packageName = getPackageName(mirror);
110 // Associate the package readme with all the libraries. This is a bit
111 // wasteful, but easier than trying to figure out which partial match
112 // is best.
113 packageIntro = _packageIntro(getPackageDirectory(mirror));
114 }
115
116 String _packageIntro(packageDir) {
117 if (packageDir == null) return null;
118 var dir = new Directory(packageDir);
119 var files = dir.listSync();
120 var readmes = files.where((FileSystemEntity each) => (each is File &&
121 each.path.substring(packageDir.length + 1, each.path.length)
122 .startsWith('README'))).toList();
123 if (readmes.isEmpty) return '';
124 // If there are multiples, pick the shortest name.
125 readmes.sort((a, b) => a.path.length.compareTo(b.path.length));
126 var readme = readmes.first;
127 var linkResolver = (name) => globalFixReference(name);
128 var contents = markdown.markdownToHtml(readme
129 .readAsStringSync(), linkResolver: linkResolver,
130 inlineSyntaxes: MARKDOWN_SYNTAXES);
131 return contents;
132 }
133
134 String get packagePrefix => packageName == null || packageName.isEmpty ?
135 '' : '$packageName/';
136
137 Map get previewMap {
138 var map = {'packageName': packageName};
139 map.addAll(super.previewMap);
140 if (packageIntro != null) {
141 map['packageIntro'] = packageIntro;
142 }
143 return map;
144 }
145
146 String get name => docName;
147
148 String get docName {
149 return dart2js_util.qualifiedNameOf(mirror).replaceAll('.', '-');
150 }
151
152 /// Generates a map describing the [Library] object.
153 Map toMap() => {
154 'name': name,
155 'qualifiedName': qualifiedName,
156 'comment': comment,
157 'variables': recurseMap(variables),
158 'functions': expandMethodMap(functions),
159 'classes': {
160 'class': classes.values.where((c) => c.isVisible)
161 .map((e) => e.previewMap).toList(),
162 'typedef': recurseMap(typedefs),
163 'error': errors.values.where((e) => e.isVisible)
164 .map((e) => e.previewMap).toList()
165 },
166 'packageName': packageName,
167 'packageIntro': packageIntro
168 };
169
170 String get typeName => 'library';
171
172 bool isValidMirror(DeclarationMirror mirror) => mirror is LibraryMirror;
173 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698