-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchless.sh
executable file
·221 lines (203 loc) · 5.51 KB
/
watchless.sh
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/sh
ver=0.1.1
#-----------------------------------------------------------------------#
#
# watchless
#
# Watches for LESS file changes in the specified directory using inotifywait
# and compiles those files to CSS. Supports imported files.
# Christian Petrov <[email protected]>
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#-----------------------------------------------------------------------#
echo_help()
{
echo "Usage: watchless [OPTION] [DIR]"
echo " Where DIR is the directory containing your LESS files, which"
echo " may contain included files."
echo " -h Display this help and exit."
echo " -v Display version number and exit."
echo " -k Keeps directory structure intact, for example"
echo " ~/Project/less/main.less will be compiled to "
echo " ~/Project/css/main.css. Without the -k option"
echo " CSS files will be created in the same directory"
echo " as the LESS file."
echo " Example: watchless -k ~/Project/less"
exit 0
}
echo_version()
{
echo "Version: "$ver
exit 0
}
keepDirStructure=false
while getopts ":khv" opt; do
case $opt in
k)
keepDirStructure=true
;;
h)
echo_help
;;
v)
echo_version
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
shift $(( ${OPTIND} - 1 ));
if [[ -n "$1" ]]
then
lessDir=$1
childrenPath=
childrenCount=0
parents=()
orphans=()
if [ -d "$lessDir" ]
then
all=$(find $lessDir -maxdepth 1 -type f -name '*.less')
allArr=( $all )
echo "Watching $1 for LESS file modifications."
if [ ! -z "$all" ]
then
filesCount=0
for lessFile in "${allArr[@]}"
do
let "filesCount+=1"
content=$(cat "$lessFile" | grep @import)
if [[ "$content" == *@* ]]
then
childName[$filesCount]=$(echo $content | sed 's/@import//g;s/ //g;s/"//g;s/;/,/g')
IFS=',' read -a childrenNames <<< "${childName[$filesCount]}"
for childName in "${childrenNames[@]}"
do
i=0
inclArr=
while [ $i -lt ${#childName} ]
do
inclArr[$i]=${childName:$i:1}
i=$((i+1))
done
dotSegments=0
nonDotReached=false
for z in "${inclArr[@]}"
do
if [ "$z" == "." ] && ! $nonDotReached
then
dotSegments=$((dotSegments+1))
elif [ "$z" != "." ]
then
nonDotReached=true
fi
done
childName=$(sed -r "s/^.{$dotSegments}//;s/^\///" <<< $childName)
_childPath=$1 #to sed
slashes="${_childPath//[^\/]}"
slashesCount="${#slashes}"
slashesToKeep=$(($slashesCount-$dotSegments))
n=$((3+$slashesToKeep))
_childPath=$(sed "s/[^/]*//$n;s/\/\/.*//g" <<< $_childPath)
_childPath=${_childPath%/}
parents[$childrenCount]=$lessFile
children[$childrenCount]=$_childPath/$childName
childrenPath[$childrenCount]=$(echo ${children[$childrenCount]} | sed "s/\/[^\/]*$//")
let "childrenCount+=1"
done
fi
done
orphans=("${allArr[@]}")
count=0
for file in ${orphans[@]}
do
for parent in ${parents[@]}
do
if [[ "$file" == "$parent" ]]
then
unset orphans[$count]
fi
done
for child in ${children[@]}
do
if [[ "$file" == "$child" ]]
then
unset orphans[$count]
fi
done
count=$(($count+1))
done
childrenPath+=($1)
paths=$(echo ${childrenPath[@]} | sed "s/ /\n/g" | sort | uniq )
while true
do
inotifywait -qe modify --format '%w%f' $paths | while read modifiedFile
do
count=0
fileIsIncluded=false
handleDirStructure()
{
if $keepDirStructure
then
cssFile=$(sed 's/\/less\//\/css\//g' <<< $cssFile)
fi
}
for child in "${children[@]}"
do
if [[ "$child" == "$modifiedFile" ]]
then
cssFile=${parents[$count]}
handleDirStructure
cssFile=$(sed 's/\.less/\.css/g' <<< $cssFile)
echo -e "\nCompiling parent of modified file \n $modifiedFile \nto:\n \e[1;32m"$cssFile"\e[00m"
lessc --verbose ${parents[$count]} > $cssFile
fileIsIncluded=true
break
fi
let "count+=1"
done
if [[ $fileIsIncluded == false ]]
then
for parent in "${parents[@]}"
do
if [[ "$parent" == "$modifiedFile" ]]
then
cssFile=$(sed 's/\.less/\.css/g' <<< $modifiedFile)
handleDirStructure
echo -e "\nCompiling modified file \n $modifiedFile \nto:\n \e[1;32m"$cssFile"\e[00m"
lessc --verbose $modifiedFile > $cssFile
break
fi
done
for orphan in "${orphans[@]}"
do
if [[ "$orphan" == "$modifiedFile" ]]
then
cssFile=$(sed 's/\.less/\.css/g' <<< $modifiedFile)
handleDirStructure
echo -e "\nCompiling modified orphan file \n $modifiedFile \nto:\n \e[1;32m"$cssFile"\e[00m"
lessc --verbose $modifiedFile > $cssFile
break
fi
done
fi
done
done
else
echo "No less files found in the specified directory." ; exit 1
fi
else
echo "Specified directory doesn't exist." ; exit 1
fi
else
echo "Less directory path not given. Usage: watchless [OPTION] [DIR]" ; exit 1
fi