|
3 | 3 | use anyhow::Result; |
4 | 4 | use postgresql_archive::configuration::{custom, theseus}; |
5 | 5 | use postgresql_archive::repository::github::repository::GitHub; |
6 | | -use postgresql_archive::{VersionReq, matcher}; |
| 6 | +use postgresql_archive::{ExactVersion, VersionReq, matcher}; |
7 | 7 | use postgresql_archive::{get_archive, repository}; |
| 8 | +use std::env::home_dir; |
8 | 9 | use std::fs::File; |
9 | 10 | use std::io::Write; |
10 | 11 | use std::path::PathBuf; |
@@ -53,7 +54,43 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> { |
53 | 54 | return Ok(()); |
54 | 55 | } |
55 | 56 |
|
56 | | - let (asset_version, archive) = get_archive(&releases_url, &version_req).await?; |
| 57 | + let (asset_version, archive); |
| 58 | + |
| 59 | + // Only works when exact version is specified and with the `bundled` feature |
| 60 | + // In runtime, the archive seems to be cached |
| 61 | + if cfg!(feature = "bundled") && let Some(exact_version) = version_req.exact_version() { |
| 62 | + println!("Using existing version: {exact_version:?}"); |
| 63 | + let ver = exact_version.to_string(); |
| 64 | + let target_os = if cfg!(target_os = "windows") { |
| 65 | + "windows" |
| 66 | + } else if cfg!(target_os = "linux") { |
| 67 | + "linux" |
| 68 | + } else { |
| 69 | + panic!("Unsupported target OS: only windows and linux are supported"); |
| 70 | + }; |
| 71 | + let cached_file_name = home_dir() |
| 72 | + .unwrap_or_else(|| env::current_dir().unwrap_or_default()) |
| 73 | + .join(".theseus") |
| 74 | + .join("postgresql") |
| 75 | + .join(format!("postgresql-{}-{}.tar.gz", ver, target_os)); |
| 76 | + |
| 77 | + println!( |
| 78 | + "Cached file name: {cached_file_name:?} - exists: {}", |
| 79 | + cached_file_name.exists() |
| 80 | + ); |
| 81 | + if !cached_file_name.is_file() { |
| 82 | + (asset_version, archive) = get_archive(&releases_url, &version_req).await?; |
| 83 | + fs::create_dir_all(cached_file_name.parent().unwrap())?; |
| 84 | + fs::write(&cached_file_name, &archive)?; |
| 85 | + println!("Cached PostgreSQL archive to: {cached_file_name:?}"); |
| 86 | + } else { |
| 87 | + asset_version = exact_version; |
| 88 | + archive = fs::read(&cached_file_name)?; |
| 89 | + println!("Using cached PostgreSQL archive: {cached_file_name:?}"); |
| 90 | + } |
| 91 | + } else { |
| 92 | + (asset_version, archive) = get_archive(&releases_url, &version_req).await?; |
| 93 | + } |
57 | 94 |
|
58 | 95 | fs::write(archive_version_file.clone(), asset_version.to_string())?; |
59 | 96 | let mut file = File::create(archive_file.clone())?; |
|
0 commit comments