hikan.ru/eye.sh

110 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
stty -echoctl # Отключает вывод управляющих символов по типу ^C
# НАСТРОЙКА СКРИПТА ТУТ ###########################################################
DURATION=1 # Задержка между проверками в секундах
WATCH_TARGETS=("assets" "mvc" "posts" "tools" "main.go") # Массив целей для наблюдения (директории и файлы)
BINARY_PATH="./main" # Путь до бинарного файла
BUILD_CMD="go build -o $BINARY_PATH main.go" # Команда для сборки
###################################################################################
# Массивы для хранения информации о целях
declare -A LAST_MODS
declare -A LAST_COUNTS
CLEANUP_DONE=0
# Вывод в синем цвете
blue() {
echo -e "\033[34mEYE | $1\033[0m"
}
# Очистка при завершении работы скрипта
cleanup() {
[ $CLEANUP_DONE -eq 1 ] && exit 0
blue "cleanup..."
kill_proc $1
rm -f $BINARY_PATH
blue "see you later!"
CLEANUP_DONE=1
exit 0
}
# Убийство процесса по его pid
kill_proc() {
local pid=$1
if [ -n "$pid" ] && kill -0 $pid 2>/dev/null; then
kill $pid
blue "process killed (PID: $pid)"
fi
}
# Проверка изменений в целях
check_changes() {
local changed=0
for target in "${WATCH_TARGETS[@]}"; do
local current_mod=0
local current_count=0
if [ -f "$target" ]; then
# Обработка файла
current_mod=$(stat -c %Y "$target")
current_count=1
elif [ -d "$target" ]; then
# Обработка директории
current_mod=$(find "$target" -type f -exec stat -c %Y {} \; | sort -nr | head -1)
current_count=$(find "$target" -type f | wc -l)
fi
if { [ -n "$current_mod" ] && [ "${LAST_MODS[$target]:-0}" -lt "$current_mod" ]; } || [ "${LAST_COUNTS[$target]:-0}" -ne "$current_count" ]; then
changed=1
LAST_MODS["$target"]=$current_mod
LAST_COUNTS["$target"]=$current_count
[ -n "$1" ] && blue "changes detected in \033[94m$target\033[0m"
fi
done
return $changed
}
# Основная функция
main() {
local pid=""
# Ловушка для сигналов завершения
trap 'cleanup $pid' SIGINT SIGTERM SIGHUP SIGQUIT EXIT
# Инициализация массивов
for target in "${WATCH_TARGETS[@]}"; do
if [ -f "$target" ]; then
LAST_MODS["$target"]=0
LAST_COUNTS["$target"]=1
elif [ -d "$target" ]; then
LAST_MODS["$target"]=0
LAST_COUNTS["$target"]=$(find "$target" -type f | wc -l)
fi
blue "started watching \033[94m$target\033[0m"
done
# Основной цикл работы скрипта
while true; do
check_changes $pid
if [ $? -eq 1 ]; then
blue "rebuilding..."
$BUILD_CMD
if [ $? -eq 0 ]; then
blue "build successful. restarting..."
kill_proc $pid
$BINARY_PATH &
pid=$!
blue "started new process (PID: $pid)"
else
blue "build failed"
fi
fi
sleep $DURATION
done
}
main