-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shell_executor] add
pathExpansion
utility method.
- Loading branch information
Showing
6 changed files
with
289 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.1.4 | ||
|
||
* add `pathExpansion` utility method. | ||
|
||
## 0.1.3 | ||
|
||
* Update dart sdk version to ">=2.16.0 <4.0.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
String pathExpansion( | ||
String path, [ | ||
Map<String, String> environment = const {}, | ||
]) { | ||
if (path.startsWith('~/')) { | ||
final home = environment['HOME'] ?? environment['USERPROFILE']; | ||
path = '$home${path.substring(1)}'; | ||
} | ||
|
||
final matches = [ | ||
...RegExp(r'\$(\w+)').allMatches(path), | ||
...RegExp(r'\$\{(\w+)\}').allMatches(path) | ||
]; | ||
for (final match in matches) { | ||
final envName = match.group(1); | ||
final envValue = environment[envName]; | ||
path = path.replaceFirst(match.group(0)!, envValue ?? ''); | ||
} | ||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/shell_executor/test/src/utils/path_expansion_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:shell_executor/src/utils/path_expansion.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
Map<String, String> environment = { | ||
'HOME': '/home/root', | ||
}; | ||
group('pathExpansion', () { | ||
test('~/Documents', () { | ||
final path = pathExpansion('~/Documents', environment); | ||
expect(path, '/home/root/Documents'); | ||
}); | ||
test('\$HOME/Documents', () { | ||
final r = pathExpansion('\$HOME/Documents', environment); | ||
expect(r, '/home/root/Documents'); | ||
}); | ||
test('\${HOME}/Documents', () { | ||
final r = pathExpansion('\${HOME}/Documents', environment); | ||
expect(r, '/home/root/Documents'); | ||
}); | ||
}); | ||
} |