Skip to content

Commit 7f50f6f

Browse files
committed
Refactor ZlibEbuild to improve download and extraction logic; remove unused options and enhance error handling
1 parent 99a849f commit 7f50f6f

1 file changed

Lines changed: 37 additions & 65 deletions

File tree

examples/zlib/zlib.ebuild.cs

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ZlibEbuild : ModuleBase
1414
{
1515
private const string ZlibUrl = "https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz";
1616
private const string ZlibSha256 = "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23";
17-
17+
1818
public ZlibEbuild(ModuleContext context) : base(context)
1919
{
2020
// Set default type, can be overridden by output transformers
@@ -24,50 +24,45 @@ public ZlibEbuild(ModuleContext context) : base(context)
2424
UseVariants = false;
2525
var downloadPath = GetDownloadPath();
2626
var extractPath = GetExtractPath();
27-
27+
2828
// Check if already downloaded and extracted
2929
if (Directory.Exists(extractPath) && File.Exists(Path.Combine(extractPath, "zlib.h")))
3030
{
3131
Console.WriteLine("Zlib already downloaded and extracted.");
3232
SetupSourceFiles(extractPath);
33-
return true;
3433
}
35-
34+
3635
// Download zlib if not already present
3736
if (!File.Exists(downloadPath) || !ValidateChecksum(downloadPath))
3837
{
39-
Console.WriteLine($"Downloading zlib {ZlibVersion}...");
40-
if (!await DownloadZlib(downloadPath))
38+
Console.WriteLine($"Downloading zlib 1.3.1 ...");
39+
if (!DownloadZlib(downloadPath).GetAwaiter().GetResult())
4140
{
4241
Console.WriteLine("Failed to download zlib");
43-
return false;
42+
throw new Exception("Zlib download failed");
4443
}
4544
}
46-
45+
4746
// Extract zlib
4847
Console.WriteLine("Extracting zlib...");
4948
if (!ExtractZlib(downloadPath, extractPath))
5049
{
5150
Console.WriteLine("Failed to extract zlib");
52-
return false;
51+
throw new Exception("Zlib extraction failed");
5352
}
54-
53+
5554
// Set up source files
5655
SetupSourceFiles(extractPath);
57-
56+
5857
Console.WriteLine("Zlib setup completed successfully.");
59-
return true;
6058
}
61-
59+
6260
[OutputTransformer("shared", "shared")]
6361
public void TransformToSharedLibrary()
6462
{
6563
Type = ModuleType.SharedLibrary;
6664
// When building as shared library, need to add export definitions
67-
if (EnableAdvancedFeatures)
68-
{
69-
Definitions.Private.Add(new Definition("ZLIB_DLL"));
70-
}
65+
Definitions.Private.Add(new Definition("ZLIB_DLL"));
7166
}
7267

7368
[OutputTransformer("static", "static")]
@@ -76,20 +71,20 @@ public void TransformToStaticLibrary()
7671
Type = ModuleType.StaticLibrary;
7772
// This is the default, so nothing special needed
7873
}
79-
74+
8075
private string GetDownloadPath()
8176
{
8277
var tempDir = Path.Combine(Path.GetTempPath(), "ebuild", "zlib");
8378
Directory.CreateDirectory(tempDir);
84-
return Path.Combine(tempDir, $"zlib-{ZlibVersion}.tar.gz");
79+
return Path.Combine(tempDir, $"zlib-1.3.1.tar.gz");
8580
}
86-
81+
8782
private string GetExtractPath()
8883
{
8984
var tempDir = Path.Combine(Path.GetTempPath(), "ebuild", "zlib");
90-
return Path.Combine(tempDir, $"zlib-{ZlibVersion}");
85+
return Path.Combine(tempDir, $"zlib-1.3.1");
9186
}
92-
87+
9388
private async Task<bool> DownloadZlib(string downloadPath)
9489
{
9590
// Directly using HttpClient to download the file
@@ -103,10 +98,10 @@ private async Task<bool> DownloadZlib(string downloadPath)
10398
Console.WriteLine($"Failed to download zlib: {response.StatusCode}");
10499
return false;
105100
}
106-
101+
107102
var bytes = await response.Content.ReadAsByteArrayAsync();
108103
await File.WriteAllBytesAsync(downloadPath, bytes);
109-
104+
110105
return ValidateChecksum(downloadPath);
111106
}
112107
catch (Exception ex)
@@ -115,7 +110,7 @@ private async Task<bool> DownloadZlib(string downloadPath)
115110
return false;
116111
}
117112
}
118-
113+
119114
private bool ValidateChecksum(string filePath)
120115
{
121116
try
@@ -124,7 +119,7 @@ private bool ValidateChecksum(string filePath)
124119
using var stream = File.OpenRead(filePath);
125120
var hash = sha256.ComputeHash(stream);
126121
var hashString = Convert.ToHexString(hash).ToLowerInvariant();
127-
122+
128123
var isValid = hashString == ZlibSha256;
129124
if (!isValid)
130125
{
@@ -138,7 +133,7 @@ private bool ValidateChecksum(string filePath)
138133
return false;
139134
}
140135
}
141-
136+
142137
private bool ExtractZlib(string downloadPath, string extractPath)
143138
{
144139
try
@@ -148,18 +143,18 @@ private bool ExtractZlib(string downloadPath, string extractPath)
148143
Directory.Delete(extractPath, true);
149144
}
150145
Directory.CreateDirectory(extractPath);
151-
146+
152147
// Extract tar.gz file
153148
using var originalFileStream = File.OpenRead(downloadPath);
154149
using var gzipStream = new GZipStream(originalFileStream, CompressionMode.Decompress);
155150
using var tarStream = new MemoryStream();
156-
151+
157152
gzipStream.CopyTo(tarStream);
158153
tarStream.Position = 0;
159-
154+
160155
// Simple tar extraction (basic implementation)
161156
ExtractTar(tarStream, Path.GetDirectoryName(extractPath)!);
162-
157+
163158
return true;
164159
}
165160
catch (Exception ex)
@@ -168,7 +163,7 @@ private bool ExtractZlib(string downloadPath, string extractPath)
168163
return false;
169164
}
170165
}
171-
166+
172167
private void ExtractTar(Stream tarStream, string destinationDirectory)
173168
{
174169
var buffer = new byte[100];
@@ -177,25 +172,25 @@ private void ExtractTar(Stream tarStream, string destinationDirectory)
177172
// Read filename
178173
var read = tarStream.Read(buffer, 0, 100);
179174
if (read < 100) break;
180-
175+
181176
var name = Encoding.ASCII.GetString(buffer).TrimEnd('\0');
182177
if (string.IsNullOrEmpty(name)) break;
183-
178+
184179
// Skip to size field (at offset 124)
185180
tarStream.Position += 24;
186-
181+
187182
// Read size
188183
tarStream.Read(buffer, 0, 12);
189184
var sizeString = Encoding.ASCII.GetString(buffer, 0, 12).TrimEnd('\0', ' ');
190185
var size = Convert.ToInt32(sizeString, 8);
191-
186+
192187
// Skip to data (at offset 512)
193188
tarStream.Position = ((tarStream.Position - 1) / 512 + 1) * 512;
194-
189+
195190
// Extract file
196191
var filePath = Path.Combine(destinationDirectory, name);
197192
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
198-
193+
199194
if (!name.EndsWith('/') && size > 0)
200195
{
201196
using var fileStream = File.Create(filePath);
@@ -208,13 +203,13 @@ private void ExtractTar(Stream tarStream, string destinationDirectory)
208203
remaining -= actualRead;
209204
}
210205
}
211-
206+
212207
// Skip to next 512-byte boundary
213208
var pos = tarStream.Position;
214209
tarStream.Position = ((pos - 1) / 512 + 1) * 512;
215210
}
216211
}
217-
212+
218213
public void SetupSourceFiles(string extractPath)
219214
{
220215
// Add main zlib source files
@@ -224,7 +219,7 @@ public void SetupSourceFiles(string extractPath)
224219
"gzread.c", "gzwrite.c", "infback.c", "inffast.c", "inflate.c", "inftrees.c",
225220
"trees.c", "uncompr.c", "zutil.c"
226221
};
227-
222+
228223
foreach (var sourceFile in sourceFiles)
229224
{
230225
var fullPath = Path.Combine(extractPath, sourceFile);
@@ -233,7 +228,7 @@ public void SetupSourceFiles(string extractPath)
233228
SourceFiles.Add(fullPath);
234229
}
235230
}
236-
231+
237232
// Add include directories
238233
Includes.Add(extractPath);
239234
if (Context.Platform.Name == "unix")
@@ -246,28 +241,5 @@ public void SetupSourceFiles(string extractPath)
246241
Definitions.Private.Add(new Definition("_CRT_NONSTDC_NO_DEPRECATE"));
247242
}
248243

249-
// Apply module options to definitions
250-
if (EnableDebug)
251-
{
252-
Definitions.Private.Add(new Definition("DEBUG"));
253-
Definitions.Private.Add(new Definition("ZLIB_DEBUG"));
254-
}
255-
256-
if (EnableAdvancedFeatures)
257-
{
258-
Definitions.Private.Add(new Definition("ZLIB_ADVANCED"));
259-
// Enable all compression features
260-
Definitions.Private.Add(new Definition("NO_GZIP=0"));
261-
}
262-
263-
if (OptimizeForSize)
264-
{
265-
Definitions.Private.Add(new Definition("ZLIB_SMALL"));
266-
OptimizationLevel = OptimizationLevel.Size;
267-
}
268-
else
269-
{
270-
OptimizationLevel = OptimizationLevel.Speed;
271-
}
272244
}
273245
}

0 commit comments

Comments
 (0)