diff --git a/bin/expressions/src/evaluator.dart b/bin/expressions/src/evaluator.dart index 99ec34d..f38b7b8 100644 --- a/bin/expressions/src/evaluator.dart +++ b/bin/expressions/src/evaluator.dart @@ -122,6 +122,10 @@ class ExpressionEvaluator { return left / right(); case '%': return left % right(); + case 'in': + return (right() as List).contains(left); + case 'notIn': + return !(right() as List).contains(left); } throw ArgumentError( 'Unknown operator ${expression.operator} in expression'); diff --git a/bin/expressions/src/parser.dart b/bin/expressions/src/parser.dart index decdfb1..51c1109 100644 --- a/bin/expressions/src/parser.dart +++ b/bin/expressions/src/parser.dart @@ -123,7 +123,9 @@ class ExpressionParser { '-': 9, '*': 10, '/': 10, - '%': 10 + '%': 10, + 'in': 11, + 'notIn': 11, }; // This function is responsible for gobbling an individual expression, diff --git a/bin/pre_script.dart b/bin/pre_script.dart index bbf0029..385133d 100644 --- a/bin/pre_script.dart +++ b/bin/pre_script.dart @@ -2,6 +2,19 @@ import 'dart:io'; import 'parse_arguments.dart'; import 'expressions/expressions.dart'; +const FLAVORS = [ + 'test', + 'tw', + 'cn', +]; + +final _ctx = { + 'debug': 'debug', + 'release': 'release', + 'profile': 'profile', + 'default': 'default', +}; + String? exp; late String mode; late String flavor; @@ -19,6 +32,13 @@ void main(List arguments) { mode = args.mode; flavor = args.flavor; + _ctx.addEntries(FLAVORS.map((e) => MapEntry(e, e))); + + _ctx.addAll({ + 'mode': mode, + 'flavor': flavor, + }); + var rootDir = Directory('./'); rootDir.listSync().forEach(walkPath); } @@ -67,8 +87,7 @@ void walkPath(FileSystemEntity path) { state = STATE.none; tmp.clear(); } else { - if (evaluator.eval( - Expression.parse(exp!), {'mode': mode, 'flavor': flavor})) { + if (evaluator.eval(Expression.parse(exp!), _ctx)) { // 匹配到 tmp.clear(); state = STATE.caching; @@ -90,7 +109,6 @@ void walkPath(FileSystemEntity path) { if (modified) { file!.renameSync(path.path + '.bak'); File(path.path).writeAsStringSync(sb.toString(), flush: true); - print(sb.toString()); } } catch (e) { if (!(e is FileSystemException)) { diff --git a/flutter.sh b/flutter.sh index a97a938..9622c4f 100644 --- a/flutter.sh +++ b/flutter.sh @@ -6,6 +6,12 @@ SCRIPT_DIR=$(dirname "$SCRIPT_ABS") # shellcheck disable=SC2005 DART_EXE=$(command -v dart) +JUST_REPLACE=0 +for i in "$@" +do + [ "$i" == "--replace" ] && JUST_REPLACE=1 +done + if [[ ! -x "$DART_EXE" ]]; then echo "Can't find dart executable file !" return 1 @@ -30,23 +36,26 @@ fi ${DART_EXE} "$SCRIPT_DIR"/bin/pre_script.dart "$@" -flutter "$@" +if [[ "$JUST_REPLACE" == 0 ]]; then -${DART_EXE} "$SCRIPT_DIR"/bin/after_script.dart "$@" + flutter "$@" -if [[ -f "./.hooks/after_script.dart" ]]; then - ${DART_EXE} ./.hooks/after_script.dart "$@" -fi + ${DART_EXE} "$SCRIPT_DIR"/bin/after_script.dart "$@" -if [[ -f "./after_script.dart" ]]; then - ${DART_EXE} ./after_script.dart "$@" -fi + if [[ -f "./.hooks/after_script.dart" ]]; then + ${DART_EXE} ./.hooks/after_script.dart "$@" + fi -if [[ -f "./.hooks/after_script.sh" ]]; then - ./.hooks/after_script.sh "$@" -fi + if [[ -f "./after_script.dart" ]]; then + ${DART_EXE} ./after_script.dart "$@" + fi -if [[ -f "./after_script.sh" ]]; then - ./after_script.sh "$@" -fi + if [[ -f "./.hooks/after_script.sh" ]]; then + ./.hooks/after_script.sh "$@" + fi + + if [[ -f "./after_script.sh" ]]; then + ./after_script.sh "$@" + fi +fi