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 31, 2024
1 parent 7e2a4b3 commit 321e183
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 78 deletions.
5 changes: 3 additions & 2 deletions packages/_you_dart_internal/lib/analyzer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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/analyzer.dart' show CompilationUnitReader;
export 'package:_you_dart_internal/src/analyzer.dart' show AnnotationReader;

export 'package:_you_dart_internal/src/pubspec.dart' show Pubspec;
66 changes: 0 additions & 66 deletions packages/_you_dart_internal/lib/src/analyzer.dart
Original file line number Diff line number Diff line change
@@ -1,82 +1,16 @@
/// 本包与应用逻辑无关的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: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);

Expand Down
82 changes: 79 additions & 3 deletions packages/you_flutter/lib/src/note/source_code.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import 'package:_you_dart_internal/analyzer.dart';
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:source_map_stack_trace/source_map_stack_trace.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:path/path.dart' as path;
import 'package:path/path.dart' as path_;
import 'package:source_maps/source_maps.dart' as source_map;

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

typedef _AddCell = ({Block belongTo, MethodInvocation invocation});

class SourceCode {
Expand Down Expand Up @@ -158,7 +167,7 @@ class CellCaller {
Frame? findCallerLineInDartTrace(StackTrace stackTrace, Uri location) {
var trace = Trace.from(stackTrace);
// 找到堆栈中连续出现的本页面中最后一个Frame,就是哪一行实际触发了异常
String expected = path.normalize("${location.path}/page.dart");
String expected = path_.normalize("${location.path}/page.dart");
Frame? found;
for (var frame in trace.frames) {
if (frame.uri.path.endsWith(expected)) {
Expand All @@ -182,7 +191,7 @@ class CellCaller {
if (frame.line == null || frame.uri.path == "unparsed") {
continue;
}
if (path.basename(frame.uri.path) != "main.dart.js") {
if (path_.basename(frame.uri.path) != "main.dart.js") {
return frame.uri.replace(path: "${frame.uri.path}.map");
}
}
Expand All @@ -200,3 +209,70 @@ class CellCaller {
return CellCallerResult(originTrace: originTrace, dartTrace: dartTrace, callerFrame: findCallerLineInDartTrace(dartTrace, location)!);
}
}

/// 内存dart sdk, 用于解析要求不高的场景
@internal
class NoteAnalyzer {
NoteAnalyzer()
: resourceProvider = MemoryResourceProvider(
context: path_.Context(
style: path_.Style.posix,
current: "/stub",
)) {
_ensureFile("/stub/pubspec.yaml", '''
name: stub
version: 0.1.0
environment:
sdk: '>=3.4.0 <4.0.0'
''');

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

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

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

final MemoryResourceProvider resourceProvider;

late final AnalysisSession session;

/// 测试页即简单的测试:500次 7s
Future<CompilationUnitReader> getResolvedLibrary(String file) async {
var result = (await session.getResolvedUnit(path_.normalize(path_.absolute(file))) as ResolvedUnitResult);
return CompilationUnitReader(result.unit);
}

// 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);
// }
// }
// }

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

File _ensureFile(String path, String content) {
String convertedPath = resourceProvider.convertPath(path);
return resourceProvider.getFile(convertedPath)..writeAsStringSync(content);
}
}
10 changes: 6 additions & 4 deletions packages/you_flutter/test/src/note/note_code_benchmark.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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 {
MemoryFsAnalyzer analyzer = MemoryFsAnalyzer();
for (int i = 0; i < 500; i++) {
var resolveUnitResult = await analyzer.getResolvedLibrary(path: "/pkg/lib/routes/notes/page.dart", content: '''
NoteAnalyzer analyzer = NoteAnalyzer();
analyzer.resourceProvider.getFile("/stub/lib/routes/notes/page.dart").writeAsStringSync('''
import 'package:flutter/widgets.dart';
import 'package:you_flutter/note.dart';
Expand All @@ -21,6 +20,9 @@ class TextExamples {
}
}
''');
for (int i = 0; i < 500; i++) {
var resolveUnitResult = await analyzer.getResolvedLibrary("/stub/lib/routes/notes/page.dart");

resolveUnitResult;
// debugPrint("$resolveUnitResult");
}
Expand Down
6 changes: 3 additions & 3 deletions packages/you_flutter/test/src/note/note_code_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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 @@ -52,8 +51,8 @@ class TextExamples {

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

0 comments on commit 321e183

Please sign in to comment.