diff --git a/.editorconfig b/.editorconfig index 1923d410..1840589a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,3 +6,6 @@ indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[*.py] +indent_size = 4 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d2e848ac..ebdc8313 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -33,3 +33,15 @@ jobs: - name: Run shfmt run: scripts/shfmt.bash + + checkstyle-py: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install asdf dependencies + uses: asdf-vm/actions/install@v1 + + - name: Run checkstyle.py + run: scripts/checkstyle.py diff --git a/.tool-versions b/.tool-versions index 71306369..c38ac57c 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,3 +1,4 @@ bats 1.7.0 shellcheck 0.8.0 shfmt 3.5.1 +python 3.10.9 diff --git a/lib/commands/command-current.bash b/lib/commands/command-current.bash index fe30f1f8..23095444 100644 --- a/lib/commands/command-current.bash +++ b/lib/commands/command-current.bash @@ -44,7 +44,7 @@ plugin_current_command() { # shellcheck disable=SC2059 current_command() { - local terminal_format="%-15s %-15s %-10s\\n" + local terminal_format="%-15s %-15s %-10s\n" local exit_status=0 local plugin @@ -75,8 +75,8 @@ check_for_deprecated_plugin() { local new_script="${plugin_path}/bin/list-legacy-filenames" if [ "$legacy_config" = "yes" ] && [ -f "$deprecated_script" ] && [ ! -f "$new_script" ]; then - printf "Heads up! It looks like your %s plugin is out of date. You can update it with:\\n\\n" "$plugin_name" - printf " asdf plugin-update %s\\n\\n" "$plugin_name" + printf "Heads up! It looks like your %s plugin is out of date. You can update it with:\n\n" "$plugin_name" + printf " asdf plugin-update %s\n\n" "$plugin_name" fi } diff --git a/lib/commands/command-env.bash b/lib/commands/command-env.bash index e694e7b2..30cf7ec4 100644 --- a/lib/commands/command-env.bash +++ b/lib/commands/command-env.bash @@ -6,7 +6,7 @@ shim_env_command() { local env_args=("${@:3}") if [ -z "$shim_name" ]; then - printf "usage: asdf env \\n" + printf "usage: asdf env \n" exit 1 fi diff --git a/lib/commands/command-exec.bash b/lib/commands/command-exec.bash index 427922c7..cb56b36c 100644 --- a/lib/commands/command-exec.bash +++ b/lib/commands/command-exec.bash @@ -6,7 +6,7 @@ shim_exec_command() { local shim_args=("${@:2}") if [ -z "$shim_name" ]; then - printf "usage: asdf exec \\n" + printf "usage: asdf exec \n" exit 1 fi @@ -16,7 +16,7 @@ shim_exec_command() { local executable_path="$3" if [ ! -x "$executable_path" ]; then - printf "No %s executable found for %s %s\\n" "$shim_name" "$plugin_name" "$version" >&2 + printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2 exit 2 fi diff --git a/lib/commands/command-export-shell-version.bash b/lib/commands/command-export-shell-version.bash index a398a005..13b779d1 100644 --- a/lib/commands/command-export-shell-version.bash +++ b/lib/commands/command-export-shell-version.bash @@ -8,8 +8,8 @@ shell_command() { shift if [ "$#" -lt "2" ]; then - printf "Usage: asdf shell {|--unset}\\n" >&2 - printf "false\\n" + printf "Usage: asdf shell {|--unset}\n" >&2 + printf "false\n" exit 1 fi @@ -23,7 +23,7 @@ shell_command() { if [ "$version" = "--unset" ]; then case "$asdf_shell" in fish) - printf "set -e %s\\n" "$version_env_var" + printf "set -e %s\n" "$version_env_var" ;; elvish) # Elvish doesn't have a `source` command, and eval is banned, so the @@ -32,7 +32,7 @@ shell_command() { printf "unset-env\n%s" "$version_env_var" ;; *) - printf "unset %s\\n" "$version_env_var" + printf "unset %s\n" "$version_env_var" ;; esac exit 0 @@ -42,13 +42,13 @@ shell_command() { fi if ! (check_if_version_exists "$plugin" "$version"); then version_not_installed_text "$plugin" "$version" 1>&2 - printf "false\\n" + printf "false\n" exit 1 fi case "$asdf_shell" in fish) - printf "set -gx %s \"%s\"\\n" "$version_env_var" "$version" + printf "set -gx %s \"%s\"\n" "$version_env_var" "$version" ;; elvish) # Elvish doesn't have a `source` command, and eval is banned, so the @@ -57,7 +57,7 @@ shell_command() { printf "set-env\n%s\n%s" "$version_env_var" "$version" ;; *) - printf "export %s=\"%s\"\\n" "$version_env_var" "$version" + printf "export %s=\"%s\"\n" "$version_env_var" "$version" ;; esac } diff --git a/lib/commands/command-help.bash b/lib/commands/command-help.bash index eba6c99e..77f1bb27 100644 --- a/lib/commands/command-help.bash +++ b/lib/commands/command-help.bash @@ -3,7 +3,7 @@ . "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" asdf_help() { - printf "version: %s\\n\\n" "$(asdf_version)" + printf "version: %s\n\n" "$(asdf_version)" cat "$(asdf_dir)/help.txt" } @@ -23,7 +23,7 @@ asdf_extension_cmds() { ext_cmd_path="$plugin_path/lib/commands" ext_cmds="$(find "$ext_cmd_path" -name "command*.bash" 2>/dev/null)" if [[ -n $ext_cmds ]]; then - printf "\\nPLUGIN %s\\n" "$plugin" + printf "\nPLUGIN %s\n" "$plugin" for ext_cmd in $ext_cmds; do ext_cmd_name="$(basename "$ext_cmd")" sed "s/-/ /g;s/.bash//;s/command-*/ asdf $plugin/;" <<<"$ext_cmd_name" @@ -78,11 +78,11 @@ help_command() { (print_plugin_help "$plugin_path") fi else - printf "No documentation for plugin %s\\n" "$plugin_name" >&2 + printf "No documentation for plugin %s\n" "$plugin_name" >&2 exit 1 fi else - printf "No plugin named %s\\n" "$plugin_name" >&2 + printf "No plugin named %s\n" "$plugin_name" >&2 exit 1 fi else diff --git a/lib/commands/command-info.bash b/lib/commands/command-info.bash index 2ab6ffbd..9304ce68 100644 --- a/lib/commands/command-info.bash +++ b/lib/commands/command-info.bash @@ -3,11 +3,11 @@ . "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" info_command() { - printf "%s:\\n%s\\n\\n" "OS" "$(uname -a)" - printf "%s:\\n%s\\n\\n" "SHELL" "$($SHELL --version)" - printf "%s:\\n%s\\n\\n" "ASDF VERSION" "$(asdf_version)" - printf "%s:\\n%s\\n\\n" "ASDF ENVIRONMENT VARIABLES" "$(env | grep -E "ASDF_DIR|ASDF_DATA_DIR|ASDF_CONFIG_FILE|ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" - printf "%s:\\n%s\\n\\n" "ASDF INSTALLED PLUGINS" "$(plugin_list_command --urls --refs)" + printf "%s:\n%s\n\n" "OS" "$(uname -a)" + printf "%s:\n%s\n\n" "SHELL" "$($SHELL --version)" + printf "%s:\n%s\n\n" "ASDF VERSION" "$(asdf_version)" + printf "%s:\n%s\n\n" "ASDF ENVIRONMENT VARIABLES" "$(env | grep -E "ASDF_DIR|ASDF_DATA_DIR|ASDF_CONFIG_FILE|ASDF_DEFAULT_TOOL_VERSIONS_FILENAME")" + printf "%s:\n%s\n\n" "ASDF INSTALLED PLUGINS" "$(plugin_list_command --urls --refs)" } info_command "$@" diff --git a/lib/commands/command-list.bash b/lib/commands/command-list.bash index 67fe7ed4..74ee8684 100644 --- a/lib/commands/command-list.bash +++ b/lib/commands/command-list.bash @@ -11,11 +11,11 @@ list_command() { if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then for plugin_path in "$plugins_path"/*/; do plugin_name=$(basename "$plugin_path") - printf "%s\\n" "$plugin_name" + printf "%s\n" "$plugin_name" display_installed_versions "$plugin_name" "$query" done else - printf "%s\\n" 'No plugins installed' + printf "%s\n" 'No plugins installed' fi else check_if_plugin_exists "$plugin_name" @@ -49,7 +49,7 @@ display_installed_versions() { if [[ "$version" == "$current_version" ]]; then flag=" *" fi - printf "%s%s\\n" "$flag" "$version" + printf "%s%s\n" "$flag" "$version" done else display_error ' No versions installed' diff --git a/lib/commands/command-plugin-list-all.bash b/lib/commands/command-plugin-list-all.bash index 3c7c3f67..33333e59 100644 --- a/lib/commands/command-plugin-list-all.bash +++ b/lib/commands/command-plugin-list-all.bash @@ -18,11 +18,11 @@ plugin_list_all_command() { [[ -d "${plugins_local_path}/${index_plugin_name}" ]] && installed_flag='*' - printf "%s\\t%s\\n" "$index_plugin_name" "$installed_flag$source_url" + printf "%s\t%s\n" "$index_plugin_name" "$installed_flag$source_url" done ) | awk '{ printf("%-28s", $1); sub(/^[^*]/, " &", $2); $1=""; print $0 }' else - printf "%s%s\\n" "error: index of plugins not found at " "$plugins_index_path" + printf "%s%s\n" "error: index of plugins not found at " "$plugins_index_path" fi } diff --git a/lib/commands/command-plugin-push.bash b/lib/commands/command-plugin-push.bash index ce81486f..e09a222f 100644 --- a/lib/commands/command-plugin-push.bash +++ b/lib/commands/command-plugin-push.bash @@ -4,14 +4,14 @@ plugin_push_command() { local plugin_name=$1 if [ "$plugin_name" = "--all" ]; then for dir in "$(asdf_data_dir)"/plugins/*/; do - printf "Pushing %s...\\n" "$(basename "$dir")" + printf "Pushing %s...\n" "$(basename "$dir")" (cd "$dir" && git push) done else local plugin_path plugin_path=$(get_plugin_path "$plugin_name") check_if_plugin_exists "$plugin_name" - printf "Pushing %s...\\n" "$plugin_name" + printf "Pushing %s...\n" "$plugin_name" (cd "$plugin_path" && git push) fi } diff --git a/lib/commands/command-plugin-test.bash b/lib/commands/command-plugin-test.bash index e253fea2..4a4bb170 100644 --- a/lib/commands/command-plugin-test.bash +++ b/lib/commands/command-plugin-test.bash @@ -53,7 +53,7 @@ plugin_test_command() { local TEST_DIR fail_test() { - printf "FAILED: %s\\n" "$1" + printf "FAILED: %s\n" "$1" rm -rf "$TEST_DIR" exit 1 } @@ -92,20 +92,20 @@ plugin_test_command() { local list_all="$plugin_path/bin/list-all" if grep api.github.com "$list_all" >/dev/null; then if ! grep Authorization "$list_all" >/dev/null; then - printf "\\nLooks like %s/bin/list-all relies on GitHub releases\\n" "$plugin_name" - printf "but it does not properly sets an Authorization header to prevent\\n" - printf "GitHub API rate limiting.\\n\\n" - printf "See https://github.com/asdf-vm/asdf/blob/master/docs/creating-plugins.md#github-api-rate-limiting\\n" + printf "\nLooks like %s/bin/list-all relies on GitHub releases\n" "$plugin_name" + printf "but it does not properly sets an Authorization header to prevent\n" + printf "GitHub API rate limiting.\n\n" + printf "See https://github.com/asdf-vm/asdf/blob/master/docs/creating-plugins.md#github-api-rate-limiting\n" fail_test "$plugin_name/bin/list-all does not set GitHub Authorization token" fi # test for most common token names we have on plugins. If both are empty show this warning if [ -z "$OAUTH_TOKEN" ] && [ -z "$GITHUB_API_TOKEN" ]; then - printf "%s/bin/list-all is using GitHub API, just be sure you provide an API Authorization token\\n" "$plugin_name" - printf "via your CI env GITHUB_API_TOKEN. This is the current rate_limit:\\n\\n" + printf "%s/bin/list-all is using GitHub API, just be sure you provide an API Authorization token\n" "$plugin_name" + printf "via your CI env GITHUB_API_TOKEN. This is the current rate_limit:\n\n" curl -s https://api.github.com/rate_limit - printf "\\n" + printf "\n" fi fi diff --git a/lib/commands/command-update.bash b/lib/commands/command-update.bash index 824e4da0..f9912645 100644 --- a/lib/commands/command-update.bash +++ b/lib/commands/command-update.bash @@ -7,7 +7,7 @@ update_command() { cd "$(asdf_dir)" || exit 1 if [ -f asdf_updates_disabled ] || ! git rev-parse --is-inside-work-tree &>/dev/null; then - printf "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf.\\n" + printf "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf.\n" exit 42 else do_update "$update_to_head" @@ -23,7 +23,7 @@ do_update() { git fetch origin master git checkout master git reset --hard origin/master - printf "Updated asdf to latest on the master branch\\n" + printf "Updated asdf to latest on the master branch\n" else # Update to latest release git fetch origin --tags || exit 1 @@ -38,7 +38,7 @@ do_update() { # Update git checkout "$tag" || exit 1 - printf "Updated asdf to release %s\\n" "$tag" + printf "Updated asdf to release %s\n" "$tag" fi } diff --git a/lib/commands/command-where.bash b/lib/commands/command-where.bash index 2ed7f0b4..d2b9d039 100644 --- a/lib/commands/command-where.bash +++ b/lib/commands/command-where.bash @@ -34,14 +34,14 @@ where_command() { install_path=$(get_install_path "$plugin_name" "$install_type" "$version") if [ -d "$install_path" ]; then - printf "%s\\n" "$install_path" + printf "%s\n" "$install_path" exit 0 else if [ "$version" = "system" ]; then - printf "System version is selected\\n" + printf "System version is selected\n" exit 1 else - printf "Version not installed\\n" + printf "Version not installed\n" exit 1 fi fi diff --git a/lib/commands/command-which.bash b/lib/commands/command-which.bash index 7704ffce..8696dbf3 100644 --- a/lib/commands/command-which.bash +++ b/lib/commands/command-which.bash @@ -5,7 +5,7 @@ which_command() { shim_name=$(basename "$1") if [ -z "$shim_name" ]; then - printf "usage: asdf which \\n" + printf "usage: asdf which \n" exit 1 fi @@ -15,11 +15,11 @@ which_command() { local executable_path="$3" if [ ! -x "$executable_path" ]; then - printf "No %s executable found for %s %s\\n" "$shim_name" "$plugin_name" "$version" >&2 + printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2 exit 1 fi - printf "%s\\n" "$executable_path" + printf "%s\n" "$executable_path" exit 0 } diff --git a/lib/commands/reshim.bash b/lib/commands/reshim.bash index 40ba12e2..8a703a4d 100644 --- a/lib/commands/reshim.bash +++ b/lib/commands/reshim.bash @@ -142,10 +142,10 @@ remove_obsolete_shims() { # comm only takes to files, so we write this data to temp files so we can # pass it to comm. formatted_shims="$(mktemp "$temp_dir/asdf-command-reshim-formatted-shims.XXXXXX")" - printf "%s\\n" "$shims" >"$formatted_shims" + printf "%s\n" "$shims" >"$formatted_shims" formatted_exec_names="$(mktemp "$temp_dir/asdf-command-reshim-formatted-exec-names.XXXXXX")" - printf "%s\\n" "$exec_names" >"$formatted_exec_names" + printf "%s\n" "$exec_names" >"$formatted_exec_names" obsolete_shims=$(comm -23 "$formatted_shims" "$formatted_exec_names") rm -f "$formatted_exec_names" "$formatted_shims" diff --git a/lib/functions/installs.bash b/lib/functions/installs.bash index 8084b683..904f87ae 100644 --- a/lib/functions/installs.bash +++ b/lib/functions/installs.bash @@ -6,7 +6,7 @@ handle_failure() { handle_cancel() { local install_path="$1" - printf "\\nreceived sigint, cleaning up" + printf "\nreceived sigint, cleaning up" handle_failure "$install_path" } @@ -32,7 +32,7 @@ get_concurrency() { elif [ -f /proc/cpuinfo ]; then grep -c processor /proc/cpuinfo else - printf "1\\n" + printf "1\n" fi } @@ -55,7 +55,7 @@ install_one_local_tool() { install_tool_version "$plugin_name" "$plugin_version" done else - printf "No versions specified for %s in config files or environment\\n" "$plugin_name" + printf "No versions specified for %s in config files or environment\n" "$plugin_name" exit 1 fi } @@ -85,7 +85,7 @@ install_local_tool_versions() { fi if [ -z "$plugins_installed" ]; then - printf "Install plugins first to be able to install tools\\n" + printf "Install plugins first to be able to install tools\n" exit 1 fi @@ -122,9 +122,9 @@ install_local_tool_versions() { fi if [ -z "$some_tools_installed" ]; then - printf "Either specify a tool & version in the command\\n" - printf "OR add .tool-versions file in this directory\\n" - printf "or in a parent directory\\n" + printf "Either specify a tool & version in the command\n" + printf "OR add .tool-versions file in this directory\n" + printf "or in a parent directory\n" exit 1 fi } @@ -180,7 +180,7 @@ install_tool_version() { trap 'handle_cancel $install_path' INT if [ -d "$install_path" ]; then - printf "%s %s is already installed\\n" "$plugin_name" "$full_version" + printf "%s %s is already installed\n" "$plugin_name" "$full_version" else if [ -f "${plugin_path}/bin/download" ]; then diff --git a/lib/functions/plugins.bash b/lib/functions/plugins.bash index ee2e2161..cbb8a5e7 100644 --- a/lib/functions/plugins.bash +++ b/lib/functions/plugins.bash @@ -28,7 +28,7 @@ plugin_list_command() { printf "%s" "$plugin_name" if [ -n "$show_repo" ]; then - printf "\\t%s" "$(git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null)" + printf "\t%s" "$(git --git-dir "$plugin_path/.git" remote get-url origin 2>/dev/null)" fi if [ -n "$show_ref" ]; then @@ -36,10 +36,10 @@ plugin_list_command() { local gitref branch=$(git --git-dir "$plugin_path/.git" rev-parse --abbrev-ref HEAD 2>/dev/null) gitref=$(git --git-dir "$plugin_path/.git" rev-parse --short HEAD 2>/dev/null) - printf "\\t%s\\t%s" "$branch" "$gitref" + printf "\t%s\t%s" "$branch" "$gitref" fi - printf "\\n" + printf "\n" done ) | awk '{ if (NF > 1) { printf("%-28s", $1) ; $1="" }; print $0}' else @@ -145,7 +145,7 @@ update_plugin() { asdf_run_hook "pre_asdf_plugin_update" "$plugin_name" asdf_run_hook "pre_asdf_plugin_update_${plugin_name}" - printf "Updating %s to %s\\n" "$plugin_name" "$gitref" + printf "Updating %s to %s\n" "$plugin_name" "$gitref" git "${common_git_options[@]}" fetch --prune --update-head-ok origin "$gitref:$gitref" prev_ref=$(git "${common_git_options[@]}" rev-parse --short HEAD) diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index df1afdb5..4dfa233f 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -4,9 +4,9 @@ version_command() { if [ "$#" -lt "3" ]; then if [ "$cmd" = "global" ]; then - printf "Usage: asdf global \\n" + printf "Usage: asdf global \n" else - printf "Usage: asdf local \\n" + printf "Usage: asdf local \n" fi exit 1 fi @@ -68,7 +68,7 @@ version_command() { [[ -n "$(tail -c1 "$file")" && -f "$file" ]] && printf '\n' >>"$file" # Add a new version line to the end of the file - printf "%s %s\\n" "$plugin_name" "${resolved_versions[*]}" >>"$file" + printf "%s %s\n" "$plugin_name" "${resolved_versions[*]}" >>"$file" fi } @@ -92,9 +92,9 @@ list_all_command() { if [[ $return_code -ne 0 ]]; then # Printing all output to allow plugin to handle error formatting - printf "Plugin %s's list-all callback script failed with output:\\n" "${plugin_name}" >&2 - printf "%s\\n" "$(cat "$std_err_file")" >&2 - printf "%s\\n" "$(cat "$std_out_file")" >&2 + printf "Plugin %s's list-all callback script failed with output:\n" "${plugin_name}" >&2 + printf "%s\n" "$(cat "$std_err_file")" >&2 + printf "%s\n" "$(cat "$std_out_file")" >&2 rm "$std_out_file" "$std_err_file" exit 1 fi @@ -115,7 +115,7 @@ list_all_command() { IFS=' ' read -r -a versions_list <<<"$output" for version in "${versions_list[@]}"; do - printf "%s\\n" "${version}" + printf "%s\n" "${version}" done # Remove temp files if they still exist @@ -144,7 +144,7 @@ latest_command() { versions=$("${plugin_path}"/bin/latest-stable "$query") if [ -z "${versions}" ]; then # this branch requires this print to mimic the error from the list-all branch - printf "No compatible versions available (%s %s)\\n" "$plugin_name" "$query" >&2 + printf "No compatible versions available (%s %s)\n" "$plugin_name" "$query" >&2 exit 1 fi else @@ -158,7 +158,7 @@ latest_command() { fi fi - printf "%s\\n" "$versions" + printf "%s\n" "$versions" } latest_all() { @@ -198,10 +198,10 @@ latest_all() { if [ -n "$installed_versions" ] && printf '%s\n' "$installed_versions" | grep -q "^$version\$"; then installed_status="installed" fi - printf "%s\\t%s\\t%s\\n" "$plugin_name" "$version" "$installed_status" + printf "%s\t%s\t%s\n" "$plugin_name" "$version" "$installed_status" done else - printf "%s\\n" 'No plugins installed' + printf "%s\n" 'No plugins installed' fi exit 0 } diff --git a/lib/utils.bash b/lib/utils.bash index dfabb252..21865104 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -13,9 +13,9 @@ asdf_version() { version="v$(cat "$(asdf_dir)/version.txt")" if [ -d "$(asdf_dir)/.git" ]; then git_rev="$(git --git-dir "$(asdf_dir)/.git" rev-parse --short HEAD)" - printf "%s-%s\\n" "$version" "$git_rev" + printf "%s-%s\n" "$version" "$git_rev" else - printf "%s\\n" "$version" + printf "%s\n" "$version" fi } @@ -29,11 +29,11 @@ asdf_dir() { ) fi - printf "%s\\n" "$ASDF_DIR" + printf "%s\n" "$ASDF_DIR" } asdf_repository_url() { - printf "https://github.com/asdf-vm/asdf-plugins.git\\n" + printf "https://github.com/asdf-vm/asdf-plugins.git\n" } asdf_data_dir() { @@ -47,7 +47,7 @@ asdf_data_dir() { data_dir=$(asdf_dir) fi - printf "%s\\n" "$data_dir" + printf "%s\n" "$data_dir" } get_install_path() { @@ -61,11 +61,11 @@ get_install_path() { mkdir -p "${install_dir}/${plugin}" if [ "$install_type" = "version" ]; then - printf "%s/%s/%s\\n" "$install_dir" "$plugin" "$version" + printf "%s/%s/%s\n" "$install_dir" "$plugin" "$version" elif [ "$install_type" = "path" ]; then - printf "%s\\n" "$version" + printf "%s\n" "$version" else - printf "%s/%s/%s-%s\\n" "$install_dir" "$plugin" "$install_type" "$version" + printf "%s/%s/%s-%s\n" "$install_dir" "$plugin" "$install_type" "$version" fi } @@ -80,11 +80,11 @@ get_download_path() { mkdir -p "${download_dir}/${plugin}" if [ "$install_type" = "version" ]; then - printf "%s/%s/%s\\n" "$download_dir" "$plugin" "$version" + printf "%s/%s/%s\n" "$download_dir" "$plugin" "$version" elif [ "$install_type" = "path" ]; then return else - printf "%s/%s/%s-%s\\n" "$download_dir" "$plugin" "$install_type" "$version" + printf "%s/%s/%s-%s\n" "$download_dir" "$plugin" "$install_type" "$version" fi } @@ -137,19 +137,19 @@ version_not_installed_text() { local plugin_name=$1 local version=$2 - printf "version %s is not installed for %s\\n" "$version" "$plugin_name" + printf "version %s is not installed for %s\n" "$version" "$plugin_name" } get_plugin_path() { if [ -n "$1" ]; then - printf "%s\\n" "$(asdf_data_dir)/plugins/$1" + printf "%s\n" "$(asdf_data_dir)/plugins/$1" else - printf "%s\\n" "$(asdf_data_dir)/plugins" + printf "%s\n" "$(asdf_data_dir)/plugins" fi } display_error() { - printf "%s\\n" "$1" >&2 + printf "%s\n" "$1" >&2 } get_version_in_dir() { @@ -163,7 +163,7 @@ get_version_in_dir() { asdf_version=$(parse_asdf_version_file "$search_path/$file_name" "$plugin_name") if [ -n "$asdf_version" ]; then - printf "%s\\n" "$asdf_version|$search_path/$file_name" + printf "%s\n" "$asdf_version|$search_path/$file_name" return 0 fi @@ -172,7 +172,7 @@ get_version_in_dir() { legacy_version=$(parse_legacy_version_file "$search_path/$filename" "$plugin_name") if [ -n "$legacy_version" ]; then - printf "%s\\n" "$legacy_version|$search_path/$filename" + printf "%s\n" "$legacy_version|$search_path/$filename" return 0 fi done @@ -190,10 +190,10 @@ find_versions() { version=$(get_version_from_env "$plugin_name") if [ -n "$version" ]; then local upcase_name - upcase_name=$(printf "%s\\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') + upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') local version_env_var="ASDF_${upcase_name}_VERSION" - printf "%s\\n" "$version|$version_env_var environment variable" + printf "%s\n" "$version|$version_env_var environment variable" return 0 fi @@ -212,7 +212,7 @@ find_versions() { while [ "$search_path" != "/" ]; do version=$(get_version_in_dir "$plugin_name" "$search_path" "$legacy_filenames") if [ -n "$version" ]; then - printf "%s\\n" "$version" + printf "%s\n" "$version" return 0 fi search_path=$(dirname "$search_path") @@ -223,7 +223,7 @@ find_versions() { if [ -f "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" ]; then versions=$(parse_asdf_version_file "$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" "$plugin_name") if [ -n "$versions" ]; then - printf "%s\\n" "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" + printf "%s\n" "$versions|$ASDF_DEFAULT_TOOL_VERSIONS_FILENAME" return 0 fi fi @@ -231,16 +231,16 @@ find_versions() { display_no_version_set() { local plugin_name=$1 - printf "No version is set for %s; please run \`asdf %s \`\\n" "$plugin_name" "$plugin_name" + printf "No version is set for %s; please run \`asdf %s \`\n" "$plugin_name" "$plugin_name" } get_version_from_env() { local plugin_name=$1 local upcase_name - upcase_name=$(printf "%s\\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') + upcase_name=$(printf "%s\n" "$plugin_name" | tr '[:lower:]-' '[:upper:]_') local version_env_var="ASDF_${upcase_name}_VERSION" local version=${!version_env_var:-} - printf "%s\\n" "$version" + printf "%s\n" "$version" } find_install_path() { @@ -251,7 +251,7 @@ find_install_path() { IFS=':' read -a version_info <<<"$version" if [ "$version" = "system" ]; then - printf "\\n" + printf "\n" elif [ "${version_info[0]}" = "ref" ]; then local install_type="${version_info[0]}" local version="${version_info[1]}" @@ -263,7 +263,7 @@ find_install_path() { # And then use the binaries there local install_type="path" local version="path" - printf "%s\\n" "${version_info[1]}" + printf "%s\n" "${version_info[1]}" else local install_type="version" local version="${version_info[0]}" @@ -281,12 +281,12 @@ get_custom_executable_path() { cmd=$(basename "$executable_path") local relative_path # shellcheck disable=SC2001 - relative_path=$(printf "%s\\n" "$executable_path" | sed -e "s|${install_path}/||") + relative_path=$(printf "%s\n" "$executable_path" | sed -e "s|${install_path}/||") relative_path="$("${plugin_path}/bin/exec-path" "$install_path" "$cmd" "$relative_path")" executable_path="$install_path/$relative_path" fi - printf "%s\\n" "$executable_path" + printf "%s\n" "$executable_path" } get_executable_path() { @@ -304,11 +304,11 @@ get_executable_path() { if [ $? -ne 0 ]; then return 1 fi - printf "%s\\n" "$cmd_path" + printf "%s\n" "$cmd_path" else local install_path install_path=$(find_install_path "$plugin_name" "$version") - printf "%s\\n" "${install_path}"/"${executable_path}" + printf "%s\n" "${install_path}"/"${executable_path}" fi } @@ -320,7 +320,7 @@ parse_asdf_version_file() { local version version=$(strip_tool_version_comments "$file_path" | grep "^${plugin_name} " | sed -e "s/^${plugin_name} //") if [ -n "$version" ]; then - printf "%s\\n" "$version" + printf "%s\n" "$version" return 0 fi fi @@ -353,7 +353,7 @@ get_preset_version_for() { local version version=$(cut -d '|' -f 1 <<<"$version_and_path") - printf "%s\\n" "$version" + printf "%s\n" "$version" } get_asdf_config_value_from_file() { @@ -367,7 +367,7 @@ get_asdf_config_value_from_file() { local result result=$(grep -E "^\s*$key\s*=\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') if [ -n "$result" ]; then - printf "%s\\n" "$result" + printf "%s\n" "$result" return 0 fi @@ -414,7 +414,7 @@ initialize_or_update_repository() { disable_plugin_short_name_repo="$(get_asdf_config_value "disable_plugin_short_name_repository")" if [ "yes" == "$disable_plugin_short_name_repo" ]; then - printf "Short-name plugin repository is disabled\\n" >&2 + printf "Short-name plugin repository is disabled\n" >&2 exit 1 fi @@ -454,7 +454,7 @@ find_file_upwards() { search_path=$(pwd) while [ "$search_path" != "/" ]; do if [ -f "$search_path/$name" ]; then - printf "%s\\n" "${search_path}/$name" + printf "%s\n" "${search_path}/$name" return 0 fi search_path=$(dirname "$search_path") @@ -475,12 +475,12 @@ resolve_symlink() { # as relative case $resolved_path in /*) - printf "%s\\n" "$resolved_path" + printf "%s\n" "$resolved_path" ;; *) ( cd "$(dirname "$symlink")" || exit 1 - printf "%s\\n" "$PWD/$resolved_path" + printf "%s\n" "$PWD/$resolved_path" ) ;; esac @@ -508,7 +508,7 @@ list_plugin_bin_paths() { else local space_separated_list_of_bin_paths="bin" fi - printf "%s\\n" "$space_separated_list_of_bin_paths" + printf "%s\n" "$space_separated_list_of_bin_paths" } list_plugin_exec_paths() { @@ -531,7 +531,7 @@ list_plugin_exec_paths() { local plugin_shims_path plugin_shims_path=$(get_plugin_path "$plugin_name")/shims if [ -d "$plugin_shims_path" ]; then - printf "%s\\n" "$plugin_shims_path" + printf "%s\n" "$plugin_shims_path" fi space_separated_list_of_bin_paths="$(list_plugin_bin_paths "$plugin_name" "$version" "$install_type")" @@ -541,7 +541,7 @@ list_plugin_exec_paths() { install_path=$(get_install_path "$plugin_name" "$install_type" "$version") for bin_path in "${all_bin_paths[@]}"; do - printf "%s\\n" "$install_path/$bin_path" + printf "%s\n" "$install_path/$bin_path" done } @@ -603,7 +603,7 @@ plugin_executables() { for bin_path in "${all_bin_paths[@]}"; do for executable_file in "$bin_path"/*; do if is_executable "$executable_file"; then - printf "%s\\n" "$executable_file" + printf "%s\n" "$executable_file" fi done done @@ -631,7 +631,7 @@ shim_plugin_versions() { if [ -x "$shim_path" ]; then grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | uniq else - printf "asdf: unknown shim %s\\n" "$executable_name" + printf "asdf: unknown shim %s\n" "$executable_name" return 1 fi } @@ -644,7 +644,7 @@ shim_plugins() { if [ -x "$shim_path" ]; then grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | cut -d' ' -f 1 | uniq else - printf "asdf: unknown shim %s\\n" "$executable_name" + printf "asdf: unknown shim %s\n" "$executable_name" return 1 fi } @@ -682,7 +682,7 @@ get_shim_versions() { preset_versions() { shim_name=$1 - shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\\n\" PLUGIN \$(get_preset_version_for PLUGIN)" + shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\n\" PLUGIN \$(get_preset_version_for PLUGIN)" } select_from_preset_version() { @@ -693,7 +693,7 @@ select_from_preset_version() { shim_versions=$(get_shim_versions "$shim_name") if [ -n "$shim_versions" ]; then preset_versions=$(preset_versions "$shim_name") - grep -F "$shim_versions" <<<"$preset_versions" | head -n 1 | xargs -IVERSION printf "%s\\n" VERSION + grep -F "$shim_versions" <<<"$preset_versions" | head -n 1 | xargs -IVERSION printf "%s\n" VERSION fi } @@ -728,10 +728,10 @@ select_version() { IFS=' ' read -r plugin_shim_name plugin_shim_version <<<"$plugin_and_version" if [[ "$plugin_name" == "$plugin_shim_name" ]]; then if [[ "$plugin_version" == "$plugin_shim_version" ]]; then - printf "%s\\n" "$plugin_name $plugin_version" + printf "%s\n" "$plugin_name $plugin_version" return elif [[ "$plugin_version" == "path:"* ]]; then - printf "%s\\n" "$plugin_name $plugin_version" + printf "%s\n" "$plugin_name $plugin_version" return fi fi @@ -746,7 +746,7 @@ with_shim_executable() { local shim_exec="${2}" if [ ! -f "$(asdf_data_dir)/shims/${shim_name}" ]; then - printf "%s %s %s\\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2 + printf "%s %s %s\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2 return 1 fi @@ -803,15 +803,15 @@ with_shim_executable() { done if [ -n "${preset_plugin_versions[*]}" ]; then - printf "%s %s\\n" "No preset version installed for command" "$shim_name" - printf "%s\\n\\n" "Please install a version by running one of the following:" + printf "%s %s\n" "No preset version installed for command" "$shim_name" + printf "%s\n\n" "Please install a version by running one of the following:" for preset_plugin_version in "${preset_plugin_versions[@]}"; do - printf "%s %s\\n" "asdf install" "$preset_plugin_version" + printf "%s %s\n" "asdf install" "$preset_plugin_version" done - printf "\\n%s %s\\n" "or add one of the following versions in your config file at" "$closest_tool_version" + printf "\n%s %s\n" "or add one of the following versions in your config file at" "$closest_tool_version" else - printf "%s %s\\n" "No version is set for command" "$shim_name" - printf "%s %s\\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version" + printf "%s %s\n" "No version is set for command" "$shim_name" + printf "%s %s\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version" fi shim_plugin_versions "${shim_name}" ) >&2 diff --git a/scripts/checkstyle.py b/scripts/checkstyle.py new file mode 100755 index 00000000..18e3cfac --- /dev/null +++ b/scripts/checkstyle.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 +import re +import sys +import os +import argparse +from pathlib import Path +from typing import List, Dict, Any # compat + +# This file checks Bash and Shell scripts for violations not found with +# shellcheck or existing methods. You can use it in several ways: +# +# Lint all .bash, .sh, and .bats files and print out violations +# $ ./scripts/checkstyle.py +# +# The former, but also fix all violations. This must be ran until there +# are zero violations since any line can have more than one violation +# $ ./scripts/checkstyle.py --fix +# +# Lint a particular file +# $ ./scripts/checkstyle.py ./lib/functions/installs.bash + +class c: + RED = '\033[91m' + GREEN = '\033[92m' + YELLOW = '\033[93m' + BLUE = '\033[94m' + MAGENTA = '\033[95m' + CYAN = '\033[96m' + RESET = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + +# Before: printf '%s\\n' '^w^' +# After: printf '%s\n' '^w^' +def noDoubleBackslashFixer(line: str, rule: Dict[str, str], m: re.Match[str]) -> str: + prestr = line[0:m.start('match')] + midstr = line[m.start('match'):m.end('match')] + poststr = line[m.end('match'):] + + fixed_line = f'{prestr}{midstr[1:]}{poststr}' + return fixed_line + +def lintfile(filepath: Path, rules: List[Dict[str, str]], options: Dict[str, Any]): + content_arr = filepath.read_text().split('\n') + + for line_i, line in enumerate(content_arr): + for rule in rules: + m = re.search(rule['regex'], line) + if m is not None and m.group('match') is not None: + dir = os.path.relpath(filepath.resolve(), Path.cwd()) + prestr = line[0:m.start('match')] + midstr = line[m.start('match'):m.end('match')] + poststr = line[m.end('match'):] + + print(f'{c.CYAN}{dir}{c.RESET}:{line_i + 1}') + print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: {rule["reason"]}') + print(f'{prestr}{c.RED}{midstr}{c.RESET}{poststr}') + print() + + if options['fix']: + content_arr[line_i] = rule['fixer_fn'](line, rule, m) + + rule['found'] += 1 + + if options['fix']: + filepath.write_text('\n'.join(content_arr)) + +def main(): + rules = [ + { + 'name': 'no-double-backslash', + 'regex': '".*?(?P\\\\\\\\[abeEfnrtv\'"?xuUc]).*?(?', + 'fixer_fn': noDoubleBackslashFixer, + 'found': 0 + }, + ] + + parser = argparse.ArgumentParser() + parser.add_argument('files', metavar='FILES', nargs='*') + parser.add_argument('--fix', action='store_true') + args = parser.parse_args() + + options = { + 'fix': args.fix + } + + # parse files and print matched lints + if len(args.files) > 0: + for file in args.files: + p = Path(file) + if p.is_file(): + lintfile(p, rules, options) + else: + for file in Path.cwd().glob('**/*'): + if file.name.endswith('.bash') or file.name.endswith('.sh') or file.name.endswith('.bats'): + if file.is_file(): + lintfile(file, rules, options) + + # print final results + print(f'{c.UNDERLINE}TOTAL ISSUES{c.RESET}') + for rule in rules: + print(f'{c.MAGENTA}{rule["name"]}{c.RESET}: {rule["found"]}') + + grand_total = sum([rule['found'] for rule in rules]) + print(f'GRAND TOTAL: {grand_total}') + + # exit + if grand_total == 0: + exit(0) + else: + exit(2) + +main()