-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
383 lines (360 loc) · 12.8 KB
/
Program.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace PyinstallerHelper
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (!Directory.Exists(Constants.AppDataPath))
{
Directory.CreateDirectory(Constants.AppDataPath);
} if (!Directory.Exists(Constants.DistPath))
{
Directory.CreateDirectory(Constants.DistPath);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Routines.main = new Form1();
if (args.Length > 0)
{
Routines.main.LoadFile(args[0]);
RuntimeConfiguration.InFile = args[0];
}
if (args.Contains("-a"))
{
RuntimeConfiguration.AutomatedBuild = true;
}
RuntimeConfiguration.c = ApplicationConfiguration.FromFile(Constants.SettingsPath);
Application.Run(Routines.main);
}
}
public static class RuntimeConfiguration
{
public static bool AutomatedBuild = false;
public static string InFile;
public static ApplicationConfiguration c;
}
public class ApplicationConfiguration
{
public string PyinstallerPath = "pyinstaller";
public ApplicationConfiguration() { }
public string OutToXML()
{
XmlSerializer xml = new XmlSerializer(typeof(ApplicationConfiguration));
Utf8StringWriter sw = new Utf8StringWriter();
xml.Serialize(sw, this);
return sw.ToString();
}
public static ApplicationConfiguration FromFile(string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(ApplicationConfiguration));
ApplicationConfiguration a = new ApplicationConfiguration();
try
{
var fs = new FileStream(path, FileMode.Open);
a = (ApplicationConfiguration)serializer.Deserialize(fs);
fs.Close();
} catch {
a.WriteTo(path);//Write default values
}
return a;
}
public void WriteTo(string path)
{
File.WriteAllText(path,OutToXML());
}
}
public static class Routines
{
public static Form1 main;
public static string GenerateNewTempdir()
{
string bases = Environment.ExpandEnvironmentVariables("%TEMP%\\PYIH-");
Random r = new Random();
bases += r.Next();
Directory.CreateDirectory(bases);
return bases;
}
public static string PadVersionRight(string inv)
{
int ln = Regex.Matches(inv,@"\.").Count+1;
if (ln == 1)
{
return inv + ".0.0.0";
} else if (ln == 2)
{
return inv + ".0.0";
} else if (ln == 3)
{
return inv + ".0";
} else
{
return inv;
}
}
public static bool ExistsOnPath(string fileName)
{
return GetFullPath(fileName) != null;
}
public static string GetFullPath(string fileName)
{
if (File.Exists(fileName))
return Path.GetFullPath(fileName);
var values = Environment.GetEnvironmentVariable("PATH");
foreach (var path in values.Split(';'))
{
var fullPath = Path.Combine(path, fileName);
if (File.Exists(fullPath))
return fullPath;
}
return null;
}
public static DialogResult RunGUICommand(string fullcommand,int expectedlines,string title,Form parent)
{
VerboseCommandExecutorForm vcf = new VerboseCommandExecutorForm();
string[] splcommand = CLISplit.SplitCommandLine(fullcommand).ToArray();
string cmdhead = splcommand[0];
string[] finall = new string[splcommand.Length - 1];
splcommand.ToList().CopyTo(1,finall,0, splcommand.Length-2);
string cmdargs = string.Join(" ", finall);
vcf.EFile = cmdhead;
vcf.EArgs = cmdargs;
vcf.Title = title;
vcf.ExpectedLines = expectedlines;
return vcf.ShowDialog(parent);
}
public static void OpenFolder(string folderPath)
{
if (Directory.Exists(folderPath))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = folderPath,
FileName = "explorer.exe"
};
Process.Start(startInfo);
}
}
}
public class PyinstallerHelperProject
{
public bool HideConsole;
public string SourceFile;
public bool UseCustomIcon;
public bool UseDefaultIcon;
public bool Onefile;
public string OutputDirectory;
public bool UseVersionFile;
public string CustomIconPath;
public string CompanyName;
public string Description;
public string AppVersion;
public string InternalName;
public string Copyright;
public string OriginalFilename;
public string AppName;
public string ProductVersion;
public bool RequireAdmin;
public PyinstallerHelperProject(Form1 inputformsettings)
{
HideConsole = inputformsettings.radioButton2.Checked;
SourceFile = inputformsettings.textBox1.Text;
UseCustomIcon = inputformsettings.radioButton5.Checked;
if (UseCustomIcon)
{
CustomIconPath = inputformsettings.textBox2.Text;
}
UseDefaultIcon = inputformsettings.radioButton4.Checked;//NOTE! UNfinished
Onefile = inputformsettings.checkBox1.Checked;
OutputDirectory = inputformsettings.textBox3.Text;
UseVersionFile = inputformsettings.checkBox2.Checked;
if (UseVersionFile)
{
CompanyName = inputformsettings.textBox4.Text;
Description = inputformsettings.textBox5.Text;
AppVersion = inputformsettings.textBox6.Text;
InternalName = inputformsettings.textBox7.Text;
Copyright = inputformsettings.textBox8.Text;
OriginalFilename = inputformsettings.textBox9.Text;
AppName = inputformsettings.textBox10.Text;
ProductVersion = inputformsettings.textBox11.Text;
}
RequireAdmin = inputformsettings.checkBox3.Checked;
}
public PyinstallerHelperProject() { }//Will this fix XML problem?
public string OutToXML ()
{
XmlSerializer xml = new XmlSerializer(typeof(PyinstallerHelperProject));
Utf8StringWriter sw = new Utf8StringWriter();
xml.Serialize(sw, this);
return sw.ToString();
}
public void WriteControls(Form1 f)
{
f.radioButton2.Checked = HideConsole;
f.textBox1.Text = SourceFile;
f.radioButton5.Checked = UseCustomIcon;
if (UseCustomIcon)
{
f.textBox2.Text = CustomIconPath;
}
f.radioButton4.Checked = UseDefaultIcon;
if (!UseCustomIcon && !UseDefaultIcon)
{
f.radioButton3.Checked = true;
}
f.checkBox1.Checked = Onefile;
f.textBox3.Text = OutputDirectory;
f.checkBox2.Checked = UseVersionFile;
if (UseVersionFile)
{
f.textBox4.Text = CompanyName;
f.textBox5.Text = Description;
f.textBox6.Text = AppVersion;
f.textBox7.Text = InternalName;
f.textBox8.Text = Copyright;
f.textBox9.Text = OriginalFilename;
f.textBox10.Text = AppName;
f.textBox11.Text = ProductVersion;
}
f.checkBox3.Checked = RequireAdmin;
}
}
public class PYIHPWrapper
{
public string TitleName;
public string FullPath;
public PyinstallerHelperProject LastSave;
public PYIHPWrapper(string filename, PyinstallerHelperProject lastSave)
{
FullPath = filename;
TitleName = filename.Split('\\').Last();
LastSave = lastSave;
}
public PYIHPWrapper() { }
public static PYIHPWrapper Blank()
{
PYIHPWrapper p = new PYIHPWrapper();
p.TitleName = "New Project";
p.FullPath = "";
p.LastSave = null;
return p;
}
public bool NeedsSaving(Form1 f)
{
var CurrentSave = new PyinstallerHelperProject(f);
return ! CurrentSave.OutToXML().Equals(LastSave.OutToXML());
}
public void UpdateTitle(Form1 f)
{
f.Text = $"Pyinstaller Helper: [{TitleName}]";
if (NeedsSaving(f))
{
f.Text = $"Pyinstaller Helper: [{TitleName}] *";
}
}
}
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
public static class Constants
{
public static string AppDataPath = Environment.ExpandEnvironmentVariables("%APPDATA%\\PyinstallerHelper");
public static string SettingsPath = AppDataPath + "\\config.xml";
public static string DistPath = Environment.ExpandEnvironmentVariables(AppDataPath+"\\dist");
public static string VersionFileTemplate = @"# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0. Must always contain 4 elements.
filevers=(#VERSSION),
prodvers=(#VERSPSION),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
flags=0x0,
# The operating system for which this file was designed.
# 0x4 - NT and there is no need to change it.
OS=0x40004,
# The general type of file.
# 0x1 - the file is an application.
fileType=0x1,
# The function of the file.
# 0x0 - the function is not defined for this fileType
subtype=0x0,
# Creation date and time stamp.
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
u'040904B0',
[StringStruct(u'CompanyName', u'#COMPANYNAME'),
StringStruct(u'FileDescription', u'#DESCR'),
StringStruct(u'FileVersion', u'#VERSION'),
StringStruct(u'InternalName', u'#INAME'),
StringStruct(u'LegalCopyright', u'#CO'),
StringStruct(u'OriginalFilename', u'#OGF'),
StringStruct(u'ProductName', u'#PNAME'),
StringStruct(u'ProductVersion', u'#VERPSION')])
]),
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
]
)";
}
public static class CLISplit
{
//100% from SO!
public static IEnumerable<string> SplitCommandLine(string commandLine)
{
bool inQuotes = false;
return commandLine.Split(c =>
{
if (c == '\"')
inQuotes = !inQuotes;
return !inQuotes && c == ' ';
})
.Select(arg => arg.Trim().TrimMatchingQuotes('\"'))
.Where(arg => !string.IsNullOrEmpty(arg));
}
public static IEnumerable<string> Split(this string str,
Func<char, bool> controller)
{
int nextPiece = 0;
for (int c = 0; c < str.Length; c++)
{
if (controller(str[c]))
{
yield return str.Substring(nextPiece, c - nextPiece);
nextPiece = c + 1;
}
}
yield return str.Substring(nextPiece);
}
public static string TrimMatchingQuotes(this string input, char quote)
{
if ((input.Length >= 2) &&
(input[0] == quote) && (input[input.Length - 1] == quote))
return input.Substring(1, input.Length - 2);
return input;
}
}
}