From 8b49531878383c52e6b77ab7a95be5dbb5a1fd7c Mon Sep 17 00:00:00 2001 From: Maikel Rehl Date: Wed, 12 Jun 2024 10:38:36 +0200 Subject: [PATCH] Add projectRoot to Version parser --- lib/puro_sidekick_plugin.dart | 5 ++++- lib/src/version_parser.dart | 20 +++++++++----------- test/workspace_version_parser_test.dart | 1 + 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/puro_sidekick_plugin.dart b/lib/puro_sidekick_plugin.dart index 77a4fb6..822cee7 100644 --- a/lib/puro_sidekick_plugin.dart +++ b/lib/puro_sidekick_plugin.dart @@ -29,7 +29,10 @@ void initializePuro(Directory sdk) { } void _setupFlutterEnvironment() { - final sdkVersion = VersionParser(packagePath: entryWorkingDirectory).getMaxFlutterSdkVersionFromPubspec(); + final sdkVersion = VersionParser( + packagePath: entryWorkingDirectory, + projectRoot: SidekickContext.projectRoot, + ).getMaxFlutterSdkVersionFromPubspec(); if (sdkVersion == null) { throw Exception('No Flutter SDK version found in pubspec.yaml'); } diff --git a/lib/src/version_parser.dart b/lib/src/version_parser.dart index 7c22b52..987c07a 100644 --- a/lib/src/version_parser.dart +++ b/lib/src/version_parser.dart @@ -13,13 +13,15 @@ import 'package:yaml/yaml.dart'; class VersionParser { VersionParser({ required this.packagePath, + this.projectRoot, this.useBeta = false, this.puroLsVersionsProvider, }); final Directory packagePath; - bool useBeta; - String Function()? puroLsVersionsProvider; + final Directory? projectRoot; + final bool useBeta; + final String Function()? puroLsVersionsProvider; /// Reads the maximum flutter version from pubspec.yaml if available /// If the flutter version is not available, it reads the dart sdk version @@ -34,16 +36,12 @@ class VersionParser { // Check if the package is part of a workspace final isInWorkspace = (pubspec['resolution'] as String?) == 'workspace'; - if (isInWorkspace) { + if (isInWorkspace && + projectRoot != null && + projectRoot!.existsSync() && + projectRoot!.absolute.path != packagePath.absolute.path) { print('Package is part of a workspace. Use the root package pubspec.yaml to get the flutter version.'); - late final Directory projectRoot; - try { - // This will crash in tests - projectRoot = SidekickContext.projectRoot; - } catch (e) { - projectRoot = packagePath.parent; - } - final newPubspec = _readPubspecFile(projectRoot); + final newPubspec = _readPubspecFile(projectRoot!); if (newPubspec != null) { pubspec = newPubspec; } diff --git a/test/workspace_version_parser_test.dart b/test/workspace_version_parser_test.dart index 8cb926e..6271e2c 100644 --- a/test/workspace_version_parser_test.dart +++ b/test/workspace_version_parser_test.dart @@ -51,6 +51,7 @@ resolution: workspace final tempDir = _createWorkspacePubspec(rootPubspec, packagePubspec); final parser = VersionParser( + projectRoot: tempDir, packagePath: Directory('${tempDir.path}/package'), puroLsVersionsProvider: () => _puroLsVersions, );