-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChangePackage.sh
executable file
·111 lines (103 loc) · 2.59 KB
/
ChangePackage.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
#!/bin/bash
##
# ChangePackage.sh
# Change Package for Android
#
# @author kuroneko
# @copyright 2015 kuroneko. All rights reserved
##
readonly SOURCE_DIRECTORY='src'
readonly FIXED_CHANGE_FILES=( "AndroidManifest.xml" )
sourcePackage=
sourceDirectory=
sourceRegularExpression=
sourceDirectories=()
destinationPackage=
destinationDirectory=
destinationRegularExpression=
function usage(){
echo ""
echo "Usage: bash $0 <Source Package Name> <Destination Package Name>"
echo " This script is to change Package Name of Android."
echo ""
return
}
function initialize(){
sourcePackage="$1"
destinationPackage="$2"
IFS='.'
for _directory in ${sourcePackage}
do
if [ -z ${sourceDirectory} ]
then
sourceDirectory=${_directory}
sourceRegularExpression=${_directory}
else
sourceDirectory=${sourceDirectory}'/'${_directory}
sourceRegularExpression=${sourceRegularExpression}'\.'${_directory}
fi
sourceDirectories=( "${sourceDirectory}" "${sourceDirectories[@]}" )
done
for _directory in ${destinationPackage}
do
if [ -z ${destinationDirectory} ]
then
destinationDirectory=${_directory}
destinationRegularExpression=${_directory}
else
destinationDirectory=${destinationDirectory}'/'${_directory}
destinationRegularExpression=${destinationRegularExpression}'\.'${_directory}
fi
done
IFS=' '
if [ -d ${SOURCE_DIRECTORY}"/${sourceDirectory}" ]
then
return 1
else
echo "No such package directory in ${sourcePackage}"
return 0
fi
}
function change(){
local _current=`pwd`
cd ${SOURCE_DIRECTORY}
if [ -n "${destinationDirectory}" ]
then
if [ ! -d "./${destinationDirectory}" ]
then
mkdir -p "./${destinationDirectory}"
fi
fi
mv -f "./${sourceDirectory}/"* "./${destinationDirectory}"
for _directory in ${sourceDirectories[@]}
do
if [ -z "$(ls -A "${_directory}")" ]
then
rmdir "${_directory}"
else
break
fi
done
cd ${_current}
return
}
function rewrite(){
local _files=${FIXED_CHANGE_FILES[@]}
while read _file
do
_files+=(${_file})
done < <(find "${SOURCE_DIRECTORY}" -name *.java )
sed -i '' -e "s/${sourceRegularExpression}/${destinationRegularExpression}/g" "${_files[@]}"
return
}
if [ $# -lt 2 ]
then
usage
else
initialize "$1" "$2"
if [ $? -ne 0 ]
then
change
rewrite
fi
fi