Skip to content

Commit

Permalink
Merge pull request #13 from Rexios80/feature/workspace-out-of-scope
Browse files Browse the repository at this point in the history
Feature/workspace out of scope
  • Loading branch information
Rexios80 authored Jan 17, 2025
2 parents 7a21e12 + ab382e6 commit 6269da1
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.38.2

- Does not skip `pub get` in workspace members if the workspace is out of scope

## 1.38.1

- Fixes FVM version not installed detection
Expand Down
3 changes: 2 additions & 1 deletion bin/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Future<int> linkDependencies({

for (final project in projects) {
// Skip workspace members (the workspace will resolve them)
if (project.type == ProjectType.workspaceMember) {
if (project.type == ProjectType.workspaceMember &&
project.workspaceInScope) {
print(yellow.wrap('Skipping workspace member: ${project.path}'));
continue;
}
Expand Down
16 changes: 14 additions & 2 deletions bin/projects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,29 @@ List<Project> findProjects({Directory? directory}) {
return packagesMap.keys.cast<String>().toSet();
}

final bool workspaceInScope;
if (projectLockFile.existsSync()) {
dependencies = dependenciesFromLockFile(projectLockFile);
workspaceInScope = false;
} else if (workspaceRefFile.existsSync()) {
final workspaceRefContent = workspaceRefFile.readAsStringSync();
final json = jsonDecode(workspaceRefContent) as Map<String, dynamic>;
final workspaceRoot = json['workspaceRoot'] as String;
final absoluteWorkspaceRoot =
p.normalize(p.join(workspaceRefParent, workspaceRoot));
final workspaceLockFile =
File(p.join(workspaceRefParent, workspaceRoot, 'pubspec.lock'));
File(p.join(absoluteWorkspaceRoot, 'pubspec.lock'));
if (workspaceLockFile.existsSync()) {
dependencies = dependenciesFromLockFile(workspaceLockFile);
} else {
dependencies = pubspecDependencies;
}

workspaceInScope =
projectIntermediates.containsKey(absoluteWorkspaceRoot);
} else {
dependencies = pubspecDependencies;
workspaceInScope = false;
}

final fvm = fvmPaths.any(absolutePath.startsWith);
Expand All @@ -132,6 +140,7 @@ List<Project> findProjects({Directory? directory}) {
fvm: fvm,
type: type,
parentType: parentType,
workspaceInScope: workspaceInScope,
);

projects.add(project);
Expand Down Expand Up @@ -195,8 +204,11 @@ extension ProjectExtension on Project {
// Do not skip if parent is a workspace member
message = 'Skipping example project: $path';
skip = true;
} else if (isPubGet && type == ProjectType.workspaceMember) {
} else if (isPubGet &&
type == ProjectType.workspaceMember &&
workspaceInScope) {
// Skip pub get in workspace members since they resolve with the workspace
// Unless the workspace is out of scope
message = 'Skipping workspace member: $path';
skip = true;
} else if (dartRunPackage != null &&
Expand Down
5 changes: 5 additions & 0 deletions lib/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Project {
/// The parent project's type
final ProjectType? parentType;

/// If this project's workspace is in scope
final bool workspaceInScope;

/// The arguments to prefix to any commands run in this project
List<String> get prefixArgs => [
if (fvm) 'fvm',
Expand All @@ -75,6 +78,7 @@ class Project {
required this.fvm,
required this.type,
this.parentType,
required this.workspaceInScope,
});

/// Create a copy of this [Project] with the specified changes
Expand All @@ -90,6 +94,7 @@ class Project {
fvm: fvm ?? this.fvm,
type: type,
parentType: parentType,
workspaceInScope: workspaceInScope,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion 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.38.1
version: 1.38.2
homepage: https://github.com/Rexios80/puby

environment:
Expand Down
22 changes: 21 additions & 1 deletion test/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void main() {
expect(
File(
path.join(
result.workingDirectory,
result.testDirectory,
'fvm_puby_test',
'.dart_tool',
'version',
Expand All @@ -75,6 +75,8 @@ void main() {
'pubspec.yaml': workspacePubspec,
...dartProject(workspace: true),
},
// Must link for workspace ref to exist
link: true,
);
final stdout = result.stdout;

Expand All @@ -87,5 +89,23 @@ void main() {
isNot(contains('Resolved dependencies for dart_puby_test\n')),
);
});

test('does not skip workspace members if workspace out of scope', () async {
final result = await testCommand(
['link'],
entities: {
'pubspec.yaml': workspacePubspec,
...dartProject(workspace: true),
},
workingPath: 'dart_puby_test',
// Must link for workspace ref to exist
link: true,
);
final stdout = result.stdout;

expect(result.exitCode, ExitCode.success.code);

expectLine(stdout, ['Resolved dependencies for .']);
});
});
}
2 changes: 1 addition & 1 deletion test/no_fvm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
expect(
File(
path.join(
result.workingDirectory,
result.testDirectory,
'fvm_puby_test',
'.dart_tool',
'version',
Expand Down
4 changes: 2 additions & 2 deletions test/projects_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void main() {
);

final dependencies =
findProjects(directory: Directory(result.workingDirectory))
findProjects(directory: Directory(result.testDirectory))
.first
.dependencies;
expect(dependencies, contains('rexios_lints'));
Expand Down Expand Up @@ -91,7 +91,7 @@ void main() {
);

final dependencies =
findProjects(directory: Directory(result.workingDirectory))
findProjects(directory: Directory(result.testDirectory))
.firstWhere((e) => e.type == ProjectType.workspaceMember)
.dependencies;
expect(dependencies, contains('rexios_lints'));
Expand Down
57 changes: 43 additions & 14 deletions test/pub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,52 @@ void main() {
});
});

test('workspace members', () async {
final result = await testCommand(
['get'],
entities: {
'pubspec.yaml': workspacePubspec,
...dartProject(workspace: true),
},
);
final stdout = result.stdout;
group('workspace members', () {
test('if workspace in scope', () async {
final result = await testCommand(
['get'],
entities: {
'pubspec.yaml': workspacePubspec,
...dartProject(workspace: true),
},
// Must link for workspace ref to exist
link: true,
);
final stdout = result.stdout;

expect(result.exitCode, ExitCode.success.code);
expect(result.exitCode, ExitCode.success.code);

// Pub get should run in the workspace
expectLine(
stdout,
['Running "dart pub get" in current directory...'],
);

// Pub get should NOT run in workspace members
expectLine(stdout, ['dart_puby_test', 'Skip']);
});

test('NOT if workspace out of scope', () async {
final result = await testCommand(
['get'],
entities: {
'pubspec.yaml': workspacePubspec,
...dartProject(workspace: true),
},
workingPath: 'dart_puby_test',
// Must link for workspace ref to exist
link: true,
);
final stdout = result.stdout;

// Pub get should run in the workspace
expectLine(stdout, ['Running "dart pub get" in current directory...']);
expect(result.exitCode, ExitCode.success.code);

// Pub get should NOT run in workspace members
expectLine(stdout, ['dart_puby_test', 'Skip']);
// Pub get should run in the workspace member
expectLine(
stdout,
['Running "dart pub get" in current directory...'],
);
});
});
});
});
Expand Down
12 changes: 7 additions & 5 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:path/path.dart' as path;
final _decoder = Utf8Decoder();

class PubyProcessResult {
final String workingDirectory;
final String testDirectory;
final int exitCode;
final String stdout;
final String stderr;

PubyProcessResult(
this.workingDirectory,
this.testDirectory,
this.exitCode,
this.stdout,
this.stderr,
Expand All @@ -25,16 +25,18 @@ Future<PubyProcessResult> testCommand(
Map<String, Object>? entities,
bool link = false,
bool debug = false,
String workingPath = '',
}) async {
final workingDirectory = createTestResources(entities ?? defaultProjects());
final testDirectory = createTestResources(entities ?? defaultProjects());
final workingDirectory = path.join(testDirectory, workingPath);
final puby = File(path.join('bin', 'puby.dart')).absolute.path;

if (link) {
// puby link was not working in the test environment
await Process.run(
'dart',
[puby, 'get'],
workingDirectory: workingDirectory,
workingDirectory: testDirectory,
);
}

Expand All @@ -55,7 +57,7 @@ Future<PubyProcessResult> testCommand(

final exitCode = await process.exitCode;
return PubyProcessResult(
workingDirectory,
testDirectory,
exitCode,
await processStdout,
await processStderr,
Expand Down

0 comments on commit 6269da1

Please sign in to comment.