-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload.rs
More file actions
818 lines (626 loc) · 25.3 KB
/
Download.rs
File metadata and controls
818 lines (626 loc) · 25.3 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
//! ==============================================================================
//! Universal Sidecar Vendor - Rust Edition
//!
//! This program automates downloading and organizing full distributions of
//! various sidecar runtimes (like Node.js) for a Tauri application. It is a
//! Rust rewrite of the original shell script, enhanced with modern features.
//!
//! Key Features:
//! - Asynchronous, Concurrent Downloads: Leverages Tokio to download multiple
//! binaries in parallel, significantly speeding up the process.
//! - Intelligent Caching: Maintains a `Cache.json` file to track downloaded
//! versions. It automatically detects if a newer patch version is available
//! for a requested major version and updates the binary.
//! - Git LFS Management: Automatically creates or updates the
//! `.gitattributes` file to ensure large binaries are tracked by Git LFS.
//! - Extensible Design: Easily configured to support new sidecars, versions,
//! and platforms.
//! - Robust Error Handling: Uses `anyhow` for clear and concise error
//! reporting.
//! - Preserved File Structure: The final output directory structure remains
//! identical to the original script (`Architecture/SidecarName/Version`).
//!
//! ==============================================================================
// --- Type Definitions and Structs ---
/// Represents a single platform target for which binaries will be downloaded.
/// This struct holds all the necessary identifiers for a given platform.
#[derive(Clone, Debug)]
struct PlatformTarget {
/// The identifier used in the download URL (e.g., "win-x64",
/// "linux-arm64").
DownloadIdentifier:String,
/// The file extension of the archive (e.g., "zip", "tar.gz").
ArchiveExtension:String,
/// The official Tauri target triple for this platform (e.g.,
/// "x86_64-pc-windows-msvc").
TauriTargetTriple:String,
}
/// Defines the type of archive being handled, which determines the extraction
/// logic.
#[derive(Clone, Debug, PartialEq)]
enum ArchiveType {
Zip,
TarGz,
}
/// Represents a specific version of Node.js as returned by the official index.
/// Used for deserializing the JSON response from `nodejs.org`.
#[derive(Deserialize, Debug)]
struct NodeVersionInfo {
version:String,
}
/// Contains all the necessary information to perform a single download and
/// installation task. An instance of this struct is created for each binary
/// that needs to be downloaded.
#[derive(Clone, Debug)]
struct DownloadTask {
/// The name of the sidecar (e.g., "NODE").
SidecarName:String,
/// The major version string requested (e.g., "24").
MajorVersion:String,
/// The full, resolved version string (e.g., "v24.0.0").
FullVersion:String,
/// The complete URL to download the archive from.
DownloadURL:String,
/// The directory where temporary folders for this task will be created.
TempParentDirectory:PathBuf,
/// The final destination directory for the extracted binaries.
DestinationDirectory:PathBuf,
/// The type of archive to be downloaded.
ArchiveType:ArchiveType,
/// The name of the root folder inside the archive once extracted.
ExtractedFolderName:String,
/// The Tauri target triple for this download task.
TauriTargetTriple:String,
}
/// Represents the structure of the `Cache.json` file.
/// It uses a HashMap to map a unique key (representing a specific
/// sidecar/version/platform) to the full version string that was last
/// downloaded.
#[derive(Serialize, Deserialize, Debug, Default)]
struct DownloadCache {
/// The core data structure for the cache.
/// Key: A unique string like "x86_64-pc-windows-msvc/NODE/24".
/// Value: The full version string, like "v24.0.0".
Entries:HashMap<String, String>,
}
impl DownloadCache {
/// Loads the cache from the `Cache.json` file in the base sidecar
/// directory. If the file doesn't exist, it returns a new, empty cache.
fn Load(CachePath:&Path) -> Self {
if !CachePath.exists() {
info!("Cache file not found. A new one will be created.");
return DownloadCache::default();
}
let FileContents = match fs::read_to_string(CachePath) {
Ok(Contents) => Contents,
Err(Error) => {
warn!("Failed to read cache file: {}. Starting with an empty cache.", Error);
return DownloadCache::default();
},
};
match serde_json::from_str(&FileContents) {
Ok(Cache) => {
info!("Successfully loaded download cache.");
Cache
},
Err(Error) => {
warn!("Failed to parse cache file: {}. Starting with an empty cache.", Error);
DownloadCache::default()
},
}
}
/// Saves the current state of the cache to the `Cache.json` file.
/// The JSON is pretty-printed with tabs for indentation.
/// Entries are sorted alphabetically by key for consistency.
fn Save(&self, CachePath:&Path) -> Result<()> {
// Create a BTreeMap to sort entries alphabetically by key
let SortedEntries:BTreeMap<_, _> = self.Entries.iter().collect();
// Create a temporary struct to hold the sorted entries for serialization
let CacheToSerialize = serde_json::json!({
"Entries": SortedEntries
});
// Create an in-memory buffer to write the serialized JSON to.
let mut Buffer = Vec::new();
// Create a formatter that uses a tab character for indentation.
let Formatter = serde_json::ser::PrettyFormatter::with_indent(b"\t");
// Create a serializer with our custom formatter.
let mut Serializer = serde_json::Serializer::with_formatter(&mut Buffer, Formatter);
// Serialize the sorted cache data into the buffer.
CacheToSerialize.serialize(&mut Serializer)?;
// Write the buffer's contents to the actual file on disk.
fs::write(CachePath, &Buffer)
.with_context(|| format!("Failed to write tab-formatted cache to {:?}", CachePath))?;
Ok(())
}
}
// --- Configuration ---
/// Returns the root directory where all sidecars will be stored.
/// This is determined dynamically by navigating up from the executable's
/// location and detecting the SideCar project root. It handles both:
/// - Standalone builds: `.../SideCar/Target/release/`
/// - Workspace builds: `.../workspace/Target/release/SideCar` (where the
/// workspace root contains multiple crates including Element/SideCar)
fn GetBaseSidecarDirectory() -> Result<PathBuf> {
// Get the full path to the currently running executable.
let CurrentExePath = env::current_exe().context("Failed to get the path of the current executable.")?;
// Start from the directory containing the executable and walk up the tree.
let mut CurrentDir = CurrentExePath
.parent()
.context("Executable must be in a directory (not the root).")?;
loop {
// Check A: Does Source/Library.rs exist in current directory? → return current
// directory
let LibraryRsPath = CurrentDir.join("Source").join("Library.rs");
if LibraryRsPath.exists() {
return Ok(CurrentDir.to_path_buf());
}
// Check B: Does a Cargo.toml exist in current directory with package.name =
// "SideCar"? → return current directory
let CargoTomlPath = CurrentDir.join("Cargo.toml");
if CargoTomlPath.exists() {
if let Ok(CargoContents) = fs::read_to_string(&CargoTomlPath) {
if let Ok(Toml) = toml::from_str::<toml::Value>(&CargoContents) {
if let Some(Package) = Toml.get("package") {
if let Some(PackageName) = Package.get("name").and_then(|v| v.as_str()) {
if PackageName == "SideCar" {
// Verify that Source subdirectory exists as additional confirmation.
let SourceDir = CurrentDir.join("Source");
if SourceDir.exists() {
return Ok(CurrentDir.to_path_buf());
}
}
}
}
}
}
}
// Check C: Does Element/SideCar/Cargo.toml exist relative to current directory
// AND does it have package.name = "SideCar"? → return Element/SideCar
// subdirectory path
let SubdirCargoTomlPath = CurrentDir.join("Element").join("SideCar").join("Cargo.toml");
if SubdirCargoTomlPath.exists() {
if let Ok(CargoContents) = fs::read_to_string(&SubdirCargoTomlPath) {
if let Ok(Toml) = toml::from_str::<toml::Value>(&CargoContents) {
if let Some(Package) = Toml.get("package") {
if let Some(PackageName) = Package.get("name").and_then(|v| v.as_str()) {
if PackageName == "SideCar" {
// Verify that the Element/SideCar/Source subdirectory exists.
let SourceDir = CurrentDir.join("Element").join("SideCar").join("Source");
if SourceDir.exists() {
// Return the full path to the Element/SideCar subdirectory.
return Ok(CurrentDir.join("Element").join("SideCar"));
}
}
}
}
}
}
}
// Move up one level.
let NextDir = match CurrentDir.parent() {
Some(Parent) => Parent,
None => break, // Reached filesystem root without finding the project
};
CurrentDir = NextDir;
}
Err(anyhow!(
"Could not determine the SideCar base directory. The executable should be built from within the SideCar crate \
or from the workspace containing Element/SideCar. Searched up from: {}",
CurrentExePath.display()
))
}
/// Defines the matrix of platforms to target. Each entry specifies how to
/// download and identify binaries for a specific architecture.
fn GetPlatformMatrix() -> Vec<PlatformTarget> {
vec![
PlatformTarget {
DownloadIdentifier:"win-x64".to_string(),
ArchiveExtension:"zip".to_string(),
TauriTargetTriple:"x86_64-pc-windows-msvc".to_string(),
},
PlatformTarget {
DownloadIdentifier:"linux-x64".to_string(),
ArchiveExtension:"tar.gz".to_string(),
TauriTargetTriple:"x86_64-unknown-linux-gnu".to_string(),
},
PlatformTarget {
DownloadIdentifier:"linux-arm64".to_string(),
ArchiveExtension:"tar.gz".to_string(),
TauriTargetTriple:"aarch64-unknown-linux-gnu".to_string(),
},
PlatformTarget {
DownloadIdentifier:"darwin-x64".to_string(),
ArchiveExtension:"tar.gz".to_string(),
TauriTargetTriple:"x86_64-apple-darwin".to_string(),
},
PlatformTarget {
DownloadIdentifier:"darwin-arm64".to_string(),
ArchiveExtension:"tar.gz".to_string(),
TauriTargetTriple:"aarch64-apple-darwin".to_string(),
},
]
}
/// Defines which sidecars and versions to fetch. This structure makes it
/// easy to add more sidecars like Deno in the future.
fn GetSidecarsToFetch() -> HashMap<String, Vec<String>> {
let mut Sidecars = HashMap::new();
Sidecars.insert(
"NODE".to_string(),
vec!["24", "23", "22", "21", "20", "19", "18", "17", "16"]
.into_iter()
.map(String::from)
.collect(),
);
Sidecars
}
// --- Helper Functions ---
/// Environment variable for setting the log level.
pub const LogEnv:&str = "RUST_LOG";
/// Manages the `.gitattributes` file to ensure binaries are tracked by Git LFS.
/// If the file does not exist, it is created. If it exists, missing rules are
/// appended.
fn UpdateGitattributes(BaseDirectory:&Path) -> Result<()> {
const GITATTRIBUTES_HEADER:&str = r#"################################################################################
# Git LFS configuration for vendored Tauri Sidecars
#
# This file tells Git to use LFS (Large File Storage) for the heavy binary
# files and modules downloaded by the sidecar vendoring script. This keeps the
# main repository history small and fast.
#
# The `-text` attribute is used to prevent Git from normalizing line endings,
# which is critical for binary files and scripts.
#
# This file is automatically managed by the sidecar vendor script.
################################################################################
# --- Rule Definitions ---"#;
const GITATTRIBUTES_RULES:&[&str] = &[
"**/NODE/**/bin/node filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/node.exe filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/bin/npm filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/bin/npx filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/bin/corepack filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/npm filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/npm.cmd filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/npx filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/npx.cmd filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/corepack filter=lfs diff=lfs merge=lfs -text",
"**/NODE/**/corepack.cmd filter=lfs diff=lfs merge=lfs -text",
"",
"# --- Rules for the SideCar build artifacts ---",
"",
"Target/debug/*.exe filter=lfs diff=lfs merge=lfs -text",
"Target/release/*.exe filter=lfs diff=lfs merge=lfs -text",
"",
"Target/debug/SideCar filter=lfs diff=lfs merge=lfs -text",
"Target/release/SideCar filter=lfs diff=lfs merge=lfs -text",
"",
"Target/debug/Download filter=lfs diff=lfs merge=lfs -text",
"Target/release/Download filter=lfs diff=lfs merge=lfs -text",
];
let GitattributesPath = BaseDirectory.join(".gitattributes");
if !GitattributesPath.exists() {
info!("Creating .gitattributes file to track binaries with Git LFS.");
let mut File = File::create(&GitattributesPath)
.with_context(|| format!("Failed to create .gitattributes file at {:?}", GitattributesPath))?;
writeln!(File, "{}", GITATTRIBUTES_HEADER)?;
for Rule in GITATTRIBUTES_RULES {
// This will write a blank line for any empty strings in the array
writeln!(File, "{}", Rule)?;
}
} else {
info!(".gitattributes file found. Verifying LFS rules...");
let Content = fs::read_to_string(&GitattributesPath)?;
let MissingRules:Vec<_> = GITATTRIBUTES_RULES
.iter()
// Filter out blank lines and comments from the check
.filter(|rule| !rule.is_empty() && !rule.starts_with('#'))
.filter(|rule| !Content.contains(*rule))
.collect();
if !MissingRules.is_empty() {
info!("Adding {} missing LFS rules to .gitattributes.", MissingRules.len());
let mut File = fs::OpenOptions::new()
.append(true)
.open(&GitattributesPath)
.with_context(|| format!("Failed to open .gitattributes for appending at {:?}", GitattributesPath))?;
writeln!(File, "\n\n# --- Rules Automatically Added by Vendor Script ---")?;
for Rule in MissingRules {
writeln!(File, "{}", Rule)?;
}
} else {
info!(".gitattributes is already up to date.");
}
}
Ok(())
}
// --- Core Logic ---
/// Fetches the official Node.js versions index from nodejs.org.
async fn FetchNodeVersions(Client:&Client) -> Result<Vec<NodeVersionInfo>> {
info!("Fetching Node.js version index for resolving versions...");
let Response = Client
.get("https://nodejs.org/dist/index.json")
.send()
.await
.context("Failed to send request to Node.js version index.")?;
if !Response.status().is_success() {
return Err(anyhow!("Received non-success status from Node.js index: {}", Response.status()));
}
let Versions = Response
.json::<Vec<NodeVersionInfo>>()
.await
.context("Failed to parse Node.js version index JSON.")?;
Ok(Versions)
}
/// Resolves a major version string (e.g., "22") to the latest full patch
/// version (e.g., "v22.3.0") using the fetched version index.
fn ResolveLatestPatchVersion(MajorVersion:&str, AllVersions:&[NodeVersionInfo]) -> Option<String> {
let VersionPrefix = format!("v{}.", MajorVersion);
AllVersions
.iter()
.find(|v| v.version.starts_with(&VersionPrefix))
.map(|v| v.version.clone())
}
/// Downloads a file from a URL to a specified path.
async fn DownloadFile(Client:&Client, URL:&str, DestinationPath:&Path) -> Result<()> {
let mut Response = Client.get(URL).send().await?.error_for_status()?;
let mut DestinationFile =
File::create(DestinationPath).with_context(|| format!("Failed to create file at {:?}", DestinationPath))?;
// Stream the download to handle large files without high memory usage.
while let Some(Chunk) = Response.chunk().await? {
DestinationFile.write_all(&Chunk)?;
}
Ok(())
}
/// Extracts the contents of a downloaded archive to a target directory.
/// This function now performs a full extraction to ensure a complete
/// distribution.
fn ExtractArchive(ArchiveType:&ArchiveType, ArchivePath:&Path, ExtractionDirectory:&Path) -> Result<()> {
info!("Performing a full extraction of the archive...");
match ArchiveType {
ArchiveType::Zip => {
let File = File::open(ArchivePath)?;
let mut Archive = zip::ZipArchive::new(File)?;
Archive.extract(ExtractionDirectory)?;
},
ArchiveType::TarGz => {
let File = File::open(ArchivePath)?;
let Decompressor = flate2::read::GzDecoder::new(File);
let mut Archive = tar::Archive::new(Decompressor);
Archive.unpack(ExtractionDirectory)?;
},
}
Ok(())
}
/// The main asynchronous function for processing a single download task.
/// This function is designed to be run concurrently for multiple tasks.
async fn ProcessDownloadTask(Task:DownloadTask, Client:Client, Cache:Arc<Mutex<DownloadCache>>) -> Result<()> {
// Create the temporary directory inside the designated "Temporary" subfolder.
let TempDirectory = Builder::new()
.prefix("SideCar-Download-")
.tempdir_in(&Task.TempParentDirectory)
.context("Failed to create temporary directory.")?;
let ArchiveName = Task.DownloadURL.split('/').last().unwrap_or("Download.tmp");
let ArchivePath = TempDirectory.path().join(ArchiveName);
info!(
" [{}/{}] Downloading from: {}",
Task.TauriTargetTriple, Task.SidecarName, Task.DownloadURL
);
if let Err(Error) = DownloadFile(&Client, &Task.DownloadURL, &ArchivePath).await {
error!(
" [{}/{}] Failed to download {}: {}",
Task.TauriTargetTriple, Task.SidecarName, ArchiveName, Error
);
return Err(Error.into());
}
info!(" [{}/{}] Extracting archive...", Task.TauriTargetTriple, Task.SidecarName);
if let Err(Error) = ExtractArchive(&Task.ArchiveType, &ArchivePath, TempDirectory.path()) {
error!(
" [{}/{}] Failed to extract {}: {}",
Task.TauriTargetTriple, Task.SidecarName, ArchiveName, Error
);
return Err(Error.into());
}
let ExtractedPath = TempDirectory.path().join(&Task.ExtractedFolderName);
if !ExtractedPath.exists() {
let ErrorMessage = format!(" Could not find extracted folder: {:?}", ExtractedPath);
error!("{}", ErrorMessage);
return Err(anyhow!(ErrorMessage));
}
// If the destination directory already exists, remove it.
if Task.DestinationDirectory.exists() {
info!(" Removing old version at: {:?}", Task.DestinationDirectory);
fs::remove_dir_all(&Task.DestinationDirectory)?;
}
// Ensure the parent of the final destination exists.
if let Some(Parent) = Task.DestinationDirectory.parent() {
fs::create_dir_all(Parent)?;
}
info!(" Installing to: {:?}", Task.DestinationDirectory);
fs::rename(&ExtractedPath, &Task.DestinationDirectory).with_context(|| {
format!(
"Failed to rename/move extracted directory from {:?} to {:?}",
ExtractedPath, Task.DestinationDirectory
)
})?;
// Update the cache with the new version.
let CacheKey = format!("{}/{}/{}", Task.TauriTargetTriple, Task.SidecarName, Task.MajorVersion);
let mut LockedCache = Cache.lock().unwrap();
LockedCache.Entries.insert(CacheKey, Task.FullVersion.clone());
info!(
" v{} ({}) for '{}' is now up to date.",
Task.MajorVersion, Task.FullVersion, Task.TauriTargetTriple
);
Ok(())
}
/// Sets up the global logger for the application.
pub fn Logger() {
let LevelText = env::var(LogEnv).unwrap_or_else(|_| "info".to_string());
let LogLevel = LevelText.parse::<LevelFilter>().unwrap_or(LevelFilter::Info);
env_logger::Builder::new()
.filter_level(LogLevel)
.format(|Buffer, Record| {
let LevelStyle = match Record.level() {
log::Level::Error => "ERROR".red().bold(),
log::Level::Warn => "WARN".yellow().bold(),
log::Level::Info => "INFO".green(),
log::Level::Debug => "DEBUG".blue(),
log::Level::Trace => "TRACE".magenta(),
};
writeln!(Buffer, "[{}] [{}]: {}", "Download".red(), LevelStyle, Record.args())
})
.parse_default_env()
.init();
}
#[tokio::main]
pub async fn Fn() -> Result<()> {
Logger();
info!("Starting Universal Sidecar vendoring process...");
// --- Setup ---
let BaseSidecarDirectory = GetBaseSidecarDirectory()?;
// Manage the .gitattributes file for Git LFS.
UpdateGitattributes(&BaseSidecarDirectory)?;
// Define and create the dedicated directory for temporary downloads.
let TempDownloadsDirectory = BaseSidecarDirectory.join("Temporary");
fs::create_dir_all(&TempDownloadsDirectory)
.with_context(|| format!("Failed to create temporary directory at {:?}", TempDownloadsDirectory))?;
let CachePath = BaseSidecarDirectory.join("Cache.json");
let Cache = Arc::new(Mutex::new(DownloadCache::Load(&CachePath)));
let HttpClient = Client::new();
let PlatformMatrix = GetPlatformMatrix();
let SidecarsToFetch = GetSidecarsToFetch();
// Fetch Node versions once to be used by all tasks.
let NodeVersions = FetchNodeVersions(&HttpClient).await?;
let mut TasksToRun = Vec::new();
// --- Task Generation Phase (Sequential) ---
// First, we determine which downloads are necessary by checking the cache.
for Platform in &PlatformMatrix {
info!("--- Processing architecture: '{}' ---", Platform.TauriTargetTriple);
for (SidecarName, MajorVersions) in &SidecarsToFetch {
info!(" -> Processing sidecar: '{}'", SidecarName);
for MajorVersion in MajorVersions {
let DestinationDirectory = BaseSidecarDirectory
.join(&Platform.TauriTargetTriple)
.join(SidecarName)
.join(MajorVersion);
// --- Sidecar-Specific Download Logic ---
if SidecarName == "NODE" {
let FullVersion = match ResolveLatestPatchVersion(MajorVersion, &NodeVersions) {
Some(Version) => Version,
None => {
warn!(
" Could not resolve a specific version for Node.js v{}. Skipping.",
MajorVersion
);
continue;
},
};
// Check cache to see if we need to download/update.
let CacheKey = format!("{}/{}/{}", &Platform.TauriTargetTriple, SidecarName, MajorVersion);
let CachedVersion = Cache.lock().unwrap().Entries.get(&CacheKey).cloned();
if Some(FullVersion.clone()) == CachedVersion {
info!(" v{} ({}) is already up to date, skipping.", MajorVersion, FullVersion);
continue;
}
if CachedVersion.is_some() {
info!(
" Found newer patch for v{}: {} -> {}. Scheduling update.",
MajorVersion,
CachedVersion.unwrap(),
FullVersion
);
} else {
info!(" Processing v{} (resolved to {})...", MajorVersion, FullVersion);
}
let ArchiveExtension = &Platform.ArchiveExtension;
let ArchiveName =
format!("node-{}-{}.{}", FullVersion, Platform.DownloadIdentifier, ArchiveExtension);
let DownloadURL = format!("https://nodejs.org/dist/{}/{}", FullVersion, ArchiveName);
let ExtractedFolderName = format!("node-{}-{}", FullVersion, Platform.DownloadIdentifier);
let Task = DownloadTask {
SidecarName:SidecarName.clone(),
MajorVersion:MajorVersion.clone(),
FullVersion,
DownloadURL,
TempParentDirectory:TempDownloadsDirectory.clone(),
DestinationDirectory,
ArchiveType:if ArchiveExtension == "zip" { ArchiveType::Zip } else { ArchiveType::TarGz },
ExtractedFolderName,
TauriTargetTriple:Platform.TauriTargetTriple.clone(),
};
TasksToRun.push(Task);
}
// To add Deno, you would add an `else if SidecarName == "DENO"`
// block here.
}
}
}
// --- Concurrent Execution Phase ---
if TasksToRun.is_empty() {
info!("All sidecar binaries are already up to date.");
} else {
info!("Found {} tasks to run. Starting concurrent downloads...", TasksToRun.len());
// Limit to 8 concurrent jobs or num CPUs, whichever is smaller.
let NumberOfConcurrentJobs = num_cpus::get().min(8);
// Spawn a Tokio task for each download.
// Run tasks concurrently.
let Results = stream::iter(TasksToRun)
.map(|Task| {
let Client = HttpClient.clone();
let Cache = Arc::clone(&Cache);
tokio::spawn(async move { ProcessDownloadTask(Task, Client, Cache).await })
})
.buffer_unordered(NumberOfConcurrentJobs)
.collect::<Vec<_>>()
.await;
// Check for any errors that occurred during the concurrent tasks.
let mut ErrorsEncountered = 0;
for Result in Results {
// The first result is from tokio::spawn, the second from our function
if let Err(JoinError) = Result {
error!("A download task panicked or was cancelled: {}", JoinError);
ErrorsEncountered += 1;
} else if let Ok(Err(AppError)) = Result {
// We already logged the error inside `ProcessDownloadTask`, so just count it.
// Re-logging here to ensure it's captured at a higher level if needed.
error!("A download task failed: {}", AppError);
ErrorsEncountered += 1;
}
}
if ErrorsEncountered > 0 {
error!("Completed with {} errors.", ErrorsEncountered);
}
}
// --- Finalization ---
info!("Saving updated cache...");
Cache.lock().unwrap().Save(&CachePath)?;
info!("All sidecar binaries have been successfully processed and organized.");
Ok(())
}
/// Main executable function.
#[allow(unused)]
fn main() {
// We use a block here to handle the Result from Fn.
if let Err(Error) = Fn() {
// The logger should already be initialized by Fn, so we can use it.
error!("The application encountered a fatal error: {}", Error);
std::process::exit(1);
}
}
// --- Imports ---
use std::{
collections::{BTreeMap, HashMap},
env,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
sync::{Arc, Mutex},
};
use anyhow::{Context, Result, anyhow};
use colored::*;
use futures::stream::{self, StreamExt};
use log::{LevelFilter, error, info, warn};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tempfile::Builder;
use toml;