Skip to content

Commit

Permalink
Separate sort script
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai2038 committed Nov 11, 2024
1 parent 9bcaea0 commit 97a5bdf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 69 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 63 additions & 0 deletions bash_ide_from_vs_code_sort_sources.sh
Original file line number Diff line number Diff line change
@@ -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 "$?"
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
# ========================================

Expand Down Expand Up @@ -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 "$?"

0 comments on commit 97a5bdf

Please sign in to comment.