-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (98 loc) · 3.7 KB
/
Copy pathProgram.cs
File metadata and controls
112 lines (98 loc) · 3.7 KB
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
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
namespace ChocoAutoInstall
{
internal class Program
{
public static string chocoPath { get; set; } = Path.Combine(Directory.GetCurrentDirectory(), "choco_args.txt");
static void Main(string[] args)
{
List<string> checkChoco = RunCMD(new List<string> { "/c where choco.exe" });
if(checkChoco.First().StartsWith("INFO:"))
Console.WriteLine("Chocolatey not found. Please install Chocolatey and try again.");
else
UserInput();
Console.ReadLine();
}
public static void UserInput()
{
List<object> answers = new List<object>();
foreach (Question question in questions)
{
Console.Write(question.Text);
string answer = Console.ReadLine();
if (question.Name == "PackageList")
PackageList(answer);
if (question.Name == "Upgrade")
{
if (answer == "y")
RunCMD(new List<string> { "/c choco upgrade all -y" });
else
continue;
}
}
}
public static void PackageList(string answer)
{
if (answer.Length > 0)
{
if (!File.Exists(answer.ToString()))
Console.WriteLine("File not found. Using default choco_args.txt");
else
chocoPath = answer.ToString();
}
else
Console.WriteLine("Using default choco_args.txt");
List<string> chocoLines = new List<string>();
string[] lines = File.ReadAllLines(chocoPath);
Console.WriteLine("\n-- List of Packages --\n");
foreach (var line in lines)
{
chocoLines.Add($"/c choco upgrade {line}");
Console.WriteLine(line);
}
Console.Write($"\nInstall ({lines.Length}) packages? (y/n): ");
if (Console.ReadLine().ToLower() == "y")
RunCMD(chocoLines);
else
Environment.Exit(0);
}
public static List<string> RunCMD(List<string> argument)
{
List<string> stdOuts = new List<string>();
foreach (string arg in argument)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = arg,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
}
};
process.Start();
stdOuts.Add(process.StandardError.ReadToEnd());
process.WaitForExit();
}
return stdOuts;
}
public static Question[] questions =
{
new Question { Text = "Upgrade installed Choco packages not in list? (y/n): ", Name = "Upgrade", ExpectedType = typeof(string) },
new Question { Text = "Path to package list file (empty for default): ", Name = "PackageList", ExpectedType = typeof(string) },
};
}
public class Question
{
public string Text { get; set; }
public string Name { get; set; }
public Type ExpectedType { get; set; }
}
}