-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
187 lines (175 loc) · 8.23 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
using System;
using System.IO;
using System.Text;
namespace WindowsPackager
{
public class Program
{
private static string LOCAL_DIR = Environment.CurrentDirectory;
private static string[] ControlElements = {
"Package: com.yourcompany.identifier",
"Name: Name of the product",
"Depends: ",
"Architecture: iphoneos-arm",
"Description: This is a sample short description",
"Maintainer: Maintainer Name",
"Author: Author Name",
"Section: Section",
"Version: 1.0"
};
private const string CREATE_DEBIAN_PACKAGE = "-b";
private const string EXTRACT_DEBIAN_PACKAGE = "-x";
private const string THEME_DEB = "--theme";
private const string HELPTEXT = "-h";
private const string ERRMSG_DIR_FAILURE = "E: Directory was not found! Aborting...";
private const string ERRMSG_FILE_FAILURE = "E: Specified file does not exist! Aborting...";
private const string ERRMSG_ARGC_FAILURE = "E: Mismatch in arguments! (perhaps missing one or one too much?) Aborting...";
private const string ERRMSG_DEB_FAILURE = "E: File is not a Debian Binary! Aborting...";
private const string ERRMSG_STRUCT_FAILURE = "E: Directory does NOT match a standard structure! (Perhaps missing control?) Aborting...";
private const int EXIT_ARGS_MISMATCH = 100;
private const int EXIT_DIR_ERROR = 200;
private const int EXIT_DEBFILE_ERROR = 300;
private const int EXIT_STRUCT_ERROR = 400;
static void Main(string[] args) {
// check because switch
if (args.Length == 0) {
InfoMessage();
Environment.Exit(-1);
}
switch (args[0])
{
case CREATE_DEBIAN_PACKAGE:
if (args.Length == 2) {
if (Directory.Exists(args[1])) {
BuilderType(args[1], true);
}
else {
ExitWithMessage(ERRMSG_DIR_FAILURE, EXIT_DIR_ERROR);
}
} else {
BuilderType(null, false);
}
break;
case EXTRACT_DEBIAN_PACKAGE:
if (args.Length == 3) {
// get properly formatted Path
string[] cmdargs = Environment.GetCommandLineArgs();
// check if file exists & create extraction stream
if (File.Exists(cmdargs[2]) && Directory.Exists(cmdargs[3])) {
ExtractorType(cmdargs[2], null, cmdargs[3]);
}
else {
ExitWithMessage(ERRMSG_ARGC_FAILURE, EXIT_ARGS_MISMATCH);
}
} else if (args.Length == 2) {
// check if we have a path or direct filename => file cannot contain the '\' char
if (args[1].Contains("\\")) {
if (File.Exists(args[1])) {
ExtractorType(args[1], null, Path.GetDirectoryName(args[1]));
} else {
ExitWithMessage(ERRMSG_ARGC_FAILURE, EXIT_ARGS_MISMATCH);
}
} else {
if (File.Exists(LOCAL_DIR + "\\" + args[1])) {
ExtractorType(LOCAL_DIR + "\\" + args[1], args[1], null);
}
else {
ExitWithMessage(ERRMSG_FILE_FAILURE, EXIT_DEBFILE_ERROR);
}
}
}
break;
case THEME_DEB:
if (args.Length != 2) {
ExitWithMessage(ERRMSG_ARGC_FAILURE, EXIT_ARGS_MISMATCH);
}
// create base theme dir
string target = LOCAL_DIR + "\\Library\\Themes\\" + args[1] + ".theme";
Directory.CreateDirectory(target);
// create the necessary subdirs
Directory.CreateDirectory(target + "\\IconBundles");
Directory.CreateDirectory(target + "\\Bundles\\com.apple.springboard");
GenerateControlFile(LOCAL_DIR);
break;
case HELPTEXT:
InfoMessage();
break;
default:
InfoMessage();
break;
}
}
private static void BuilderType(string WorkDir, bool IsSpecified) {
string dir = (IsSpecified) ? WorkDir : LOCAL_DIR;
VerifyStructure(dir);
Builder.BuildControlTarball(dir);
Builder.BuildDataTarball(dir);
Builder.BuildPackage(dir);
}
private static void ExtractorType(string PassedFilePath, string FileName, string TargetDirectory) {
VerifyFile(PassedFilePath);
Extractor.DebName = Path.GetFileNameWithoutExtension(PassedFilePath);
if (String.IsNullOrEmpty(TargetDirectory)) {
Stream DebFileStream = Builder.CreateStream(FileName);
Extractor.ExtractEverything(DebFileStream, LOCAL_DIR);
} else {
Stream DebFileStream = Builder.CreateStream(PassedFilePath, 3);
Extractor.ExtractEverything(DebFileStream, TargetDirectory);
}
}
private static void VerifyFile(string PathToFile) {
if (Extractor.IsDebianBinary(PathToFile) == false) {
ExitWithMessage(ERRMSG_DEB_FAILURE, EXIT_DEBFILE_ERROR);
}
}
public static void VerifyStructure(string directory) {
int passed = 0;
// check if we AT LEAST have 1 dir
DirectoryInfo[] subdirs = new DirectoryInfo(directory).GetDirectories();
if (subdirs.Length > 0) {
passed++;
}
// check if we have a control file
if (File.Exists(directory + "\\control")) {
passed++;
}
// check if our struct matches
if (passed != 2) {
ExitWithMessage(ERRMSG_STRUCT_FAILURE, EXIT_STRUCT_ERROR);
}
}
private static void GenerateControlFile(string WorkingDir) {
File.WriteAllLines(WorkingDir + "\\control", ControlElements, Encoding.ASCII);
}
public static void ExitWithMessage(string Message, int ExitCode) {
Console.WriteLine(Message);
Environment.Exit(ExitCode);
}
private static void InfoMessage() {
Console.WriteLine("Windows Packager (wpkg) v1.0 Guide");
ColorizedMessage("Building:\n" +
"wpkg -b - Build .deb inside the local directory\n" +
"wpkg -b <Path> - Build .deb in the given path\n",
ConsoleColor.DarkCyan);
ColorizedMessage("Extraction:\n" +
"wpkg -x <PathToDeb> <DestFolder> - Extract .deb to given path\n" +
"wpkg -x <PathToDeb> - Extract .deb inside the original folder\n" +
"wpkg -x <DebfileName> - Extract a .deb inside the folder you're in*\n" +
" *: only works if you're in the same folder as the .deb!\n",
ConsoleColor.DarkGreen);
ColorizedMessage("Extras:\n" +
"wpkg -h - Show this helptext\n" +
"wpkg --theme - Create a base for an iOS Theme in the directory you are currently\n\n",
ConsoleColor.DarkMagenta);
ColorizedMessage("If you stumble upon an error, please send an email at\n" +
"support@saadat.dev\n",
ConsoleColor.DarkRed);
}
private static void ColorizedMessage(string Message, ConsoleColor cColor) {
Console.ForegroundColor = cColor;
Console.WriteLine(Message);
Console.ForegroundColor = ConsoleColor.White;
}
// <-- FIN -->
}
}