UNPKG

5.88 kBapplication/x-shView Raw
1#!/bin/bash
2
3DEFAULT_INCLUDE='*.{ts,tsx,scss,less}'
4DEFAULT_EXCLUDE='{dist,build,node_modules}'
5DEFAULT_PREFIX='(?!(?<=public )|(?<=protected )|(?<=abstract )|(?<=private )|(?<=this\.)|(?<=-))'
6
7function usage() {
8 cat << EOF
9
10Blueprint 3.0.0 Upgrade Script
11
12Usage
13
14 $0 [--path=path] [--include=glob] [--exclude=glob] [--prefix=regexp]
15
16Description
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
22Options
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
49EOF
50}
51
52# Arguments
53for 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
78done
79
80# Default argument values
81SEARCH_PATH=${SEARCH_PATH:-'.'}
82INCLUDE_GLOB=${INCLUDE_GLOB:-$DEFAULT_INCLUDE}
83EXCLUDE_GLOB=${EXCLUDE_GLOB:-$DEFAULT_EXCLUDE}
84PREFIX=${PREFIX:-$DEFAULT_PREFIX}
85
86RED='\033[0;31m'
87GREEN='\033[0;32m'
88BLUE='\033[0;34m'
89RESET='\033[0m'
90
91function matchingFiles() {
92 # Find all files containing regexp. Use grep's glob syntax for
93 # include/exclude paths. Use perl's regexp syntax.
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
106function rename() {
107 # Name parameters
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 # Iterate and search&replace
120 echo "$files" | while read -r file ; do
121 echo -n "$file ... "
122
123 # Search&replace in place with perl
124 perl -p -i -e "s/$findRegexp/$toString/g" "$file"
125 echo -e "${GREEN}done${RESET}"
126 done
127 fi
128}
129
130function warn() {
131 # Name parameters
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
149function renameProp() {
150 # Add prefix and word boundaries to search string
151 rename "$1" "$2" "$PREFIX\\b$1\\b"
152}
153
154function warnProp() {
155 warn "$1" "$2" "$PREFIX\\b$1\\b"
156}
157
158function renamePartialClass() {
159 # Don't add word boundary so that partial css classnames rename
160 rename "$1" "$2" "\\b$1"
161}
162
163function renameString() {
164 # Add simple word boundaries
165 rename "$1" "$2" "\\b$1\\b"
166}
167
168echo "
169Blueprint 3.0.0 Upgrade Script
170"
171
172renameProp visual icon
173renameProp didClose onClosed
174renameProp didOpen onOpened
175renameProp popoverDidClose onClosed
176renameProp popoverDidOpen onOpened
177renameProp popoverWillClose onClosing
178renameProp popoverWillOpen onOpening
179rename 'requiredLabel={true}' "labelInfo=\"(required)\""
180renameProp requiredLabel labelInfo
181renameProp rootElementTag wrapperTagName
182renameProp targetElementTag targetTagName
183renameProp tooltipClassName popoverClassName
184
185# Classes constants
186renameString "Classes\.CALLOUT_TITLE" "Classes.HEADING"
187renameString "Classes\.DIALOG_TITLE" "Classes.HEADING"
188renameString "Classes\.HOTKEY_GROUP" "Classes.HEADING"
189renameString "Classes\.NON_IDEAL_STATE_TITLE" "Classes.HEADING"
190renameString "Classes\.UI_TEXT_LARGE" "Classes.UI_TEXT, Classes.TEXT_LARGE"
191renameString "Classes\.RUNNING_TEXT" "Classes.RUNNING_TEXT, Classes.TEXT_LARGE"
192renameString "Classes\.RUNNING_TEXT_SMALL" "Classes.RUNNING_TEXT"
193
194# Deleted things
195renameString "Classes\.TAG_REMOVABLE,?" ''
196renameString "Classes\.NON_IDEAL_STATE_(ACTION|DESCRIPTION|ICON),?" ''
197renameString "Classes\.SPINNER_SVG_CONTAINER,?" ''
198renameString "Classes\.SVG_SPINNER,?" ''
199warn SVGSpinner 'DELETED. Spinner now supports usage in an SVG.'
200warn SVGPopover 'DELETED. Set *TagName props to SVG elements.'
201warn SVGTooltip 'DELETED. Set *TagName props to SVG elements.'
202warn "\\bTable\\b" '@blueprintjs/core Table component renamed to HTMLTable (@blueprintjs/table package unchanged).'
203
204# String enums
205renameProp CollapseFrom Boundary
206renameProp DateRangeBoundary Boundary
207renameProp TimePickerPrecision TimePrecision