Skip to content

Commit 896b797

Browse files
committed
Upgrade translation tool to v2 - work on a copy of the files; automatically generate a release zip; better logging; more stuff
1 parent 6c902c8 commit 896b797

File tree

4 files changed

+177
-25
lines changed

4 files changed

+177
-25
lines changed

tools/ReleaseTool/ReleaseTool/Program.cs

Lines changed: 164 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,197 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using ICSharpCode.SharpZipLib.Zip;
45

56
namespace ReleaseTool
67
{
78
internal static class ReleaseTool
89
{
910
private static TextWriter _originalOut;
1011

12+
private static string CleanPath(string path)
13+
{
14+
return path.Trim().Replace('\\', '/').Trim('/').Trim();
15+
}
16+
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
17+
{
18+
Directory.CreateDirectory(target.FullName);
19+
20+
// Copy each file into the new directory.
21+
foreach (FileInfo fi in source.GetFiles())
22+
{
23+
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
24+
}
25+
26+
// Copy each subdirectory using recursion.
27+
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
28+
{
29+
DirectoryInfo nextTargetSubDir =
30+
target.CreateSubdirectory(diSourceSubDir.Name);
31+
CopyAll(diSourceSubDir, nextTargetSubDir);
32+
}
33+
}
34+
1135
private static void Main(string[] args)
1236
{
13-
var fgc = Console.ForegroundColor;
14-
if (args.Length != 1 || !Directory.Exists(args[0]) || !args[0].Replace('/', '\\').TrimEnd('\\').EndsWith("\\translation", StringComparison.OrdinalIgnoreCase))
37+
if (args.Length != 1 || !Directory.Exists(args[0]))
1538
{
16-
Console.ForegroundColor = ConsoleColor.Red;
17-
Console.WriteLine("Error: Invalid arguments");
18-
Console.ForegroundColor = fgc;
19-
Console.WriteLine("Drag the translation folder from the repository on to this tool's exe to generate a release. Detailed results will be saved to results.txt in the program directory.");
39+
ShowInvalidArgsError();
2040
}
2141
else
2242
{
2343
var root = new DirectoryInfo(args[0]);
24-
if (root.Exists)
44+
var translationName = root.Name.Replace("-master", ""); //todo let user enter it
45+
46+
var translationDir = Path.Combine(root.FullName, "translation");
47+
if (!Directory.Exists(translationDir))
48+
{
49+
ShowInvalidArgsError();
50+
}
51+
else
2552
{
2653
Console.ForegroundColor = ConsoleColor.Yellow;
2754
Console.WriteLine("Beginning release creation!");
55+
Console.ForegroundColor = ConsoleColor.White;
56+
57+
var programDir = Path.GetDirectoryName(typeof(ReleaseTool).Assembly.Location) ??
58+
Environment.CurrentDirectory;
59+
60+
var detailsPath = Path.Combine(programDir, "results.txt");
61+
62+
var outputPath = Path.Combine(programDir, translationName + "_Release_" + DateTime.Now.ToString("yyyy-MM-dd") + ".zip");
63+
File.Delete(outputPath);
64+
using (var output = ZipFile.Create(outputPath))
65+
{
66+
Console.WriteLine("Writing the release to " + outputPath);
67+
68+
output.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
69+
70+
var readmePath = Path.Combine(root.FullName, "README.md");
71+
if (File.Exists(readmePath)) output.Add(readmePath, "README.md");
72+
73+
var licensePath = Path.Combine(root.FullName, "LICENSE");
74+
if (File.Exists(licensePath)) output.Add(licensePath, "LICENSE");
2875

29-
_originalOut = Console.Out;
30-
var detailsPath = Path.Combine(Environment.CurrentDirectory, "results.txt");
31-
Console.SetOut(new StreamWriter(detailsPath));
76+
var configDir = Path.Combine(root.FullName, "config");
77+
foreach (var file in Directory.GetFiles(configDir, "*", SearchOption.AllDirectories))
78+
{
79+
var entryName = Path.Combine("BepInEx", CleanPath(file.Substring(root.FullName.Length)));
80+
output.Add(file, entryName);
81+
}
3282

33-
var result = ScanResursively(root);
83+
var tlDir = Path.Combine(root.FullName, "Translation\\en");
3484

35-
Console.Out.Flush();
36-
Console.SetOut(_originalOut);
85+
// Make a working copy
86+
Console.WriteLine("Creating a work copy of the translation files...");
87+
var copyDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "releasetool_tl_temp"));
88+
if (copyDir.Exists) copyDir.Delete(true);
89+
CopyAll(new DirectoryInfo(tlDir), copyDir);
3790

38-
Console.ForegroundColor = ConsoleColor.Green;
39-
Console.WriteLine($"Creating release done, {result.GetPercent() * 100:F3}% completed.");
40-
Console.ForegroundColor = ConsoleColor.Gray;
41-
Console.WriteLine("Detailed results were saved to " + detailsPath);
91+
Console.WriteLine("Cleaning untranslated lines and files...");
92+
_originalOut = Console.Out;
93+
Console.SetOut(new StreamWriter(detailsPath));
94+
// Clean up the copied files
95+
var result = CleanTranslations(copyDir);
96+
Console.Out.Flush();
97+
Console.SetOut(_originalOut);
98+
99+
// Use the cleaned up copy
100+
tlDir = copyDir.FullName;
101+
102+
Console.WriteLine("Writing cleaned translation files into archives...");
103+
104+
var texDir = Path.Combine(tlDir, "Texture");
105+
if (Directory.Exists(texDir) && Directory.GetFiles(texDir, "*.png", SearchOption.AllDirectories).Any())
106+
{
107+
var texZipPath = Path.GetTempFileName();
108+
using (var texZipFile = ZipFile.Create(texZipPath))
109+
{
110+
texZipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
111+
112+
foreach (var file in Directory.GetFiles(texDir, "*.png", SearchOption.AllDirectories))
113+
{
114+
var entryName = CleanPath(file.Substring(texDir.Length));
115+
//Console.WriteLine("Adding texture to texture archive: " + entryName);
116+
texZipFile.Add(file, entryName);
117+
}
118+
119+
texZipFile.CommitUpdate();
120+
}
121+
output.Add(texZipPath, "BepInEx\\Translation\\en\\Texture\\" + translationName + "_Textures.zip");
122+
}
123+
124+
var assetDir = Path.Combine(tlDir, "RedirectedResources\\assets");
125+
if (Directory.Exists(assetDir) && Directory.GetFiles(assetDir, "*.txt", SearchOption.AllDirectories).Any())
126+
{
127+
var assZipPath = Path.GetTempFileName();
128+
using (var assZipFile = ZipFile.Create(assZipPath))
129+
{
130+
assZipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
131+
132+
foreach (var file in Directory.GetFiles(assetDir, "*.txt", SearchOption.AllDirectories))
133+
{
134+
var entryName = CleanPath(file.Substring(assetDir.Length));
135+
//Console.WriteLine("Adding to redirected assets archive: " + entryName);
136+
assZipFile.Add(file, entryName);
137+
}
138+
139+
assZipFile.CommitUpdate();
140+
}
141+
output.Add(assZipPath, "BepInEx\\Translation\\en\\RedirectedResources\\assets\\" + translationName + "_Assets.zip");
142+
}
143+
144+
var textDir = Path.Combine(tlDir, "Text");
145+
if (Directory.Exists(textDir) && Directory.GetFiles(textDir, "*.txt", SearchOption.AllDirectories).Any())
146+
{
147+
var textZipPath = Path.GetTempFileName();
148+
using (var textZipFile = ZipFile.Create(textZipPath))
149+
{
150+
textZipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct));
151+
152+
foreach (var file in Directory.GetFiles(textDir, "*.txt", SearchOption.AllDirectories))
153+
{
154+
var entryName = CleanPath(file.Substring(textDir.Length));
155+
//Console.WriteLine("Adding to redirected assets archive: " + entryName);
156+
textZipFile.Add(file, entryName);
157+
}
158+
159+
textZipFile.CommitUpdate();
160+
}
161+
output.Add(textZipPath, "BepInEx\\Translation\\en\\Text\\" + translationName + "_Text.zip");
162+
}
163+
164+
foreach (ZipEntry entry in output)
165+
{
166+
entry.CompressionMethod = CompressionMethod.Deflated;
167+
}
168+
169+
output.CommitUpdate();
170+
171+
Console.ForegroundColor = ConsoleColor.Green;
172+
Console.WriteLine($"Creating release done, {result.GetPercent() * 100:F3}% completed.");
173+
Console.ForegroundColor = ConsoleColor.Gray;
174+
Console.WriteLine("Detailed results were saved to " + detailsPath);
175+
176+
copyDir.Delete(true);
177+
}
42178
}
43179
}
44180

45-
Console.ForegroundColor = fgc;
181+
Console.ForegroundColor = ConsoleColor.White;
46182
Console.WriteLine("Press any key to exit...");
47183
Console.ReadKey(true);
48184
}
49185

50-
private static Results ScanResursively(DirectoryInfo dir)
186+
private static void ShowInvalidArgsError()
187+
{
188+
Console.ForegroundColor = ConsoleColor.Red;
189+
Console.WriteLine("Error: Invalid arguments");
190+
Console.ForegroundColor = ConsoleColor.White;
191+
Console.WriteLine("Drag the repository folder (it must contain a translation folder) on to this tool's exe to generate a release. Detailed results will be saved to results.txt in the program directory.");
192+
}
193+
194+
private static Results CleanTranslations(DirectoryInfo dir)
51195
{
52196
var files = dir.GetFiles("*.txt", SearchOption.TopDirectoryOnly);
53197
var folders = dir.GetDirectories();
@@ -82,7 +226,7 @@ private static Results ScanResursively(DirectoryInfo dir)
82226

83227
foreach (var folder in folders)
84228
{
85-
var r = ScanResursively(folder);
229+
var r = CleanTranslations(folder);
86230
folderPercentage.Add(r);
87231
}
88232

tools/ReleaseTool/ReleaseTool/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("2.0")]

tools/ReleaseTool/ReleaseTool/ReleaseTool.csproj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,28 @@
1818
<DebugSymbols>true</DebugSymbols>
1919
<DebugType>full</DebugType>
2020
<Optimize>false</Optimize>
21-
<OutputPath>bin\Debug\</OutputPath>
21+
<OutputPath>bin\</OutputPath>
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
2525
</PropertyGroup>
2626
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2727
<PlatformTarget>AnyCPU</PlatformTarget>
28-
<DebugType>pdbonly</DebugType>
28+
<DebugType>full</DebugType>
2929
<Optimize>true</Optimize>
30-
<OutputPath>bin\Release\</OutputPath>
30+
<OutputPath>bin\</OutputPath>
3131
<DefineConstants>TRACE</DefineConstants>
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
34+
<DebugSymbols>true</DebugSymbols>
3435
</PropertyGroup>
3536
<PropertyGroup>
3637
<StartupObject>ReleaseTool.ReleaseTool</StartupObject>
3738
</PropertyGroup>
3839
<ItemGroup>
40+
<Reference Include="ICSharpCode.SharpZipLib, Version=1.2.0.246, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
41+
<HintPath>..\packages\SharpZipLib.1.2.0\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
42+
</Reference>
3943
<Reference Include="System" />
4044
<Reference Include="System.Core" />
4145
<Reference Include="System.Xml.Linq" />
@@ -51,6 +55,7 @@
5155
</ItemGroup>
5256
<ItemGroup>
5357
<None Include="App.config" />
58+
<None Include="packages.config" />
5459
</ItemGroup>
5560
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5661
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="SharpZipLib" version="1.2.0" targetFramework="net462" />
4+
</packages>

0 commit comments

Comments
 (0)