aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.AdvancedInstaller/InstallerBuilder.cs
blob: ed52aeb05791712a74ac4857db40dd5e22675ea0 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Components;

namespace Tango.AdvancedInstaller
{
    public class InstallerBuilder
    {
        public String AdvancedInstallerPath { get; set; }

        public String ProjectFile { get; private set; }

        private InstallerBuilder()
        {
            AdvancedInstallerPath = @"C:\Program Files (x86)\Caphyon\Advanced Installer 15.6\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, $"/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;
            });
        }
    }
}