3. 버전 유지 프로그램 만들기
실행 파일에는 버전 정보를 넣을 수 있는데 그 정보와 서버에 올려진 정보를 비교해서 구버전일 경우 서버로부터 새로운 버전의 파일을 다운 받고 파일을 교체하는 프로그램이 필요하다. 하나의 프로그램으로 버전 유지 기능을 넣을 수도 있지만 간단하게 별개의 버전 유지 프로그램을 만들어보자.
버전 유지를 처리하기 전에 TImage를 이용해서 처리 중이라는 메세지를 보여주는 다이얼로그를 띄운다. TImage의 AutoSize를 True로 설정해서 이미지 파일이 바뀌더라도 자동으로 크기가 바뀌게 한다.
procedure TdlgVerChk.FormShow(Sender: TObject);
var
l_File: string;
begin
l_File := ExtractFilePath(ParamStr(0)) + 'download.jpg';
if FileExists(l_File) then
begin
imgLogo.Picture.LoadFromFile(l_File);
imgLogo.Refresh;
end;
Self.Width := imgLogo.Width;
Self.Height := imgLogo.Height;
end;
실행 파일 버전 확인 및 DB에서 버전 정보를 가져오기, 파일 다운 받기 등의 처리는 이미 앞의 DataModule에 다 구현해놨기때문에 DataModule을 프로젝트에 추가한다. 압축 해제를 위해서 TUnZipper 객체가 있는 zipper를 uses에 추가한다.
처리순서는 메세지 창을 띄워놓고 현재 설치되어있는 실행파일의 버전을 가져온 후, 서버에서 최신 정보를 받아서 비교한다. 만약 설치된 파일이 구버전이면 파일을 삭제한 후 최신 버전의 파일을 다운 받아 압축을 해제한다. 버전 확인이 끝났으면 실행파일을 실행하고 메세지 창을 닫는다.
program simlaz_verchk;
{$mode objfpc}{$H+}
uses
windows, Interfaces, sysutils, zipper, indylaz, simlaz_lib, simlaz_dm, Forms, verchk_dlg;
var
l_ZipFile: TUnZipper;
g_verID, g_exefile, g_zipfile, g_oldver, g_newver: string;
{$R *.res}
begin
RequireDerivedFormResource := True;
Application.Title := '프로그램 버전 확인';
Application.Initialize;
// 메세지 창 띄우기
dlgVerChk := TdlgVerChk.Create(Application);
dlgVerChk.Show;
dlgVerChk.Refresh;
// DataModule 생성
Application.CreateForm(TdmSIMLaz, dmSIMLaz);
g_verID := 'TESTPRG'; // 프로그램ID
g_exefile := ExtractFilePath(ParamStr(0)) + 'simlaz_test.exe'; // 실행파일명
g_zipfile := ExtractFilePath(ParamStr(0)) + 'new.zip'; // 다운받을 zip 파일명
// 현재 실행파일의 버전 가져오기
if not FileExists(g_exefile) then g_oldver := '0.0'
else g_oldver := GF_SIMLazFVer(g_exefile);
// 서버에서 버전 정보 가져오기
g_newver := dmSIMLaz.UF_SIMLazVer(g_verID);
// 구버전일 경우
if g_newver <> g_oldver then
begin
// 최신 버전의 실행파일이 포함된 zip 파일 가져오기
dmSIMLaz.UP_SIMLazFile(g_verID, g_zipfile);
if FileExists(g_zipfile) then
begin
// 구버전인 현재 실행파일 삭제
if FileExists(g_exefile) then DeleteFile(g_exefile);
// 다운 받은 zip 파일 압축해제
l_ZipFile := TUnZipper.Create;
try
l_ZipFile.FileName := g_zipfile;
l_ZipFile.OutputPath:= ExtractFilePath(ParamStr(0));
l_ZipFile.Examine;
l_ZipFile.UnZipAllFiles;
finally
FreeAndNil(l_ZipFile);
end;
end;
end;
// 실행파일 실행
ShellExecute(0, nil, PChar(g_exefile), nil, nil, SW_SHOWNORMAL);
// DataModule 제거 및 메세지창 닫기
dmSIMLaz.Free;
dlgVerChk.Close;
// 프로그램 종료
Application.Terminate;
end.
동일한 미들웨어를 사용하는 경우 프로그램ID와 실행파일명만 바꾸면 얼마든지 다른 프로그램에 사용할 수 있다.