2017-09-01 78 views
1

我使用Inno Setup打包爲Windows的Java應用程序中刪除安裝文件;應用樹是這樣的:Inno Setup的:由以前的版本

| MyApp.jar 
\---lib 
    | dependency-A-1.2.3.jar 
    | dependency-B-2.3.4.jar 
    | dependency-Z-x.y.z.jar 

我用Ant準備整棵樹(所有文件和文件夾)事前,包括lib目錄(使用*.jar通配符複製的依賴關係),然後我簡單調用ISCC有:

[Files] 
Source: "PreparedFolder\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs 

現在,我需要清理lib目錄每次用戶升級應用因爲我想刪除y過時的依賴關係。我可以添加以下部分我.iss文件:

[InstallDelete] 
{app}\lib\*.jar 

,但我不覺得安全,因爲如果用戶決定安裝在現有的文件夾中包含不空lib子(罕見的,但不是應用程序不可能),在升級時有可能會刪除一些用戶文件。

是否有避免這種麻煩的一些最佳做法?其他安裝人員是否會照顧到這些問題?謝謝。

回答

1

您可以在安裝之前卸載以前的版本:


如果你不能做一個完整的卸載,你就必須實現部分卸載。

理想的做法是對卸載程序日誌(unins000.dat)進行逆向工程,只提取安裝到lib子文件夾並處理(撤消)它們。但由於這是一個未公開的二進制文件,因此很難做到。


如果您維護被安裝在[Files]部分文件的顯式列表,像

[Files] 
Source: "lib\dependency-A-1.2.3.jar"; Dest: "{app}\lib" 
Source: "lib\dependency-B-2.3.4.jar"; Dest: "{app}\lib" 

那麼無論什麼時候依賴的變化,將以前的版本到[InstallDelete]部分:

[Files] 
Source: "lib\dependency-A-1.3.0.jar"; Dest: "{app}" 
Source: "lib\dependency-B-2.3.4.jar"; Dest: "{app}" 

[InstallDelete] 
{app}\lib\dependency-A-1.2.3.jar 

如果您使用通配符安裝依賴,

[Files] 
Source: "lib\*.jar"; Dest: "{app}\lib" 

並且您無法對卸載程序日誌進行逆向工程,您將不得不通過自己的方式複製其功能。

可以使用preprocessor產生與安裝的依賴關係的文件。將該文件安裝到{app}文件夾並在安裝之前處理該文件。

[Files] 
Source: "MyApp.jar"; DestDir: "{app}" 
Source: "lib\*.jar"; DestDir: "{app}\lib" 

#define ProcessFile(Source, FindResult, FindHandle) \ 
    Local[0] = FindGetFileName(FindHandle), \ 
    Local[1] = Source + "\\" + Local[0], \ 
    Local[2] = FindNext(FindHandle), \ 
    "'" + Local[0] + "'#13#10" + \ 
     (Local[2] ? ProcessFile(Source, Local[2], FindHandle) : "") 

#define ProcessFolder(Source) \ 
    Local[0] = FindFirst(Source + "\\*.jar", faAnyFile), \ 
    ProcessFile(Source, Local[0], Local[0]) 

#define DepedenciesToInstall ProcessFolder("lib") 
#define DependenciesLog "{app}\dependencies.log" 

[UninstallDelete] 
Type: files; Name: "{#DependenciesLog}" 
[Code] 

procedure CurStepChanged(CurStep: TSetupStep); 
var 
    AppPath, DependenciesLogPath: string; 
    Dependencies: TArrayOfString; 
    Count, I: Integer; 
begin 
    DependenciesLogPath := ExpandConstant('{#DependenciesLog}'); 

    if CurStep = ssInstall then 
    begin 
    { If dependencies log already exists, remove the previously installed dependencies } 
    if LoadStringsFromFile(DependenciesLogPath, Dependencies) then 
    begin 
     Count := GetArrayLength(Dependencies); 
     Log(Format('Loaded %d dependencies, deleting...', [Count])); 
     for I := 0 to Count - 1 do 
     DeleteFile(ExpandConstant('{app}\lib\' + Dependencies[I])); 
    end; 
    end 
    else 
    if CurStep = ssPostInstall then 
    begin 
    { Now that the app folder already exists, } 
    { save dependencies log (to be processed by future upgrade) } 
    if SaveStringToFile(DependenciesLogPath, {#DepedenciesToInstall}, False) then 
    begin 
     Log('Created dependencies log'); 
    end 
     else 
    begin 
     Log('Failed to create dependencies log'); 
    end; 
    end; 
end; 
+0

謝謝你很多。它工作正常,除了第一次安裝,因爲在安裝之前{app} \ lib'不存在,所以我試圖解決這個問題。 –

+1

是的,我已經修復了我的答案。 –