Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command workspace list #4378

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/src/command/workspace.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import '../command.dart';
import 'workspace_list.dart';

class WorkspaceCommand extends PubCommand {
@override
String get description => 'Work with the pub workspace.';

@override
String get name => 'workspace';

WorkspaceCommand() {
addSubcommand(WorkspaceListCommand());
}
}
49 changes: 49 additions & 0 deletions lib/src/command/workspace_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';

import 'package:path/path.dart' as p;

import '../command.dart';
import '../log.dart';

class WorkspaceListCommand extends PubCommand {
@override
String get description =>
'List all packages in the workspace, and their directory';

@override
String get name => 'list';

WorkspaceListCommand() {
argParser.addFlag(
'json',
negatable: false,
help: 'output information in a json format',
);
}

@override
void runProtected() {
if (argResults.flag('json')) {
message(
const JsonEncoder.withIndent(' ').convert({
'packages': [
...entrypoint.workspaceRoot.transitiveWorkspace.map(
(package) => {
'name': package.name,
'path': p.canonicalize(package.dir),
},
),
],
}),
);
} else {
for (final package in entrypoint.workspaceRoot.transitiveWorkspace) {
message('${package.name}: ${p.relative(p.absolute(package.dir))}');
}
}
}
}
2 changes: 2 additions & 0 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'command/unpack.dart';
import 'command/upgrade.dart';
import 'command/uploader.dart';
import 'command/version.dart';
import 'command/workspace.dart';
import 'exit_codes.dart' as exit_codes;
import 'git.dart' as git;
import 'io.dart';
Expand Down Expand Up @@ -156,6 +157,7 @@ class PubCommandRunner extends CommandRunner<int> implements PubTopLevel {
addCommand(LoginCommand());
addCommand(LogoutCommand());
addCommand(VersionCommand());
addCommand(WorkspaceCommand());
addCommand(TokenCommand());
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/pub_embeddable_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'command/token.dart';
import 'command/unpack.dart';
import 'command/upgrade.dart';
import 'command/uploader.dart';
import 'command/workspace.dart';
import 'log.dart' as log;
import 'log.dart';
import 'utils.dart';
Expand Down Expand Up @@ -86,6 +87,7 @@ class PubEmbeddableCommand extends PubCommand implements PubTopLevel {
addSubcommand(LoginCommand());
addSubcommand(LogoutCommand());
addSubcommand(TokenCommand());
addSubcommand(WorkspaceCommand());
}

@override
Expand Down
1 change: 1 addition & 0 deletions test/testdata/goldens/embedding/embedding_test/--help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Available subcommands:
token Manage authentication tokens for hosted pub repositories.
unpack Downloads a package and unpacks it in place.
upgrade Upgrade the current package's dependencies to latest versions.
workspace Work with the pub workspace.

Run "pub_command_runner help" to see global options.
See https://dart.dev/tools/pub/cmd/pub-global for detailed documentation.
Expand Down
14 changes: 14 additions & 0 deletions test/testdata/goldens/help_test/pub workspace --help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# GENERATED BY: test/help_test.dart

## Section 0
$ pub workspace --help
Work with the pub workspace

Usage: pub workspace [arguments...]
-h, --help Print this usage information.

Available subcommands:
list List all packages in the workspace, and their directory

Run "pub help" to see global options.

12 changes: 12 additions & 0 deletions test/testdata/goldens/help_test/pub workspace list --help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GENERATED BY: test/help_test.dart

## Section 0
$ pub workspace list --help
List all packages in the workspace, and their directory

Usage: pub workspace list <subcommand> [arguments...]
-h, --help Print this usage information.
--json output information in a json format

Run "pub help" to see global options.

73 changes: 73 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,79 @@ Consider removing one of the overrides.''',
),
);
});

test('workspace list', () async {
await dir(appPath, [
libPubspec(
'myapp',
'1.2.3',
extras: {
'workspace': ['pkgs/a'],
},
sdk: '^3.5.0',
),
dir('pkgs', [
dir('a', [
libPubspec(
'a',
'1.1.1',
resolutionWorkspace: true,
extras: {
'workspace': ['b'],
},
),
dir('b', [
libPubspec(
'b',
'1.2.2',
resolutionWorkspace: true,
),
]),
]),
]),
]).create();
await runPub(
args: ['workspace', 'list'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: '''
myapp: .
a: pkgs/a
b: pkgs/a/b
''',
);
await runPub(
args: ['workspace', 'list'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
workingDirectory: p.join(sandbox, appPath, 'pkgs'),
output: '''
myapp: ..
a: a
b: a/b
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
''',
);
await runPub(
args: ['workspace', 'list', '--json'],
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
output: '''
{
"packages": [
{
"name": "myapp",
"path": "${p.join(sandbox, appPath)}"
},
{
"name": "a",
"path": "${p.join(sandbox, appPath, 'pkgs', 'a')}"
},
{
"name": "b",
"path": "${p.join(sandbox, appPath, 'pkgs', 'a', 'b')}"
}
]
}
''',
);
});
}

final s = p.separator;
Loading