/
auto_res_tool.sh
201 lines (176 loc) · 5.07 KB
/
auto_res_tool.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
#!/bin/bash
#此版本生成3套dimen,以 sw360dp 为基准,
############################################
# dos2unix auto_res_tool.sh #
# change the format dos to unix command #
############################################
DIMEN_VALUE=("360" "320" "411")
#dp属性名
DIMEN_ATTRIBUTE_NAME="xdp"
#生成的dimen的个数
DIMEN_NUM=1000
#步进
DIMEN_STEP=0.5
#sp属性名
SP_ATTRIBUTE_NAME="xsp"
#sp个数
SP_NUM=40
#步进
SP_STEP=1
#exit
function fun_exit_shell() {
echo "exit"
exit
}
# create hint
# $1 = 160 , 320 ...; $2 = file_name like dimen.xml etc.
function fuc_create_hint() {
for value in ${DIMEN_VALUE[@]}
do
if [ $1 == $value ]
then
hint="\n<!-- the auto generate dimen values with ${value}dp minimum width -->\n"
echo -e $hint
echo -e $hint >> $2
fi
done
}
function fun_create_values_dir() {
#check dir swXXX is exist, if not, create it!
for value in ${DIMEN_VALUE[@]}
do
# if dir don't equal value and value-swXXXdp don't exist, then create this dir
dir_name="values-sw${value}dp"
if [ ! -d $dir_name ]
then
mkdir $dir_name
echo "create dir ${dir_name} success."
fi
done
echo "all value dir have been created success!"
}
#--------------------------------------- the shell start ---------------------------------------
# check current dir
if [ ! -d "src/main/res" ]
then
echo "please execute this shell in the main module root dir!"
fun_exit_shell
fi
echo "read dir success!"
cd src/main/res
if [ ! -d "values" ]
then
fun_exit_shell
fi
echo "there have dir values."
#---------------------------------- in res dir ----------------------------------
#检查并创建目录
fun_create_values_dir
#the base base-dpi dimen be created -- start --
echo "the base ${DIMEN_VALUE[0]}dpi dimen be created -- start --"
cd values-sw${DIMEN_VALUE[0]}dp
#check the dimen.xml file is exist
if [ ! -f "dimens.xml" ]
then
echo "create new file dimens.xml in values."
touch "dimens.xml"
echo -e "<resources xmlns:tools=\"http://schemas.android.com/tools\">" >> dimens.xml
fuc_create_hint ${DIMEN_VALUE[0]} 'dimens.xml'
else
#have file, and delete the end </resources>
sed -i "/<\/resources>/d" dimens.xml
fuc_create_hint ${DIMEN_VALUE[0]} 'dimens.xml'
fi
#循环生成 dp
echo "create dp in values-sw360dp start"
for ((i=0;i<${DIMEN_NUM};i++))
do
name=`echo "scale=2; ${DIMEN_STEP}*${i} " | bc -l`
if [ ${name:0:1} == "." ]
then
name="0"$name
fi
echo "create <dimen name=\"${DIMEN_ATTRIBUTE_NAME}_${name}\">${name}dp</dimen>"
echo -e " <dimen name=\"${DIMEN_ATTRIBUTE_NAME}_${name}\">${name}dp</dimen>" >> dimens.xml
done
echo "create dp in values-sw360dp end"
echo -e "\nthe sp res" >> dimens.xml
#循环生成sp
echo "create sp in values-sw360dp start"
for ((i=0;i<${SP_NUM};i++))
do
name=`echo "scale=2; ${SP_STEP}*${i} " | bc -l`
if [ ${name:0:1} == "." ]
then
name="0"$name
fi
echo "<dimen name=\"${SP_ATTRIBUTE_NAME}_${name}\">${name}sp</dimen>"
echo -e " <dimen name=\"${SP_ATTRIBUTE_NAME}_${name}\">${name}sp</dimen>" >> dimens.xml
done
echo "create sp in values-sw360dp end"
echo -e "</resources>" >> dimens.xml
cd -
echo "the base sw360dp dimen be created -- end --"
#the base 360dpi dimen be created -- end --
#the other dir be created -- start --
for ((index=1;index<${#DIMEN_VALUE[@]};index++))
do
SCALE=`echo "scale=2; ${DIMEN_VALUE[$index]}/360 " | bc -l`
echo "the SCALE is "$SCALE
cd values-sw${DIMEN_VALUE[$index]}dp
#check the dimen.xml file is exist
if [ ! -f "dimens.xml" ]
then
echo "create new file dimens.xml in values-sw${DIMEN_VALUE[$index]}dp."
touch "dimens.xml"
echo -e "<resources xmlns:tools=\"http://schemas.android.com/tools\">" >> dimens.xml
fuc_create_hint ${DIMEN_VALUE[$index]} 'dimens.xml'
else
#have file, and delete the end </resources>
sed -i "/<\/resources>/d" dimens.xml
fuc_create_hint ${DIMEN_VALUE[$index]} 'dimens.xml'
fi
#循环生成 dp
echo "create dp in values-sw${DIMEN_VALUE[$index]}dp start"
for ((i=0;i<${DIMEN_NUM};i++))
do
#caculate the res dp
name=`echo "scale=2; ${DIMEN_STEP}*${i} " | bc -l`
res=`echo "scale=2; ${DIMEN_STEP}*${SCALE}*${i} " | bc -l`
if [ ${name:0:1} == "." ]
then
name="0"$name
fi
if [ ${res:0:1} == "." ]
then
res="0"$res
fi
echo "<dimen name=\"${DIMEN_ATTRIBUTE_NAME}_${name}\">${res}dp</dimen>"
echo -e " <dimen name=\"${DIMEN_ATTRIBUTE_NAME}_${name}\">${res}dp</dimen>" >> dimens.xml
done
echo "create dp in values-sw${DIMEN_VALUE[$index]}dp end"
echo -e "\nthe sp res" >> dimens.xml
#循环生成 sp
echo "create sp in values-sw${DIMEN_VALUE[$index]}dp start"
for ((i=0;i<${SP_NUM};i++))
do
#caculate the res sp
name=`echo "scale=2; ${SP_STEP}*${i} " | bc -l`
res=`echo "scale=2; ${SP_STEP}*${SCALE}*${i} " | bc -l`
if [ ${name:0:1} == "." ]
then
name="0"$name
fi
if [ ${res:0:1} == "." ]
then
res="0"$res
fi
echo "<dimen name=\"${SP_ATTRIBUTE_NAME}_${name}\">${res}sp</dimen>"
echo -e " <dimen name=\"${SP_ATTRIBUTE_NAME}_${name}\">${res}sp</dimen>" >> dimens.xml
done
echo "create sp in values-sw${DIMEN_VALUE[$index]}dp end"
echo -e "</resources>" >> dimens.xml
cd -
done
#the other dir be created -- end --
#echo "scale=2; 1.55 * 1.55 " | bc -l