-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhard
executable file
·151 lines (132 loc) · 4.62 KB
/
hard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
# This script is inspired by Laravel Sail
# https://github.com/laravel/sail/blob/1.x/bin/sail
# Set the path to the hard directory
HARD_PATH="/home/${USER}/.hard"
# Check if docker-compose file exists
if [ ! -f $HARD_PATH/docker-compose.yml ]; then
echo "ERROR: Couldn't find a 'docker-compose.yml' file in the hard directory."
exit 1
fi
# Check if .env file exists
if [ ! -f $HARD_PATH/.env ]; then
echo "ERROR: Couldn't find '.env' in the hard directory."
exit 1
fi
# Check if WWW_PATH is a valid directory
if [ ! -d $WWW_PATH ]; then
echo "ERROR: Invalid 'WWW_PATH' in the hard '.env' file."
exit 1
fi
# Load the .env file
. $HARD_PATH/.env
# Build the docker compose command based on the docker compose version
if docker compose &> /dev/null; then
DOCKER_COMPOSE=(docker compose -f ${HARD_PATH}/docker-compose.yml)
elif docker-compose &> /dev/null; then
DOCKER_COMPOSE=(docker-compose -f ${HARD_PATH}/docker-compose.yml)
else
echo "ERROR: Couldn't find 'docker-compose' or 'docker compose' command."
exit 1
fi
# Determine if stdout is a terminal...
if test -t 1; then
# Determine if colors are supported...
ncolors=$(tput colors)
if test -n "$ncolors" && test "$ncolors" -ge 8; then
BOLD="$(tput bold)"
YELLOW="$(tput setaf 3)"
GREEN="$(tput setaf 2)"
NC="$(tput sgr0)"
fi
fi
# Function that prints the available commands...
function display_help {
echo "Laravel Hard"
echo
echo "${YELLOW}docker-compose Commands:${NC}"
echo " ${GREEN}hard up${NC} Start the environment"
echo " ${GREEN}hard up -d${NC} Start the environment in the background"
echo " ${GREEN}hard down${NC} Stop the environment"
echo " ${GREEN}hard restart${NC} Restart the environment"
echo " ${GREEN}hard ps${NC} Display the status of all containers"
echo " ${GREEN}hard bash${NC} Start a shell session within the app container"
echo
echo "${YELLOW}Laravel Commands:${NC}"
echo " ${GREEN}hard laravel ...${NC} Run the Laravel command"
echo " ${GREEN}hard laravel new awesome-project${NC}"
echo
echo "${YELLOW}PHP Commands:${NC}"
echo " ${GREEN}hard [project] php ...${NC} Run a snippet of PHP code"
echo " ${GREEN}hard awesome-project php artisan migrate${NC}"
echo
echo "${YELLOW}Composer Commands:${NC}"
echo " ${GREEN}hard [project] composer ...${NC} Run a Composer command"
echo " ${GREEN}hard awesome-project composer require laravel/sanctum${NC}"
echo
echo "${YELLOW}Node Commands:${NC}"
echo " ${GREEN}hard [project] node ...${NC} Run a Node command"
echo " ${GREEN}hard awesome-project node --version${NC}"
echo
echo "${YELLOW}NPM Commands:${NC}"
echo " ${GREEN}hard [project] npm ...${NC} Run a npm command"
echo " ${GREEN}hard awesome-project npm run prod${NC}"
echo
echo "${YELLOW}Yarn Commands:${NC}"
echo " ${GREEN}hard [project] yarn ...${NC} Run a Yarn command"
echo " ${GREEN}hard awesome-project yarn prod${NC}"
echo
exit 0
}
# Proxy the "help" command...
if [ $# -gt 0 ]; then
if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
display_help
fi
else
display_help
fi
# Check if the command is a docker compose command...
case "$1" in
up|down|build|ps|restart) YES=1;;
*) YES=0;;
esac
if [ $YES = 1 ]; then
"${DOCKER_COMPOSE[@]}" "$@"
exit 0
fi
# Check if the command going to run in the app container...
case "$1" in
bash|laravel) YES=1;;
*) YES=0;;
esac
if [ $YES = 1 ]; then
"${DOCKER_COMPOSE[@]}" exec app "$@"
exit 0
fi
# Check if the command going to run in the app container with a project...
# Case when the project is the current directory, there is no need to pass the project name
# as an argument. In this case, the project name is the current directory name.
# Example: hard php artisan migrate
# Case when the project is not the current directory,
# the project name must be passed as an argument.
# Example: hard awesome-project php artisan migrate
if [ "$1" != "" ] && [ -d $WWW_PATH/$(basename $PWD) ] || [ -d $WWW_PATH/"$1" ]; then
if [ -d $WWW_PATH/"$1" ]; then
PROJECT="$1"
shift 1
else
PROJECT="$(basename $PWD)"
fi
# Commands thar are currently supported
case "$1" in
php|composer|npm|npx|yarn) YES=1;;
*) YES=0;;
esac
if [ $YES = 1 ]; then
"${DOCKER_COMPOSE[@]}" exec -it --workdir "/var/www/$PROJECT" app "$@"
exit 0
fi
fi
# Display help if no command is found...
display_help