using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Tango.Core.Components; namespace Tango.AdvancedInstaller { public class InstallerBuilder { private const string ADVANCED_INSTALLER_BASE_FOLDER = @"C:\Program Files (x86)\Caphyon"; public String AdvancedInstallerPath { get; set; } public String ProjectFile { get; private set; } private InstallerBuilder() { double latestVersion = 0.0; //Extract the latest advanced installer version... foreach (var folder in Directory.GetDirectories(ADVANCED_INSTALLER_BASE_FOLDER)) { try { var versionString = Regex.Match(Path.GetFileName(folder), @"\d+\.*\d+").Value; double version = double.Parse(versionString); if (version > latestVersion) { latestVersion = version; } } catch { } } AdvancedInstallerPath = $@"{ADVANCED_INSTALLER_BASE_FOLDER}\Advanced Installer {latestVersion}\bin\x86\AdvancedInstaller.com"; } public InstallerBuilder(String projectFile) : this() { ProjectFile = projectFile; } public InstallerBuilder(String advancedInstallerPath, String projectFile) : this(projectFile) { AdvancedInstallerPath = advancedInstallerPath; } public Task GetProperty(ProjectProperty property) { return Task.Factory.StartNew(() => { CmdCommand command = new CmdCommand(AdvancedInstallerPath, $"/edit \"{ProjectFile}\" /GetProperty {property.ToString()}"); var result = command.Run().Result; return result.StandardOutput.Replace("\r", "").Replace("\n", ""); }); } public Task SetProperty(ProjectProperty property, String value) { return Task.Factory.StartNew(() => { CmdCommand command = new CmdCommand(AdvancedInstallerPath, $"/edit \"{ProjectFile}\" /SetProperty {property.ToString()}=\"{value}\""); command.Run().Wait(); String propValue = GetProperty(property).Result; if (propValue != value) { throw new ArgumentException("The property set command had succeeded but the property value validation has failed."); } }); } public Task Build(String productVersion, String outputFile) { return Task.Factory.StartNew(() => { CmdCommand command = new CmdCommand(AdvancedInstallerPath, $"/edit \"{ProjectFile}\" /SetVersion {productVersion}"); command.Run().Wait(); command = new CmdCommand(AdvancedInstallerPath, $"/edit \"{ProjectFile}\" /SetPackageName \"{outputFile}\" -buildname DefaultBuild"); command.Run().Wait(); command = new CmdCommand(AdvancedInstallerPath, $"/edit \"{ProjectFile}\" /SetPackageName \"{Path.ChangeExtension(outputFile, ".msi")}\" -buildname DefaultBuild"); command.Run().Wait(); command = new CmdCommand(AdvancedInstallerPath, $"/rebuild \"{ProjectFile}\""); command.Timeout = TimeSpan.FromMinutes(2); command.Run().Wait(); }); } public Task Uninstall() { return Task.Factory.StartNew(() => { String productName = GetProperty(ProjectProperty.ProductName).Result; CmdCommand command = new CmdCommand("wmic", $"product where name=\"{productName}\" call uninstall /nointeractive"); command.ExitStrategy = CmdCommand.ExitStrategies.StandardOutput; command.Run().Wait(); }); } public Task IsInstalled() { return Task.Factory.StartNew(() => { String productName = GetProperty(ProjectProperty.ProductName).Result; CmdCommand command = new CmdCommand("wmic", $"product get name"); command.ExitStrategy = CmdCommand.ExitStrategies.StandardOutput; var result = command.Run().Result; List list = result.StandardOutput.Split(new[] { '\r', '\n' }).ToList(); bool found = list.Any(x => x.Trim() == productName); return found; }); } } }