Added $(appNameUpper) handling in sources and headers.
[apps/native/sample/sample-core-components.git] / tool / development / convert-tpk-to-sample-project.sh
1 #!/bin/bash
2
3 function print_debug_info() {
4     if [ "$verbose" == "1" ]; then
5         echo "$1"
6     fi
7 }
8
9 function replaceContentOfFiles() {
10     pattern=$1
11     replaceString=$2
12     for fileName in $(grep -rl "$pattern" .); do
13         sed -i "s/$pattern/$replaceString/g" $fileName
14     done
15 }
16
17 set +e
18
19 usage()
20 {
21 cat << EOF
22 usage: $0 options
23
24 This script will import tpk source from revision into current Sample SDK project branch directory
25
26 OPTIONS:
27    -h                Show this message
28    -r REVISION       Revision to import tpk sources (revision is looked in current directory which expects to be git repository)
29    -o                output directory. If it doesn't exist it will be created. By default current working directory.
30    -v                verbose output
31
32 examples:
33 #example 1:
34 #tizen_2.3.1 is a branch containing sample sdk project
35 #tpk_2.3.1 branch is branch containing ready to build tpk source.
36 git checkout tizen_2.3.1
37 $0 -v -r tpk_2.3.1
38
39 #example 2:
40 #you have App directory created by SDK.
41 #you initialized git repo inside App directory locally and added tpk sources to tpk branch
42 #If you want to create SDK sample structure in ~/SDK/Samples/App directory follow steps
43
44 #1 create sdk sample related metafiles
45 mkdir sample-project-src
46 touch description.xml  LICENSE  sample.xml  screenshot.png #fill files with correct data
47 git add sample-project-src
48 git commit -m "Added sample sdk project metafiles"
49 $0 -r tpk -o ~/SDK/Samples/App
50 EOF
51 }
52
53 CURRENT_DIR=$(pwd)
54 outputDir="$CURRENT_DIR"
55 verbose=""
56 revision=""
57
58 while getopts ":hr:o:v" opt; do
59   case $opt in
60     h)
61       usage
62       exit
63       ;;
64     r)
65       revision="$OPTARG"
66       ;;
67     v)
68       verbose="1"
69       ;;
70     o)
71       outputDir="$OPTARG"
72       ;;
73     ?)
74       echo "Invalid option: -$OPTARG"
75       usage
76       exit
77       ;;
78   esac
79 done
80
81 if [ "$revision" == "" ]; then
82     echo "Revision is empty. Please provide revision"
83     usage
84     exit 1
85 fi
86
87 if [ ! -d $CURRENT_DIR/.git ]; then
88     echo "No $PWD/.git. Script can be invoked only in tpk sources git directory"
89     usage
90     exit 1
91 fi
92
93 outputDir=$(cd $outputDir; pwd)
94
95 tmpDir=$(mktemp -d)
96
97 print_debug_info "extracting revision $revision into $tmpDir"
98
99 git archive $revision | tar -x -C $tmpDir
100
101 print_debug_info "looking for application name in $tmpDir/tizen-manifest.xml"
102
103 appName=$(grep -Po 'exec\s*=\s*\".*?\"' $tmpDir/tizen-manifest.xml | sed 's/\([\"= ]\|\(exec\)\)//g')
104 appNameLower=${appName,,}
105 appNameUpper=${appName,}
106 packageName=$(grep -Po 'package\s*=\s*\".*?\"' $tmpDir/tizen-manifest.xml | sed 's/\([\"= ]\|\(package\)\)//g')
107 packageNameLower=${packageName,,}
108 packageNameUpper=${packageName,}
109
110 echo "found application name \"$appName\" \"$appNameLower\" and packageName: \"$packageName\""
111
112 print_debug_info "Copying sample project files to $PWD"
113
114 mkdir -p $outputDir/project
115
116 cp $tmpDir/sample-project-src/* $outputDir
117
118 TPK_SOURCE_DIR=$outputDir/project
119
120 mv $tmpDir/shared/res/$appName.png $tmpDir/shared/res/icon.png
121 if [ -e $tmpDir/inc/$appName.h ]; then
122         mv $tmpDir/inc/$appName.h $tmpDir/inc/main.h
123 fi
124
125 if [ -e $tmpDir/src/$appName.c ]; then
126         mv $tmpDir/src/$appName.c $tmpDir/src/main.c
127 fi
128
129 if [ -e $tmpDir/res/edje/$appName.edc ]; then
130         mv $tmpDir/res/edje/$appName.edc $tmpDir/res/edje/main.edc
131 fi
132
133 cd $tmpDir
134
135 print_debug_info "Making tizen-manifest.xml application independent"
136 sed -i "s/$packageName/\$\(packageName\)/g" tizen-manifest.xml
137 sed -i "s/$appName/\$\(appName\)/g" tizen-manifest.xml
138 sed -i "s/$packageNameUpper/\$\(packageNameUpper\)/g" tizen-manifest.xml
139 sed -i "s/$appNameUpper/\$\(appNameUpper\)/g" tizen-manifest.xml
140 sed -i "s/$packageNameLower/\$\(packageNameLower\)/g" tizen-manifest.xml
141 sed -i "s/$appNameLower/\$\(appNameLower\)/g" tizen-manifest.xml
142 sed -i "s/profile\s\+name=\".*\"/profile name=\"\$\(packageProfile\)\"/g" tizen-manifest.xml
143 sed -i "s|value=\"inc/$appName.h\"|value=\"inc/\$\(appName\).h\"|g" sample-project-src/sample.xml
144
145 print_debug_info "Replacing LOG_TAG, APP_NAME, PACKAGE"
146
147 replaceContentOfFiles "#_${appNameUpper}_H_" "_\$(appNameUpper)_H_"
148 replaceContentOfFiles "#define\s\+LOG_TAG\s\+\"$appName\"" "#define LOG_TAG \"\$(appName)\""
149 replaceContentOfFiles "#define\s\+APP_NAME\s\+\"$appName\"" "#define APP_NAME \"\$(appName)\""
150 replaceContentOfFiles "#define\s\+PACKAGE\s\+\"$packageName\"" "#define PACKAGE \"\$(packageName)\""
151 replaceContentOfFiles "#include\s\+\"$appName.h\"" "#include \"\$(appName).h\""
152
153 print_debug_info "Copying tpk source files to $TPK_SOURCE_DIR"
154 rm -rf $TPK_SOURCE_DIR
155 mkdir -p $TPK_SOURCE_DIR
156 cp -r $(ls | grep -v sample-project-src) $TPK_SOURCE_DIR
157 cd - &> /dev/null
158 echo "New sources imported. Invoke git diff to see changes, git add and commit to update sample with new source code."