Skip to content

Commit

Permalink
Initial workspace support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Jan 7, 2025
1 parent 8fae493 commit b3fb7cc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
38 changes: 31 additions & 7 deletions bin/projects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,38 @@ List<Project> findProjects() {
.split(Platform.pathSeparator)
.any((e) => e.length > 1 && e.startsWith('.'));

var dependencies = <String>{};
try {
final lockFile = File(p.join(absolutePath, 'pubspec.lock'));
final lockFileContent = lockFile.readAsStringSync();
final Set<String> 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<String> dependenciesFromLockFile(File file) {
final lockFileContent = file.readAsStringSync();
final packagesMap = loadYaml(lockFileContent)['packages'] as YamlMap;
dependencies = packagesMap.keys.cast<String>().toSet();
} catch (e) {
// This is handled elsewhere
return packagesMap.keys.cast<String>().toSet();
}

if (projectLockFile.existsSync()) {
dependencies = dependenciesFromLockFile(projectLockFile);
} else if (workspaceRefFile.existsSync()) {
final workspaceRefContent = workspaceRefFile.readAsStringSync();
final json = jsonDecode(workspaceRefContent) as Map<String, dynamic>;
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);
Expand Down
6 changes: 5 additions & 1 deletion bin/puby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -55,7 +56,10 @@ void main(List<String> 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();
Expand Down
5 changes: 5 additions & 0 deletions lib/text.dart
Original file line number Diff line number Diff line change
@@ -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';
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -18,7 +18,7 @@ dependencies:

dev_dependencies:
test: ^1.20.2
rexios_lints: ^8.2.0
rexios_lints: ^9.1.0

executables:
puby: puby

0 comments on commit b3fb7cc

Please sign in to comment.