diff --git a/README.md b/README.md index 33d3b88..ad92eaa 100644 --- a/README.md +++ b/README.md @@ -55,22 +55,32 @@ We use it to update all links to the file and from the file to other files. { "event": "onFileRename", "match": ".*\\.sh", - "cmd": "/usr/bin/rename_or_move_shell_script \"${workspaceRoot}\" \"${fileOld}\" \"${file}\"" + "cmd": "/usr/bin/bash_ide_from_vs_code_update_sources \"${workspaceRoot}\" \"${fileOld}\" \"${file}\"" + }, + { + "event": "onFileChange", + "match": ".*\\.sh", + "cmd": "/usr/bin/bash_ide_from_vs_code_sort_sources \"${file}\"" } ], ``` -I wrote the script `rename_or_move_shell_script.sh` which will do all the renames, and put it in this repository. -You can install it by running: +I wrote scripts, which will do all the renames, and put them in this repository. +You can install them by running: ```sh -sudo wget -O /usr/bin/rename_or_move_shell_script https://raw.githubusercontent.com/Nikolai2038/bash-ide-from-vs-code/refs/heads/main/rename_or_move_shell_script.sh && \ -sudo chmod +x /usr/bin/rename_or_move_shell_script +sudo wget -O /usr/bin/bash_ide_from_vs_code_update_sources https://raw.githubusercontent.com/Nikolai2038/bash-ide-from-vs-code/refs/heads/main/bash_ide_from_vs_code_update_sources.sh && \ +sudo chmod +x /usr/bin/bash_ide_from_vs_code_update_sources && \ +sudo wget -O /usr/bin/bash_ide_from_vs_code_sort_sources https://raw.githubusercontent.com/Nikolai2038/bash-ide-from-vs-code/refs/heads/main/bash_ide_from_vs_code_sort_sources.sh && \ +sudo chmod +x /usr/bin/bash_ide_from_vs_code_sort_sources ``` -On how the script works - you can check by yourself. -It replaces links in `source` commands both in moved file and in files, where moved file is referenced. -Also, this script sorts `source` commands in the file. +On how the scripts works - you can check by yourself. +They: + +- Replace links in `source` commands both in moved file and in files, where moved file is referenced; +- Sort `source` commands in changed files. + If you found a bug or have a suggestion on how to optimize it, you will be the awesome man to let me know something that will make it better! ### 3.3. [Path Autocomplete](https://marketplace.visualstudio.com/items?itemName=ionutvmi.path-autocomplete) diff --git a/bash_ide_from_vs_code_sort_sources.sh b/bash_ide_from_vs_code_sort_sources.sh new file mode 100755 index 0000000..68a673e --- /dev/null +++ b/bash_ide_from_vs_code_sort_sources.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Fail command if any of pipeline blocks fail +set -o pipefail + +# Sort sources in the specified file +bash_ide_from_vs_code_sort_sources() { + local file_path="${1}" && { shift || true; } + + # File content + local file_content + file_content="$(cat "${file_path}")" || return "$?" + + # Lines of the file content + declare -a file_lines + # Make sure to add empty line, so the "for" cycle will check all source blocks + mapfile -t file_lines <<< "${file_content} +" || return "$?" + + # New file content - with sorted "source" blocks + local new_file_content + + local was_source=0 + local sources_to_sort="" + + local line + for line in "${file_lines[@]}"; do + if [[ ! ${line} =~ ^source[[:blank:]] ]]; then + if ((was_source)); then + # Sort and remove empty line at the end + sources_to_sort="$(echo -n "${sources_to_sort}" | sort | grep -v '^$')" || return "$?" + if [ -n "${new_file_content}" ]; then + new_file_content+=" +" || return "$?" + fi + new_file_content+="${sources_to_sort}" || return "$?" + fi + + was_source=0 + sources_to_sort="" + if [ -n "${new_file_content}" ]; then + new_file_content+=" +" || return "$?" + fi + new_file_content+="${line}" || return "$?" + else + was_source=1 + if [ -n "${sources_to_sort}" ]; then + sources_to_sort+=" +" || return "$?" + fi + sources_to_sort+="${line}" || return "$?" + fi + done + + # Remove one empty line at the end because we added it when converting to array + # shellcheck disable=SC2320 + echo -n "${new_file_content}" > "${file_path}" || return "$?" + + return 0 +} + +bash_ide_from_vs_code_sort_sources "${@}" || exit "$?" diff --git a/rename_or_move_shell_script.sh b/bash_ide_from_vs_code_update_sources.sh similarity index 71% rename from rename_or_move_shell_script.sh rename to bash_ide_from_vs_code_update_sources.sh index 287289a..9e21a58 100755 --- a/rename_or_move_shell_script.sh +++ b/bash_ide_from_vs_code_update_sources.sh @@ -9,65 +9,8 @@ escape_sed() { return 0 } -# Sort sources in the specified file -sort_sources() { - local file_path="${1}" && { shift || true; } - - # File content - local file_content - file_content="$(cat "${file_path}")" || return "$?" - - # Lines of the file content - declare -a file_lines - # Make sure to add empty line, so the "for" cycle will check all source blocks - mapfile -t file_lines <<< "${file_content} -" || return "$?" - - # New file content - with sorted "source" blocks - local new_file_content - - local was_source=0 - local sources_to_sort="" - - local line - for line in "${file_lines[@]}"; do - if [[ ! ${line} =~ ^source[[:blank:]] ]]; then - if ((was_source)); then - # Sort and remove empty line at the end - sources_to_sort="$(echo -n "${sources_to_sort}" | sort | grep -v '^$')" || return "$?" - if [ -n "${new_file_content}" ]; then - new_file_content+=" -" || return "$?" - fi - new_file_content+="${sources_to_sort}" || return "$?" - fi - - was_source=0 - sources_to_sort="" - if [ -n "${new_file_content}" ]; then - new_file_content+=" -" || return "$?" - fi - new_file_content+="${line}" || return "$?" - else - was_source=1 - if [ -n "${sources_to_sort}" ]; then - sources_to_sort+=" -" || return "$?" - fi - sources_to_sort+="${line}" || return "$?" - fi - done - - # Remove one empty line at the end because we added it when converting to array - # shellcheck disable=SC2320 - echo -n "${new_file_content}" > "${file_path}" || return "$?" - - return 0 -} - # Update references in sources when file was moved -rename_or_move_shell_script() { +bash_ide_from_vs_code_update_sources() { local workspace_full_path="${1}" && { shift || true; } local file_full_path_old="${1}" && { shift || true; } local file_full_path_new="${1}" && { shift || true; } @@ -103,7 +46,7 @@ rename_or_move_shell_script() { sed -Ei "s#$(escape_sed "${relative_file_path_old}")#$(escape_sed "${relative_file_path_new}")#g" "${shell_script_in_workspace}" || return "$?" # Sort sources in referenced file - sort_sources "${shell_script_in_workspace}" || return "$?" + bash_ide_from_vs_code_sort_sources "${shell_script_in_workspace}" || return "$?" done # ======================================== @@ -172,9 +115,9 @@ rename_or_move_shell_script() { # ======================================== # Sort sources in moved file - sort_sources "${file_full_path_new}" || return "$?" + bash_ide_from_vs_code_bash_ide_from_vs_code_sort_sources "${file_full_path_new}" || return "$?" return 0 } -rename_or_move_shell_script "${@}" || exit "$?" +bash_ide_from_vs_code_update_sources "${@}" || exit "$?"