From 4c7ce873a62cdc0e2da4a521e94d7225a6134b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=81=A5=E8=8E=B9?= Date: Wed, 31 May 2023 23:40:30 +0800 Subject: [PATCH] [shell_executor] add `pathExpansion` utility method. --- packages/shell_executor/CHANGELOG.md | 4 + .../shell_executor/lib/shell_executor.dart | 1 + .../lib/src/utils/path_expansion.dart | 20 ++ packages/shell_executor/pubspec.lock | 240 ++++++++++++++++++ packages/shell_executor/pubspec.yaml | 3 +- .../test/src/utils/path_expansion_test.dart | 22 ++ 6 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 packages/shell_executor/lib/src/utils/path_expansion.dart create mode 100644 packages/shell_executor/test/src/utils/path_expansion_test.dart diff --git a/packages/shell_executor/CHANGELOG.md b/packages/shell_executor/CHANGELOG.md index 41a309c4..46847ecc 100644 --- a/packages/shell_executor/CHANGELOG.md +++ b/packages/shell_executor/CHANGELOG.md @@ -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" diff --git a/packages/shell_executor/lib/shell_executor.dart b/packages/shell_executor/lib/shell_executor.dart index 77b5522a..23dbbbee 100644 --- a/packages/shell_executor/lib/shell_executor.dart +++ b/packages/shell_executor/lib/shell_executor.dart @@ -2,3 +2,4 @@ library shell_executor; export 'src/command.dart'; export 'src/shell_executor.dart'; +export 'src/utils/path_expansion.dart'; diff --git a/packages/shell_executor/lib/src/utils/path_expansion.dart b/packages/shell_executor/lib/src/utils/path_expansion.dart new file mode 100644 index 00000000..71e8a69c --- /dev/null +++ b/packages/shell_executor/lib/src/utils/path_expansion.dart @@ -0,0 +1,20 @@ +String pathExpansion( + String path, [ + Map 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; +} diff --git a/packages/shell_executor/pubspec.lock b/packages/shell_executor/pubspec.lock index 0dddf4a7..99929efc 100644 --- a/packages/shell_executor/pubspec.lock +++ b/packages/shell_executor/pubspec.lock @@ -1,6 +1,22 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + sha256: ae92f5d747aee634b87f89d9946000c2de774be1d6ac3e58268224348cd0101a + url: "https://pub.dev" + source: hosted + version: "61.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + sha256: ea3d8652bda62982addfd92fdc2d0214e5f82e43325104990d4f4c4a2a313562 + url: "https://pub.dev" + source: hosted + version: "5.13.0" args: dependency: transitive description: @@ -17,6 +33,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.11.0" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" + url: "https://pub.dev" + source: hosted + version: "2.1.1" build_config: dependency: transitive description: @@ -41,6 +65,30 @@ packages: url: "https://pub.dev" source: hosted version: "1.17.2" + convert: + dependency: transitive + description: + name: convert + sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" + url: "https://pub.dev" + source: hosted + version: "3.1.1" + coverage: + dependency: transitive + description: + name: coverage + sha256: "2fb815080e44a09b85e0f2ca8a820b15053982b2e714b59267719e8a9ff17097" + url: "https://pub.dev" + source: hosted + version: "1.6.3" + crypto: + dependency: transitive + description: + name: crypto + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" + source: hosted + version: "3.0.3" dependency_validator: dependency: "direct dev" description: @@ -57,6 +105,14 @@ packages: url: "https://pub.dev" source: hosted version: "7.0.0" + frontend_server_client: + dependency: transitive + description: + name: frontend_server_client + sha256: "408e3ca148b31c20282ad6f37ebfa6f4bdc8fede5b74bc2f08d9d92b55db3612" + url: "https://pub.dev" + source: hosted + version: "3.2.0" glob: dependency: transitive description: @@ -65,6 +121,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + http_multi_server: + dependency: transitive + description: + name: http_multi_server + sha256: "97486f20f9c2f7be8f514851703d0119c3596d14ea63227af6f7a481ef2b2f8b" + url: "https://pub.dev" + source: hosted + version: "3.2.1" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" io: dependency: transitive description: @@ -73,6 +145,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.4" + js: + dependency: transitive + description: + name: js + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + url: "https://pub.dev" + source: hosted + version: "0.6.7" json_annotation: dependency: transitive description: @@ -89,6 +169,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.0" + matcher: + dependency: transitive + description: + name: matcher + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + url: "https://pub.dev" + source: hosted + version: "0.12.16" meta: dependency: transitive description: @@ -97,6 +185,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.9.1" + mime: + dependency: transitive + description: + name: mime + sha256: e4ff8e8564c03f255408decd16e7899da1733852a9110a58fe6d1b817684a63e + url: "https://pub.dev" + source: hosted + version: "1.0.4" + node_preamble: + dependency: transitive + description: + name: node_preamble + sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db" + url: "https://pub.dev" + source: hosted + version: "2.0.2" package_config: dependency: transitive description: @@ -113,6 +217,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + pool: + dependency: transitive + description: + name: pool + sha256: "20fe868b6314b322ea036ba325e6fc0711a22948856475e2c2b6306e8ab39c2a" + url: "https://pub.dev" + source: hosted + version: "1.5.1" pub_semver: dependency: transitive description: @@ -129,6 +241,54 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.3" + shelf: + dependency: transitive + description: + name: shelf + sha256: ad29c505aee705f41a4d8963641f91ac4cee3c8fad5947e033390a7bd8180fa4 + url: "https://pub.dev" + source: hosted + version: "1.4.1" + shelf_packages_handler: + dependency: transitive + description: + name: shelf_packages_handler + sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e" + url: "https://pub.dev" + source: hosted + version: "3.0.2" + shelf_static: + dependency: transitive + description: + name: shelf_static + sha256: a41d3f53c4adf0f57480578c1d61d90342cd617de7fc8077b1304643c2d85c1e + url: "https://pub.dev" + source: hosted + version: "1.1.2" + shelf_web_socket: + dependency: transitive + description: + name: shelf_web_socket + sha256: "9ca081be41c60190ebcb4766b2486a7d50261db7bd0f5d9615f2d653637a84c1" + url: "https://pub.dev" + source: hosted + version: "1.0.4" + source_map_stack_trace: + dependency: transitive + description: + name: source_map_stack_trace + sha256: "84cf769ad83aa6bb61e0aa5a18e53aea683395f196a6f39c4c881fb90ed4f7ae" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + source_maps: + dependency: transitive + description: + name: source_maps + sha256: "708b3f6b97248e5781f493b765c3337db11c5d2c81c3094f10904bfa8004c703" + url: "https://pub.dev" + source: hosted + version: "0.10.12" source_span: dependency: transitive description: @@ -137,6 +297,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.10.0" + stack_trace: + dependency: transitive + description: + name: stack_trace + sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + url: "https://pub.dev" + source: hosted + version: "1.11.0" + stream_channel: + dependency: transitive + description: + name: stream_channel + sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + url: "https://pub.dev" + source: hosted + version: "2.1.1" string_scanner: dependency: transitive description: @@ -153,6 +329,70 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.1" + test: + dependency: "direct dev" + description: + name: test + sha256: "13b41f318e2a5751c3169137103b60c584297353d4b1761b66029bae6411fe46" + url: "https://pub.dev" + source: hosted + version: "1.24.3" + test_api: + dependency: transitive + description: + name: test_api + sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + url: "https://pub.dev" + source: hosted + version: "0.6.0" + test_core: + dependency: transitive + description: + name: test_core + sha256: "99806e9e6d95c7b059b7a0fc08f07fc53fabe54a829497f0d9676299f1e8637e" + url: "https://pub.dev" + source: hosted + version: "0.5.3" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" + vm_service: + dependency: transitive + description: + name: vm_service + sha256: f3743ca475e0c9ef71df4ba15eb2d7684eecd5c8ba20a462462e4e8b561b2e11 + url: "https://pub.dev" + source: hosted + version: "11.6.0" + watcher: + dependency: transitive + description: + name: watcher + sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8" + url: "https://pub.dev" + source: hosted + version: "1.1.0" + web_socket_channel: + dependency: transitive + description: + name: web_socket_channel + sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b + url: "https://pub.dev" + source: hosted + version: "2.4.0" + webkit_inspection_protocol: + dependency: transitive + description: + name: webkit_inspection_protocol + sha256: "67d3a8b6c79e1987d19d848b0892e582dbb0c66c57cc1fef58a177dd2aa2823d" + url: "https://pub.dev" + source: hosted + version: "1.2.0" yaml: dependency: transitive description: diff --git a/packages/shell_executor/pubspec.yaml b/packages/shell_executor/pubspec.yaml index bd3bc8f6..3c8f7a84 100644 --- a/packages/shell_executor/pubspec.yaml +++ b/packages/shell_executor/pubspec.yaml @@ -1,6 +1,6 @@ name: shell_executor description: A simple shell commands executor. -version: 0.1.3 +version: 0.1.4 homepage: https://distributor.leanflutter.org repository: https://github.com/leanflutter/flutter_distributor/tree/main/packages/shell_executor @@ -9,3 +9,4 @@ environment: dev_dependencies: dependency_validator: ^3.0.0 + test: ^1.23.1 diff --git a/packages/shell_executor/test/src/utils/path_expansion_test.dart b/packages/shell_executor/test/src/utils/path_expansion_test.dart new file mode 100644 index 00000000..fbcf92e9 --- /dev/null +++ b/packages/shell_executor/test/src/utils/path_expansion_test.dart @@ -0,0 +1,22 @@ +import 'package:shell_executor/src/utils/path_expansion.dart'; +import 'package:test/test.dart'; + +void main() { + Map 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'); + }); + }); +}