Skip to content

Commit

Permalink
Add projectRoot to Version parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rehlma committed Jun 12, 2024
1 parent 41326d9 commit 8b49531
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
5 changes: 4 additions & 1 deletion lib/puro_sidekick_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
20 changes: 9 additions & 11 deletions lib/src/version_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/workspace_version_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ resolution: workspace

final tempDir = _createWorkspacePubspec(rootPubspec, packagePubspec);
final parser = VersionParser(
projectRoot: tempDir,
packagePath: Directory('${tempDir.path}/package'),
puroLsVersionsProvider: () => _puroLsVersions,
);
Expand Down

0 comments on commit 8b49531

Please sign in to comment.