-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
178 lines (164 loc) · 7.96 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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.CompilerServices;
using QtiPackageConverter.Implementations.Qti21;
using QtiPackageConverter.Implementations.Qti22;
using QtiPackageConverter.Implementations.Qti30;
using QtiPackageConverter.Implementations.ValidatePackage;
using QtiPackageConverter.Interface;
using QtiPackageConverter.Model;
namespace QtiPackageConverter
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 2 && args[1].ToLower().IndexOf("help", StringComparison.Ordinal) >= 0)
{
// give help
Console.WriteLine(@"dotnet run QtiPackageConverter package.zip task:(30)");
}
{
var storeNewPackage = true;
var appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()?.Location ?? string.Empty);
var packages = new List<string>();
if (File.Exists(args[0]))
{
packages.Add(args[0]);
}
else if (Directory.Exists(args[0]))
{
packages.AddRange(Directory.GetFiles(args[0], "*.zip"));
}
foreach (var package in packages)
{
var packageRef = package;
if (!Path.IsPathRooted(packageRef)) packageRef = Path.Combine(appPath, packageRef);
if (!File.Exists(packageRef)) throw new Exception($"cannot find package file: {packageRef}");
var packageFolder = ExtractPackage(packageRef);
IConvertPackage converter = null;
switch (args[1].ToLower())
{
case "validate30":
{
storeNewPackage = false; converter = new Validate(packageFolder, QtiVersion.Qti30);
break;
}
case "validate22":
{
storeNewPackage = false;
converter = new Validate(packageFolder, QtiVersion.Qti22);
break;
}
case "validate21":
{
storeNewPackage = false;
converter = new Validate(packageFolder, QtiVersion.Qti21);
break;
}
case "22_30":
{
var local = args.ToList().Contains("--local");
converter = new Qti30Converter(packageFolder, local, QtiVersion.Qti22);
break;
}
case "21_30":
{
var local = args.ToList().Contains("--local");
converter = new Qti30Converter(packageFolder, local, QtiVersion.Qti21);
break;
}
case "21_22":
{
var local = args.ToList().Contains("--local");
converter = new Qti22Converter(packageFolder, local, QtiVersion.Qti21);
break;
}
case "22_21":
{
var local = args.ToList().Contains("--local");
converter = new Qti21Converter(packageFolder, local, QtiVersion.Qti22);
break;
}
default:
throw new Exception($"ConvertManifest method '{args[1]}' not found. Options: dynamischeleestoets");
}
// call convert function
converter.Convert();
// save new package
var newPackage = string.Empty;
if (storeNewPackage)
{
// if it's a folder then create a new folder and add converted package with original names
if (packages.Count > 1)
{
var newFolder = Path.Combine(Path.GetDirectoryName(packageRef), args[1].ToLower());
if (!Directory.Exists(newFolder))
{
Directory.CreateDirectory(newFolder);
}
newPackage = Path.Combine(newFolder, Path.GetFileName(packageRef));
ZipFile.CreateFromDirectory(packageFolder.FullName, newPackage, CompressionLevel.Optimal, false);
}
else // if there is only one package then add the converted package next to the orginal one with date time stamp
{
newPackage = Path.Combine(Path.GetDirectoryName(packageRef),
$"{Path.GetFileNameWithoutExtension(packageRef)}-{DateTime.Now:yyyy-MM-dd_hh-mm-ss}.zip");
ZipFile.CreateFromDirectory(packageFolder.FullName, newPackage, CompressionLevel.Optimal, false);
}
Console.WriteLine($"package successfully converted: {newPackage}");
if (args.ToList().Contains("--validate"))
{
Console.WriteLine($"validating converted package.");
var newpackageFolder = ExtractPackage(newPackage);
if (args[1].ToLower().Length > 2)
{
var newFormat =
}
switch (args[1].ToLower())
{
case "30":
{
converter = new Validate(newpackageFolder, QtiVersion.Qti30);
break;
}
case "22":
{
converter = new Validate(newpackageFolder, QtiVersion.Qti22);
break;
}
case "21":
{
converter = new Validate(newpackageFolder, QtiVersion.Qti21);
break;
}
}
converter?.Convert();
Console.WriteLine($"Done.");
};
}
else
{
Console.WriteLine($"Done.");
}
}
Console.WriteLine($"All done");
}
}
private static DirectoryInfo ExtractPackage(string packageRef)
{
// extract package
var extractPath = new DirectoryInfo(Path.Combine(Path.Combine(Path.GetTempPath(), "_temp"), Path.GetFileNameWithoutExtension(Path.GetRandomFileName())));
if (!extractPath.Exists) extractPath.Create();
var packageFolder = new DirectoryInfo(Path.Combine(extractPath.FullName, "package"));
if (!packageFolder.Exists) packageFolder.Create();
using var packageStream = new FileStream(packageRef, FileMode.Open);
using var packageZip = new ZipArchive(packageStream);
packageZip.ExtractToDirectory(packageFolder.FullName);
return packageFolder;
}
}
}