Coding4ever’s Blog

Just coding… coding… and coding… because coding should be fun :)

Membuat Paket Instalasi Vb + Mysql Dengan NSIS

| Comments

Melengkapi tulisan saya Membuat paket instalasi vb + mysql dengan inno setup kali ini kita akan membuatnya menggunakan 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
2
3
[Run]
;mysqld –install MySQL
Filename: “{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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Section “Install MySQL 5”
  Version::IsWindowsXP

  Pop $0 ;nilai var $0 akan bersisi 1 jika windows XP selain itu 0
  StrCmp $0 “1” ItIsWindowsXP ItIsNotWindowsXP ;StrCmp sama seperti fungsi IIF di VB

  ItIsWindowsXP:
  Goto installservice

  ItIsNotWindowsXP:
  Goto done

  installservice:
      DetailPrint “Sedang menginstall service MySQL …”
      ExecWait ‘“$MYSQL_DIR\bin\mysqld.exe” install “MySQL”’

  done:
      ;do nothing
SectionEnd

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 :

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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
;Skrip instalasi by k4m4r82
;http://coding4ever.wordpress.com

!include VB6RunTime.nsh

;deklarasi konstanta
;format pemanggilan ${NAMA_KONSTANTA}
!define APP_NAME “Sistem Pembelian Bahan Baku PT. ALBASI”
!define APP_PUBLISHER “K4m4r82’s Laboratory”
!define APP_VERSION “2.0.50”

BrandingText /TRIMCENTER “– ${APP_PUBLISHER} –”
Name “${APP_NAME} ${APP_VERSION}”
Caption “${APP_NAME} ${APP_VERSION}”

CompletedText “Instalasi sudah selesai”

Icon “setup.ico”
LoadLanguageFile “${NSISDIR}\Contrib\Language files\Indonesian.nlf”
OutFile “output\DemoSetupNSIS.exe”
ShowInstDetails show
ShowUninstDetails show
WindowIcon on
XPStyle on

;informasi default folder instalasi
InstallDir “$PROGRAMFILES\SPBB”

;informasi folder instalasi disimpan disini
;informasi ini akan memudahkan kita untuk membuat program update
InstallDirRegKey HKCU “Software\PT ALBASI\SPBB” “InstallDir”

VIAddVersionKey /LANG=${LANG_INDONESIAN} “ProductName” “${APP_NAME}”
VIAddVersionKey /LANG=${LANG_INDONESIAN} “Comments” “”
VIAddVersionKey /LANG=${LANG_INDONESIAN} “CompanyName” “${APP_PUBLISHER}”
VIAddVersionKey /LANG=${LANG_INDONESIAN} “LegalTrademarks” “${APP_NAME} is a trademark of ${APP_PUBLISHER}”
VIAddVersionKey /LANG=${LANG_INDONESIAN} “LegalCopyright” “Copyright © 2009. ${APP_PUBLISHER}”
VIAddVersionKey /LANG=${LANG_INDONESIAN} “FileDescription” “${APP_NAME}”
VIAddVersionKey /LANG=${LANG_INDONESIAN} “FileVersion” “2.0.0.50”
VIProductVersion “2.0.0.50”

RequestExecutionLevel admin

AddBrandingImage left 150|234
Page custom BrandingImage
Page components
Page directory
Page instfiles

UninstPage custom un.BrandingImage
UninstPage uninstConfirm
UninstPage instfiles

;deklarsi variabel
;format pemanggilan $NAMA_VARIABEL ingat ada sedikit perbedaan dg konstanta
Var MainDir
Var MySQLDir
Var AlreadyInstalled

;section yang diawali karakter -, pilihannya tidak ditampilkan
Section “-Inisialisasi Variabel”
    StrCpy $MainDir $INSTDIR
  StrCpy $MySQLDir $MainDir\mysql
SectionEnd

Section “-Visual Basic Runtime”
  ;download file Visual Basic Runtime di: http://nsis.sourceforge.net/vb6runtime.zip
  IfFileExists “$MainDir\Albasi.exe” 0 new_installation
      StrCpy $AlreadyInstalled 1

  new_installation:
      !insertmacro VB6RunTimeInstall “dll&ocx\vb6runtime” $AlreadyInstalled
SectionEnd

Section “-My Application Runtime”
  SetOutPath $MainDir
        File “main\Albasi.exe.manifest”
  File “main\Albasi.exe”

  SetOutPath $SYSDIR

  File “dll&ocx\LVbuttons.OCX”
  RegDLL “$SYSDIR\LVbuttons.ocx”

  File “dll&ocx\MSMASK32.OCX”
  RegDLL “$SYSDIR\MSMASK32.ocx”

  File “dll&ocx\cTreeOpt6.ocx”
  RegDLL “$SYSDIR\cTreeOpt6.ocx”

  File “dll&ocx\Comdlg32.ocx”
  RegDLL “$SYSDIR\Comdlg32.ocx”

  File “dll&ocx\vbalDTab6.ocx”
  RegDLL “$SYSDIR\vbalDTab6.ocx”

  File “dll&ocx\vbalExpBar6.ocx”
  RegDLL “$SYSDIR\vbalExpBar6.ocx”

  File “dll&ocx\MSCOMCTL.OCX”
  RegDLL “$SYSDIR\MSCOMCTL.ocx”

  File “dll&ocx\vbalIml6.ocx”
  RegDLL “$SYSDIR\vbalIml6.ocx”

  File “dll&ocx\cPopMenu6.ocx”
  RegDLL “$SYSDIR\cPopMenu6.ocx”

  File “dll&ocx\cNewMenu6.dll”
  RegDLL “$SYSDIR\cNewMenu6.DLL”

  File “dll&ocx\scrrun.dll”
  RegDLL “$SYSDIR\scrrun.DLL”

  File “dll&ocx\vbalMDITabs6.dll”
  RegDLL “$SYSDIR\vbalMDITabs6.DLL”

  File “dll&ocx\SSubTmr6.dll”
  RegDLL “$SYSDIR\SSubTmr6.DLL”

  File “dll&ocx\msado21.tlb”
  RegDLL “$SYSDIR\msado21.tlb”
SectionEnd

; param /e -> expand
SectionGroup /e “Komponen Server”
  Section “Install MySQL 5”
      ; param /r -> recursive
      SetOutPath $MySQLDir\bin
      File /r “mysql-5.1.36-win32\bin*.
      SetOutPath $MySQLDir\Docs
      File /r “mysql-5.1.36-win32\Docs*.
      SetOutPath $MySQLDir\lib
      File /r “mysql-5.1.36-win32\lib*.
      SetOutPath $MySQLDir\share
      File /r “mysql-5.1.36-win32\share*.
      SetOutPath $MySQLDir\data
      File /r “mysql-5.1.36-win32\data*.
      SetOutPath $MySQLDir
      File “mysql-5.1.36-win32*.
      ;informasi lokasi instalasi mysql
      WriteINIStr $MySQLDir\my.ini “mysqld” “basedir” $MySQLDir
      WriteINIStr $MySQLDir\my.ini “mysqld” “datadir” $MySQLDir\data

      ;proses membuat dan menjalankan service mysql, cek versi windows terlebih dulu
      ;untuk contoh disini baru di tes untuk windows xp sp2
      Version::IsWindowsXP

      Pop $0 ;nilai var $0 akan bersisi 1 jika windows XP selain itu 0
      StrCmp $0 “1” ItIsWindowsXP ItIsNotWindowsXP ;StrCmp sama seperti fungsi IIF di VB

      ItIsWindowsXP:
      Goto installservice

      ItIsNotWindowsXP:
      Goto done

      installservice:
          DetailPrint “Sedang menginstall service MySQL …”
          ExecWait ‘“$MySQLDir\bin\mysqld.exe” install “MySQL”’

          ;jalankan service MySQL
          DetailPrint “Sedang menjalankan service MySQL …”
          ExecWait ‘“$SYSDIR\net.exe” start “MySQL”’

          ;mendaftarkan port default mysql (3306) ke firewall
          DetailPrint “Sedang mendaftarkan port MySQL …”
          ExecWait ‘“$SYSDIR\netsh.exe” firewall add portopening TCP 3306 “Port MySQL”’

          ;mengganti password default root (blank). ex : masterkey
          DetailPrint “Mengganti password root”
          ExecWait ‘“$MySQLDir\bin\mysqladmin.exe” -uroot password masterkey’

          ;menghapus user default1 (user=blank, password=blank)
          ExecWait ‘“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “DELETE FROM mysql.user WHERE Host=$\'localhost$\’ AND User=$\‘$\’”‘
          ExecWait ’“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “FLUSH PRIVILEGES”‘

          ;menghapus user default2 (user=root, password=blank)
          ExecWait ’“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “DELETE FROM mysql.user WHERE Host=$\‘127.0.0.1$\’ AND User=$\‘root$\’”‘
          ExecWait ’“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “FLUSH PRIVILEGES”‘

          ;set agar user root bisa login dari mesin lain (kalo diperlukan)
          ExecWait ’“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “GRANT ALL PRIVILEGES ON . TO root@$\‘%$\’ IDENTIFIED BY $\‘masterkey$\’”‘
          ExecWait ’“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “FLUSH PRIVILEGES”‘

      done:
          ;do nothing
    SectionEnd

  Section “Install MySQL Connector ODBC”
      SetOutPath $SYSDIR

      ;dll mysql odbc tidak perlu diregistrasikan
      ;jadi otomatis tidak perlu memanggil fungsi RegDLL
      File “C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5.dll”
      File “C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5S.dll”
      File “C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5.lib”
      File “C:\Program Files\MySQL\Connector ODBC 5.1\myodbc5S.lib”
      File “C:\Program Files\MySQL\Connector ODBC 5.1\myodbc-installer.exe”

      Version::IsWindowsXP

      Pop $0
      StrCmp $0 “1” ItIsWindowsXP ItIsNotWindowsXP

      ItIsWindowsXP:
      Goto installdriverodbc

      ItIsNotWindowsXP:
      Goto done

      installdriverodbc:
          ;install driver myodbc
          DetailPrint “Tunggu sedang mendaftarkan driver MySQL Connector ODBC 5.1.5”
          ExecWait ’“$SYSDIR\myodbc-installer.exe” -d -a -n “MySQL ODBC 5.1 Driver” -t “DRIVER=myodbc5.dll;SETUP=myodbc5S.dll”‘

      done:
          ;do nothing
  SectionEnd

  Section “Install Database”
      SetOutPath $MySQLDir\bin

      File “main\albasi.sql”
      File “main\exec.cmd”

      Version::IsWindowsXP

      Pop $0
      StrCmp $0 “1” ItIsWindowsXP ItIsNotWindowsXP

      ItIsWindowsXP:
      Goto installdatabase

      ItIsNotWindowsXP:
      Goto done

      installdatabase:
          ;membuat database kosong
          ExecWait ’“$MySQLDir\bin\mysql.exe” -uroot -pmasterkey -e “CREATE DATABASE albasi”‘

          ;menjalankan file batch exec.cmd untuk melakukan proses undump
          ExecWait ’“$MySQLDir\bin\exec.cmd”‘

      done:
          ;do nothing
  SectionEnd
SectionGroupEnd

SectionGroup /e “Buat Shortcut”
  Section “Start Programs”
      SectionIn RO ;RO -> Read Only

      CreateDirectory “$SMPROGRAMS\PT. ALBASI”
      CreateShortCut “$SMPROGRAMS\PT. ALBASI\${APP_NAME}.lnk” “$MainDir\Albasi.exe” “” “$MainDir\Albasi.exe” 0
  SectionEnd

    Section “Desktop”
      ;CreateDirectory “$DESKTOP\SPBB”
      CreateShortCut “$DESKTOP\${APP_NAME}.lnk” “$MainDir\Albasi.exe” “” “$MainDir\Albasi.exe” 0
  SectionEnd

  Section “Quick Launch”
      ;CreateDirectory “$QUICKLAUNCH\SPBB”
      CreateShortCut “$QUICKLAUNCH\${APP_NAME}.lnk” “$MainDir\Albasi.exe” “” “$MainDir\Albasi.exe” 0
  SectionEnd
SectionGroupEnd

Section “-Registry Windows”
  ;informasi uninstall
  WriteRegStr HKLM “Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB” “DisplayName” “${APP_NAME}”
    WriteRegStr HKLM “Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB” “UninstallString” ’“$MainDir\uninstaller.exe”‘
  WriteRegStr HKLM “Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB” “DisplayIcon” ’“$MainDir\uninstaller.exe”‘

    WriteRegDWORD HKLM “Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB” “NoModify” 1
    WriteRegDWORD HKLM “Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB” “NoRepair” 1

    WriteUninstaller $MainDir\uninstaller.exe
SectionEnd

Section “-File Konfigurasi Program”
    WriteINIStr $MainDir\infoprogram.ini “Sistem” “serverName” “127.0.0.1”
    WriteINIStr $MainDir\infoprogram.ini “Sistem” “dbName” “albasi”
SectionEnd

Section “Uninstall”
  ;stop service MySQL
  DetailPrint “Menghentikan Service MySQL …”
  ExecWait ’“$SYSDIR\net.exe” stop “MySQL”‘

  ;hapus service MySQL
  DetailPrint “Sedang menghapus service MySQL …”
  ExecWait ’“$MySQLDir\bin\mysqld.exe” remove “MySQL”‘

  ;driver MySQL Connector ODBC 5.1
  DetailPrint “Tunggu sedang menghapus driver MySQL Connector ODBC 5.1”
  ExecWait ’“$SYSDIR\myodbc-installer.exe” -d -r -n “MySQL ODBC 5.1 Driver”‘

  ; Remove registry keys
  DeleteRegKey HKLM “Software\Microsoft\Windows\CurrentVersion\Uninstall\SPBB”

  ; Remove files and uninstaller
  Delete $MainDir*.
  RMDir $MainDir

  ; Remove shortcuts
  Delete “$SMPROGRAMS\PT. ALBASI*.  RMDir “$SMPROGRAMS\PT. ALBASI”

  Delete “$DESKTOP\${APP_NAME}.lnk”
  Delete “$QUICKLAUNCH\${APP_NAME}.lnk”
SectionEnd

;fungsi untuk menampilkan gambar/banner pada saat instalasi
;untuk contoh disini posisi gambar di sebelah kiri
Function BrandingImage
  SetOutPath “$TEMP”

  SetFileAttributes SetupModern21.bmp temporary
  File SetupModern21.bmp
  SetBrandingImage “$TEMP\SetupModern21.bmp” /resizetofit
FunctionEnd

;fungsi untuk menampilkan gambar/banner pada saat uninstall
Function un.BrandingImage
  SetBrandingImage “$TEMP\SetupModern21.bmp” /resizetofit
FunctionEnd

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

1
mysql -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 :)

mysql, nsis, tools installer

Tentang Penulis

Software developer yang fokus mengembangkan aplikasi di atas platform .NET (Desktop, ASP.NET MVC, Web Service, Microservice) dan Android. Senang mempelajari teknologi baru terutama di bidang OOP, Design Pattern, ORM, Database, Continuous Integration & Deployment dan arsitektur Microservice.
Selain mengajar, saat ini penulis juga bekerja sebagai staf IT di salah satu PTS di Yogyakarta sebagai senior software developer. Di waktu luang insya Alloh akan terus berbagi pengalaman di blog ini :)

« Menonaktifkan tombol Minimize, Maximize dan Close Menambahkan tombol baru di Title Bar »

Comments