Skip to content

Commit

Permalink
Merge pull request #110 from ConchbrainClub/main
Browse files Browse the repository at this point in the history
deploy to prod site
  • Loading branch information
lixinyang123 authored Dec 24, 2023
2 parents ffefeeb + 1b036df commit 82007db
Show file tree
Hide file tree
Showing 21 changed files with 299 additions and 604 deletions.
16 changes: 16 additions & 0 deletions MixApp.Client/Commands/InitCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.CommandLine;

namespace MixApp.Client.Commands;

internal class InitCommand : Command
{
public InitCommand() : base("init", "Initialize")
{
this.SetHandler(Execute);
}

private void Execute()
{

}
}
67 changes: 67 additions & 0 deletions MixApp.Client/Commands/InstallCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.CommandLine;
using System.CommandLine.Binding;
using System.Diagnostics;

namespace MixApp.Client.Commands;

internal class InstallCommand : Command
{
const string INSTALL_DIR = "C:/Program Files/";

public InstallCommand() : base("install", "Install a package")
{
IValueDescriptor<string> installer = new Argument<string>("installer", "installer path");
AddArgument((Argument)installer);

IValueDescriptor<bool> silent = new Option<bool>("--silent", "silent install");
AddOption((Option)silent);

this.SetHandler(Execute, installer, silent);
}

private void Execute(string installer, bool silent)
{
Process process = new()
{
StartInfo = new FileInfo(installer).Extension switch
{
"msi" => InstallMSI(installer, silent),
"exe" => InstallNSIS(installer, silent),
_ => throw new ArgumentException("Invalid package type"),
}
};

process.Start();
process.WaitForExit();
}

private static ProcessStartInfo InstallMSI(string installer, bool silent)
{
string arguments = $"/i \"{installer}\" INSTALLDIR=\"{INSTALL_DIR}\" /l*v \"{INSTALL_DIR}\\install.log\" ";
if (silent) arguments = "/quiet ";

return new()
{
FileName = "msiexec.exe",
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas",
Arguments = arguments
};
}

private static ProcessStartInfo InstallNSIS(string installer, bool silent)
{
string arguments = $"/D={INSTALL_DIR} ";
if (silent) arguments = $"/S";

return new()
{
FileName = installer,
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas",
Arguments = arguments
};
}
}
102 changes: 102 additions & 0 deletions MixApp.Client/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.CommandLine;
using System.Runtime.Versioning;
using System.Text.Json;
using Microsoft.Win32;
using MixApp.Client.Model;

namespace MixApp.Client.Commands;

[SupportedOSPlatform("windows")]
public class ListCommand : Command
{
const string UNINSTALL_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

public ListCommand() : base("list", "list installed softwares")
{
this.SetHandler(Execute);
}

private void Execute()
{
List<SoftwareInfo> softwareInfos = [];

RegistryKey? regUninstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(UNINSTALL_KEY, false);
if (regUninstall != null) FindSoft(regUninstall, ref softwareInfos, RegistryView.Registry32);

regUninstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(UNINSTALL_KEY, false);
if (regUninstall != null) FindSoft(regUninstall, ref softwareInfos, RegistryView.Registry64);

regUninstall = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(UNINSTALL_KEY, false);
if (regUninstall != null) FindSoft(regUninstall, ref softwareInfos, RegistryView.Registry32);

regUninstall = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(UNINSTALL_KEY, false);
if (regUninstall != null) FindSoft(regUninstall, ref softwareInfos, RegistryView.Registry64);

string result = JsonSerializer.Serialize(softwareInfos, ListSoftwareInfoJsonCtx.Default.ListSoftwareInfo) ?? string.Empty;

Console.WriteLine(result);
}

private static void FindSoft(RegistryKey regUninstall, ref List<SoftwareInfo> lst, RegistryView registryView)
{
foreach (var item in regUninstall.GetSubKeyNames())
{
RegistryKey? regSub = regUninstall.OpenSubKey(item, false);
if (regSub == null) continue;

string displayName = regSub.GetValue("DisplayName") as string ?? string.Empty;
string installLocation = regSub.GetValue("InstallLocation") as string ?? string.Empty;
string uninstallString = regSub.GetValue("UninstallString") as string ?? string.Empty;
string displayVersion = regSub.GetValue("DisplayVersion") as string ?? string.Empty;
string installDate = regSub.GetValue("InstallDate") as string ?? string.Empty;
string publisher = regSub.GetValue("Publisher") as string ?? string.Empty;
string displayIcon = regSub.GetValue("DisplayIcon") as string ?? string.Empty;
int estimatedSize = (int)regSub.GetValue("EstimatedSize", 0);
int systemComponent = (int)regSub.GetValue("SystemComponent", 0);

if (string.IsNullOrWhiteSpace(displayName)) continue;
if (string.IsNullOrWhiteSpace(uninstallString)) continue;
if (string.IsNullOrWhiteSpace(displayVersion) && string.IsNullOrWhiteSpace(displayIcon)) continue;
if (systemComponent == 1) continue;

if (string.IsNullOrWhiteSpace(installDate) && !string.IsNullOrWhiteSpace(displayIcon))
{
try
{
string[] array = displayIcon.Split(',');
if (array.Length >= 2)
{
uninstallString = array[0];
}
FileInfo fileInfo = new(uninstallString);
installDate = fileInfo.CreationTime.ToShortDateString();
}
catch (Exception)
{
}
}

SoftwareInfo? softModel = lst.FirstOrDefault(item1 => item1.Name == displayName);

if (softModel == null)
{
lst.Add(new(
displayName,
displayVersion,
installDate,
uninstallString,
publisher,
estimatedSize == 0 ? "未知" : (estimatedSize / 1024.0).ToString("0.00M"),
installLocation,
registryView));
}
else
{
if (string.IsNullOrWhiteSpace(softModel.DateTime) && !string.IsNullOrWhiteSpace(installDate))
{
softModel.DateTime = installDate;
}
}
}
}
}
21 changes: 21 additions & 0 deletions MixApp.Client/Commands/OpenFolderCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.CommandLine;
using System.Diagnostics;

namespace MixApp.Client.Commands;

public class OpenFolderCommand : Command
{
public OpenFolderCommand() : base("folder", "open a folder")
{
this.SetHandler(Execute);
}

private void Execute()
{
string downloadsPath = Path.Combine
(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads"
);
Process.Start("explorer.exe", downloadsPath);
}
}
19 changes: 19 additions & 0 deletions MixApp.Client/Commands/UnInstallCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.CommandLine;
using System.Diagnostics;

namespace MixApp.Client.Commands;

public class UnInstallCommand : Command
{
public UnInstallCommand() : base("uninstall", "uninstall a package")
{
this.SetHandler(Execute);
}

private void Execute()
{
var fileName = Path.Combine(Environment.SystemDirectory, "control.exe");
var arguments = "/name Microsoft.ProgramsAndFeatures";
Process.Start(fileName, arguments);
}
}
23 changes: 23 additions & 0 deletions MixApp.Client/Extensions/RootCommandExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.CommandLine;
using MixApp.Client.Commands;

namespace MixApp.Client.Extensions
{
public static class RootCommandExtension
{
public static RootCommand Initialize(this RootCommand rootCommand)
{
rootCommand.Add(new InitCommand());

if(OperatingSystem.IsWindows())
{
rootCommand.Add(new InstallCommand());
rootCommand.Add(new UnInstallCommand());
rootCommand.Add(new OpenFolderCommand());
rootCommand.Add(new ListCommand());
}

return rootCommand;
}
}
}
Loading

0 comments on commit 82007db

Please sign in to comment.