aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.AdvancedInstaller/InstallerBuilder.cs
blob: 40d18c991bf7fc60ff77e41ae030af1349c6460f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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<String> GetProperty(ProjectProperty property)
        {
            return Task.Factory.StartNew<String>(() =>
            {
                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<bool> IsInstalled()
        {
            return Task.Factory.StartNew<bool>(() =>
            {
                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<String> list = result.StandardOutput.Split(new[] { '\r', '\n' }).ToList();

                bool found = list.Any(x => x.Trim() == productName);

                return found;
            });
        }
    }
}