Skip to content

Commit

Permalink
[process_run] v1.2.0-0 export ShellBinCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Aug 21, 2024
1 parent 7dc8aee commit c41a02b
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 33 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
*.iml

# Local files
.local/
.local/

# Workaround for analyser issue, allow project at root
/pubspec.lock
/pubspec.yaml
/.dart_tool/
5 changes: 5 additions & 0 deletions packages/process_run/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.2.0-0

* Export ShellBinCommand
* requires dart 3.5

## 1.1.0

* Remove deprecated methods
Expand Down
2 changes: 1 addition & 1 deletion packages/process_run/lib/process_run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
library;

export 'package:process_run/src/shell_utils_common.dart'
show argumentsToString, argumentToString;
show argumentsToString, argumentToString, stringToArguments;

export 'shell.dart';
export 'which.dart' show which, whichSync;
11 changes: 10 additions & 1 deletion packages/process_run/lib/src/bin/shell/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:process_run/src/user_config.dart';
import 'env_alias.dart';
import 'env_delete.dart';
import 'env_file_content.dart';
import 'env_info.dart';
import 'env_path.dart';
import 'env_var.dart';
import 'import.dart';
Expand All @@ -27,7 +28,13 @@ class ShellEnvCommandBase extends ShellBinCommand {

/// Local env
bool get local {
final user = results[flagUser] as bool;
if (!results.wasParsed(flagUser)) {
var parent = this.parent;
if (parent is ShellEnvCommandBase) {
return parent.local;
}
}
final user = results.flag(flagUser);
final local = !user;
return local;
}
Expand Down Expand Up @@ -113,6 +120,8 @@ class ShellEnvCommand extends ShellEnvCommandBase {

addCommand(ShellEnvAliasCommand());
addCommand(ShellEnvPathCommand());
addCommand(ShellEnvInfoCommand());

parser.addFlag(flagInfo, abbr: 'i', help: 'display info', negatable: false);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/process_run/lib/src/bin/shell/env_alias_delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ShellEnvAliasDeleteCommand extends ShellEnvCommandBase {
stderr.writeln('At least 1 arguments expected');
exit(1);
} else {
if (verbose!) {
if (verbose) {
stdout.writeln('file $label: $envFilePath');
stdout.writeln('before: ${jsonEncode(ShellEnvironment().aliases)}');
}
Expand All @@ -38,15 +38,15 @@ class ShellEnvAliasDeleteCommand extends ShellEnvCommandBase {
modified = fileContent.deleteAlias(name) || modified;
}
if (modified) {
if (verbose!) {
if (verbose) {
stdout.writeln('writing file');
}
await fileContent.write();
}

// Force reload
shellEnvironment = null;
if (verbose!) {
if (verbose) {
stdout.writeln('After: ${jsonEncode(ShellEnvironment().vars)}');
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions packages/process_run/lib/src/bin/shell/env_alias_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ShellEnvAliasSetCommand extends ShellEnvCommandBase {
stderr.writeln('At least 2 arguments expected');
exit(1);
} else {
if (verbose!) {
if (verbose) {
stdout.writeln('file $label: $envFilePath');
stdout.writeln('before: ${jsonEncode(ShellEnvironment().aliases)}');
}
Expand All @@ -39,7 +39,7 @@ class ShellEnvAliasSetCommand extends ShellEnvCommandBase {
}
// Force reload
shellEnvironment = null;
if (verbose!) {
if (verbose) {
stdout.writeln('After: ${jsonEncode(ShellEnvironment().aliases)}');
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions packages/process_run/lib/src/bin/shell/env_delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class ShellEnvDeleteCommand extends ShellEnvCommandBase {
@override
FutureOr<bool> onRun() async {
var path = envFilePath;
if (verbose!) {
if (verbose) {
stdout.writeln('envFilePath: $path');
}
var force = getFlag(flagForce)!;
var force = getFlag(flagForce);

if (force ||
await promptConfirm('Confirm that you want to delete file ($label)')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/process_run/lib/src/bin/shell/env_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ShellEnvEditCommand extends ShellEnvCommandBase {

@override
FutureOr<bool> onRun() async {
if (verbose!) {
if (verbose) {
stdout.writeln('envFilePath: $envFilePath');
}
await envFileReadOrCreate(write: true);
Expand Down
42 changes: 42 additions & 0 deletions packages/process_run/lib/src/bin/shell/env_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:path/path.dart';
import 'package:process_run/src/user_config.dart';

import 'import.dart';

/// pub run process_run:shell run
class ShellEnvInfoCommand extends ShellBinCommand {
/// pub run process_run:shell run
ShellEnvInfoCommand()
: super(name: 'info', description: 'Display environment info');

@override
void printUsage() {
stdout.writeln('Run a command');
stdout.writeln();
stdout.writeln('Usage: $script env info');
stdout.writeln(' Environment information');

super.printUsage();
}

@override
FutureOr<bool> onRun() async {
void displayInfo(String title, String path) {
var config = loadFromPath(path);
stdout.writeln('# $title');
stdout.writeln('file: ${relative(path, from: Directory.current.path)}');
stdout.writeln('vars: ${config.vars}');
stdout.writeln('paths: ${config.paths}');
}

displayInfo('user_env', getUserEnvFilePath()!);
displayInfo('local_env', getLocalEnvFilePath());

return true;
}
}

/// Direct shell env Alias dump run helper for testing.
Future<void> main(List<String> arguments) async {
await ShellEnvInfoCommand().parseAndRun(arguments);
}
3 changes: 2 additions & 1 deletion packages/process_run/lib/src/bin/shell/env_path.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'package:process_run/src/bin/shell/env_path_dump.dart';
import 'package:process_run/src/bin/shell/env_path_prepend.dart';

import 'env.dart';
import 'env_path_delete.dart';
import 'env_path_get.dart';
import 'import.dart';

/// Path operations
class ShellEnvPathCommand extends ShellBinCommand {
class ShellEnvPathCommand extends ShellEnvCommandBase {
/// Path operations
ShellEnvPathCommand() : super(name: 'path', description: 'Path operations') {
addCommand(ShellEnvPathDumpCommand());
Expand Down
4 changes: 2 additions & 2 deletions packages/process_run/lib/src/bin/shell/env_path_delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ShellEnvPathDeleteCommand extends ShellEnvCommandBase {
stderr.writeln('At least 1 path argument expected');
exit(1);
} else {
if (verbose!) {
if (verbose) {
stdout.writeln('File $label: $envFilePath');
stdout.writeln('before: ${jsonEncode(ShellEnvironment().paths)}');
}
Expand All @@ -37,7 +37,7 @@ class ShellEnvPathDeleteCommand extends ShellEnvCommandBase {
}
// Force reload
shellEnvironment = null;
if (verbose!) {
if (verbose) {
stdout.writeln('After: ${jsonEncode(ShellEnvironment().paths)}');
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/process_run/lib/src/bin/shell/env_path_get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ShellEnvPathGetCommand extends ShellEnvCommandBase {
stderr.writeln('At least 1 path argument expected');
exit(1);
} else {
if (verbose!) {
if (verbose) {
stdout.writeln('File $label: $envFilePath');
}
dumpStringList(ShellEnvironment()
Expand Down
4 changes: 2 additions & 2 deletions packages/process_run/lib/src/bin/shell/env_path_prepend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ShellEnvPathPrependCommand extends ShellEnvCommandBase {
stderr.writeln('At least 1 path argument expected');
exit(1);
} else {
if (verbose!) {
if (verbose) {
stdout.writeln('before: ${jsonEncode(ShellEnvironment().paths)}');
}
var fileContent = await envFileReadOrCreate();
Expand All @@ -36,7 +36,7 @@ class ShellEnvPathPrependCommand extends ShellEnvCommandBase {
}
// Force reload
shellEnvironment = null;
if (verbose!) {
if (verbose) {
stdout.writeln('After: ${jsonEncode(ShellEnvironment().paths)}');
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions packages/process_run/lib/src/bin/shell/env_var_delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:process_run/src/io/io.dart';

/// Delete an environment variable from a user/local config file
class ShellEnvVarDeleteCommand extends ShellEnvCommandBase {
late final _helper = ShellEnvVarDeleteIoHelper(
shell: Shell(), local: local, verbose: verbose ?? false);
late final _helper =
ShellEnvVarDeleteIoHelper(shell: Shell(), local: local, verbose: verbose);

/// Delete an environment variable from a user/local config file
ShellEnvVarDeleteCommand()
Expand Down
4 changes: 2 additions & 2 deletions packages/process_run/lib/src/bin/shell/env_var_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:process_run/src/io/io.dart';

/// Set an environment variable in a user/local config file
class ShellEnvVarSetCommand extends ShellEnvCommandBase {
late final _helper = ShellEnvVarSetIoHelper(
shell: Shell(), local: local, verbose: verbose ?? false);
late final _helper =
ShellEnvVarSetIoHelper(shell: Shell(), local: local, verbose: verbose);

/// Set an environment variable in a user/local config file
ShellEnvVarSetCommand()
Expand Down
2 changes: 1 addition & 1 deletion packages/process_run/lib/src/bin/shell/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ShellRunCommand extends ShellBinCommand {
if (command == null) {
exit(1);
}
if (verbose!) {
if (verbose) {
stdout.writeln('command: $command');
}
await run(command);
Expand Down
27 changes: 17 additions & 10 deletions packages/process_run/lib/src/bin/shell/shell_bin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ShellBinCommand {
bool? _verbose;

/// Set before run
bool? get verbose => _verbose ??= parent?.verbose;
bool get verbose => _verbose ??= parent?.verbose ?? false;

String? _description;

Expand Down Expand Up @@ -76,12 +76,12 @@ class ShellBinCommand {

/// Parse the arguments
ArgResults parse(List<String> arguments) {
// Add missing common commands
parser.addFlag(flagVersion,
help: 'Print the command version', negatable: false);
parser.addFlag(flagVerbose,
abbr: 'v', help: 'Verbose mode', negatable: false);
return results = parser.parse(arguments);
results = parser.parse(arguments);
if (parent == null) {
// Handle verbose
_verbose = getFlag(flagVerbose);
}
return results;
}

/// Parse and run the command
Expand All @@ -100,12 +100,19 @@ class ShellBinCommand {
ArgParser? parser,
ShellBinCommand? parent,
String? description}) {
_onRun = onRun;
//_onRun = onRun;
_parser = parser;
_description = description;
_version = version;
// read or create
parser = this.parser;
// Add missing common commands
if (parent == null) {
parser.addFlag(flagVersion,
help: 'Print the command version', negatable: false);
parser.addFlag(flagVerbose,
abbr: 'v', help: 'Verbose mode', negatable: false);
}
parser.addFlag(flagHelp, abbr: 'h', help: 'Usage help', negatable: false);
}

Expand All @@ -128,7 +135,7 @@ class ShellBinCommand {
}

/// Get a flag
bool? getFlag(String name) => results[name] as bool?;
bool getFlag(String name) => results.flag(name);

/// Run
@nonVirtual
Expand All @@ -138,7 +145,7 @@ class ShellBinCommand {
// Handle verbose
_verbose = getFlag(flagVerbose);

final hasVersion = getFlag(flagVersion)!;
final hasVersion = getFlag(flagVersion);
if (hasVersion) {
stdout.writeln(version);
return true;
Expand Down
1 change: 1 addition & 0 deletions packages/process_run/lib/utils/shell_bin_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'package:process_run/src/mixin/shell_bin.dart';
2 changes: 1 addition & 1 deletion packages/process_run/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: process_run
version: 1.1.0
version: 1.2.0-0
description: Process run helpers for Linux/Win/Mac and which like feature for finding executables.
homepage: https://github.com/tekartik/process_run.dart/blob/master/packages/process_run

Expand Down
54 changes: 54 additions & 0 deletions packages/process_run/test/bin/compiled_bin_shell_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@TestOn('vm')
library process_run.test.bin.shell_bin_test;

import 'package:process_run/shell.dart';
import 'package:process_run/src/bin/shell/import.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';

import '../src/compile_shell.dart';

void main() {
late Shell shell;
group('compiled_bin_shell', () {
setUpAll(() async {
var dsExePath = await compileShellBin(force: false);
shell = Shell(
environment: ShellEnvironment()..aliases['ds'] = dsExePath,
verbose: false);
});
test('version', () async {
var output = (await shell.run('ds --version')).outText.trim();
await shell.run('ds env edit -h');
expect(Version.parse(output), shellBinVersion);
});
group('env', () {
group('path', () {
test('user prepend', () async {
var dummyPath = 'dummyUserPathPrepend';
await shell.run('ds env --user path delete $dummyPath');
var lines = (await shell.run('ds env --user path dump')).outLines;
expect(lines, isNot(contains(dummyPath)));
await shell.run('ds env --user path prepend $dummyPath');
lines = (await shell.run('ds env --user path dump')).outLines;
expect(lines, contains(dummyPath));
await shell.run('ds env --user path delete $dummyPath');
lines = (await shell.run('ds env --user path dump')).outLines;
expect(lines, isNot(contains(dummyPath)));
});
test('local prepend', () async {
var dummyPath = 'dummyLocalPathPrepend';
await shell.run('ds env --user path delete $dummyPath');
var lines = (await shell.run('ds env --user path dump')).outLines;
expect(lines, isNot(contains(dummyPath)));
await shell.run('ds env --user path prepend $dummyPath');
lines = (await shell.run('ds env --user path dump')).outLines;
expect(lines, contains(dummyPath));
await shell.run('ds env --user path delete $dummyPath');
lines = (await shell.run('ds env --user path dump')).outLines;
expect(lines, isNot(contains(dummyPath)));
});
});
});
});
}
Loading

0 comments on commit c41a02b

Please sign in to comment.