1 | #!/bin/bash
|
2 |
|
3 | DEFAULT_INCLUDE='*.{ts,tsx,scss,less}'
|
4 | DEFAULT_EXCLUDE='{dist,build,node_modules}'
|
5 | DEFAULT_PREFIX='(?!(?<=public )|(?<=protected )|(?<=abstract )|(?<=private )|(?<=this\.)|(?<=-))'
|
6 |
|
7 | function usage() {
|
8 | cat << EOF
|
9 |
|
10 | Blueprint 3.0.0 Upgrade Script
|
11 |
|
12 | Usage
|
13 |
|
14 | $0 [--path=path] [--include=glob] [--exclude=glob] [--prefix=regexp]
|
15 |
|
16 | Description
|
17 |
|
18 | This script finds instances of various renamed methods and properties and
|
19 | replaces them in place. This includes renames for React props, css classes,
|
20 | and enum constants.
|
21 |
|
22 | Options
|
23 |
|
24 | -h,--help
|
25 |
|
26 | Display this message and exit.
|
27 |
|
28 | --path=.
|
29 |
|
30 | The path where the recursive search begins, relative to the current
|
31 | working directory.
|
32 |
|
33 | --include=$DEFAULT_INCLUDE
|
34 |
|
35 | A glob string to match specific file extensions in the search path.
|
36 |
|
37 | --exclude=$DEFAULT_EXCLUDE
|
38 |
|
39 | A glob string to omit specific directories from the search path.
|
40 |
|
41 | --prefix=$DEFAULT_PREFIX
|
42 |
|
43 | A regexp prefix for each find/replace prop string. The default includes
|
44 | groups of non-capturing negative lookbehinds. This helps limit the
|
45 | renames to props and not class methods. If you are applying this script
|
46 | to files other than typescript (e.g. markdown), you should probably set
|
47 | this to ''.
|
48 |
|
49 | EOF
|
50 | }
|
51 |
|
52 |
|
53 | for i in "$@" ; do
|
54 | case $i in
|
55 | --path=*)
|
56 | SEARCH_PATH="${i#*=}"
|
57 | shift
|
58 | ;;
|
59 | --include=*)
|
60 | INCLUDE_GLOB="${i#*=}"
|
61 | shift
|
62 | ;;
|
63 | --exclude=*)
|
64 | EXCLUDE_GLOB="${i#*=}"
|
65 | shift
|
66 | ;;
|
67 | --prefix=*)
|
68 | PREFIX="${i#*=}"
|
69 | shift
|
70 | ;;
|
71 | -h|--help)
|
72 | usage
|
73 | exit 1
|
74 | ;;
|
75 | *)
|
76 | ;;
|
77 | esac
|
78 | done
|
79 |
|
80 |
|
81 | SEARCH_PATH=${SEARCH_PATH:-'.'}
|
82 | INCLUDE_GLOB=${INCLUDE_GLOB:-$DEFAULT_INCLUDE}
|
83 | EXCLUDE_GLOB=${EXCLUDE_GLOB:-$DEFAULT_EXCLUDE}
|
84 | PREFIX=${PREFIX:-$DEFAULT_PREFIX}
|
85 |
|
86 | RED='\033[0;31m'
|
87 | GREEN='\033[0;32m'
|
88 | BLUE='\033[0;34m'
|
89 | RESET='\033[0m'
|
90 |
|
91 | function matchingFiles() {
|
92 |
|
93 |
|
94 | cmd="grep \
|
95 | --recursive \
|
96 | --files-with-matches \
|
97 | --include=$INCLUDE_GLOB \
|
98 | --exclude-dir=$EXCLUDE_GLOB \
|
99 | . \
|
100 | ${SEARCH_PATH} \
|
101 | | xargs perl -lne 'if (/$1/) { print \$ARGV; close ARGV }'
|
102 | "
|
103 | echo "$(eval $cmd)"
|
104 | }
|
105 |
|
106 | function rename() {
|
107 |
|
108 | fromString=$1
|
109 | toString=$2
|
110 | findRegexp=${3:-$fromString}
|
111 | files=$(matchingFiles "$findRegexp")
|
112 |
|
113 | if [[ -z ${files// } ]]; then
|
114 | echo -e "No files contain ${BLUE}${fromString}${RESET}"
|
115 | else
|
116 | count=$(echo "$files" | wc -l | awk '{print $1}')
|
117 | echo -e "Renaming ${BLUE}${fromString}${RESET} -> ${GREEN}${toString}${RESET} in ${BLUE}${count}${RESET} files"
|
118 |
|
119 |
|
120 | echo "$files" | while read -r file ; do
|
121 | echo -n "$file ... "
|
122 |
|
123 |
|
124 | perl -p -i -e "s/$findRegexp/$toString/g" "$file"
|
125 | echo -e "${GREEN}done${RESET}"
|
126 | done
|
127 | fi
|
128 | }
|
129 |
|
130 | function warn() {
|
131 |
|
132 | fromString=$1
|
133 | toString=$2
|
134 | findRegexp=${3:-$fromString}
|
135 | files=$(matchingFiles "$findRegexp")
|
136 |
|
137 | if [[ -z ${files// } ]]; then
|
138 | echo -e "No files contain ${BLUE}${fromString}${RESET}"
|
139 | else
|
140 | count=$(echo "$files" | wc -l | awk '{print $1}')
|
141 | echo ""
|
142 | echo -e "${RED}WARNING: Skipping ${BLUE}${fromString}${RESET} -> ${GREEN}${toString}${RESET}"
|
143 | echo -e "${RED}Can't safely change ${BLUE}${fromString}${RED}, you'll need to manually rename/remove this string.${RESET}"
|
144 | echo -e "${RED}It was found in these files ${BLUE}${count}${RED} files:${RESET}"
|
145 | echo "$files"
|
146 | fi
|
147 | }
|
148 |
|
149 | function renameProp() {
|
150 |
|
151 | rename "$1" "$2" "$PREFIX\\b$1\\b"
|
152 | }
|
153 |
|
154 | function warnProp() {
|
155 | warn "$1" "$2" "$PREFIX\\b$1\\b"
|
156 | }
|
157 |
|
158 | function renamePartialClass() {
|
159 |
|
160 | rename "$1" "$2" "\\b$1"
|
161 | }
|
162 |
|
163 | function renameString() {
|
164 |
|
165 | rename "$1" "$2" "\\b$1\\b"
|
166 | }
|
167 |
|
168 | echo "
|
169 | Blueprint 3.0.0 Upgrade Script
|
170 | "
|
171 |
|
172 | renameProp visual icon
|
173 | renameProp didClose onClosed
|
174 | renameProp didOpen onOpened
|
175 | renameProp popoverDidClose onClosed
|
176 | renameProp popoverDidOpen onOpened
|
177 | renameProp popoverWillClose onClosing
|
178 | renameProp popoverWillOpen onOpening
|
179 | rename 'requiredLabel={true}' "labelInfo=\"(required)\""
|
180 | renameProp requiredLabel labelInfo
|
181 | renameProp rootElementTag wrapperTagName
|
182 | renameProp targetElementTag targetTagName
|
183 | renameProp tooltipClassName popoverClassName
|
184 |
|
185 |
|
186 | renameString "Classes\.CALLOUT_TITLE" "Classes.HEADING"
|
187 | renameString "Classes\.DIALOG_TITLE" "Classes.HEADING"
|
188 | renameString "Classes\.HOTKEY_GROUP" "Classes.HEADING"
|
189 | renameString "Classes\.NON_IDEAL_STATE_TITLE" "Classes.HEADING"
|
190 | renameString "Classes\.UI_TEXT_LARGE" "Classes.UI_TEXT, Classes.TEXT_LARGE"
|
191 | renameString "Classes\.RUNNING_TEXT" "Classes.RUNNING_TEXT, Classes.TEXT_LARGE"
|
192 | renameString "Classes\.RUNNING_TEXT_SMALL" "Classes.RUNNING_TEXT"
|
193 |
|
194 |
|
195 | renameString "Classes\.TAG_REMOVABLE,?" ''
|
196 | renameString "Classes\.NON_IDEAL_STATE_(ACTION|DESCRIPTION|ICON),?" ''
|
197 | renameString "Classes\.SPINNER_SVG_CONTAINER,?" ''
|
198 | renameString "Classes\.SVG_SPINNER,?" ''
|
199 | warn SVGSpinner 'DELETED. Spinner now supports usage in an SVG.'
|
200 | warn SVGPopover 'DELETED. Set *TagName props to SVG elements.'
|
201 | warn SVGTooltip 'DELETED. Set *TagName props to SVG elements.'
|
202 | warn "\\bTable\\b" '@blueprintjs/core Table component renamed to HTMLTable (@blueprintjs/table package unchanged).'
|
203 |
|
204 |
|
205 | renameProp CollapseFrom Boundary
|
206 | renameProp DateRangeBoundary Boundary
|
207 | renameProp TimePickerPrecision TimePrecision
|