├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── bin ├── .editorconfig └── gen_source_guard ├── docs ├── logo-social-original.png ├── logo-social.png ├── logo.meta.txt └── logo.png ├── lib ├── common_utils.sh ├── java_utils.sh ├── maven_utils.sh ├── prepare_jdks.sh └── trap_error_info.sh └── templates └── integration_test.template.sh /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | tab_width = 4 8 | indent_style = space 9 | trim_trailing_whitespace = true 10 | 11 | [*.xml] 12 | indent_size = 4 13 | indent_style = tab 14 | 15 | [*.{sh,ps1,yml}] 16 | indent_size = 2 17 | 18 | [*.{md,mkd,markdown}] 19 | trim_trailing_whitespace = false 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | !templates/ 2 | 3 | ################################################# 4 | # Programming Language 5 | ################################################# 6 | 7 | ### Python template 8 | # Byte-compiled / optimized / DLL files 9 | __pycache__/ 10 | *.py[cod] 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | env/ 18 | build/ 19 | develop-eggs/ 20 | dist/ 21 | downloads/ 22 | eggs/ 23 | .eggs/ 24 | lib/ 25 | lib64/ 26 | parts/ 27 | sdist/ 28 | var/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | 33 | # PyInstaller 34 | # Usually these files are written by a python script from a template 35 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 36 | *.manifest 37 | *.spec 38 | 39 | # Installer logs 40 | pip-log.txt 41 | pip-delete-this-directory.txt 42 | 43 | # Unit test / coverage reports 44 | htmlcov/ 45 | .tox/ 46 | .coverage 47 | .coverage.* 48 | .cache 49 | nosetests.xml 50 | coverage.xml 51 | *,cover 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | 60 | # Sphinx documentation 61 | docs/_build/ 62 | 63 | .ipynb_checkpoints/ 64 | 65 | ### Ruby template 66 | *.gem 67 | *.rbc 68 | /.config 69 | /coverage/ 70 | /InstalledFiles 71 | /pkg/ 72 | /spec/reports/ 73 | /test/tmp/ 74 | /test/version_tmp/ 75 | 76 | ## Specific to RubyMotion: 77 | .dat* 78 | .repl_history 79 | 80 | ## Documentation cache and generated files: 81 | /.yardoc/ 82 | /_yardoc/ 83 | /doc/ 84 | /rdoc/ 85 | 86 | ## Environment normalisation: 87 | /.bundle/ 88 | /vendor/bundle 89 | /lib/bundler/man/ 90 | 91 | # for a library or gem, you might want to ignore these files since the code is 92 | # intended to run in multiple environments; otherwise, check them in: 93 | # Gemfile.lock 94 | # .ruby-version 95 | # .ruby-gemset 96 | 97 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 98 | .rvmrc 99 | 100 | 101 | ### Node template 102 | # Logs 103 | logs 104 | 105 | # Runtime data 106 | pids 107 | *.pid 108 | *.seed 109 | 110 | # Directory for instrumented libs generated by jscoverage/JSCover 111 | lib-cov 112 | 113 | # Coverage directory used by tools like istanbul 114 | coverage 115 | 116 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 117 | .grunt 118 | 119 | # node-waf configuration 120 | .lock-wscript 121 | 122 | # Compiled binary addons (http://nodejs.org/api/addons.html) 123 | build/Release 124 | 125 | # Dependency directory 126 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 127 | node_modules 128 | 129 | 130 | ################################################# 131 | # IDE/Editor 132 | ################################################# 133 | 134 | ### JetBrains template 135 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 136 | 137 | *.iml 138 | 139 | ## Directory-based project format: 140 | .idea/ 141 | # if you remove the above rule, at least ignore the following: 142 | 143 | # User-specific stuff: 144 | # .idea/workspace.xml 145 | # .idea/tasks.xml 146 | # .idea/dictionaries 147 | 148 | # Sensitive or high-churn files: 149 | # .idea/dataSources.ids 150 | # .idea/dataSources.xml 151 | # .idea/sqlDataSources.xml 152 | # .idea/dynamic.xml 153 | # .idea/uiDesigner.xml 154 | 155 | # Gradle: 156 | # .idea/gradle.xml 157 | # .idea/libraries 158 | 159 | # Mongo Explorer plugin: 160 | # .idea/mongoSettings.xml 161 | 162 | ## File-based project format: 163 | *.ipr 164 | *.iws 165 | 166 | ## Plugin-specific files: 167 | 168 | # IntelliJ 169 | /out/ 170 | 171 | # mpeltonen/sbt-idea plugin 172 | .idea_modules/ 173 | 174 | # JIRA plugin 175 | atlassian-ide-plugin.xml 176 | 177 | # Crashlytics plugin (for Android Studio and IntelliJ) 178 | com_crashlytics_export_strings.xml 179 | crashlytics.properties 180 | crashlytics-build.properties 181 | 182 | 183 | ### Eclipse template 184 | *.pydevproject 185 | .metadata 186 | .gradle 187 | tmp/ 188 | *.bak 189 | *.swp 190 | *~.nib 191 | .settings/ 192 | .loadpath 193 | 194 | # Eclipse Core 195 | .project 196 | 197 | # External tool builders 198 | .externalToolBuilders/ 199 | 200 | # Locally stored "Eclipse launch configurations" 201 | *.launch 202 | 203 | # CDT-specific 204 | .cproject 205 | 206 | # JDT-specific (Eclipse Java Development Tools) 207 | .classpath 208 | 209 | # PDT-specific 210 | .buildpath 211 | 212 | # sbteclipse plugin 213 | .target 214 | 215 | # TeXlipse plugin 216 | .texlipse 217 | 218 | .factorypath 219 | 220 | 221 | ### SublimeText template 222 | # cache files for sublime text 223 | *.tmlanguage.cache 224 | *.tmPreferences.cache 225 | *.stTheme.cache 226 | 227 | # workspace files are user-specific 228 | *.sublime-workspace 229 | 230 | # project files should be checked into the repository, unless a significant 231 | # proportion of contributors will probably not be using SublimeText 232 | # *.sublime-project 233 | 234 | # sftp configuration file 235 | sftp-config.json 236 | 237 | 238 | ### Vim template 239 | [._]*.s[a-w][a-z] 240 | [._]s[a-w][a-z] 241 | *.un~ 242 | Session.vim 243 | .netrwhist 244 | *~ 245 | 246 | 247 | ### vs code 248 | .vscode/* 249 | !.vscode/settings.json 250 | !.vscode/tasks.json 251 | !.vscode/launch.json 252 | !.vscode/extensions.json 253 | *.code-workspace 254 | 255 | # Local History for Visual Studio Code 256 | .history/ 257 | 258 | 259 | ################################################# 260 | # OS 261 | ################################################# 262 | 263 | ### OSX template 264 | .DS_Store 265 | .AppleDouble 266 | .LSOverride 267 | 268 | # Icon must end with two \r 269 | Icon 270 | 271 | 272 | ### Linux template 273 | # KDE directory preferences 274 | .directory 275 | 276 | # Linux trash folder which might appear on any partition or disk 277 | .Trash-* 278 | 279 | 280 | ### Windows template 281 | # Windows image file caches 282 | Thumbs.db 283 | ehthumbs.db 284 | ehthumbs_vista.db 285 | 286 | # Folder config file 287 | Desktop.ini 288 | 289 | # Recycle Bin used on file shares 290 | $RECYCLE.BIN/ 291 | 292 | # Windows Installer files 293 | *.cab 294 | *.msi 295 | *.msm 296 | *.msp 297 | 298 | # Windows shortcuts 299 | *.lnk 300 | 301 | 302 | ################################################# 303 | # Repo customization config 304 | ################################################# 305 | 306 | !/bin/ 307 | !/lib/ 308 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | 3 | Apache License 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "[]" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright [yyyy] [name of copyright owner] 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #
🚼 Bash Buddy
2 | 3 |

4 | License 5 | GitHub release 6 | GitHub Stars 7 | GitHub Forks 8 | GitHub issues 9 | GitHub repo size 10 |

11 | 12 | 🚼 Bash Buddy(aka. BaBy) contains `bash` libs and tools that extracted from `CI` scripts of my open-source projects. 13 | 14 | ----------------------------------- 15 | 16 | 17 | 18 | 19 | - [🗂 Lib](#-lib) 20 | - [`trap_error_info.sh`](#trap_error_infosh) 21 | - [`common_utils.sh`](#common_utilssh) 22 | - [`java_utils.sh`](#java_utilssh) 23 | - [`maven_utils.sh`](#maven_utilssh) 24 | - [`prepare_jdks.sh`](#prepare_jdkssh) 25 | - [⚒️ Tool](#-tool) 26 | - [`gen_source_guard`](#gen_source_guard) 27 | 28 | 29 | 30 | ----------------------------------- 31 | 32 | # 🗂 Lib 33 | 34 | ## [`trap_error_info.sh`](lib/trap_error_info.sh) 35 | 36 | a common lib to show trapped error info including stack trace. 37 | 38 | provide function `trap_error_info::register_show_error_info_handler` 39 | to register the error-trap handler which show error info when trapped error. 40 | 41 | by default, auto call `trap_error_info::register_show_error_info_handler` when source this script; disable by 42 | define `TRAP_ERROR_NO_AUTO_REGISTER` var. 43 | 44 | api functions: 45 | 46 | - `trap_error_info::get_stack_trace` 47 | - `trap_error_info::register_show_error_info_handler` 48 | 49 | ## [`common_utils.sh`](lib/common_utils.sh) 50 | 51 | common util functions. 52 | 53 | use short namespace `cu`, since these functions will be used frequently. 54 | 55 | api functions: 56 | 57 | - simple color print functions: 58 | - `cu::red_echo` 59 | - `cu::yellow_echo` 60 | - `cu::blue_echo` 61 | - `cu::head_line_echo` 62 | - validation functions: 63 | - `cu::is_number_string` 64 | - `cu::is_blank_string` 65 | - version related functions: 66 | - `cu::version_le` 67 | - `cu::version_lt` 68 | - `cu::version_ge` 69 | - `cu::version_gt` 70 | - `cu::is_version_match` 71 | - `cu::get_latest_version_match` 72 | - `cu::get_oldest_version_match` 73 | - execution helper functions: 74 | - `cu::log_then_run` 75 | - `cu::loose_run` 76 | - `cu::print_calling_command_line` 77 | - `cu::die` 78 | 79 | ## [`java_utils.sh`](lib/javautils.sh) 80 | 81 | java util functions. 82 | 83 | api functions: 84 | 85 | - `jvu::get_java_version` 86 | - `jvu::switch_to_jdk` 87 | - `jvu::java_cmd` 88 | 89 | ## [`maven_utils.sh`](lib/maven_utils.sh) 90 | 91 | maven util functions for build. 92 | 93 | api functions: 94 | 95 | - maven operation functions: 96 | - `mvu::mvn_cmd` 97 | 98 | ## [`prepare_jdks.sh`](lib/prepare_jdks.sh) 99 | 100 | a lib to prepare jdks by [sdkman](https://sdkman.io/). 101 | 102 | api functions: 103 | 104 | - `prepare_jdks::switch_to_jdk` 105 | - `prepare_jdks::prepare_jdks` 106 | - `prepare_jdks::install_jdk_by_sdkman` 107 | - `prepare_jdks::load_sdkman` 108 | - `prepare_jdks::install_sdkman` 109 | 110 | # ⚒️ Tool 111 | 112 | ## [`gen_source_guard`](bin/gen_source_guard) 113 | 114 | Generate source guard to bash lib scripts. 115 | 116 | Example: 117 | 118 | ```sh 119 | $ gen_source_guard 120 | #_ source guard begin _# 121 | [ -n "${source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828:+has_value}" ] && return 122 | source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828=$(realpath -- "${BASH_SOURCE[0]}") 123 | # the value of source guard is the canonical dir path of this script 124 | readonly source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828=${source_guard_0EDD6400_96EC_43E4_871A_E65F6781B828%/*} 125 | #_ source guard end _# 126 | ``` 127 | -------------------------------------------------------------------------------- /bin/.editorconfig: -------------------------------------------------------------------------------- 1 | [*] 2 | indent_size = 2 3 | -------------------------------------------------------------------------------- /bin/gen_source_guard: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # gen a source guard code snippet for a shell lib file. 4 | # 5 | set -eEuo pipefail 6 | 7 | p_uuid_gen() { 8 | if command -v uuidgen &>/dev/null; then 9 | uuidgen 10 | else 11 | python3 -c 'import uuid; print(str(uuid.uuid4()).upper())' 12 | fi 13 | } 14 | 15 | new_source_guard_var_name() { 16 | local uuid 17 | uuid=$(p_uuid_gen) 18 | uuid=${uuid//-/_} 19 | 20 | echo "source_guard_$uuid" 21 | } 22 | 23 | new_source_guard() { 24 | local source_guard_var_name 25 | source_guard_var_name="$(new_source_guard_var_name)" 26 | 27 | cat < 105 | cu::version_le() { 106 | (($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*" 107 | 108 | local ver=$1 109 | local destVer=$2 110 | 111 | [ "$ver" = "$destVer" ] && return 0 112 | 113 | [ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$ver" ] 114 | } 115 | 116 | # version comparison, is version1 less than version2? 117 | # 118 | # usage: 119 | # cu::version_lt 120 | cu::version_lt() { 121 | (($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*" 122 | 123 | local ver=$1 124 | local destVer=$2 125 | 126 | [ "$ver" = "$destVer" ] && return 1 127 | 128 | [ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$ver" ] 129 | } 130 | 131 | # version comparison, is version1 greater than or equal to version2? 132 | # 133 | # usage: 134 | # cu::version_ge 135 | cu::version_ge() { 136 | (($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*" 137 | 138 | local ver=$1 139 | local destVer=$2 140 | 141 | [ "$ver" = "$destVer" ] && return 0 142 | 143 | [ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$destVer" ] 144 | } 145 | 146 | # version comparison, is version1 greater than version2? 147 | # 148 | # usage: 149 | # cu::version_gt 150 | cu::version_gt() { 151 | (($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*" 152 | 153 | local ver=$1 154 | local destVer=$2 155 | 156 | [ "$ver" = "$destVer" ] && return 1 157 | 158 | [ "$(printf '%s\n' "$ver" "$destVer" | sort -V | head -n1)" = "$destVer" ] 159 | } 160 | 161 | # version match, is version match version pattern? 162 | # 163 | # usage: 164 | # cu::version_gt 165 | # 166 | # example: 167 | # cu::is_version_match 11.0.1-p1 11 # true 168 | # cu::is_version_match 11.0.1-p1 11.0 # true 169 | # cu::is_version_match 11.0.1-p1 11.0.1 # true 170 | # cu::is_version_match 11.0.1-p1 11.0.1-p1 # true 171 | # 172 | # cu::is_version_match 11.0.1-p1 10 # false 173 | # cu::is_version_match 11.0.1-p1 12 # false 174 | # cu::is_version_match 11.0.1-p1 11.1 # false 175 | # cu::is_version_match 11.0.1-p1 11.0.0 # false 176 | # cu::is_version_match 11.0.1-p1 11.0.1-rc # false 177 | cu::is_version_match() { 178 | (($# == 2)) || cu::die "${FUNCNAME[0]} requires exact 2 arguments! But provided $#: $*" 179 | 180 | local ver="$1" version_pattern="$2" 181 | [[ "$ver" == "$version_pattern" || "$ver" == "$version_pattern"[.-]* ]] 182 | } 183 | 184 | cu::_get_first_version_match() { 185 | (($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 186 | 187 | local version_pattern="$1" ver 188 | while read -r ver; do 189 | if cu::is_version_match "$ver" "$version_pattern"; then 190 | echo "$ver" 191 | # drain the rest content of stdin 192 | cat >/dev/null 193 | return 194 | fi 195 | done 196 | } 197 | 198 | # get the latest version of versions(one version per line) from stdin 199 | # 200 | # usage: 201 | # cu::get_latest_version_match 202 | cu::get_latest_version_match() { 203 | (($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 204 | 205 | local -r version_pattern="$1" 206 | sort -V -r | cu::_get_first_version_match "$version_pattern" 207 | } 208 | 209 | # get the oldest version of versions(one version per line) from stdin 210 | # 211 | # usage: 212 | # cu::get_oldest_version_match 213 | cu::get_oldest_version_match() { 214 | (($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 215 | 216 | local -r version_pattern="$1" 217 | sort -V | cu::_get_first_version_match "$version_pattern" 218 | } 219 | 220 | ################################################################################ 221 | # execution helper functions 222 | ################################################################################ 223 | 224 | # log the command line, then run it. 225 | # 226 | # usage: 227 | # cu::log_then_run command_to_run command_args... 228 | # 229 | # example: 230 | # cu::log_then_run echo hello world 231 | cu::log_then_run() { 232 | local simple_mode=false 233 | [ "$1" = "-s" ] && { 234 | simple_mode=true 235 | shift 236 | } 237 | 238 | (($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided" 239 | 240 | if $simple_mode; then 241 | echo "Run under work directory $PWD : $(cu::print_calling_command_line "$@")" >&2 242 | "$@" 243 | else 244 | cu::blue_echo "Run under work directory $PWD :" >&2 245 | cu::blue_echo "$(cu::print_calling_command_line "$@")" >&2 246 | time "$@" 247 | fi 248 | } 249 | 250 | cu::loose_run() { 251 | (($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided" 252 | 253 | set +eEuo pipefail 254 | "$@" 255 | local exit_code=$? 256 | set -eEuo pipefail 257 | return $exit_code 258 | } 259 | 260 | # print calling(quoted) command line which is able to copy and paste to rerun safely 261 | # 262 | # How to get the complete calling command of a BASH script from inside the script (not just the arguments) 263 | # https://stackoverflow.com/questions/36625593 264 | # 265 | # bash metacharacter: 266 | # https://www.gnu.org/software/bash/manual/html_node/Definitions.html 267 | cu::print_calling_command_line() { 268 | local arg isFirst=true bash_meta_char_regex=$'[ \t\n|&;()<>$!"]' 269 | 270 | for arg; do 271 | if $isFirst; then 272 | isFirst=false 273 | else 274 | printf ' ' 275 | fi 276 | 277 | if [[ "$arg" =~ \' ]]; then 278 | printf '%q' "$arg" 279 | elif [[ "$arg" =~ $bash_meta_char_regex ]]; then 280 | printf "'%s'" "$arg" 281 | else 282 | printf "%s" "$arg" 283 | fi 284 | done 285 | echo 286 | } 287 | 288 | # output the error message then exit with error(exit code is 1) 289 | cu::die() { 290 | (($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided" 291 | 292 | cu::red_echo "Error: $*" >&2 293 | exit 1 294 | } 295 | -------------------------------------------------------------------------------- /lib/java_utils.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # java util functions. 4 | # 5 | ################################################################################ 6 | # api functions: 7 | # 8 | # - java operation functions: 9 | # - jvu::get_java_version 10 | # - jvu::java_cmd 11 | ################################################################################ 12 | # 13 | #_ source guard begin _# 14 | [ -n "${source_guard_ED79675F_4289_4394_A843_03D06DB48AFA:+has_value}" ] && return 15 | source_guard_ED79675F_4289_4394_A843_03D06DB48AFA=$(realpath -- "${BASH_SOURCE[0]}") 16 | # the value of source guard is the canonical dir path of this script 17 | readonly source_guard_ED79675F_4289_4394_A843_03D06DB48AFA=${source_guard_ED79675F_4289_4394_A843_03D06DB48AFA%/*} 18 | #_ source guard end _# 19 | 20 | set -eEuo pipefail 21 | 22 | # shellcheck source=common_utils.sh 23 | source "$source_guard_ED79675F_4289_4394_A843_03D06DB48AFA/common_utils.sh" 24 | 25 | ################################################################################# 26 | # java info functions 27 | ################################################################################# 28 | 29 | # output the java version extracted from `java -version` 30 | jvu::get_java_version() { 31 | (($# <= 1)) || cu::die "${FUNCNAME[0]} requires at most 1 argument! But provided $#: $*" 32 | 33 | local java_home_path="${1:-$JAVA_HOME}" 34 | "$java_home_path/bin/java" -version 2>&1 | awk -F\" '/ version "/{print $2}' 35 | } 36 | 37 | ################################################################################# 38 | # java operation functions 39 | ################################################################################# 40 | 41 | jvu::_validate_java_home() { 42 | _JVU_VALIDATE_JAVA_HOME_ERR_MSG= 43 | 44 | local -r java_home="$1" 45 | if cu::is_blank_string "$java_home"; then 46 | _JVU_VALIDATE_JAVA_HOME_ERR_MSG="java home value($java_home) is BLANK" 47 | return 1 48 | fi 49 | 50 | if [ ! -e "$java_home" ]; then 51 | _JVU_VALIDATE_JAVA_HOME_ERR_MSG="java home value($java_home) is NOT a existed file" 52 | return 1 53 | fi 54 | if [ ! -d "$java_home" ]; then 55 | _JVU_VALIDATE_JAVA_HOME_ERR_MSG="java home value($java_home) is NOT a directory" 56 | return 1 57 | fi 58 | 59 | local java_path="$java_home/bin/java" 60 | if [ ! -f "$java_path" ]; then 61 | _JVU_VALIDATE_JAVA_HOME_ERR_MSG="java_home/bin/java($java_path) is NOT existed" 62 | return 1 63 | fi 64 | if [ ! -x "$java_path" ]; then 65 | _JVU_VALIDATE_JAVA_HOME_ERR_MSG="java_home/bin/java($java_path) is NOT executable" 66 | return 1 67 | fi 68 | } 69 | 70 | # switch JAVA_HOME to target 71 | # 72 | # available switch target: 73 | # - version pattern, e.g. 11, 17 74 | # determinate JAVA_HOME by env var JAVAx_HOME 75 | # - /path/to/java/home 76 | # 77 | # usage: 78 | # jvu::switch_to_jdk 17 79 | # jvu::switch_to_jdk /path/to/java/home 80 | # 81 | jvu::switch_to_jdk() { 82 | [ $# == 1 ] || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 83 | 84 | local -r switch_target="$1" 85 | cu::is_blank_string "$switch_target" && cu::die "switch target($switch_target) is BLANK!" 86 | 87 | # 1. first check env var JAVAx_HOME(e.g. JAVA11_HOME) 88 | # 89 | # aka. set by java version, e.g. 90 | # jvu::switch_to_jdk 11 91 | if cu::is_number_string "$switch_target"; then 92 | local jdk_home_var_name="JAVA${switch_target}_HOME" 93 | # check env var JAVAx_HOME is defined or not 94 | if [ -z "${!jdk_home_var_name+defined}" ]; then 95 | cu::die "use \$$jdk_home_var_name as java home for switch target($switch_target), but \$$jdk_home_var_name is NOT defined!" 96 | fi 97 | local jdk_home="${!jdk_home_var_name:-}" 98 | if ! jvu::_validate_java_home "$jdk_home"; then 99 | cu::die "use \$$jdk_home_var_name($jdk_home) as java home for switch target($switch_target), but $_JVU_VALIDATE_JAVA_HOME_ERR_MSG!" 100 | fi 101 | 102 | export JAVA_HOME="$jdk_home" 103 | return 104 | fi 105 | 106 | # 2. then switch as a directory 107 | # 108 | # aka. set by path of java installation, e.g. 109 | # jvu::switch_to_jdk /path/to/java/home 110 | local jdk_home="$switch_target" 111 | if ! jvu::_validate_java_home "$jdk_home"; then 112 | cu::die "use switch target($switch_target) as java home, but $_JVU_VALIDATE_JAVA_HOME_ERR_MSG!" >&2 113 | fi 114 | export JAVA_HOME="$jdk_home" 115 | } 116 | 117 | readonly JVU_DEFAULT_JAVA_OPTS=( 118 | -ea 119 | -Duser.language=en -Duser.country=US 120 | -Dfile.encoding=UTF-8 121 | ) 122 | 123 | readonly JVU_JAVA_OPT_DEFAULT_DEBUG_PORT=5050 124 | 125 | # set env variable ENABLE_JAVA_RUN_DEBUG to enable java debug mode 126 | jvu::java_cmd() { 127 | (($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided" 128 | 129 | local debug_opts="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${JVU_JAVA_OPT_DEBUG_PORT}" 130 | 131 | cu::log_then_run "$JAVA_HOME/bin/java" \ 132 | "${JVU_JAVA_OPTS[@]}" \ 133 | ${JVU_ENABLE_JAVA_RUN_VERBOSE_CLASS+-verbose:class} \ 134 | ${JVU_ENABLE_JAVA_RUN_DEBUG+$debug_opts} \ 135 | "$@" 136 | } 137 | 138 | ################################################################################ 139 | # auto run logic when source 140 | ################################################################################ 141 | 142 | jvu::__detect_java_home_when_github_actions() { 143 | [[ "${GITHUB_ACTIONS:-}" = true ]] || return 0 144 | 145 | local jh_name matched_version jh assignment 146 | local -a detected=() 147 | for jh_name in "${!JAVA_HOME_@}"; do 148 | [[ "$jh_name" =~ ^JAVA_HOME_([0-9]+)_ ]] || continue 149 | matched_version="${BASH_REMATCH[1]}" 150 | 151 | jh="${!jh_name:-}" 152 | [ -d "$jh" ] || continue 153 | 154 | printf -v assignment 'JAVA%q_HOME=%q' "$matched_version" "$jh" 155 | detected=(${detected[@]:+"${detected[@]}"} "$assignment") 156 | done 157 | 158 | (("${#detected[@]}" > 0)) || return 0 159 | cu::yellow_echo "Detected JAVA_HOME when running in GitHub Actions:" 160 | for assignment in "${detected[@]}"; do 161 | # shellcheck disable=SC2163 162 | export "$assignment" 163 | echo " export $assignment" 164 | done 165 | } 166 | 167 | jvu::__auto_run_when_source() { 168 | # set VAR if absent 169 | 170 | if [ -z "${JVU_JAVA_OPT_DEBUG_PORT:-}" ]; then 171 | JVU_JAVA_OPT_DEBUG_PORT="$JVU_JAVA_OPT_DEFAULT_DEBUG_PORT" 172 | fi 173 | 174 | if [ -z "${JVU_JAVA_OPTS[*]:-}" ]; then 175 | JVU_JAVA_OPTS=("${JVU_DEFAULT_JAVA_OPTS[@]}") 176 | fi 177 | 178 | jvu::__detect_java_home_when_github_actions 179 | } 180 | 181 | jvu::__auto_run_when_source 182 | -------------------------------------------------------------------------------- /lib/maven_utils.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # maven util functions for build. 4 | # 5 | ################################################################################ 6 | # api functions: 7 | # 8 | # - mvu::mvn_cmd 9 | ################################################################################ 10 | # 11 | #_ source guard begin _# 12 | [ -n "${source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F:+has_value}" ] && return 13 | source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F=$(realpath -- "${BASH_SOURCE[0]}") 14 | # the value of source guard is the canonical dir path of this script 15 | readonly source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F=${source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F%/*} 16 | #_ source guard end _# 17 | 18 | set -eEuo pipefail 19 | 20 | # shellcheck source=common_utils.sh 21 | source "$source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F/common_utils.sh" 22 | 23 | ################################################################################# 24 | # maven operation functions 25 | ################################################################################# 26 | 27 | readonly MVU_DEFAULT_MVN_OPTS=( 28 | -V --no-transfer-progress 29 | ) 30 | 31 | mvu::_find_mvn_cmd_path() { 32 | if [ -n "${_MVU_MVN_PATH:-}" ]; then 33 | echo "$_MVU_MVN_PATH" 34 | return 35 | fi 36 | 37 | local -r maven_wrapper_name="mvnw" 38 | 39 | # 1. find the mvnw from project root dir 40 | if [[ -n "${PROJECT_ROOT_DIR:-}" && -e "$PROJECT_ROOT_DIR/$maven_wrapper_name" ]]; then 41 | _MVU_MVN_PATH="$PROJECT_ROOT_DIR/$maven_wrapper_name" 42 | echo "$_MVU_MVN_PATH" 43 | return 44 | fi 45 | 46 | # 2. find mvnw from parent dirs 47 | local d 48 | d=$(realpath -- "$PWD") 49 | while true; do 50 | local mvnw_path="$d/$maven_wrapper_name" 51 | [ -x "$mvnw_path" ] && { 52 | _MVU_MVN_PATH="$mvnw_path" 53 | echo "$_MVU_MVN_PATH" 54 | return 55 | } 56 | 57 | [ "/" = "$d" ] && break 58 | d=${d%/*} 59 | done 60 | 61 | # 3. find mvn from $PATH 62 | if command -v mvn &>/dev/null; then 63 | _MVU_MVN_PATH=mvn 64 | echo "$_MVU_MVN_PATH" 65 | return 66 | fi 67 | 68 | cu::die "$( 69 | echo "fail to find mvn cmd!" 70 | echo "found locations:" 71 | echo " - \$PROJECT_ROOT_DIR/mvnw($PROJECT_ROOT_DIR/mvnw)" 72 | echo " - \$PWD/mvnw($PWD/mvnw) and its parent dirs" 73 | echo " - mvn on \$PATH" 74 | )" 75 | } 76 | 77 | mvu::mvn_cmd() { 78 | (($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided" 79 | 80 | cu::log_then_run "$(mvu::_find_mvn_cmd_path)" \ 81 | "${MVU_MVN_OPTS[@]}" \ 82 | "$@" 83 | } 84 | 85 | mvu::get_mvn_local_repository_dir() { 86 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 87 | 88 | if [ -z "${_MVU_MVN_LOCAL_REPOSITORY_DIR:-}" ]; then 89 | echo "$_MVU_MVN_LOCAL_REPOSITORY_DIR" 90 | fi 91 | 92 | _MVU_MVN_LOCAL_REPOSITORY_DIR="$( 93 | mvu::mvn_cmd --no-transfer-progress help:evaluate -Dexpression=settings.localRepository | 94 | grep '^/' 95 | )" 96 | 97 | [ -n "${_MVU_MVN_LOCAL_REPOSITORY_DIR:-}" ] || cu::die "Fail to find maven local repository directory" 98 | } 99 | 100 | ################################################################################ 101 | # auto run logic when source 102 | ################################################################################ 103 | 104 | mvu::__auto_run_when_source() { 105 | # set VAR if absent 106 | 107 | if [ -z "${MVU_MVN_OPTS[*]:-}" ]; then 108 | MVU_MVN_OPTS=("${MVU_DEFAULT_MVN_OPTS[@]}") 109 | fi 110 | } 111 | 112 | mvu::__auto_run_when_source 113 | -------------------------------------------------------------------------------- /lib/prepare_jdks.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # a lib to prepare jdks by sdkman https://sdkman.io/ 4 | # 5 | ################################################################################ 6 | # api functions: 7 | # 8 | # - prepare_jdks::switch_to_jdk 9 | # 10 | # - prepare_jdks::prepare_jdks 11 | # 12 | # - prepare_jdks::install_jdk_by_sdkman 13 | # - prepare_jdks::load_sdkman 14 | # - prepare_jdks::install_sdkman 15 | ################################################################################ 16 | # 17 | #_ source guard begin _# 18 | [ -n "${source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465:+has_value}" ] && return 19 | source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465=$(realpath -- "${BASH_SOURCE[0]}") 20 | # the value of source guard is the canonical dir path of this script 21 | readonly source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465=${source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465%/*} 22 | #_ source guard end _# 23 | 24 | set -eEuo pipefail 25 | 26 | # shellcheck source=common_utils.sh 27 | source "$source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465/common_utils.sh" 28 | source "$source_guard_E2AA8C4F_215B_4CDA_9816_429C7A2CD465/java_utils.sh" 29 | 30 | # install sdkman. 31 | # 32 | # shellcheck disable=SC2120 33 | prepare_jdks::install_sdkman() { 34 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 35 | 36 | PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN="${PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN:-false}" 37 | 38 | # install sdkman if not installed yet 39 | if [ ! -f "$HOME/.sdkman/bin/sdkman-init.sh" ]; then 40 | [ -d "$HOME/.sdkman" ] && rm -rf "$HOME/.sdkman" 41 | 42 | curl -s get.sdkman.io | bash || cu::die "fail to install sdkman" 43 | PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN=true 44 | 45 | # FIXME: hard-coded config logic 46 | { 47 | echo sdkman_auto_answer=true 48 | echo sdkman_auto_selfupdate=false 49 | echo sdkman_disable_auto_upgrade_check=true 50 | } >>"$HOME/.sdkman/etc/config" 51 | fi 52 | } 53 | 54 | # load sdkman. 55 | # 56 | # install sdkman if not installed yet. 57 | # 58 | # shellcheck disable=SC2120 59 | prepare_jdks::load_sdkman() { 60 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 61 | 62 | prepare_jdks::install_sdkman 63 | 64 | # load sdkman if not loaded yet 65 | if ! command -v sdk &>/dev/null; then 66 | # shellcheck disable=SC1090 67 | cu::loose_run source "$HOME/.sdkman/bin/sdkman-init.sh" 68 | fi 69 | } 70 | 71 | prepare_jdks::_get_sdkman_ls_java_content() { 72 | [ -n "${_PREPARE_JDKS_SDKMAN_LS_JAVA_CONTENT:-}" ] && { 73 | echo "$_PREPARE_JDKS_SDKMAN_LS_JAVA_CONTENT" 74 | return 75 | } 76 | 77 | _PREPARE_JDKS_SDKMAN_LS_JAVA_CONTENT="$(cu::loose_run sdk ls java | sed -n '/^ Vendor/,/^===========/p')" 78 | echo "$_PREPARE_JDKS_SDKMAN_LS_JAVA_CONTENT" 79 | } 80 | 81 | # shellcheck disable=SC2120 82 | prepare_jdks::ls_java() { 83 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 84 | 85 | prepare_jdks::load_sdkman 86 | 87 | cu::blue_echo "sdk ls java:" 88 | prepare_jdks::_get_sdkman_ls_java_content 89 | } 90 | 91 | # shellcheck disable=SC2120 92 | prepare_jdks::get_available_java_versions_of_sdkman() { 93 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 94 | 95 | [ -n "${_PREPARE_JDKS_AVAILABLE_JAVA_VERSIONS_OF_SDKMAN:-}" ] && { 96 | echo "$_PREPARE_JDKS_AVAILABLE_JAVA_VERSIONS_OF_SDKMAN" 97 | return 98 | } 99 | 100 | prepare_jdks::load_sdkman >&2 101 | 102 | _PREPARE_JDKS_AVAILABLE_JAVA_VERSIONS_OF_SDKMAN="$( 103 | prepare_jdks::_get_sdkman_ls_java_content | 104 | awk -F'[ \\t]*\\|[ \\t]*' 'NR > 2 && /\|/ {print $NF}' | 105 | sort -Vr 106 | )" 107 | 108 | echo "$_PREPARE_JDKS_AVAILABLE_JAVA_VERSIONS_OF_SDKMAN" 109 | } 110 | 111 | # shellcheck disable=SC2120 112 | prepare_jdks::get_available_local_java_versions_of_sdkman() { 113 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 114 | 115 | [ -n "${_PREPARE_JDKS_AVAILABLE_LOCAL_JAVA_VERSIONS_OF_SDKMAN:-}" ] && { 116 | echo "$_PREPARE_JDKS_AVAILABLE_LOCAL_JAVA_VERSIONS_OF_SDKMAN" 117 | return 118 | } 119 | 120 | prepare_jdks::load_sdkman >&2 121 | 122 | _PREPARE_JDKS_AVAILABLE_LOCAL_JAVA_VERSIONS_OF_SDKMAN="$( 123 | printf '%s\n' "$SDKMAN_CANDIDATES_DIR/java"/*/ | 124 | awk -F/ '$(NF-1) !~ /^current$/ {print $(NF-1)}' | 125 | sort -Vr 126 | )" 127 | 128 | echo "$_PREPARE_JDKS_AVAILABLE_LOCAL_JAVA_VERSIONS_OF_SDKMAN" 129 | } 130 | 131 | # shellcheck disable=SC2120 132 | prepare_jdks::get_available_remote_java_versions_of_sdkman() { 133 | (($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*" 134 | 135 | [ -n "${_PREPARE_JDKS_AVAILABLE_REMOTE_JAVA_VERSIONS_OF_SDKMAN:-}" ] && { 136 | echo "$_PREPARE_JDKS_AVAILABLE_REMOTE_JAVA_VERSIONS_OF_SDKMAN" 137 | return 138 | } 139 | 140 | prepare_jdks::load_sdkman >&2 141 | 142 | _PREPARE_JDKS_AVAILABLE_REMOTE_JAVA_VERSIONS_OF_SDKMAN="$( 143 | prepare_jdks::_get_sdkman_ls_java_content | 144 | awk -F'[ \\t]*\\|[ \\t]*' 'NR > 2 && /\|/ && $5 !~ /^local / {print $NF}' | 145 | sort -Vr 146 | )" 147 | 148 | echo "$_PREPARE_JDKS_AVAILABLE_REMOTE_JAVA_VERSIONS_OF_SDKMAN" 149 | } 150 | 151 | prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman() { 152 | local jdk_name_of_sdkman="$1" 153 | local jdk_home_path="$SDKMAN_CANDIDATES_DIR/java/$jdk_name_of_sdkman" 154 | echo "$jdk_home_path" 155 | } 156 | 157 | # install the jdk by sdkman. 158 | prepare_jdks::install_jdk_by_sdkman() { 159 | (($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 160 | 161 | prepare_jdks::load_sdkman 162 | 163 | local jdk_name_of_sdkman="$1" jdk_home_path 164 | jdk_home_path="$(prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman "$jdk_name_of_sdkman")" 165 | 166 | # install jdk by sdkman 167 | if [ ! -d "$jdk_home_path" ]; then 168 | cu::loose_run cu::log_then_run sdk install java "$jdk_name_of_sdkman" || 169 | cu::die "fail to install jdk $jdk_name_of_sdkman by sdkman" 170 | fi 171 | } 172 | 173 | prepare_jdks::_get_latest_java_version() { 174 | (($# == 1)) || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 175 | 176 | local version_pattern="$1" input result 177 | # exclude 178 | # - fx: \.fx- 179 | # - GraalVM: -(grl|gln|nik)\> 180 | input=$(cat | grep -vE '\.fx-|-(grl|gln|nik|mandrel)\>') 181 | 182 | # 1. first find non-ea and non-fx versions 183 | result="$(echo "$input" | grep -vE '\.ea\.|\.fx-' | cu::get_latest_version_match "$version_pattern")" 184 | if [ -n "$result" ]; then 185 | echo "$result" 186 | return 0 187 | fi 188 | 189 | # 2. then find versions 190 | echo "$input" | cu::get_latest_version_match "$version_pattern" 191 | } 192 | 193 | # switch JAVA_HOME to target 194 | # 195 | # available switch target: 196 | # - version pattern, e.g. 197 | # - 11, 17 198 | # - 11.0, 11.0.14 199 | # - 11.0.14-ms 200 | # exact version of sdkman 201 | # - /path/to/java/home 202 | # 203 | # usage: 204 | # prepare_jdks::switch_to_jdk 17 205 | # prepare_jdks::switch_to_jdk 11.0.14-ms 206 | # prepare_jdks::switch_to_jdk /path/to/java/home 207 | # 208 | prepare_jdks::switch_to_jdk() { 209 | local verbose_mode=false prepare_mode=false 210 | while true; do 211 | case "$1" in 212 | -v) 213 | verbose_mode=true 214 | shift 215 | ;; 216 | -p) 217 | prepare_mode=true 218 | shift 219 | ;; 220 | *) 221 | break 222 | ;; 223 | esac 224 | done 225 | 226 | [ $# == 1 ] || cu::die "${FUNCNAME[0]} requires exact 1 argument! But provided $#: $*" 227 | 228 | local -r switch_target="$1" 229 | cu::is_blank_string "$switch_target" && cu::die "switch target($switch_target) is BLANK!" 230 | 231 | # 1. first check env var JDKx_HOME(e.g. JDK11_HOME) 232 | # 233 | # aka. set by java version, e.g. 234 | # prepare_jdks::switch_to_jdk 11 235 | if cu::is_number_string "$switch_target"; then 236 | local jdk_home_var_name="JDK${switch_target}_HOME" 237 | 238 | # check env var JDKx_HOME is defined or not 239 | if [ -n "${!jdk_home_var_name+defined}" ]; then 240 | local jdk_home="${!jdk_home_var_name:-}" 241 | 242 | if jvu::_validate_java_home "$jdk_home"; then 243 | if ! $prepare_mode; then 244 | export JAVA_HOME="$jdk_home" 245 | fi 246 | if $verbose_mode; then 247 | cu::blue_echo "use \$$jdk_home_var_name($jdk_home) as switch target $switch_target" >&2 248 | fi 249 | return 250 | else 251 | cu::yellow_echo "found \$$jdk_home_var_name($jdk_home) for switch target $switch_target, but $_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG, ignored!" >&2 252 | fi 253 | fi 254 | fi 255 | 256 | # 2. then check switch is a directory or not 257 | # 258 | # aka. set by path of java installation, e.g. 259 | # prepare_jdks::switch_to_jdk /path/to/java/home 260 | if [ -d "$switch_target" ]; then 261 | local jdk_home="$switch_target" 262 | 263 | if prepare_jdks::_validate_java_home "$jdk_home"; then 264 | if ! $prepare_mode; then 265 | export JAVA_HOME="$switch_target" 266 | fi 267 | if $verbose_mode; then 268 | cu::blue_echo "use $switch_target directory as switch target, since it is a existed directory" >&2 269 | fi 270 | return 271 | else 272 | cu::yellow_echo "found switch target $switch_target is a existed directory, but $_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG, ignored!" >&2 273 | fi 274 | fi 275 | 276 | # 3. set by java version pattern of sdkman 277 | local -r version_pattern="$switch_target" 278 | local version 279 | # 3.1 check *local* java versions of sdkman 280 | version=$( 281 | prepare_jdks::get_available_local_java_versions_of_sdkman | 282 | prepare_jdks::_get_latest_java_version "$version_pattern" 283 | ) 284 | # 3.2 check *remote* java versions of sdkman 285 | if [ -z "$version" ]; then 286 | version=$( 287 | prepare_jdks::get_available_remote_java_versions_of_sdkman | 288 | prepare_jdks::_get_latest_java_version "$version_pattern" 289 | ) 290 | fi 291 | 292 | [ -n "$version" ] || cu::die "fail to find available java version in sdkman for switch $switch_target!" 293 | 294 | prepare_jdks::install_jdk_by_sdkman "$version" 295 | 296 | local jdk_home 297 | jdk_home="$(prepare_jdks::_get_jdk_path_from_jdk_name_of_sdkman "$version")" 298 | 299 | if jvu::_validate_java_home "$jdk_home"; then 300 | if ! $prepare_mode; then 301 | export JAVA_HOME="$jdk_home" 302 | fi 303 | if $verbose_mode; then 304 | cu::blue_echo "use java version($version) in sdkman($jdk_home) as switch target $switch_target" >&2 305 | fi 306 | else 307 | cu::die "found available java version($version) in sdkman($jdk_home), but $_PREPARE_JDKS_VALIDATE_JAVA_HOME_ERR_MSG!" 308 | fi 309 | } 310 | 311 | # prepare jdks 312 | # 313 | # usage: 314 | # prepare_jdks::prepare_jdks ... 315 | # 316 | # example: 317 | # prepare_jdks::prepare_jdks 8 11 17 318 | # prepare_jdks::prepare_jdks 11.0.14-ms 17.0.2-zulu 319 | # prepare_jdks::prepare_jdks 8 11.0.14-ms 18 320 | # 321 | # see: `prepare_jdks::switch_to_jdk` 322 | # 323 | prepare_jdks::prepare_jdks() { 324 | (($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided" 325 | 326 | cu::blue_echo "prepare jdks: ${*}" 327 | local switch_target 328 | for switch_target; do 329 | prepare_jdks::switch_to_jdk -p "$switch_target" 330 | done 331 | } 332 | 333 | ################################################################################ 334 | # auto run logic when source 335 | # 336 | # auto load sdkman. 337 | # disable by define `PREPARE_JDKS_NO_AUTO_LOAD_SDKMAN` var 338 | # 339 | # auto prepare jdks, 340 | # if `PREPARE_JDKS_INSTALL_BY_SDKMAN` is has values 341 | ################################################################################ 342 | 343 | prepare_jdks::__auto_run_when_source() { 344 | [ -n "${PREPARE_JDKS_NO_AUTO_LOAD_SDKMAN+defined}" ] && return 0 345 | 346 | prepare_jdks::load_sdkman 347 | 348 | case "${PREPARE_JDKS_AUTO_SHOW_LS_JAVA:-}" in 349 | never) ;; 350 | always) 351 | prepare_jdks::ls_java 352 | ;; 353 | when_sdkman_install | '') 354 | if "$PREPARE_JDKS_IS_THIS_TIME_INSTALLED_SDKMAN"; then 355 | prepare_jdks::ls_java 356 | fi 357 | ;; 358 | esac 359 | 360 | if [[ -z "${PREPARE_JDKS_NO_AUTO_INSTALL_BY_SDKMAN+defined}" && -n "${PREPARE_JDKS_INSTALL_BY_SDKMAN:+has_values}" ]]; then 361 | prepare_jdks::prepare_jdks "${PREPARE_JDKS_INSTALL_BY_SDKMAN[@]}" 362 | fi 363 | } 364 | 365 | prepare_jdks::__auto_run_when_source 366 | -------------------------------------------------------------------------------- /lib/trap_error_info.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # a common lib to show trapped error info. 4 | # 5 | # provide function `trap_error_info::register_show_error_info_handler` to register 6 | # the error-trap handler which show error info when trapped error. 7 | # 8 | # by default, auto call `trap_error_info::register_show_error_info_handler` when source this script; 9 | # disable by define `TRAP_ERROR_NO_AUTO_REGISTER` var 10 | # 11 | ################################################################################ 12 | # api functions: 13 | # - trap_error_info::register_show_error_info_handler 14 | # - trap_error_info::get_stack_trace 15 | ################################################################################ 16 | # 17 | #_ source guard begin _# 18 | [ -n "${source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE:+has_value}" ] && return 19 | source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE=$(realpath -- "${BASH_SOURCE[0]}") 20 | # the value of source guard is the canonical dir path of this script 21 | readonly source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE=${source_guard_84949D19_1C7A_40AF_BC28_BA5967A0B6CE%/*} 22 | #_ source guard end _# 23 | 24 | set -eEu -o pipefail -o functrace 25 | 26 | # show stack trace. 27 | # 28 | # usage: 29 | # trap_error_info::get_stack_trace 30 | # 31 | # - indentation default is empty(""). 32 | # - hide level default is 0. 33 | # level 0 means show from the caller level stack trace. 34 | # 35 | # the format of stack trace: 36 | # (:) 37 | # example: 38 | # foo_function(bar.sh:42) 39 | # 40 | trap_error_info::get_stack_trace() { 41 | local indentation="${1:-}" hide_level="${2:-0}" 42 | local func_stack_size="${#FUNCNAME[@]}" 43 | 44 | TRAP_ERROR_INFO_STACK_TRACE= 45 | 46 | local i stack_trace= 47 | for ((i = hide_level + 1; i < func_stack_size; i++)); do 48 | [ -n "${stack_trace:-}" ] && printf -v stack_trace '%s\n' "$stack_trace" 49 | printf -v stack_trace '%s%s%s(%s:%s)' "$stack_trace" \ 50 | "$indentation" "${FUNCNAME[i]}" "${BASH_SOURCE[i]}" "${BASH_LINENO[i - 1]}" 51 | done 52 | 53 | TRAP_ERROR_INFO_STACK_TRACE="$stack_trace" 54 | } 55 | 56 | # official document of `Bash Variables`, e.g. 57 | # BASH_SOURCE 58 | # BASH_LINENO 59 | # LINENO 60 | # BASH_COMMAND 61 | # https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html 62 | # 63 | # 64 | # related info: 65 | # 66 | # https://stackoverflow.com/questions/6928946/mysterious-lineno-in-bash-trap-err 67 | # https://stackoverflow.com/questions/64786/error-handling-in-bash 68 | # https://stackoverflow.com/questions/24398691/how-to-get-the-real-line-number-of-a-failing-bash-command 69 | # https://unix.stackexchange.com/questions/39623/trap-err-and-echoing-the-error-line 70 | # https://unix.stackexchange.com/questions/462156/how-do-i-find-the-line-number-in-bash-when-an-error-occured 71 | # https://unix.stackexchange.com/questions/365113/how-to-avoid-error-message-during-the-execution-of-a-bash-script 72 | # https://shapeshed.com/unix-exit-codes/#how-to-suppress-exit-statuses 73 | # https://stackoverflow.com/questions/30078281/raise-error-in-a-bash-script/50265513#50265513 74 | # 75 | trap_error_info::_show_trapped_error_info() { 76 | local exit_code="$1" error_command_line="$2" 77 | 78 | echo '================================================================================' 79 | echo "Trapped error!" 80 | echo 81 | echo "Exit status code: $exit_code" 82 | 83 | echo "Stack trace:" 84 | # set hide level 1, hide `trap_error_info::_show_trapped_error_info` self stack trace 85 | trap_error_info::get_stack_trace ' ' 1 86 | echo "$TRAP_ERROR_INFO_STACK_TRACE" 87 | 88 | echo "Error code line:" 89 | echo " $error_command_line" 90 | echo '================================================================================' 91 | } >&2 92 | 93 | trap_error_info::register_show_error_info_handler() { 94 | trap 'trap_error_info::_show_trapped_error_info $? "$BASH_COMMAND"' ERR 95 | } 96 | 97 | ################################################################################ 98 | # auto run logic when source 99 | # 100 | # auto call `trap_error_info::register_show_error_info_handler` when source this script; 101 | # disable by define `TRAP_ERROR_NO_AUTO_REGISTER` var 102 | ################################################################################ 103 | 104 | if [ -z "${TRAP_ERROR_NO_AUTO_REGISTER+defined}" ]; then 105 | trap_error_info::register_show_error_info_handler 106 | fi 107 | -------------------------------------------------------------------------------- /templates/integration_test.template.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -eEuo pipefail 3 | # the canonical path of this script 4 | SELF_PATH=$(realpath -- "$0") 5 | readonly SELF_PATH SELF_DIR=${SELF_PATH%/*} 6 | cd "$SELF_DIR" 7 | 8 | FIXME BASH_BUDDY_ROOT=...... 9 | readonly BASH_BUDDY_ROOT 10 | # shellcheck disable=SC1091 11 | source "$BASH_BUDDY_ROOT/lib/trap_error_info.sh" 12 | # shellcheck disable=SC1091 13 | source "$BASH_BUDDY_ROOT/lib/common_utils.sh" 14 | # shellcheck disable=SC1091 15 | source "$BASH_BUDDY_ROOT/lib/java_utils.sh" 16 | # shellcheck disable=SC1091 17 | source "$BASH_BUDDY_ROOT/lib/maven_utils.sh" 18 | 19 | ################################################################################ 20 | # prepare 21 | ################################################################################ 22 | 23 | readonly default_build_jdk_version=17 24 | readonly JDK_VERSIONS=( 25 | 8 26 | 11 27 | "$default_build_jdk_version" 28 | 21 29 | ) 30 | 31 | ################################################################################ 32 | # ci build logic 33 | ################################################################################ 34 | 35 | FIXME PROJECT_ROOT=...... 36 | readonly PROJECT_ROOT 37 | cd "$PROJECT_ROOT" 38 | 39 | ######################################## 40 | # do build and test by default version jdk 41 | ######################################## 42 | 43 | jvu::switch_to_jdk "$default_build_jdk_version" 44 | 45 | cu::head_line_echo "build and test with Java: $JAVA_HOME" 46 | mvu::mvn_cmd clean install 47 | 48 | ######################################## 49 | # test by multi-version jdk 50 | ######################################## 51 | for jdk_version in "${JDK_VERSIONS[@]}"; do 52 | # already tested above 53 | [ "$jdk_version" = "$default_build_jdk_version" ] && continue 54 | 55 | jvu::switch_to_jdk "$jdk_version" 56 | 57 | cu::head_line_echo "test with Java: $JAVA_HOME" 58 | # just test without build 59 | mvu::mvn_cmd surefire:test 60 | done 61 | --------------------------------------------------------------------------------