Initial import
[experiments/php-drupal-console-code-refactoring.git] / addOption-null-shortname.sh
1 #!/bin/sh
2
3 # Change the second argument of "addOption" to null.
4 #
5 # Copyright (C) 2017  Antonio Ospite <ao2@ao2.it>
6
7 set -e
8 set -x
9
10 [ "x$1" = "x" ] && { echo "usage: $(basename $0) <drupal-console-dir>" 2>&1; exit 1; }
11
12 DRUPAL_CONSOLE_DIR="$1"
13 PHP_PARSER_RESULTS_DIR=__php_parser_results
14 LOG_FILE=null_shortname.log
15
16 echo "This will loose all unstaged changes, Continue?"
17 read response
18 case $response in
19   y|Y|yes|YES) ;;
20   *)           echo "Aborting" 2>&1; exit 1; ;;
21 esac
22
23 rm -rf "$PHP_PARSER_RESULTS_DIR"
24 mkdir "$PHP_PARSER_RESULTS_DIR"
25
26 rm -f "$LOG_FILE"
27
28 git -C "$DRUPAL_CONSOLE_DIR" checkout .
29
30 git -C "$DRUPAL_CONSOLE_DIR" grep -l addOption |
31 while read file;
32 do
33   PARSED_FILE="$(echo "$file" | sed -e 's/\//_/g')"
34   SRC_FILE="${DRUPAL_CONSOLE_DIR}/${file}"
35
36   # Do a first conversion with php-parser, this looses the original
37   # formatting, but it's robust because it uses an actual parser, so the
38   # result can be used to validate the sed solution below.
39   php addOption-null-shortname-php-parse.php "$SRC_FILE" > "${PHP_PARSER_RESULTS_DIR}/${PARSED_FILE}"
40
41   # The sed expression does this:
42   # - look for addOption;
43   # - append the next two lines to the pattern space (N;N;), to cover the case
44   #   when the arguments on different lines;
45   # - separate the arguments, but also consider what the characters after the
46   #   first comma are, sometimes there is a space and sometimes there is
47   #   a newline with the indentation following, and we want to keep those as
48   #   they were.
49   # - if it's false or an empty string, replace the second argument which
50   #   would be in \3, with null
51   # - keep everything else as it was.
52   #
53   # The sed commands after the substitution tell to go on (N;N;) if there is
54   # no substitution (t) and to print and delete the pattern (P;D;) when
55   # a substitution succeed.
56   #
57   # This covers the case when another pattern is in the three lines which are
58   # being analyzed, e.g:
59   #
60   #   ->addOption('quick', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.quick'))
61   #   ->addOption('debug', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.debug'))
62   #   ->addOption('html', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.html'))
63   #   ->addOption('xml', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.xml'))
64   #   ->addOption('raw', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.raw'))
65   #   ->addOption('vertical', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.vertical'))
66   #   ->addOption('batch', '', InputOption::VALUE_NONE, $this->trans('commands.database.query.options.batch'))
67   #
68   # See also https://unix.stackexchange.com/questions/26284
69   sed -e "/addOption/{N;N;s/addOption(\([^,]*\),\([ \n]*\)\(''\|false\),\(.*\)/addOption(\1,\2null,\4/;ty;N;N;:y;P;D}" -i "$SRC_FILE"
70
71   # Pretty print with php-parse the files converted with sed, so that they can be compared
72   # with the ones converted with addOption-null-shortname-php-parse.php from above.
73   php-parse  -p "$SRC_FILE" | grep -v "^==" > "${PHP_PARSER_RESULTS_DIR}/${PARSED_FILE}.sed"
74
75   # And print a report of the difference between the two conversion, if the
76   # sed command is correct there should be no difference at all.
77   echo "$file" >> "$LOG_FILE"
78   wdiff -3 "${PHP_PARSER_RESULTS_DIR}/${PARSED_FILE}" "${PHP_PARSER_RESULTS_DIR}/${PARSED_FILE}.sed" >> "$LOG_FILE" || true
79   echo >> "$LOG_FILE"
80   echo >> "$LOG_FILE"
81 done