From 0de527a78c266a5da0d1eef1d3292a07d69a0772 Mon Sep 17 00:00:00 2001 From: melvin Date: Tue, 3 Dec 2013 09:38:14 +0800 Subject: [PATCH] awk script for autmatic refactoring of groovy script from anon class style to closure style --- scripts/convert-anon-class-to-closure.awk | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scripts/convert-anon-class-to-closure.awk diff --git a/scripts/convert-anon-class-to-closure.awk b/scripts/convert-anon-class-to-closure.awk new file mode 100644 index 0000000000..aa659c98f5 --- /dev/null +++ b/scripts/convert-anon-class-to-closure.awk @@ -0,0 +1,41 @@ +function leading_space() { + cnt = 0 + split($0, chars, "") + for (i = 1; i < length(chars); i++) { + if (chars[i] ~ " ") { + cnt++ + } else { + break; + } + } + return cnt +} + +/processTarget.*new Magic.*Action()/ { + gsub(",new Magic.*Action\\(\\) {", ", {") + gsub("new Magic.*Action\\(\\) {", "{") + state = 1 +} + +# remove terminating } +state == 2 && leading_space() == space - 4 { + state = 0 + next +} + +# remove one level of indentation +state == 2 && leading_space() >= space { + sub(" ", ""); +} + +# remove doAction with just param +state == 1 && /doAction/ { + gsub("public void doAction\\(", "") + gsub("\\) {", " ->") + space = leading_space() + 4; + state = 2 +} + +{ + print $0 +}