feat: add replace mode.

master
DebuggerX 4 years ago
parent 760e574728
commit 8c0b57386b

@ -1,17 +1,18 @@
class Args { class Args {
Args(this.mode, this.flavor); Args(this.mode, this.flavor, this.isReplaceMode);
String mode; String mode;
String flavor; String flavor;
bool isReplaceMode;
@override @override
String toString() { String toString() {
return 'Current mode is $mode, and flavor is ${flavor == '' ? 'default' : flavor}'; return 'Current mode is $mode, and flavor is ${flavor == '' ? 'default' : flavor}${isReplaceMode ? ' , for replace' : ''}';
} }
} }
Args parse(arguments) { Args parse(arguments) {
var args = Args('debug', 'default'); var args = Args('debug', 'default', false);
for (var value in arguments) { for (var value in arguments) {
if (value == '--release') { if (value == '--release') {
args.mode = 'release'; args.mode = 'release';
@ -21,6 +22,9 @@ Args parse(arguments) {
if (value == '--flavor') { if (value == '--flavor') {
args.flavor = arguments[arguments.indexOf('--flavor') + 1]; args.flavor = arguments[arguments.indexOf('--flavor') + 1];
} }
if (value == '--replace') {
args.isReplaceMode = true;
}
} }
print(args); print(args);
return args; return args;

@ -8,6 +8,10 @@ const FLAVORS = [
'cn', 'cn',
]; ];
const BLACK_FILE_EXT = [
'md',
];
final _ctx = { final _ctx = {
'debug': 'debug', 'debug': 'debug',
'release': 'release', 'release': 'release',
@ -18,12 +22,14 @@ final _ctx = {
String? exp; String? exp;
late String mode; late String mode;
late String flavor; late String flavor;
late bool isReplace;
enum STATE { enum STATE {
none, none,
notMatch, notMatch,
caching, caching,
replace, replace,
inDefault,
} }
void main(List<String> arguments) { void main(List<String> arguments) {
@ -31,6 +37,7 @@ void main(List<String> arguments) {
var args = parse(arguments); var args = parse(arguments);
mode = args.mode; mode = args.mode;
flavor = args.flavor; flavor = args.flavor;
isReplace = args.isReplaceMode;
_ctx.addEntries(FLAVORS.map((e) => MapEntry(e, e))); _ctx.addEntries(FLAVORS.map((e) => MapEntry(e, e)));
@ -54,39 +61,83 @@ bool modified = false;
const evaluator = const ExpressionEvaluator(); const evaluator = const ExpressionEvaluator();
// vars for replace mode
int currentLineIndex = 0;
List<ReplaceOperation> operations = [];
List<ReplaceOperation> tempOperations = [];
List<ReplaceOperation> currentTempOperations = [];
final _commentReg = RegExp(' +\/\/');
var lastIndent = -1;
void walkPath(FileSystemEntity path) { void walkPath(FileSystemEntity path) {
var stat = path.statSync(); var stat = path.statSync();
if (stat.type == FileSystemEntityType.directory) { if (stat.type == FileSystemEntityType.directory) {
Directory(path.path).listSync().forEach(walkPath); Directory(path.path).listSync().forEach(walkPath);
} else if (stat.type == FileSystemEntityType.file) { } else if (stat.type == FileSystemEntityType.file &&
BLACK_FILE_EXT.indexWhere((ele) => path.path.endsWith(ele)) < 0) {
file = File(path.path); file = File(path.path);
sb.clear(); sb.clear();
modified = false; modified = false;
if (isReplace) {
currentLineIndex = 0;
operations.clear();
tempOperations.clear();
currentTempOperations.clear();
}
try { try {
file!.readAsLinesSync().forEach((line) { file!.readAsLinesSync().forEach((line) {
currentLineIndex++;
ma = re.firstMatch(line); ma = re.firstMatch(line);
if (ma != null) { if (ma != null) {
lastIndent = line.indexOf('// #{{');
modified = true; modified = true;
exp = ma!.group(1); exp = ma!.group(1);
if (exp == "default") { if (exp == "default") {
if (isReplace) {
tempOperations.addAll(currentTempOperations);
currentTempOperations.clear();
}
// //
if (tmp.isNotEmpty) { if (tmp.isNotEmpty) {
sb.write(tmp); sb.write(tmp);
print([ print([
"${file!.path} modified", "${file!.path} modified" + '\n',
"-" * 80, "-" * 80 + '\n',
tmp.toString(), tmp.toString(),
"-" * 80, "-" * 80 + '\n',
].join("\n")); ].join());
state = STATE.replace; state = STATE.replace;
} else { } else {
state = STATE.none; state = STATE.inDefault;
} }
} else if (exp == "end") { } else if (exp == "end") {
// //
state = STATE.none; state = STATE.none;
if (isReplace) {
if (tmp.isEmpty) {
//
tempOperations.forEach((ele) => ele.commented = true);
} else {
//
currentTempOperations.forEach((ele) => ele.commented = true);
}
tempOperations.addAll(currentTempOperations);
operations.addAll(tempOperations);
tempOperations.clear();
currentTempOperations.clear();
}
tmp.clear(); tmp.clear();
} else { } 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)) { if (evaluator.eval(Expression.parse(exp!), _ctx)) {
// //
tmp.clear(); tmp.clear();
@ -97,18 +148,46 @@ void walkPath(FileSystemEntity path) {
} }
} else { } else {
// nonelinesb // nonelinesb
if (state == STATE.none) { if ([STATE.none, STATE.inDefault].contains(state)) {
sb.writeln(line); sb.writeln(line);
} }
// //
else if (state == STATE.caching) else if (state == STATE.caching)
tmp.writeln(line.replaceFirst('// ', '')); tmp.writeln(line.replaceFirst('// ', ''));
// //
if (isReplace &&
[STATE.notMatch, STATE.caching, STATE.replace, STATE.inDefault]
.contains(state)) {
currentTempOperations.add(ReplaceOperation(
currentLineIndex, lastIndent, state == STATE.notMatch));
}
} }
}); });
if (modified) { if (modified) {
file!.renameSync(path.path + '.bak'); if (isReplace) {
File(path.path).writeAsStringSync(sb.toString(), flush: true); 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);
});
} else {
file!.renameSync(path.path + '.bak');
File(path.path).writeAsStringSync(sb.toString(), flush: true);
}
} }
} catch (e) { } catch (e) {
if (!(e is FileSystemException)) { if (!(e is FileSystemException)) {
@ -117,3 +196,11 @@ void walkPath(FileSystemEntity path) {
} }
} }
} }
class ReplaceOperation {
int lineNumber;
int indent;
bool commented;
ReplaceOperation(this.lineNumber, this.indent, this.commented);
}

Loading…
Cancel
Save