Skip to content

Commit

Permalink
feat: update dev script
Browse files Browse the repository at this point in the history
  • Loading branch information
csvenke committed Sep 15, 2024
1 parent 0820cdb commit 86a8fd9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 48 deletions.
3 changes: 2 additions & 1 deletion packages/dev/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pkgs.writeShellApplication {
name = "dev";
runtimeInputs = with pkgs; [ fd findutils gnused gawk ];
runtimeInputs = with pkgs; [ fd fzf gnused gawk ];
text = builtins.readFile ./script.bash;
}

82 changes: 35 additions & 47 deletions packages/dev/script.bash
Original file line number Diff line number Diff line change
@@ -1,59 +1,47 @@
function getEnvOrDefault() {
local value="$1"
local defaultValue="$2"
echo "${!value:-$defaultValue}"
}
function main() {
local search_pattern="(\.git$|package\.json$|\.sln$|\.csproj$)"
local search_paths

SEARCH_DIRS=$(getEnvOrDefault "DEV_SEARCH_DIRS" "$HOME/repos")
ROOT_FILE_PATTERN=$(getEnvOrDefault "DEV_ROOT_FILE_PATTERN" "(\.git$|package\.json$|\.sln$|\.csproj$)")

function findRoots() {
local search_dir=$1
local root_files="$ROOT_FILE_PATTERN"
fd --hidden --follow --regex "$root_files" "$search_dir" |
xargs -I {} dirname {} |
sort -u |
sed "s|^$search_dir/||" |
awk -v prefix="$search_dir" 'BEGIN { gray="\033[90m"; blue="\033[34m"; reset="\033[0m"; folderIcon=" "; } { print blue folderIcon $0 reset " " gray "(" prefix "/" $0 ")" reset }'
}
search_paths=$(find_search_paths)
read -r -a search_paths_array <<<"$search_paths"

function gatherRoots() {
local directory_paths=""
local project_paths
project_paths=$(find_project_paths "$search_pattern" "${search_paths_array[@]}")

for dir in "$@"; do
if [ -e "$dir" ]; then
matches=$(findRoots "$dir")
directory_paths=$(printf "%s\n%s" "$directory_paths" "$matches")
fi
done
local selected_path
selected_path=$(select_path "$project_paths")

echo "$directory_paths"
}
if [[ -z "$selected_path" ]]; then
exit 1
fi

function selectDir() {
local formatted_directories="$1"
echo "$formatted_directories" |
fzf --ansi --border=none |
sed 's/.*(\(.*\)).*/\1/'
}
cd "$selected_path" || exit 1

function openWithEditor() {
local target_path="$1"
if [ -n "$target_path" ]; then
cd "$target_path" || exit
$VISUAL .
if [ -n "$VISUAL" ]; then
"$VISUAL" .
exit 0
fi
}

function dev() {
if [ $# -eq 0 ]; then
return 1
if [ -n "$EDITOR" ]; then
"$EDITOR" .
exit 0
fi
}

function find_search_paths() {
fd --type d --max-depth 1 --absolute-path . "$HOME" | sed 's@/$@@' | tr '\n' ' '
}

function find_project_paths() {
local root_files="$1"
shift
local search_dir=("$@")
fd --hidden --follow --regex "$root_files" "${search_dir[@]}" -x dirname | sort -u
}

project_dirs=$(gatherRoots "$@")
selected_project_dir=$(selectDir "$project_dirs")
openWithEditor "$selected_project_dir"
function select_path() {
local paths="$1"
echo "$paths" | sed 's|.*/\([^/]*\)|\1 (\0)|' | fzf --ansi --border=none | sed -n 's/.*(\(.*\)).*/\1/p'
}

read -ra search_dir_args <<<"$SEARCH_DIRS"
dev "${search_dir_args[@]}"
main

0 comments on commit 86a8fd9

Please sign in to comment.