Skip to content

Commit 424a837

Browse files
committed
some code review on unix-only module
1 parent a84fa70 commit 424a837

1 file changed

Lines changed: 12 additions & 27 deletions

File tree

  • clang-installer/src/downloader/native_packages

clang-installer/src/downloader/native_packages/unix.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ impl Display for UnixPackageManager {
3636
}
3737
}
3838

39-
#[cfg(target_os = "linux")]
40-
impl crate::downloader::caching::Cacher for UnixPackageManager {}
41-
4239
impl UnixPackageManager {
4340
fn as_str(&self) -> &'static str {
4441
match self {
@@ -60,28 +57,15 @@ impl UnixPackageManager {
6057
match self {
6158
#[cfg(target_os = "linux")]
6259
UnixPackageManager::Apt | UnixPackageManager::Dnf => {
63-
if let Some(ver) = version {
64-
format!("{package_name}-{}", ver.major)
65-
} else {
66-
package_name.to_string()
67-
}
60+
version.map(|ver| format!("{package_name}-{}", ver.major))
6861
}
6962
#[cfg(target_os = "linux")]
70-
UnixPackageManager::PacMan => {
71-
if let Some(ver) = version {
72-
format!("{package_name}{}", ver.major)
73-
} else {
74-
package_name.to_string()
75-
}
76-
}
63+
UnixPackageManager::PacMan => version.map(|ver| format!("{package_name}{}", ver.major)),
7764
UnixPackageManager::Homebrew => {
78-
if let Some(ver) = version {
79-
format!("{package_name}@{}", ver.major)
80-
} else {
81-
package_name.to_string()
82-
}
65+
version.map(|ver| format!("{package_name}@{}", ver.major))
8366
}
8467
}
68+
.unwrap_or(package_name.to_string())
8569
}
8670
}
8771

@@ -160,13 +144,10 @@ impl PackageManager for UnixPackageManager {
160144
if matches!(self, UnixPackageManager::Apt)
161145
&& let Some(version) = version
162146
{
163-
use crate::downloader::caching::Cacher;
164-
165147
log::info!(
166148
"trying to install from official LLVM PPA repository (for Debian-based `apt` package manager)"
167149
);
168150
return llvm_apt_install::install_llvm_via_apt(
169-
Self::get_cache_dir().as_path(),
170151
version.major.to_string(),
171152
package_id.as_str(),
172153
)
@@ -216,24 +197,28 @@ impl PackageManager for UnixPackageManager {
216197
#[cfg(target_os = "linux")]
217198
mod llvm_apt_install {
218199
use crate::downloader::{
200+
caching::Cacher,
219201
chmod_file, download,
220202
native_packages::{PackageManagerError, unix::UnixPackageManager},
221203
};
222-
use std::{path::Path, process::Command};
204+
use std::{process::Command, time::Duration};
223205
use url::Url;
224206

207+
impl Cacher for UnixPackageManager {}
208+
225209
const LLVM_INSTALL_SCRIPT_URL: &str = "https://apt.llvm.org/llvm.sh";
226210

227211
/// Installs the official LLVM APT repository and its GPG key.
228212
///
229213
/// This is required to install specific versions of clang tools on Debian-based distributions using `apt`.}
230214
pub async fn install_llvm_via_apt(
231-
cache_path: &Path,
232215
ver_major: String,
233216
package_name: &str,
234217
) -> Result<(), PackageManagerError> {
235-
let download_path = cache_path.join("llvm_apt_install.sh");
236-
if !download_path.exists() {
218+
let download_path = UnixPackageManager::get_cache_dir().join("llvm_apt_install.sh");
219+
if !download_path.exists()
220+
|| !UnixPackageManager::is_cache_valid(&download_path, Some(Duration::from_hours(24)))
221+
{
237222
log::info!(
238223
"Downloading LLVM APT repository installation script from {LLVM_INSTALL_SCRIPT_URL}"
239224
);

0 commit comments

Comments
 (0)