RSS

Membuat paket instalasi vb + mysql dengan NSIS

NSIS adalah salah satu tool gratis dan open source untuk membuat paket instalasi. Oke sebelum kita melangkah lebih jauh, berikut adalah hal-hal yang perlu kita persiapkan :

  1. NSIS
  2. MySQL versi noninstall disesuaikan dengan versi MySQL yang terinstall di komputer Anda
  3. Connector ODBC yang sudah terinstall biasanya ada di C:\Program Files\MySQL\Connector ODBC 5.1
  4. Database yang sudah di backup/dump
  5. Untuk editor, bagi Anda yang buta warna cukup pake notepad :D , saya sendiri menggunakan notepad++ atau kalo pengen lebih nyaman bisa menggunakan eclipse kemudian download plug-in NSIS

Memang kalo dilihat dari segi skrip, Inno Setup lebih simple dibandingkan NSIS tetapi setelah saya bandingkan hasil paket instalasi NSIS lebih kecil dibandingkan Inno Setup.

Skrip Inno Setup dan NSIS sama-sama dikelompokkan dalam section-section bedanya kalo Inno Setup untuk nama sectionnya sudah fix (misal Setup, Tasks, Files de el el) sedangkan NSIS untuk nama sectionnya bebas dan tentunya dalam penamaan section di NSIS disesuaikan dengan isinya (misal Install MySQL 5).

Salah satu bagian penting dari skrip NSIS yang kita bahas disini adalah skrip yang digunakan untuk membuat service MySQL, mendaftarkan driver MySQL Connector ODBC dan melakukan proses undump skrip databse (.sql).

Saya ambil contoh untuk membuat service MySQL tentunya dilihat dari versi windows yang terinstall kalo keluarga Win9x jelas tidak bisa karena belum mendukung pembuatan service, oleh karena itu sebelum menjalankan perintah membuat service akan dilakukan pengecekan versi windows terlebih dulu.

Nah disini akan terlihat jelas perbedaan skrip Inno Setup dan NSIS dimana untuk melakukan ini inno setup cukup menambahkan flag MinVersion, misal :

1[Run]
2;mysqld --install MySQL
3Filename: "{app}\mysql\bin\mysqld.exe"; Parameters: "install ""MySQL"""; StatusMsg: "Sedang menginstall service MySQL ..."; Flags: runhidden; MinVersion: 0,5.01.2600sp2; Tasks: installmysql

Sedangkan untuk NSIS kita perlu mendownload Version plug-in terlebih dulu dan mengekstraknya kemudiakan mengcopykan file version.dll ke folder instalasi NSIS\Plugins, walaupun sebenarnya kita bisa membuat fungsi sendiri di NSIS untuk mengecek versi windows yang terinstall.

Contoh skrip NSIS untuk mengecek versi windows dan menginstall service MySQL :

01Section "Install MySQL 5"
02 Version::IsWindowsXP
03
04 Pop $0 ;nilai var $0 akan bersisi 1 jika windows XP selain itu 0
05 StrCmp $0 "1" ItIsWindowsXP ItIsNotWindowsXP ;StrCmp sama seperti fungsi IIF di VB
06
07 ItIsWindowsXP:
08 Goto installservice
09
10 ItIsNotWindowsXP:
11 Goto done
12
13 installservice:
14 DetailPrint "Sedang menginstall service MySQL ..."
15 ExecWait '"$MYSQL_DIR\bin\mysqld.exe" install "MySQL"'
16
17 done:
18 ;do nothing
19SectionEnd

Gimana lumayan beda kan? :)

Khusus untuk file-file Runtime VB memerlukan penanganan khusus dan untuk memudahkan Anda mencoba sample skrip ini download terlebih dahulu file Runtime VB disini dan jangan lupa diekstrak.

Persiapan terakhir untuk struktur folder saya buat seperti berikut :

Dan saya tidak akan menjelaskan fungsi-fungsi/perintah yang digunakan dalam skrip NSIS karena sudah saya sisipkan komentar di sample skrip instalasi NSIS.

Jika masih kesulitan Anda bisa langsung membaca manual NSIS yang penjelasannya sudah sangat lengkap.

Berikut contoh skrip instalasi lengkap menggunakan NSIS :

001;Skrip instalasi by k4m4r82
002;http://coding4ever.wordpress.com
003
004!include VB6RunTime.nsh
005
006;deklarasi konstanta
007;format pemanggilan ${NAMA_KONSTANTA}
008!define APP_NAME "Sistem Pembelian Bahan Baku PT. ALBASI"
009!define APP_PUBLISHER "K4m4r82's Laboratory"
010!define APP_VERSION "2.0.50"
011
012BrandingText /TRIMCENTER "-- ${APP_PUBLISHER} --"
013Name "${APP_NAME} ${APP_VERSION}"
014Caption "${APP_NAME} ${APP_VERSION}"
015
016CompletedText "Instalasi sudah selesai"
017
018Icon "setup.ico"
019LoadLanguageFile "${NSISDIR}\Contrib\Language files\Indonesian.nlf"
020OutFile "output\DemoSetupNSIS.exe"
021ShowInstDetails show
022ShowUninstDetails show
023WindowIcon on
024XPStyle on
025
026;informasi default folder instalasi
027InstallDir "$PROGRAMFILES\SPBB"
028
029;informasi folder instalasi disimpan disini
030;informasi ini akan memudahkan kita untuk membuat program update
031InstallDirRegKey HKCU "Software\PT ALBASI\SPBB" "InstallDir"
032
033VIAddVersionKey /LANG=${LANG_INDONESIAN} "ProductName" "${APP_NAME}"
034VIAddVersionKey /LANG=${LANG_INDONESIAN} "Comments" ""
035VIAddVersionKey /LANG=${LANG_INDONESIAN} "CompanyName" "${APP_PUBLISHER}"
036VIAddVersionKey /LANG=${LANG_INDONESIAN} "LegalTrademarks" "${APP_NAME} is a trademark of ${APP_PUBLISHER}"
037VIAddVersionKey /LANG=${LANG_INDONESIAN} "LegalCopyright" "Copyright © 2009. ${APP_PUBLISHER}"
038VIAddVersionKey /LANG=${LANG_INDONESIAN} "FileDescription" "${APP_NAME}"
039VIAddVersionKey /LANG=${LANG_INDONESIAN} "FileVersion" "2.0.0.50"
040VIProductVersion "2.0.0.50"
041
042RequestExecutionLevel admin
043
044AddBrandingImage left 150|234
045Page custom BrandingImage
046Page components
047Page directory
048Page instfiles
049
050UninstPage custom un.BrandingImage
051UninstPage uninstConfirm
052UninstPage instfiles
053
054;deklarsi variabel
055;format pemanggilan $NAMA_VARIABEL ingat ada sedikit perbedaan dg konstanta
056Var MainDir
057Var MySQLDir
058Var AlreadyInstalled
059
060;section yang diawali karakter -, pilihannya tidak ditampilkan
061Section "-Inisialisasi Variabel"
062 StrCpy $MainDir $INSTDIR
063 StrCpy $MySQLDir $MainDir\mysql
064SectionEnd
065
066Section "-Visual Basic Runtime"
067 ;download file Visual Basic Runtime di: http://nsis.sourceforge.net/vb6runtime.zip
068 IfFileExists "$MainDir\Albasi.exe" 0 new_installation
069 StrCpy $AlreadyInstalled 1
070
071 new_installation:
072 !insertmacro VB6RunTimeInstall "dll&ocx\vb6runtime" $AlreadyInstalled
073SectionEnd
074
075Section "-My Application Runtime"
076 SetOutPath $MainDir
077 File "main\Albasi.exe.manifest"
078 File "main\Albasi.exe"
079
080 SetOutPath $SYSDIR
081
082 File "dll&ocx\LVbuttons.OCX"
083 RegDLL "$SYSDIR\LVbuttons.ocx"
084
085 File "dll&ocx\MSMASK32.OCX"
086 RegDLL "$SYSDIR\MSMASK32.ocx"
087
088 File "dll&ocx\cTreeOpt6.ocx"
089 RegDLL "$SYSDIR\cTreeOpt6.ocx"
090
091 File "dll&ocx\Comdlg32.ocx"
092 RegDLL "$SYSDIR\Comdlg32.ocx"
093
094 File "dll&ocx\vbalDTab6.ocx"
095 RegDLL "$SYSDIR\vbalDTab6.ocx"
096
097 File "dll&ocx\vbalExpBar6.ocx"
098 RegDLL "$SYSDIR\vbalExpBar6.ocx"
099
100 File "dll&ocx\MSCOMCTL.OCX"
101 RegDLL "$SYSDIR\MSCOMCTL.ocx"
102
103 File "dll&ocx\vbalIml6.ocx"
104 RegDLL "$SYSDIR\vbalIml6.ocx"
105
106 File "dll&ocx\cPopMenu6.ocx"
107 RegDLL "$SYSDIR\cPopMenu6.ocx"
108
109 File "dll&ocx\cNewMenu6.dll"
110 RegDLL "$SYSDIR\cNewMenu6.DLL"
111
112 File "dll&ocx\scrrun.dll"
113 RegDLL "$SYSDIR\scrrun.DLL"
114
115 File "dll&ocx\vbalMDITabs6.dll"
116 RegDLL "$SYSDIR\vbalMDITabs6.DLL"
117
118 File "dll&ocx\SSubTmr6.dll"
119 RegDLL "$SYSDIR\SSubTmr6.DLL"
120
121 File "dll&ocx\msado21.tlb"
122 RegDLL "$SYSDIR\msado21.tlb"
123SectionEnd
124
125; param /e -> expand
126SectionGroup /e "Komponen Server"
127 Section "Install MySQL 5"
128 ; param /r -> recursive
129 SetOutPath $MySQLDir\bin
130 File /r "mysql-5.1.36-win32\bin\*.*"
131
132 SetOutPath $MySQLDir\Docs
133 File /r "mysql-5.1.36-win32\Docs\*.*"
134
135 SetOutPath $MySQLDir\lib
136 File /r "mysql-5.1.36-win32\lib\*.*"
137
138 SetOutPath $MySQLDir\share
139 File /r "mysql-5.1.36-win32\share\*.*"
140
141 SetOutPath $MySQLDir\data
142 File /r "mysql-5.1.36-win32\data\*.*"
143
144 SetOutPath $MySQLDir
145 File "mysql-5.1.36-win32\*.*"
146
147 ;informasi lokasi instalasi mysql
148 WriteINIStr $MySQLDir\my.ini "mysqld" "basedir" $MySQLDir
149 WriteINIStr $MySQLDir\my.ini "mysqld" "datadir" $MySQLDir\data
150
151 ;proses membuat dan menjalankan service mysql, cek versi windows terlebih dulu
152 ;untuk contoh disini baru di tes untuk windows xp sp2
153 Version::IsWindowsXP
154
155 Pop $0 ;nilai var $0 akan bersisi 1 jika windows XP selain itu 0
156 StrCmp $0 "1" ItIsWindowsXP ItIsNotWindowsXP ;StrCmp sama seperti fungsi IIF di VB
157
158 ItIsWindowsXP:
159 Goto installservice
160
161 ItIsNotWindowsXP:
162 Goto done
163
164 installservice:
165 DetailPrint "Sedang menginstall service MySQL ..."
166 ExecWait '"$MySQLDir\bin\mysqld.exe" install "MySQL"'
167
168 ;jalankan service MySQL
169 DetailPrint "Sedang menjalankan service MySQL ..."
170 ExecWait '"$SYSDIR\net.exe" start "MySQL"'
171
172 ;mendaftarkan port default mysql (3306) ke firewall
173 DetailPrint "Sedang mendaftarkan port MySQL ..."
174 ExecWait '"$SYSDIR\netsh.exe" firewall add portopening TCP 3306 "Port MySQL"'
175
176 ;mengganti password default root (blank). ex : masterkey
177 DetailPrint "Mengganti password root"
178 ExecWait '"$MySQLDir\bin\mysqladmin.exe" -uroot password masterkey'
179
180 ;menghapus user default1 (user=blank, password=blank)
181 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "DELETE FROM mysql.user WHERE Host=$\'localhost$\' AND User=$\'$\'"'
182 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "FLUSH PRIVILEGES"'
183
184 ;menghapus user default2 (user=root, password=blank)
185 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "DELETE FROM mysql.user WHERE Host=$\'127.0.0.1$\' AND User=$\'root$\'"'
186 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "FLUSH PRIVILEGES"'
187
188 ;set agar user root bisa login dari mesin lain (kalo diperlukan)
189 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "GRANT ALL PRIVILEGES ON *.* TO root@$\'%$\' IDENTIFIED BY $\'masterkey$\'"'
190 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "FLUSH PRIVILEGES"'
191
192 done:
193 ;do nothing
194 SectionEnd
195
196 Section "Install MySQL Connector ODBC"
197 SetOutPath $SYSDIR
198
199 ;dll mysql odbc tidak perlu diregistrasikan
200 ;jadi otomatis tidak perlu memanggil fungsi RegDLL
201 File "C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5.dll"
202 File "C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5S.dll"
203 File "C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5.lib"
204 File "C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5S.lib"
205 File "C:\Program Files\MySQL\Connector ODBC 5.1\myodbc-installer.exe"
206
207 Version::IsWindowsXP
208
209 Pop $0
210 StrCmp $0 "1" ItIsWindowsXP ItIsNotWindowsXP
211
212 ItIsWindowsXP:
213 Goto installdriverodbc
214
215 ItIsNotWindowsXP:
216 Goto done
217
218 installdriverodbc:
219 ;install driver myodbc
220 DetailPrint "Tunggu sedang mendaftarkan driver MySQL Connector ODBC 5.1.5"
221 ExecWait '"$SYSDIR\myodbc-installer.exe" -d -a -n "MySQL ODBC 5.1 Driver" -t "DRIVER=myodbc5.dll;SETUP=myodbc5S.dll"'
222
223 done:
224 ;do nothing
225 SectionEnd
226
227 Section "Install Database"
228 SetOutPath $MySQLDir\bin
229
230 File "main\albasi.sql"
231 File "main\exec.cmd"
232
233 Version::IsWindowsXP
234
235 Pop $0
236 StrCmp $0 "1" ItIsWindowsXP ItIsNotWindowsXP
237
238 ItIsWindowsXP:
239 Goto installdatabase
240
241 ItIsNotWindowsXP:
242 Goto done
243
244 installdatabase:
245 ;membuat database kosong
246 ExecWait '"$MySQLDir\bin\mysql.exe" -uroot -pmasterkey -e "CREATE DATABASE albasi"'
247
248 ;menjalankan file batch exec.cmd untuk melakukan proses undump
249 ExecWait '"$MySQLDir\bin\exec.cmd"'
250
251 done:
252 ;do nothing
253 SectionEnd
254SectionGroupEnd
255
256SectionGroup /e "Buat Shortcut"
257 Section "Start Programs"
258 SectionIn RO ;RO -> Read Only
259
260 CreateDirectory "$SMPROGRAMS\PT. ALBASI"
261 CreateShortCut "$SMPROGRAMS\PT. ALBASI\${APP_NAME}.lnk" "$MainDir\Albasi.exe" "" "$MainDir\Albasi.exe" 0
262 SectionEnd
263
264 Section "Desktop"
265 ;CreateDirectory "$DESKTOP\SPBB"
266 CreateShortCut "$DESKTOP\${APP_NAME}.lnk" "$MainDir\Albasi.exe" "" "$MainDir\Albasi.exe" 0
267 SectionEnd
268
269 Section "Quick Launch"
270 ;CreateDirectory "$QUICKLAUNCH\SPBB"
271 CreateShortCut "$QUICKLAUNCH\${APP_NAME}.lnk" "$MainDir\Albasi.exe" "" "$MainDir\Albasi.exe" 0
272 SectionEnd
273SectionGroupEnd
274
275Section "-Registry Windows"
276 ;informasi uninstall
277 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB" "DisplayName" "${APP_NAME}"
278 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB" "UninstallString" '"$MainDir\uninstaller.exe"'
279 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB" "DisplayIcon" '"$MainDir\uninstaller.exe"'
280
281 WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB" "NoModify" 1
282 WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB" "NoRepair" 1
283
284 WriteUninstaller $MainDir\uninstaller.exe
285SectionEnd
286
287Section "-File Konfigurasi Program"
288 WriteINIStr $MainDir\infoprogram.ini "Sistem" "serverName" "127.0.0.1"
289 WriteINIStr $MainDir\infoprogram.ini "Sistem" "dbName" "albasi"
290SectionEnd
291
292Section "Uninstall"
293 ;stop service MySQL
294 DetailPrint "Menghentikan Service MySQL ..."
295 ExecWait '"$SYSDIR\net.exe" stop "MySQL"'
296
297 ;hapus service MySQL
298 DetailPrint "Sedang menghapus service MySQL ..."
299 ExecWait '"$MySQLDir\bin\mysqld.exe" remove "MySQL"'
300
301 ;driver MySQL Connector ODBC 5.1
302 DetailPrint "Tunggu sedang menghapus driver MySQL Connector ODBC 5.1"
303 ExecWait '"$SYSDIR\myodbc-installer.exe" -d -r -n "MySQL ODBC 5.1 Driver"'
304
305 ; Remove registry keys
306 DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB"
307
308 ; Remove files and uninstaller
309 Delete $MainDir\*.*
310 RMDir $MainDir
311
312 ; Remove shortcuts
313 Delete "$SMPROGRAMS\PT. ALBASI\*.*"
314 RMDir "$SMPROGRAMS\PT. ALBASI"
315
316 Delete "$DESKTOP\${APP_NAME}.lnk"
317 Delete "$QUICKLAUNCH\${APP_NAME}.lnk"
318SectionEnd
319
320;fungsi untuk menampilkan gambar/banner pada saat instalasi
321;untuk contoh disini posisi gambar di sebelah kiri
322Function BrandingImage
323 SetOutPath "$TEMP"
324
325 SetFileAttributes SetupModern21.bmp temporary
326 File SetupModern21.bmp
327 SetBrandingImage "$TEMP\SetupModern21.bmp" /resizetofit
328FunctionEnd
329
330;fungsi untuk menampilkan gambar/banner pada saat uninstall
331Function un.BrandingImage
332 SetBrandingImage "$TEMP\SetupModern21.bmp" /resizetofit
333FunctionEnd

Di dalam skrip instalasi ada file exec.cmd, isinya adalah :

1mysql -uroot -pmasterkey albasi < albasi.sql

Isi file exec.cmd sebenarnya untuk proses undump dan ternyata Inno Setup dan NSIS gagal menjalankan perintah tersebut, padahal perintah-perintah yang lainnya sukses.

Contoh hasil instalasi :

Gambar 1

Gambar 2

Gambar 3

Gambar 4

Selamat mencoba :)

sumber

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Posting Komentar