-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define your commands in a text file #24
Comments
this is awesome, thank you @tolicodes #!/bin/bash
# create_alias_json_files
#
# Description:
# This script extracts aliases from a specified source file and generates Command List JSON files for each alias.
# The JSON files are stored in the global Command List directory, with filenames corresponding to the aliases.
# Files are grouped by the alias source file.
#
# Parameters:
# -e|--extension-settings-path (optional): The path where the JSON files will be saved.
# Defaults to "$HOME/Library/Application Support/Code/User/globalStorage/yamajyn.commandlist".
# -a|--alias-source-path (optional): The file containing aliases. Defaults to "~/.zshrc".
#
# Example usage:
# - Specify both extension settings path and alias source path:
# create_alias_json_files -e "$HOME/Library/Application Support/Code/User/globalStorage/yamajyn.commandlist" -a "$HOME/.zshrc"
#
# - Specify only the alias source path, using default extension settings path:
# create_alias_json_files -a "$HOME/.zshrc"
#
# - Specify only the alias source path with a custom file:
# create_alias_json_files -a "$HOME/.zshrc_aliases"
#
# Note:
# Script ensures that the vscode entension `Command List` is installed. (https://marketplace.visualstudio.com/items?itemName=yamajyn.commandlist)
create_alias_json_files() {
NC=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
local extension_settings_path="$HOME/Library/Application Support/Code/User/globalStorage/yamajyn.commandlist"
local alias_source_path="$HOME/.zshrc"
while [[ "$#" -gt 0 ]]; do
case "$1" in
-e | --extension-settings-path)
shift
extension_settings_path="${1:-$extension_settings_path}"
;;
-a | --alias-source-path)
shift
alias_source_path="${1:-$alias_source_path}"
;;
*)
echo "${RED}Error: Unknown option $1.${NC}"
return 1
;;
esac
shift
done
# Ensure that the global Command List directory exists before running the script.
# Check if does not exist create it
if [ ! -d "${extension_settings_path}" ]; then
echo "${GREEN}Installing VS Code Extension 'Command list'${NC}"
code --install-extension yamajyn.commandlist --force
mkdir -p "${extension_settings_path}" || {
echo "${RED}Error: Unable to create the directory ${extension_settings_path}${NC}"
return 1
}
fi
# Check if the file exists
if [ -f "$alias_source_path" ]; then
# Use grep to find lines starting with "alias" but not preceded by # to ignore commented out aliases
mapfile -t aliases < <(
grep -oE '^[^#]*alias[[:space:]]+[^=]+' "$alias_source_path" | awk '{print $2}'
)
# Check if there are aliases
if [ ${#aliases[@]} -gt 0 ]; then
# Extract the group name from the alias source path file
group=$(basename "$alias_source_path")
group="${group#"."}" # Remove dot from the beginning of the file name
# Create the group folder if it doesn't exist
group_folder="${extension_settings_path}/$group"
if [ ! -d "$group_folder" ]; then
mkdir -p "$group_folder"
fi
# Loop through the aliases
for alias_name in "${aliases[@]}"; do
printf "\n${GREEN}Alias: $alias_name${NC}\n"
# Construct the JSON content
json_content=$(printf '{"script":"%s","label":"%s"}' "${alias_name}" "${alias_name}")
# Write the JSON content to a file inside the group folder
echo "$json_content" >"${group_folder}/${alias_name}.json"
if [ $? -eq 0 ]; then
printf "Created JSON file: \"$group_folder/${alias_name}.json\"\n"
else
echo "${RED}Error: Unable to create JSON file for ${alias_name}${NC}"
fi
done
else
echo "${RED}No aliases found in the file - ${NC} ${GREEN}$(basename "$alias_source_path")${NC}"
fi
else
echo "${RED}File not found: $alias_source_path${NC}"
fi
}
# Script is not being sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
create_alias_json_files -a "$HOME/.zshrc"
create_alias_json_files -a "$HOME/.zshrc_aliases"
fi
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be very useful if you could define your commands in a text file.
Through must searching I found where you store the data...
I also wrote a shell script to convert your zsh aliases to commands. Perhaps you can have an option )
$HOME/Library/Application Support/Code/User/globalStorage/yamajyn.commandlis
The text was updated successfully, but these errors were encountered: