Skip to content

Commit 6154145

Browse files
Merge pull request #332 from Workiva/process_working_directory
CPLAT-8451: Add optional `workingDirectory` param for ProcessTool.
2 parents 23fbc34 + c5ed7d6 commit 6154145

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [3.2.0](https://github.com/Workiva/dart_dev/compare/3.1.0...3.2.0)
4+
5+
- Add an optional `String workingDirectory` parameter when creating a
6+
`DevTool.fromProcess()` or `ProcessTool()`.
7+
- Expose the `Process` created by a `ProcessTool` via a public field.
8+
39
## [3.1.0](https://github.com/Workiva/dart_dev/compare/3.0.0...3.1.0)
410

511
- Update `FormatTool.getInputs()` to support an optional `followLinks` param.

lib/src/dart_dev_tool.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ abstract class DevTool {
1717
FunctionTool(function, argParser: argParser);
1818

1919
factory DevTool.fromProcess(String executable, List<String> args,
20-
{ProcessStartMode mode}) =>
21-
ProcessTool(executable, args, mode: mode);
20+
{ProcessStartMode mode, String workingDirectory}) =>
21+
ProcessTool(executable, args,
22+
mode: mode, workingDirectory: workingDirectory);
2223

2324
/// The argument parser for this tool, if needed.
2425
///

lib/src/tools/process_tool.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22
import 'dart:io';
33

4+
import 'package:dart_dev/src/utils/start_process_and_ensure_exit.dart';
45
import 'package:logging/logging.dart';
56

67
import '../dart_dev_tool.dart';
@@ -25,14 +26,20 @@ final _log = Logger('Process');
2526
/// It is also possible to run this tool directly in a dart script:
2627
/// ProcessTool(exe, args).run();
2728
class ProcessTool extends DevTool {
28-
ProcessTool(String executable, List<String> args, {ProcessStartMode mode})
29+
ProcessTool(String executable, List<String> args,
30+
{ProcessStartMode mode, String workingDirectory})
2931
: _args = args,
3032
_executable = executable,
31-
_mode = mode;
33+
_mode = mode,
34+
_workingDirectory = workingDirectory;
3235

3336
final List<String> _args;
3437
final String _executable;
3538
final ProcessStartMode _mode;
39+
final String _workingDirectory;
40+
41+
Process get process => _process;
42+
Process _process;
3643

3744
@override
3845
FutureOr<int> run([DevToolExecutionContext context]) async {
@@ -43,8 +50,10 @@ class ProcessTool extends DevTool {
4350
commandName: context.commandName);
4451
}
4552
logSubprocessHeader(_log, '$_executable ${_args.join(' ')}');
46-
return runProcessAndEnsureExit(
47-
ProcessDeclaration(_executable, _args, mode: _mode),
53+
_process = await startProcessAndEnsureExit(
54+
ProcessDeclaration(_executable, _args,
55+
mode: _mode, workingDirectory: _workingDirectory),
4856
log: _log);
57+
return _process.exitCode;
4958
}
5059
}

lib/src/utils/process_declaration.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class ProcessDeclaration {
44
final List<String> args;
55
final String executable;
66
final ProcessStartMode mode;
7+
final String workingDirectory;
78

8-
ProcessDeclaration(this.executable, this.args, {this.mode});
9+
ProcessDeclaration(this.executable, this.args,
10+
{this.mode, this.workingDirectory});
911
}

lib/src/utils/run_process_and_ensure_exit.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Future<int> runProcessAndEnsureExit(ProcessDeclaration processDeclaration,
99
{Logger log}) async {
1010
final process = await Process.start(
1111
processDeclaration.executable, processDeclaration.args,
12-
mode: processDeclaration.mode ?? ProcessStartMode.normal);
12+
mode: processDeclaration.mode ?? ProcessStartMode.normal,
13+
workingDirectory: processDeclaration.workingDirectory);
1314
ensureProcessExit(process, log: log);
1415
return process.exitCode;
1516
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:io';
2+
3+
import 'package:logging/logging.dart';
4+
5+
import 'process_declaration.dart';
6+
import 'ensure_process_exit.dart';
7+
8+
Future<Process> startProcessAndEnsureExit(ProcessDeclaration processDeclaration,
9+
{Logger log}) async {
10+
final process = await Process.start(
11+
processDeclaration.executable, processDeclaration.args,
12+
mode: processDeclaration.mode ?? ProcessStartMode.normal,
13+
workingDirectory: processDeclaration.workingDirectory);
14+
ensureProcessExit(process, log: log);
15+
return process;
16+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: dart_dev
2-
version: 3.1.0
2+
version: 3.2.0
33
description: Centralized tooling for Dart projects. Consistent interface across projects. Easily configurable.
44
authors:
55
- Workiva Client Platform Team <[email protected]>

test/tools/process_tool_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
@TestOn('vm')
24
import 'package:args/args.dart';
35
import 'package:args/command_runner.dart';
@@ -17,6 +19,15 @@ void main() {
1719
expect(await tool.run(), isNonZero);
1820
});
1921

22+
test('can run from a custom working directory', () async {
23+
final tool = DevTool.fromProcess('pwd', [], workingDirectory: 'lib')
24+
as ProcessTool;
25+
expect(await tool.run(), isZero);
26+
final stdout =
27+
(await tool.process.stdout.transform(utf8.decoder).join('')).trim();
28+
expect(stdout, endsWith('/dart_dev/lib'));
29+
});
30+
2031
test('throws UsageException when args are present', () {
2132
final tool = DevTool.fromProcess('true', []);
2233
expect(

0 commit comments

Comments
 (0)