OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/applistation/AppliStation
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages.Install / Uninstallation.cs
1 using System;\r
2 using System.Diagnostics;\r
3 using System.Text.RegularExpressions;\r
4 using System.Collections;\r
5 using System.CodeDom.Compiler;\r
6 using System.IO;\r
7 \r
8 namespace NaGet.Packages.Install\r
9 {\r
10         /// <summary>\r
11         /// Description of Uninstallation.\r
12         /// </summary>\r
13         public class Uninstallation\r
14         {\r
15                 /// <summary>\r
16                 /// アンインストールするパッケージ\r
17                 /// </summary>\r
18                 public InstalledPackage UninstalledPackage;\r
19                 \r
20                 /// <summary>\r
21                 /// 外部アプリのエラー出力の受信ハンドラ\r
22                 /// </summary>\r
23                 public event EventHandler<NaGet.Utils.AnyDataEventArgs<string>> ErrorDataReceived;\r
24                 \r
25                 /// <summary>\r
26                 /// 外部アプリの標準出力の受信ハンドラ\r
27                 /// </summary>\r
28                 public event EventHandler<NaGet.Utils.AnyDataEventArgs<string>> OutputDataReceived;\r
29                 \r
30                 /// <summary>\r
31                 /// サイレントアンインストールするか否か\r
32                 /// </summary>\r
33                 public bool Silent = false;\r
34                 \r
35                 /// <summary>\r
36                 /// コンストラクタ\r
37                 /// </summary>\r
38                 /// <param name="package">アンインストールするパッケージ</param>\r
39                 public Uninstallation(InstalledPackage package)\r
40                 {\r
41                         UninstalledPackage = package;\r
42                 }\r
43                 \r
44                 /// <summary>\r
45                 /// インストールされた状態か否か\r
46                 /// </summary>\r
47                 public bool Installed\r
48                 {\r
49                         get {\r
50                                 if ((UninstalledPackage.Type == InstallerType.ARCHIVE)\r
51                                     || UninstalledPackage.Type == InstallerType.ITSELF) {\r
52                                         return Directory.Exists(UninstalledPackage.UninstallInfo.InstallLocation);\r
53                                 } else {\r
54                                         foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {\r
55                                                 if (! string.IsNullOrEmpty(UninstalledPackage.UninstallerKey)) {\r
56                                                         Match match = Regex.Match(info.DisplayName, UninstalledPackage.UninstallerKey);\r
57                                                 \r
58                                                         if (match.Success) {\r
59                                                                 return true;\r
60                                                         }\r
61                                                 }\r
62                                         }\r
63                                 }\r
64                                 return false;\r
65                         }\r
66                 }\r
67                 \r
68                 /// <summary>\r
69                 /// アンインストーラ等を起動してアンインストール作業を行う\r
70                 /// </summary>\r
71                 /// <returns>アンインストーラの終了コード</returns>\r
72                 public int Uninstall()\r
73                 {\r
74                         if (! Installed) {\r
75                                 throw new ApplicationException("Program not found, may be already uninstalled");\r
76                         }\r
77                         \r
78                         int exitValue = 0;\r
79                         string uninstallString = Silent? UninstalledPackage.UninstallInfo.QuietUninstallString : UninstalledPackage.UninstallInfo.UninstallString;\r
80                         if (string.IsNullOrEmpty(uninstallString)) {\r
81                                 throw new ApplicationException(string.Format("Could not found {0}install script", Silent? "silent " : ""));\r
82                         }\r
83                         \r
84                         if (UninstalledPackage.UninstallInfo.WindowsInstaller &&\r
85                             Regex.Match(uninstallString.Substring("MsiExec.exe /I".Length),\r
86                                         @"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$").Success) {\r
87                                 string guid = uninstallString.Substring("MsiExec.exe /I".Length);\r
88                                 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture(\r
89                                         new ProcessStartInfo("msiexec", string.Format("/X{0}", guid)),\r
90                                         NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived),\r
91                                         NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) {\r
92                                         \r
93                                         if (NaGet.Env.InstallProcessOnBackground) {\r
94                                                 try {\r
95                                                         hProcess.PriorityClass = ProcessPriorityClass.Idle;\r
96                                                 } catch (Exception) {}\r
97                                         }\r
98                                         \r
99                                         hProcess.WaitForExit();\r
100                                         \r
101                                         exitValue = hProcess.ExitCode;\r
102                                 }\r
103                         } else if (File.Exists(uninstallString)) {\r
104                                 // 単独のファイルの場合\r
105                                 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture(\r
106                                         new ProcessStartInfo(uninstallString),\r
107                                         NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived),\r
108                                         NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) {\r
109                                         \r
110                                         if (NaGet.Env.InstallProcessOnBackground) {\r
111                                                 try {\r
112                                                         hProcess.PriorityClass = ProcessPriorityClass.Idle;\r
113                                                 } catch (Exception) {}\r
114                                         }\r
115                                         \r
116                                         hProcess.WaitForExit();\r
117                                         \r
118                                         exitValue = hProcess.ExitCode;\r
119                                 }\r
120                         } else {\r
121                                 ProcessStartInfo procInfo = new ProcessStartInfo(null, uninstallString);\r
122                                 procInfo.UseShellExecute = false;\r
123                                 procInfo.CreateNoWindow = true;\r
124                                 using (NaGet.InteropServices.CreateProcessCaller p = new NaGet.InteropServices.CreateProcessCaller(procInfo)) {\r
125                                         \r
126                                         if (NaGet.Env.InstallProcessOnBackground) {\r
127                                                 try {\r
128                                                         p.PriorityClass = ProcessPriorityClass.Idle;\r
129                                                 } catch (Exception) {}\r
130                                         }\r
131                                         \r
132                                         p.WaitForExit();\r
133                                         \r
134                                         exitValue = p.ExitCode;\r
135                                 }\r
136                         }\r
137                         \r
138                         return exitValue;\r
139                 }\r
140                 \r
141                 public override string ToString()\r
142                 {\r
143                         return string.Format("{0}({1})", UninstalledPackage.Name, UninstalledPackage.Version);\r
144                 }\r
145         }\r
146 }\r