Skip to content

Commit

Permalink
[_you_dart_internal] clean
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed May 29, 2024
1 parent ac03a6c commit 7e2a4b3
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 110 deletions.
1 change: 1 addition & 0 deletions packages/_you_dart_internal/lib/analyzer.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'package:_you_dart_internal/src/analyzer.dart' show CompilationUnitReader, AnnotationReader;
export 'package:_you_dart_internal/src/analyzer.dart' show MemoryFsAnalyzer;
export 'package:_you_dart_internal/src/pubspec.dart' show Pubspec;
81 changes: 73 additions & 8 deletions packages/_you_dart_internal/lib/src/analyzer.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,88 @@
/// 本包与应用逻辑无关的common analyzer逻辑
library;

import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/file_system/file_system.dart' as file_system;
import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:code_builder/code_builder.dart';
import 'package:file/file.dart';
import 'package:path/path.dart' as path;

import 'package:path/path.dart' as path_;

// ignore: implementation_imports, there is no other way i don t want to copy it .
import 'package:analyzer/src/test_utilities/mock_sdk.dart';

/// 实验mock sdk 看能否用element模式而不是ast,未成功暂放
class MemoryFsAnalyzer {
MemoryFsAnalyzer() {
_ensureFile("/pkg/pubspec.yaml", '''
name: mock_lib
version: 0.1.0
environment:
sdk: '>=3.4.0 <4.0.0'
''');

file_system.File initLibFile = _ensureFile("/pkg/lib/__init__.dart", """
var v="first file use by create session";
""");
;

String sdkPath = '/sdk';
createMockSdk(
resourceProvider: _resourceProvider,
root: _ensureFolder(sdkPath),
);

var collection = AnalysisContextCollection(
includedPaths: [initLibFile.path],
resourceProvider: _resourceProvider,
sdkPath: sdkPath,
);
session = collection.contexts[0].currentSession;
}

final MemoryResourceProvider _resourceProvider = MemoryResourceProvider();
late final AnalysisSession session;
/// 测试页即简单的测试:500次 7s
Future<CompilationUnitReader> getResolvedLibrary({required String path, required String content}) async {
var file = _ensureFile(path_.absolute(path), content);
return CompilationUnitReader.resolve(session, file.path);
}

static Iterable<file_system.Resource> toList(file_system.Resource resource) sync* {
if (resource is file_system.File) {
yield resource;
}
if (resource is file_system.Folder) {
for (var x in resource.getChildren()) {
yield* toList(x);
}
}
}

file_system.Folder _ensureFolder(String path) {
String convertedPath = _resourceProvider.convertPath(path);
return _resourceProvider.getFolder(convertedPath)..create();
}

file_system.File _ensureFile(String path, String content) {
String convertedPath = _resourceProvider.convertPath(path);
return _resourceProvider.getFile(convertedPath)..writeAsStringSync(content);
}
}

class CompilationUnitReader {
CompilationUnitReader(this.unit);

static Future<CompilationUnitReader> resolve(AnalysisSession analysisSession, File file) async {
assert(await file.exists(), "file:$file");
var result = (await analysisSession.getResolvedUnit(path.normalize(path.absolute(file.path))) as ResolvedUnitResult);
static Future<CompilationUnitReader> resolve(AnalysisSession analysisSession, String file) async {
var result = (await analysisSession.getResolvedUnit(path_.normalize(path_.absolute(file))) as ResolvedUnitResult);
assert(result.errors.isEmpty, "expect no error, but:${result.errors}");
return CompilationUnitReader(result.unit);
}

Expand Down Expand Up @@ -48,7 +114,7 @@ class CompilationUnitReader {
if (t == null) continue;

if (t is! InterfaceType) continue;
var findAnnoType = [t, ...t.allSupertypes].where((e) => e.getDisplayString(withNullability:true) == annoType).firstOrNull;
var findAnnoType = [t, ...t.allSupertypes].where((e) => e.getDisplayString(withNullability: true) == annoType).firstOrNull;
if (findAnnoType == null) continue;
if (annoUrl != null) {
var publicExportFrom = findPublicExportLib(findAnnoType, library);
Expand Down Expand Up @@ -104,13 +170,12 @@ class AnnotationReader {
Reference? getFieldTypeAsRef(String name) {
var type = getField(name)?.toTypeValue();
if (type == null) return null;
var symbol = type.getDisplayString(withNullability:true);
var symbol = type.getDisplayString(withNullability: true);
if (symbol == "") return null;
var publicExportFrom = findPublicExportLib(type, unit.library);
var url = publicExportFrom?.identifier;
return refer(symbol, url);
}

}

/// given a internal lib: package:you_flutter/src/router.dart
Expand Down
6 changes: 3 additions & 3 deletions packages/you_cli/lib/src/cli_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ class YouCli {
return null;
}

CompilationUnitReader unit = await CompilationUnitReader.resolve(analysisSession, file);
CompilationUnitReader unit = await CompilationUnitReader.resolve(analysisSession, file.path);
return unit.topFunction(layoutFunctionName);
}

Future<FunctionElement?> analyzePage(File file) async {
if (!await file.exists()) {
return null;
}
CompilationUnitReader unit = await CompilationUnitReader.resolve(analysisSession, file);
CompilationUnitReader unit = await CompilationUnitReader.resolve(analysisSession, file.path);
return unit.topFunction(pageFunctionName);
}

Future<PageAnnotation?> analyzePageAnno(File file) async {
if (!await file.exists()) {
return null;
}
CompilationUnitReader unit = await CompilationUnitReader.resolve(analysisSession, file);
CompilationUnitReader unit = await CompilationUnitReader.resolve(analysisSession, file.path);
return PageAnnotation.find(unit);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/you_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:

# you_*
you_dart: ^0.0.6-dev.3
_you_dart_internal: ^0.0.6-dev.3
dev_dependencies:
lints: ^3.0.0
test:
Expand Down
4 changes: 2 additions & 2 deletions packages/you_cli/test/src/analyzer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ void main() {
File notePage = fs.file("../../notes/flutter_web/lib/routes/notes/page.dart");
group("AnalyzedUnit element", () {
test('class_', () async {
var result = await CompilationUnitReader.resolve(cli.analysisSession, fs.file("lib/src/cli_core.dart"));
var result = await CompilationUnitReader.resolve(cli.analysisSession, "lib/src/cli_core.dart");
check(result.class_("YouCli")!.displayName).equals("YouCli");
});
test('annotation', () async {
var result = await CompilationUnitReader.resolve(cli.analysisSession, notePage);
var result = await CompilationUnitReader.resolve(cli.analysisSession, notePage.path);
var pageMeta = result.annotationOnTopFunction(funcName: "build", annoType: "PageAnnotation")!;
check(pageMeta.ast.toSource()).equals('@NoteAnnotation(label: "笔记")');
});
Expand Down
91 changes: 0 additions & 91 deletions packages/you_flutter/lib/src/note/source_code.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import 'package:_you_dart_internal/utils.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path_;

// ignore: implementation_imports, there is no other way i don t want to copy it .
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
import 'package:source_map_stack_trace/source_map_stack_trace.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:path/path.dart' as path;
Expand Down Expand Up @@ -127,89 +119,6 @@ class _CodeVisitor extends GeneralizingAstVisitor {
}
}

/// 实验mock sdk 看能否用element模式而不是ast,未成功暂放
@internal
class MemoryCodeAnalyzer {
@visibleForTesting
final resourceProvider = MemoryResourceProvider();
late final AnalysisSession session;
final ({String path, String content}) _defaultInitLib = (
path: "/pkg/lib/note.dart",
content: """
class Cell{
void call(Object? content) {}
Cell addCell() => Cell();
Cell addCellWith() => Cell();
}
"""
);

MemoryCodeAnalyzer() {
var libs = [_defaultInitLib];
for (var lib in libs) {
_newFile(lib.path, lib.content);
}
_newFile("/pkg/pubspec.yaml", '''
name: mock_lib
version: 0.1.0
environment:
sdk: '>=3.4.0 <4.0.0'
''');

String sdkPath = '/sdk';
createMockSdk(
resourceProvider: resourceProvider,
root: _newFolder(sdkPath),
);

var collection = AnalysisContextCollection(
includedPaths: libs.map((e) => e.path).toList(),
resourceProvider: resourceProvider,
sdkPath: sdkPath,
);
session = collection.contexts[0].currentSession;
}

Future<ResolvedLibraryResult> getResolvedLibrary({required String path, required String content}) async {
var file = _newFile(resourceProvider.convertPath(path_.absolute(path)), content);
return session.getResolvedLibrary(file.path) as ResolvedLibraryResult;
}

SomeParsedUnitResult getParsedUnit({required String path, required String content}) {
var file = _newFile(resourceProvider.convertPath(path_.absolute(path)), content);
return session.getParsedUnit(file.path);
}

/// 测试页即简单的测试:500次 7s
Future<SomeResolvedUnitResult> getResolvedUnit({required String path, required String content}) async {
var file = _newFile(resourceProvider.convertPath(path_.absolute(path)), content);
var result= await session.getResolvedUnit(file.path);
return result as ResolvedUnitResult;
}

static Iterable<Resource> toList(Resource resource) sync* {
if (resource is File) {
yield resource;
}
if (resource is Folder) {
for (var x in resource.getChildren()) {
yield* toList(x);
}
}
}

Folder _newFolder(String path) {
String convertedPath = resourceProvider.convertPath(path);
return resourceProvider.getFolder(convertedPath)..create();
}

File _newFile(String path, String content) {
String convertedPath = resourceProvider.convertPath(path);
return resourceProvider.getFile(convertedPath)..writeAsStringSync(content);
}
}

class CellCallerResult {
final StackTrace originTrace;
final StackTrace dartTrace;
Expand Down
6 changes: 3 additions & 3 deletions packages/you_flutter/test/src/note/note_code_benchmark.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:_you_dart_internal/analyzer.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:you_flutter/src/note/source_code.dart';

void main() {
group("CodeAnalyzer", () {
test('resolve', () async {
MemoryCodeAnalyzer analyzer = MemoryCodeAnalyzer();
MemoryFsAnalyzer analyzer = MemoryFsAnalyzer();
for (int i = 0; i < 500; i++) {
var resolveUnitResult = await analyzer.getResolvedUnit(path: "/pkg/lib/routes/notes/page.dart", content: '''
var resolveUnitResult = await analyzer.getResolvedLibrary(path: "/pkg/lib/routes/notes/page.dart", content: '''
import 'package:flutter/widgets.dart';
import 'package:you_flutter/note.dart';
Expand Down
7 changes: 4 additions & 3 deletions packages/you_flutter/test/src/note/note_code_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:_you_dart_internal/analyzer.dart';
import 'package:checks/checks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:you_flutter/src/note/source_code.dart';
Expand Down Expand Up @@ -51,8 +52,8 @@ class TextExamples {

group("CodeAnalyzer", () {
test('resolve', () async {
MemoryCodeAnalyzer analyzer = MemoryCodeAnalyzer();
var resolveUnitResult = await analyzer.getResolvedUnit(path: "/pkg/lib/routes/notes/page.dart", content: '''
MemoryFsAnalyzer analyzer = MemoryFsAnalyzer();
var r = await analyzer.getResolvedLibrary(path: "/pkg/lib/routes/notes/page.dart", content: '''
import 'package:flutter/widgets.dart';
import 'package:you_flutter/note.dart';
Expand All @@ -67,7 +68,7 @@ class TextExamples {
}
}
''');
check(resolveUnitResult);
check(r.unit).isNotNull();
});
});
}

0 comments on commit 7e2a4b3

Please sign in to comment.