Repository navigation
Expand file tree
/
Copy pathmigrate-v2-symbols
More file actions
executable file
·214 lines (201 loc) · 9.79 KB
/
Copy pathmigrate-v2-symbols
File metadata and controls
executable file
·214 lines (201 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env bash
# Rewrite documented v1 and pre-release v2 base-bash-libs symbols to the v2
# `base_` names.
# This is a mechanical aid, not a compatibility layer; review the diff and run
# the consumer's tests after applying it.
usage() {
printf '%s\n' "Usage: migrate-v2-symbols [--check] <file>..." >&2
}
check_only=0
if [[ "${1-}" == "--check" ]]; then
check_only=1
shift
fi
if (($# == 0)); then
usage
exit 2
fi
files=("$@")
legacy_names=(
is_interactive check_bash_version import add_to_path dedupe_path print_path
set_log_level set_log_category_level log_is_enabled log_fatal log_error
log_warn log_info log_debug log_verbose log_info_file log_debug_file
log_verbose_file log_info_enter log_debug_enter log_verbose_enter
log_info_leave log_debug_leave log_verbose_leave print_error print_warn
print_info print_success print_bold print_message print_tty dump_trace
exit_if_error fatal_error is_dry_run std_run safe_mkdir safe_touch
safe_truncate safe_cd safe_unalias std_register_cleanup_hook
std_unregister_cleanup_hook std_register_cleanup_path
std_unregister_cleanup_path std_make_temp_file std_make_temp_dir
std_command_path std_function_exists assert_variable_name
assert_indexed_array assert_associative_array assert_function_exists
assert_not_null assert_integer assert_integer_range assert_arg_count
assert_command_exists assert_file_exists assert_executable assert_dir_exists
get_my_source_dir ask_yes_no wait_for_enter
file_section_exists file_section_needs_update update_file_section
git_detect_default_branch git_worktree_path_for_branch
git_list_worktree_branches git_branch_upstream git_branch_merged_to_ref
git_list_remote_branches git_update_repo git_get_current_branch
check_script_up_to_date gh_require_cli gh_auth_status_diagnostics
gh_report_command_failure gh_run gh_repo_from_remote_url
gh_infer_repo_from_origin gh_repo_default_branch gh_api_with_retry
str_lower str_upper str_ltrim str_rtrim str_trim str_contains
str_starts_with str_ends_with str_split str_join arg_parse list_append
list_prepend list_remove list_contains list_unique list_length
base_bash_die base_bash_resolve_path base_bash_package_root
base_bash_ensure_supported_bash base_bash_lib_dir_is_usable
base_bash_resolve_lib_dir base_bash_source_stdlib import_base_bash_lib
base_bash_run_script base_bash_usage
)
v2_names=(
base_std_is_interactive base_std_check_bash_version
base_std_import base_std_add_to_path
base_std_dedupe_path base_std_print_path
base_std_set_log_level base_std_set_log_category_level
base_std_log_is_enabled base_std_log_fatal
base_std_log_error base_std_log_warn
base_std_log_info base_std_log_debug
base_std_log_verbose base_std_log_info_file
base_std_log_debug_file base_std_log_verbose_file
base_std_log_info_enter base_std_log_debug_enter
base_std_log_verbose_enter base_std_log_info_leave
base_std_log_debug_leave base_std_log_verbose_leave
base_std_print_error base_std_print_warn
base_std_print_info base_std_print_success
base_std_print_bold base_std_print_message
base_std_print_tty base_std_dump_trace
base_std_exit_if_error base_std_fatal_error
base_std_is_dry_run base_std_run
base_std_safe_mkdir base_std_safe_touch
base_std_safe_truncate base_std_safe_cd
base_std_safe_unalias base_std_register_cleanup_hook
base_std_unregister_cleanup_hook
base_std_register_cleanup_path
base_std_unregister_cleanup_path base_std_make_temp_file
base_std_make_temp_dir base_std_command_path
base_std_function_exists base_std_assert_variable_name
base_std_assert_indexed_array
base_std_assert_associative_array
base_std_assert_function_exists base_std_assert_not_null
base_std_assert_integer base_std_assert_integer_range
base_std_assert_arg_count base_std_assert_command_exists
base_std_assert_file_exists base_std_assert_executable
base_std_assert_dir_exists base_std_get_my_source_dir
base_std_ask_yes_no base_std_wait_for_enter
base_file_section_exists
base_file_section_needs_update
base_file_update_file_section base_git_detect_default_branch
base_git_worktree_path_for_branch
base_git_list_worktree_branches base_git_branch_upstream
base_git_branch_merged_to_ref base_git_list_remote_branches
base_git_update_repo base_git_get_current_branch
base_git_check_script_up_to_date base_gh_require_cli
base_gh_auth_status_diagnostics
base_gh_report_command_failure base_gh_run
base_gh_repo_from_remote_url base_gh_infer_repo_from_origin
base_gh_repo_default_branch base_gh_api_with_retry
base_str_lower base_str_upper base_str_ltrim
base_str_rtrim base_str_trim base_str_contains
base_str_starts_with base_str_ends_with
base_str_split base_str_join base_arg_parse
base_list_append base_list_prepend
base_list_remove base_list_contains
base_list_unique base_list_length
base_launcher_die base_launcher_resolve_path
base_launcher_package_root base_launcher_ensure_supported_bash
base_launcher_lib_dir_is_usable base_launcher_resolve_lib_dir
base_launcher_source_stdlib base_launcher_import_base_bash_lib
base_launcher_run_script base_launcher_usage
)
global_legacy_names=(
__SCRIPT_ARGS__ __SCRIPT_DIR__ __color__ COLOR_BOLD COLOR_RED COLOR_GREEN
COLOR_YELLOW COLOR_BLUE COLOR_OFF LOG_DEBUG LOG_UTC
BASE_BASH_BOOTSTRAP_SOURCE BASE_CLI_PRIMARY_LOG BASE_GIT_PULL_MAX_ATTEMPTS
BASE_BASH_FILE_START_MARKER BASE_BASH_FILE_END_MARKER
BASE_BASH_FILE_NEW_CONTENT_FILE DRY_RUN __lib_std_sourced__
__lib_file_sourced__ __lib_git_sourced__ __lib_gh_sourced__
__lib_str_sourced__ __lib_arg_sourced__ __lib_list_sourced__
_log_levels _loggers_level_map _log_category_level_map
_log_primary_sink_failed_paths
)
global_v2_names=(
BASE_BASH_LIBS_SCRIPT_ARGS BASE_BASH_LIBS_SCRIPT_DIR
BASE_BASH_LIBS_STD_COLOR_ENABLED BASE_BASH_LIBS_STD_COLOR_BOLD
BASE_BASH_LIBS_STD_COLOR_RED BASE_BASH_LIBS_STD_COLOR_GREEN
BASE_BASH_LIBS_STD_COLOR_YELLOW BASE_BASH_LIBS_STD_COLOR_BLUE
BASE_BASH_LIBS_STD_COLOR_OFF BASE_BASH_LIBS_LOG_DEBUG
BASE_BASH_LIBS_LOG_UTC BASE_BASH_LIBS_BOOTSTRAP_SOURCE
BASE_BASH_LIBS_PRIMARY_LOG BASE_BASH_LIBS_GIT_PULL_MAX_ATTEMPTS
BASE_BASH_LIBS_FILE_START_MARKER BASE_BASH_LIBS_FILE_END_MARKER
BASE_BASH_LIBS_FILE_NEW_CONTENT_FILE BASE_BASH_LIBS_DRY_RUN
BASE_BASH_LIBS_STDLIB_LOADED BASE_BASH_LIBS_FILE_LOADED
BASE_BASH_LIBS_GIT_LOADED BASE_BASH_LIBS_GH_LOADED BASE_BASH_LIBS_STR_LOADED
BASE_BASH_LIBS_ARG_LOADED BASE_BASH_LIBS_LIST_LOADED
BASE_BASH_LIBS_STD_LOG_LEVELS BASE_BASH_LIBS_STD_LOGGER_LEVELS
BASE_BASH_LIBS_STD_LOG_CATEGORY_LEVELS BASE_BASH_LIBS_STD_LOG_FAILED_SINKS
)
internal_legacy_prefixes=(
__lib_std_require_supported_bash__ __std_ __log_ __print_ __init_colors__
__join_message__ __resolve_log_category_level__ __is_valid_variable_name__
__file_ __preserve_file_mode__ __git_ __gh_ __arg_ __list_ __str_
)
internal_v2_prefixes=(
__base_bash_libs_std_require_supported_bash__ __base_bash_libs_std_
__base_bash_libs_std_log_ __base_bash_libs_std_print_
__base_bash_libs_std_init_colors__ __base_bash_libs_std_join_message__
__base_bash_libs_std_resolve_log_category_level__
__base_bash_libs_std_is_valid_variable_name__ __base_bash_libs_file_
__base_bash_libs_file_preserve_file_mode__ __base_bash_libs_git_
__base_bash_libs_gh_ __base_bash_libs_arg_ __base_bash_libs_list_
__base_bash_libs_str_
)
rewrite_file() {
local file="$1" index old new
# #223 briefly shipped the longer pre-release function namespace. Rewrite
# it first so the legacy-name pass below can handle the original v1 names.
perl -pi -e 's/\bbase_bash_libs_std_/base_std_/g; s/\bbase_bash_libs_file_/base_file_/g; s/\bbase_bash_libs_git_/base_git_/g; s/\bbase_bash_libs_gh_/base_gh_/g; s/\bbase_bash_libs_str_/base_str_/g; s/\bbase_bash_libs_arg_/base_arg_/g; s/\bbase_bash_libs_list_/base_list_/g; s/\bbase_bash_libs_launcher_/base_launcher_/g; s/\bbase_bash_libs_init\b/base_init/g; s/\bbase_bash_libs_require_version\b/base_require_version/g' "$file"
# `DRY_RUN` and `dry_run` were both v1 spellings for the same global. Keep
# that one-to-many migration explicit so the parallel global map cannot
# shift every following marker name by one position.
perl -pi -e 's/(?<![A-Za-z0-9_])dry_run(?![A-Za-z0-9_])/BASE_BASH_LIBS_DRY_RUN/g' "$file"
for index in "${!legacy_names[@]}"; do
old="${legacy_names[$index]}"
new="${v2_names[$index]}"
perl -pi -e "s/\\b\\Q$old\\E\\b/$new/g" "$file"
done
for index in "${!global_legacy_names[@]}"; do
old="${global_legacy_names[$index]}"
new="${global_v2_names[$index]}"
perl -pi -e "s/\\b\\Q$old\\E\\b/$new/g" "$file"
done
for index in "${!internal_legacy_prefixes[@]}"; do
old="${internal_legacy_prefixes[$index]}"
new="${internal_v2_prefixes[$index]}"
perl -pi -e "s/(?<![A-Za-z0-9_])\\Q$old\\E/$new/g" "$file"
done
}
if ((check_only)); then
found=0
for file in "${files[@]}"; do
for old in "${legacy_names[@]}" "${global_legacy_names[@]}"; do
if rg -n --pcre2 "(?<![A-Za-z0-9_])\\Q$old\\E(?![A-Za-z0-9_])" "$file" >/dev/null 2>&1; then
printf '%s: legacy symbol %s\n' "$file" "$old" >&2
found=1
fi
done
for old in "${internal_legacy_prefixes[@]}"; do
if rg -n --pcre2 "(?<![A-Za-z0-9_])\\Q$old\\E" "$file" >/dev/null 2>&1; then
printf '%s: legacy internal prefix %s\n' "$file" "$old" >&2
found=1
fi
done
done
exit "$found"
fi
for file in "${files[@]}"; do
[[ -f "$file" ]] || {
printf '%s: file not found\n' "$file" >&2
exit 1
}
rewrite_file "$file"
done