From b3fb7cc1025dcf4e083e2dd870f06468509d1d2f Mon Sep 17 00:00:00 2001 From: Rexios Date: Tue, 7 Jan 2025 10:07:39 -0500 Subject: [PATCH] Initial workspace support --- CHANGELOG.md | 5 +++++ bin/projects.dart | 38 +++++++++++++++++++++++++++++++------- bin/puby.dart | 6 +++++- lib/text.dart | 5 +++++ pubspec.yaml | 4 ++-- 5 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 lib/text.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 261e12a..8a87271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.38.0 + +- Adds preliminary support for workspaces +- Fixes grammar when only one project is found + ## 1.37.0 - `puby gen` and `puby run` now map to `dart run` instead of `[engine] pub run` diff --git a/bin/projects.dart b/bin/projects.dart index 034006e..dd83209 100644 --- a/bin/projects.dart +++ b/bin/projects.dart @@ -52,14 +52,38 @@ List findProjects() { .split(Platform.pathSeparator) .any((e) => e.length > 1 && e.startsWith('.')); - var dependencies = {}; - try { - final lockFile = File(p.join(absolutePath, 'pubspec.lock')); - final lockFileContent = lockFile.readAsStringSync(); + final Set dependencies; + + final projectLockFile = File(p.join(absolutePath, 'pubspec.lock')); + final workspaceRefParent = p.join(absolutePath, '.dart_tool', 'pub'); + final workspaceRefFile = + File(p.join(workspaceRefParent, 'workspace_ref.json')); + final pubspecDependencies = { + ...pubspec.dependencies.keys, + ...pubspec.devDependencies.keys, + }; + + Set dependenciesFromLockFile(File file) { + final lockFileContent = file.readAsStringSync(); final packagesMap = loadYaml(lockFileContent)['packages'] as YamlMap; - dependencies = packagesMap.keys.cast().toSet(); - } catch (e) { - // This is handled elsewhere + return packagesMap.keys.cast().toSet(); + } + + if (projectLockFile.existsSync()) { + dependencies = dependenciesFromLockFile(projectLockFile); + } else if (workspaceRefFile.existsSync()) { + final workspaceRefContent = workspaceRefFile.readAsStringSync(); + final json = jsonDecode(workspaceRefContent) as Map; + final workspaceRoot = json['workspaceRoot'] as String; + final workspaceLockFile = + File(p.join(workspaceRefParent, workspaceRoot, 'pubspec.lock')); + if (workspaceLockFile.existsSync()) { + dependencies = dependenciesFromLockFile(workspaceLockFile); + } else { + dependencies = pubspecDependencies; + } + } else { + dependencies = pubspecDependencies; } final fvm = fvmPaths.any(absolutePath.startsWith); diff --git a/bin/puby.dart b/bin/puby.dart index f74ab45..310049d 100644 --- a/bin/puby.dart +++ b/bin/puby.dart @@ -7,6 +7,7 @@ import 'package:pub_update_checker/pub_update_checker.dart'; import 'package:puby/command.dart'; import 'package:io/ansi.dart'; import 'package:puby/project.dart'; +import 'package:puby/text.dart'; import 'package:puby/time.dart'; import 'commands.dart'; @@ -55,7 +56,10 @@ void main(List arguments) async { exit(ExitCode.usage.code); } - print(green.wrap('Found ${projects.length} projects\n')); + final numProjects = projects.length; + print( + green.wrap('Found $numProjects ${pluralize('project', numProjects)}\n'), + ); if (projects.any((e) => e.fvm)) { fvmCheck(); diff --git a/lib/text.dart b/lib/text.dart new file mode 100644 index 0000000..f943188 --- /dev/null +++ b/lib/text.dart @@ -0,0 +1,5 @@ +/// Returns the plural form of a word based on the count +String pluralize(String word, int count) { + if (count == 1) return word; + return '${word}s'; +} diff --git a/pubspec.yaml b/pubspec.yaml index ac9598e..7fc55e5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: puby description: Run commands in all projects in the current directory. Handle monorepos with ease. -version: 1.37.0 +version: 1.38.0 homepage: https://github.com/Rexios80/puby environment: @@ -18,7 +18,7 @@ dependencies: dev_dependencies: test: ^1.20.2 - rexios_lints: ^8.2.0 + rexios_lints: ^9.1.0 executables: puby: puby