-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.Loader.cs
131 lines (127 loc) · 4.54 KB
/
MainForm.Loader.cs
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
126
127
128
129
130
131
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
using Visual_PowerShell.Models;
namespace Visual_PowerShell
{
public partial class MainForm
{
void OpenFile()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;
dialog.Title = "Load Local JSON";
dialog.DefaultExt = "json";
dialog.Filter = "JSON files (*.json)|*.json";
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.ShowDialog();
try
{
foreach (var fileName in dialog.FileNames)
{
LoadFile(fileName);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
void LoadFile(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
{
string json = reader.ReadToEnd();
Repository repository = JsonConvert.DeserializeObject<Repository>(json);
repository.Address = fileName;
int updateRepositoryIndex = commandRepositories.FindLastIndex((r) => r.Address == repository.Address);
if (updateRepositoryIndex != -1)
{
commandRepositories[updateRepositoryIndex] = repository;
}
else
{
commandRepositories.Add(repository);
}
UpdateRepositoryList();
SetRepositoryIndex(commandRepositories.Count - 1);
}
}
private async Task LoadFromURL(string url, bool setRepoIndex = false)
{
try
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(ParseGistURL(url));
var json = await response.Content.ReadAsStringAsync();
Repository repository = JsonConvert.DeserializeObject<Repository>(json);
repository.Address = url;
int updateRepositoryIndex = commandRepositories.FindLastIndex((r) => r.Address == repository.Address);
if (updateRepositoryIndex != -1)
{
commandRepositories[updateRepositoryIndex] = repository;
}
else
{
commandRepositories.Add(repository);
}
UpdateRepositoryList();
if (setRepoIndex) SetRepositoryIndex(commandRepositories.Count - 1);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, url);
}
// done
}
private string ParseGistURL(string url)
{
if (url.StartsWith("https://gist.github.com/"))
{
url = url.Replace("https://gist.github.com/", "https://gist.githubusercontent.com/");
if (!url.Contains("/raw"))
{
url = url + (url.EndsWith("/") ? "raw?update=" : "/raw?update=") + DateTime.Now.ToFileTime();
}
}
return url;
}
private async Task RetreivePackages()
{
// split to lines
var packages = packageList.Text.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
// foreach package
foreach (var package in packages)
{
try
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(ParseGistURL(package));
var json = await response.Content.ReadAsStringAsync();
var list = JsonConvert.DeserializeObject<List<string>>(json);
foreach (var url in list)
{
await LoadFromURL(url);
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
UpdateRepositoryList();
}
}
}