From 202b738cb021af3e24868aab758da2f324af89f6 Mon Sep 17 00:00:00 2001 From: debuggerx Date: Wed, 17 Mar 2021 00:10:42 +0800 Subject: [PATCH] fix: bug. --- bin/flavors.dart | 5 +++++ bin/pre_script.dart | 54 ++++++++++++++++++++++++++--------------------------- 2 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 bin/flavors.dart diff --git a/bin/flavors.dart b/bin/flavors.dart new file mode 100644 index 0000000..aad79f5 --- /dev/null +++ b/bin/flavors.dart @@ -0,0 +1,5 @@ +const FLAVORS = [ + 'test', + 'tw', + 'cn', +]; \ No newline at end of file diff --git a/bin/pre_script.dart b/bin/pre_script.dart index 27ad591..8542efc 100644 --- a/bin/pre_script.dart +++ b/bin/pre_script.dart @@ -1,12 +1,7 @@ import 'dart:io'; import 'parse_arguments.dart'; import 'expressions/expressions.dart'; - -const FLAVORS = [ - 'test', - 'tw', - 'cn', -]; +import 'flavors.dart'; const BLACK_FILE_EXT = [ 'md', @@ -60,6 +55,7 @@ Match? ma; bool modified = false; const evaluator = const ExpressionEvaluator(); +late List lines; // vars for replace mode int currentLineIndex = 0; @@ -87,7 +83,8 @@ void walkPath(FileSystemEntity path) { currentTempOperations.clear(); } try { - file!.readAsLinesSync().forEach((line) { + lines = file!.readAsLinesSync(); + lines.forEach((line) { currentLineIndex++; ma = re.firstMatch(line); if (ma != null) { @@ -96,6 +93,9 @@ void walkPath(FileSystemEntity path) { exp = ma!.group(1); if (exp == "default") { if (isReplace) { + if (currentTempOperations.isNotEmpty && + !currentTempOperations.first.commented) + tempOperations.forEach((ele) => ele.commented = true); tempOperations.addAll(currentTempOperations); currentTempOperations.clear(); } @@ -131,13 +131,6 @@ void walkPath(FileSystemEntity path) { } tmp.clear(); } else { - if (isReplace) { - if (currentTempOperations.isNotEmpty && - !currentTempOperations.first.commented) - tempOperations.forEach((ele) => ele.commented = true); - tempOperations.addAll(currentTempOperations); - currentTempOperations.clear(); - } if (evaluator.eval(Expression.parse(exp!), _ctx)) { // 匹配到 tmp.clear(); @@ -145,6 +138,12 @@ void walkPath(FileSystemEntity path) { } else { state = STATE.notMatch; } + if (isReplace) { + if (state == STATE.caching) + tempOperations.forEach((ele) => ele.commented = true); + tempOperations.addAll(currentTempOperations); + currentTempOperations.clear(); + } } } else { // none状态时直接将line写入sb @@ -169,21 +168,20 @@ void walkPath(FileSystemEntity path) { operations.forEach((ele) { print('${ele.lineNumber} : ${ele.commented}'); }); - file!.readAsLines().then((lines) { - operations.forEach((operation) { - if (operation.commented && - !lines[operation.lineNumber - 1].startsWith(_commentReg)) - lines[operation.lineNumber - 1] = - '${' ' * operation.indent}// ${lines[operation.lineNumber - 1].substring(operation.indent)}'; - else if (!operation.commented && - lines[operation.lineNumber - 1].startsWith(_commentReg)) - lines[operation.lineNumber - 1] = - lines[operation.lineNumber - 1].replaceFirst('// ', ''); - }); - print(lines.join('\n')); - file!.deleteSync(); - File(path.path).writeAsStringSync(lines.join('\n'), flush: true); + + operations.forEach((operation) { + if (operation.commented && + !lines[operation.lineNumber - 1].startsWith(_commentReg)) + lines[operation.lineNumber - 1] = + '${' ' * operation.indent}// ${lines[operation.lineNumber - 1].substring(operation.indent)}'; + else if (!operation.commented && + lines[operation.lineNumber - 1].startsWith(_commentReg)) + lines[operation.lineNumber - 1] = + lines[operation.lineNumber - 1].replaceFirst('// ', ''); }); + print(lines.join('\n')); + file!.deleteSync(); + File(path.path).writeAsStringSync(lines.join('\n'), flush: true); } else { file!.renameSync(path.path + '.bak'); File(path.path).writeAsStringSync(sb.toString(), flush: true);