-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSetup.cs
More file actions
280 lines (233 loc) · 9.71 KB
/
Setup.cs
File metadata and controls
280 lines (233 loc) · 9.71 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO.Compression;
/// <summary>
/// Execute with `dotnet run --project Tools/Setup`
/// </summary>
public class PowerSyncSetup
{
private const string VERSION = "0.4.13";
private const string GITHUB_BASE_URL = $"https://github.com/powersync-ja/powersync-sqlite-core/releases/download/v{VERSION}";
private const string MAVEN_BASE_URL = $"https://repo1.maven.org/maven2/com/powersync/powersync-sqlite-core/{VERSION}";
private readonly HttpClient _httpClient;
private readonly string _basePath;
public PowerSyncSetup()
{
_httpClient = new HttpClient();
_basePath = Path.Combine(AppContext.BaseDirectory, "../../../../..", "PowerSync");
}
public async Task RunSetup()
{
try
{
await SetupDesktop();
await SetupMauiIos();
await SetupMauiAndroid();
await SetupMauiMacCatalyst();
}
finally
{
_httpClient?.Dispose();
}
}
public async Task SetupDesktop()
{
Console.WriteLine("Setting up Desktop libraries...");
var runtimeConfigs = GetDesktopRuntimeConfigs();
var commonPath = Path.Combine(_basePath, "PowerSync.Common");
foreach (var config in runtimeConfigs)
{
await ProcessDesktopRuntime(commonPath, config);
}
}
private static Dictionary<string, RuntimeConfig> GetDesktopRuntimeConfigs()
{
return new Dictionary<string, RuntimeConfig>
{
{ "osx-x64", new RuntimeConfig("libpowersync_x64.macos.dylib", "libpowersync.dylib") },
{ "osx-arm64", new RuntimeConfig("libpowersync_aarch64.macos.dylib", "libpowersync.dylib") },
{ "linux-x64", new RuntimeConfig("libpowersync_x64.linux.so", "libpowersync.so") },
{ "linux-arm64", new RuntimeConfig("libpowersync_aarch64.linux.so", "libpowersync.so") },
{ "win-x64", new RuntimeConfig("powersync_x64.dll", "powersync.dll") },
{ "win-arm64", new RuntimeConfig("powersync_aarch64.dll", "powersync.dll") }
};
}
private async Task ProcessDesktopRuntime(string basePath, KeyValuePair<string, RuntimeConfig> runtimeConfig)
{
var (rid, config) = runtimeConfig;
var nativeDir = Path.Combine(basePath, "runtimes", rid, "native");
try
{
Directory.CreateDirectory(nativeDir);
var downloadPath = Path.Combine(nativeDir, config.OriginalFileName);
var finalPath = Path.Combine(nativeDir, config.FinalFileName);
var downloadUrl = $"{GITHUB_BASE_URL}/{config.OriginalFileName}";
await DownloadFile(downloadUrl, downloadPath);
File.Move(downloadPath, finalPath, overwrite: true);
Console.WriteLine($"✓ {rid}: {config.OriginalFileName} → {config.FinalFileName}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"✗ Failed to process {rid}: {ex.Message}");
}
}
public async Task SetupMauiIos()
{
Console.WriteLine("Setting up MAUI iOS libraries...");
var nativeDir = Path.Combine(_basePath, "PowerSync.Maui", "Platforms", "iOS", "NativeLibs");
var config = new ArchiveConfig(
"powersync-sqlite-core.xcframework.zip",
"powersync-sqlite-core.xcframework"
);
await ProcessArchiveDownload(nativeDir, config, GITHUB_BASE_URL);
}
public async Task SetupMauiAndroid()
{
Console.WriteLine("Setting up MAUI Android libraries...");
var nativeDir = Path.Combine(_basePath, "PowerSync.Maui", "Platforms", "Android", "jniLibs");
var aarFileName = $"powersync-sqlite-core-{VERSION}.aar";
try
{
Directory.CreateDirectory(nativeDir);
var aarPath = Path.Combine(nativeDir, aarFileName);
var downloadUrl = $"{MAVEN_BASE_URL}/{aarFileName}";
await DownloadFile(downloadUrl, aarPath);
ExtractAarNativeLibraries(aarPath, nativeDir);
Console.WriteLine($"✓ Android: Extracted native libraries from {aarFileName}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"✗ Failed to setup Android: {ex.Message}");
}
}
public async Task SetupMauiMacCatalyst()
{
Console.WriteLine("Setting up MAUI MacCatalyst libraries...");
var nativeDir = Path.Combine(_basePath, "PowerSync.Maui", "Platforms", "MacCatalyst", "NativeLibs");
var config = new ArchiveConfig(
"powersync-sqlite-core.xcframework.zip",
"powersync-sqlite-core.xcframework"
);
await ProcessArchiveDownload(nativeDir, config, GITHUB_BASE_URL);
}
private void ExtractAarNativeLibraries(string aarPath, string nativeDir)
{
var extractedDir = Path.Combine(nativeDir, "temp_extracted");
try
{
// Clean up any existing extraction
if (Directory.Exists(extractedDir))
Directory.Delete(extractedDir, recursive: true);
// Extract AAR (which is a ZIP file)
ZipFile.ExtractToDirectory(aarPath, extractedDir);
// Copy native libraries organized by architecture
var jniLibsPath = Path.Combine(extractedDir, "jni");
if (Directory.Exists(jniLibsPath))
{
CopyNativeLibrariesByArchitecture(jniLibsPath, nativeDir);
}
}
finally
{
CleanupPaths(extractedDir, aarPath);
}
}
private static void CopyNativeLibrariesByArchitecture(string jniLibsPath, string nativeDir)
{
foreach (var archDir in Directory.GetDirectories(jniLibsPath))
{
var archName = Path.GetFileName(archDir);
var targetArchDir = Path.Combine(nativeDir, archName);
Directory.CreateDirectory(targetArchDir);
foreach (var libFile in Directory.GetFiles(archDir, "*.so"))
{
var targetPath = Path.Combine(targetArchDir, Path.GetFileName(libFile));
File.Copy(libFile, targetPath, overwrite: true);
}
}
}
private async Task ProcessArchiveDownload(string nativeDir, ArchiveConfig config, string baseUrl)
{
try
{
Directory.CreateDirectory(nativeDir);
var downloadPath = Path.Combine(nativeDir, config.ArchiveFileName);
var extractedPath = Path.Combine(nativeDir, config.ExtractedName);
var downloadUrl = $"{baseUrl}/{config.ArchiveFileName}";
await DownloadFile(downloadUrl, downloadPath);
// Clean up existing extraction
if (Directory.Exists(extractedPath))
Directory.Delete(extractedPath, recursive: true);
ExtractZipPreservingSymlinks(downloadPath, nativeDir);
File.Delete(downloadPath);
Console.WriteLine($"✓ Extracted {config.ArchiveFileName} → {config.ExtractedName}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"✗ Failed to process archive: {ex.Message}");
}
}
private static void ExtractZipPreservingSymlinks(string zipPath, string destDir)
{
// ZipFile.ExtractToDirectory does not preserve symlinks, which breaks
// macOS/Catalyst .xcframework bundles. Use `unzip` on Unix instead.
if (!OperatingSystem.IsWindows())
{
var proc = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "unzip",
ArgumentList = { "-o", zipPath, "-d", destDir },
RedirectStandardOutput = true,
RedirectStandardError = true,
})!;
proc.WaitForExit();
if (proc.ExitCode != 0)
throw new Exception($"unzip exited with code {proc.ExitCode}: {proc.StandardError.ReadToEnd()}");
}
else
{
ZipFile.ExtractToDirectory(zipPath, destDir);
}
}
private async Task DownloadFile(string url, string outputPath)
{
Console.WriteLine($"📥 Downloading: {Path.GetFileName(outputPath)}");
using var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"Download failed: {response.StatusCode} {response.ReasonPhrase}");
}
await using var fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.None);
await response.Content.CopyToAsync(fileStream);
}
private static void CleanupPaths(params string[] paths)
{
foreach (var path in paths)
{
try
{
if (File.Exists(path))
File.Delete(path);
else if (Directory.Exists(path))
Directory.Delete(path, recursive: true);
}
catch (Exception ex)
{
Console.WriteLine($"⚠️ Cleanup warning for {path}: {ex.Message}");
}
}
}
private record RuntimeConfig(string OriginalFileName, string FinalFileName);
private record ArchiveConfig(string ArchiveFileName, string ExtractedName);
}
public class Program
{
static async Task Main(string[] args)
{
var setup = new PowerSyncSetup();
await setup.RunSetup();
}
}