using System;
using NaGet.Packages.Install;
using NaGet.Packages;
using NaGet.Net;
namespace NaGet.SubCommands
{
public class NaGetUninstall : NaGetTaskSet
{
private bool done = false;
private int currentTaskSetIndex = -1;
private PackageListsManager pkgListMan;
///
/// アンインストールするパッケージ
///
public Uninstallation[] Uninstallations;
///
/// コンストラクタ
///
/// アンインストールするパッケージ
public NaGetUninstall(PackageListsManager pkgMan, InstalledPackage[] pkgs)
{
pkgListMan = pkgMan;
Uninstallations = new Uninstallation[pkgs.Length];
for (int i = 0; i < pkgs.Length; i++) {
Uninstallations[i] = new Uninstallation(pkgs[i]);
}
initializeMainTaskSetNames();
}
///
/// コンストラクタ
///
/// アンインストール処理の配列
public NaGetUninstall(PackageListsManager pkgMan, Uninstallation[] uninstallations)
{
pkgListMan = pkgMan;
Uninstallations = uninstallations;
initializeMainTaskSetNames();
}
private void initializeMainTaskSetNames()
{
System.Collections.Generic.List taskSetNames = new System.Collections.Generic.List();
for (int i =0; i < Uninstallations.Length; i++) {
taskSetNames.Add(string.Format("アンインストール: {0}", Uninstallations[i].ToString()));
}
taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.ArchiveInstalledPackageListFile));
taskSetNames.Add(string.Format("リスト更新: {0}", NaGet.Env.SystemInstalledPackageListFile));
TaskSetNames = taskSetNames.ToArray();
}
public override void Run()
{
currentTaskSetIndex ++;
RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "アンインストール処理開始");
foreach (Uninstallation uninst in Uninstallations) {
RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, uninst.ToString());
if (uninst.Installed) {
try {
uninst.OutputDataReceived += this.ReceivedOutputData;
uninst.ErrorDataReceived += this.ReceivedErrorData;
int exitCode = uninst.Uninstall();
if (exitCode != 0) {
RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "アンインストールが正常に終えていない可能性があります。アンインストーラの終了コード:"+exitCode);
}
} catch (Exception e) {
RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);
done = true;
return;
}
} else {
RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, string.Format("{0}は既にアンインストールされているか、ソフトの存在を確認できませんでした", uninst));
}
RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, uninst.ToString());
}
runLocalUpdate();
done = true;
RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "終了", 100);
}
private void runLocalUpdate()
{
// インストールトリストの更新
RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);
pkgListMan.DetectInstalledPkgs();
pkgListMan.SaveInstalledPackageList();
currentTaskSetIndex++;
RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);
// システムにインストールされているリストの更新
RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);
pkgListMan.DetectSystemInstalledPkgs();
pkgListMan.SaveSystemInstalledPackageList();
currentTaskSetIndex++;
RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);
}
public override bool Done {
get { return done; }
}
public override int CurrentTaskSetIndex {
get { return currentTaskSetIndex; }
}
}
}