Skip to content

Commit 48bd4f8

Browse files
author
Saadat-HPWorkstation
committed
Entrypoint / Correctly check for triggers
1 parent a431ee0 commit 48bd4f8

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

Program.cs

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
5+
namespace WindowsPackager
6+
{
7+
8+
public class Program
9+
{
10+
private static string LOCAL_DIR = Environment.CurrentDirectory;
11+
private static string[] ControlElements = {
12+
"Package: com.yourcompany.identifier",
13+
"Name: Name of the product",
14+
"Depends: ",
15+
"Architecture: iphoneos-arm",
16+
"Description: This is a sample short description",
17+
"Maintainer: Maintainer Name",
18+
"Author: Author Name",
19+
"Section: Section",
20+
"Version: 1.0"
21+
};
22+
private const string CREATE_DEBIAN_PACKAGE = "-b";
23+
private const string EXTRACT_DEBIAN_PACKAGE = "-x";
24+
private const string THEME_DEB = "--theme";
25+
private const string HELPTEXT = "-h";
26+
private const string ERRMSG_DIR_FAILURE = "E: Directory was not found! Aborting...";
27+
private const string ERRMSG_FILE_FAILURE = "E: Specified file does not exist! Aborting...";
28+
private const string ERRMSG_ARGC_FAILURE = "E: Mismatch in arguments! (perhaps missing one or one too much?) Aborting...";
29+
private const string ERRMSG_DEB_FAILURE = "E: File is not a Debian Binary! Aborting...";
30+
private const string ERRMSG_STRUCT_FAILURE = "E: Directory does NOT match a standard structure! (Perhaps missing control?) Aborting...";
31+
private const int EXIT_ARGS_MISMATCH = 100;
32+
private const int EXIT_DIR_ERROR = 200;
33+
private const int EXIT_DEBFILE_ERROR = 300;
34+
private const int EXIT_STRUCT_ERROR = 400;
35+
36+
static void Main(string[] args) {
37+
// check because switch
38+
if (args.Length == 0) {
39+
InfoMessage();
40+
Environment.Exit(-1);
41+
}
42+
switch (args[0])
43+
{
44+
case CREATE_DEBIAN_PACKAGE:
45+
if (args.Length == 2) {
46+
if (Directory.Exists(args[1])) {
47+
BuilderType(args[1], true);
48+
}
49+
else {
50+
ExitWithMessage(ERRMSG_DIR_FAILURE, EXIT_DIR_ERROR);
51+
}
52+
} else {
53+
BuilderType(null, false);
54+
}
55+
break;
56+
case EXTRACT_DEBIAN_PACKAGE:
57+
if (args.Length == 3) {
58+
// get properly formatted Path
59+
string[] cmdargs = Environment.GetCommandLineArgs();
60+
// check if file exists & create extraction stream
61+
if (File.Exists(cmdargs[2]) && Directory.Exists(cmdargs[3])) {
62+
ExtractorType(cmdargs[2], null, cmdargs[3]);
63+
}
64+
else {
65+
ExitWithMessage(ERRMSG_ARGC_FAILURE, EXIT_ARGS_MISMATCH);
66+
}
67+
} else if (args.Length == 2) {
68+
// check if we have a path or direct filename => file cannot contain the '\' char
69+
if (args[1].Contains("\\")) {
70+
if (File.Exists(args[1])) {
71+
ExtractorType(args[1], null, Path.GetDirectoryName(args[1]));
72+
} else {
73+
ExitWithMessage(ERRMSG_ARGC_FAILURE, EXIT_ARGS_MISMATCH);
74+
}
75+
} else {
76+
if (File.Exists(LOCAL_DIR + "\\" + args[1])) {
77+
ExtractorType(LOCAL_DIR + "\\" + args[1], args[1], null);
78+
}
79+
else {
80+
ExitWithMessage(ERRMSG_FILE_FAILURE, EXIT_DEBFILE_ERROR);
81+
}
82+
}
83+
}
84+
break;
85+
case THEME_DEB:
86+
if (args.Length != 2) {
87+
ExitWithMessage(ERRMSG_ARGC_FAILURE, EXIT_ARGS_MISMATCH);
88+
}
89+
// create base theme dir
90+
string target = LOCAL_DIR + "\\Library\\Themes\\" + args[1] + ".theme";
91+
Directory.CreateDirectory(target);
92+
// create the necessary subdirs
93+
Directory.CreateDirectory(target + "\\IconBundles");
94+
Directory.CreateDirectory(target + "\\Bundles\\com.apple.springboard");
95+
GenerateControlFile(LOCAL_DIR);
96+
break;
97+
case HELPTEXT:
98+
InfoMessage();
99+
break;
100+
default:
101+
InfoMessage();
102+
break;
103+
}
104+
}
105+
106+
private static void BuilderType(string WorkDir, bool IsSpecified) {
107+
string dir = (IsSpecified) ? WorkDir : LOCAL_DIR;
108+
VerifyStructure(dir);
109+
Builder.BuildControlTarball(dir);
110+
Builder.BuildDataTarball(dir);
111+
Builder.BuildPackage(dir);
112+
}
113+
114+
private static void ExtractorType(string PassedFilePath, string FileName, string TargetDirectory) {
115+
VerifyFile(PassedFilePath);
116+
Extractor.DebName = Path.GetFileNameWithoutExtension(PassedFilePath);
117+
if (String.IsNullOrEmpty(TargetDirectory)) {
118+
Stream DebFileStream = Builder.CreateStream(FileName);
119+
Extractor.ExtractEverything(DebFileStream, LOCAL_DIR);
120+
} else {
121+
Stream DebFileStream = Builder.CreateStream(PassedFilePath, 3);
122+
Extractor.ExtractEverything(DebFileStream, TargetDirectory);
123+
}
124+
}
125+
126+
private static void VerifyFile(string PathToFile) {
127+
if (Extractor.IsDebianBinary(PathToFile) == false) {
128+
ExitWithMessage(ERRMSG_DEB_FAILURE, EXIT_DEBFILE_ERROR);
129+
}
130+
}
131+
132+
public static void VerifyStructure(string directory) {
133+
int passed = 0;
134+
// check if we AT LEAST have 1 dir
135+
DirectoryInfo[] subdirs = new DirectoryInfo(directory).GetDirectories();
136+
if (subdirs.Length > 0) {
137+
passed++;
138+
}
139+
// check if we have a control file
140+
if (File.Exists(directory + "\\control")) {
141+
passed++;
142+
}
143+
// check if our struct matches
144+
if (passed != 2) {
145+
ExitWithMessage(ERRMSG_STRUCT_FAILURE, EXIT_STRUCT_ERROR);
146+
}
147+
}
148+
149+
private static void GenerateControlFile(string WorkingDir) {
150+
File.WriteAllLines(WorkingDir + "\\control", ControlElements, Encoding.ASCII);
151+
}
152+
153+
public static void ExitWithMessage(string Message, int ExitCode) {
154+
Console.WriteLine(Message);
155+
Environment.Exit(ExitCode);
156+
}
157+
158+
private static void InfoMessage() {
159+
Console.WriteLine("Windows Packager (wpkg) v1.0 Guide");
160+
ColorizedMessage("Building:\n" +
161+
"wpkg -b - Build .deb inside the local directory\n" +
162+
"wpkg -b <Path> - Build .deb in the given path\n",
163+
ConsoleColor.DarkCyan);
164+
ColorizedMessage("Extraction:\n" +
165+
"wpkg -x <PathToDeb> <DestFolder> - Extract .deb to given path\n" +
166+
"wpkg -x <PathToDeb> - Extract .deb inside the original folder\n" +
167+
"wpkg -x <DebfileName> - Extract a .deb inside the folder you're in*\n" +
168+
" *: only works if you're in the same folder as the .deb!\n",
169+
ConsoleColor.DarkGreen);
170+
ColorizedMessage("Extras:\n" +
171+
"wpkg -h - Show this helptext\n" +
172+
"wpkg --theme - Create a base for an iOS Theme in the directory you are currently\n\n",
173+
ConsoleColor.DarkMagenta);
174+
ColorizedMessage("If you stumble upon an error, please send an email at\n" +
175+
"support@saadat.dev\n",
176+
ConsoleColor.DarkRed);
177+
}
178+
179+
private static void ColorizedMessage(string Message, ConsoleColor cColor) {
180+
Console.ForegroundColor = cColor;
181+
Console.WriteLine(Message);
182+
Console.ForegroundColor = ConsoleColor.White;
183+
}
184+
185+
// <-- FIN -->
186+
}
187+
}

0 commit comments

Comments
 (0)