Skip to content

Commit 66bc253

Browse files
authored
qt_minimal: allow for constructing from a PathBuf (#1422)
1 parent 61f6b60 commit 66bc253

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

crates/qt-build-utils/src/installation/qt_minimal/mod.rs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,48 @@ pub struct QtInstallationQtMinimal {
1818
version: semver::Version,
1919
}
2020

21+
impl TryFrom<PathBuf> for QtInstallationQtMinimal {
22+
type Error = anyhow::Error;
23+
24+
fn try_from(path_qt: PathBuf) -> Result<Self, Self::Error> {
25+
// Verify that the expected folders exist
26+
for folder in ["bin", "include", "lib", "libexec"] {
27+
if !path_qt.join(folder).exists() {
28+
return Err(anyhow::anyhow!(
29+
"Failed to find {folder} in Qt path: {}",
30+
path_qt.display()
31+
));
32+
}
33+
}
34+
35+
// Find qtpaths binary
36+
let Some(qtpaths) = ["bin", "libexec"]
37+
.into_iter()
38+
.map(|folder| {
39+
path_qt
40+
.join(folder)
41+
.join(crate::QtTool::QtPaths.binary_name())
42+
})
43+
.find(|path| path.exists())
44+
else {
45+
return Err(anyhow::anyhow!(
46+
"Failed to find qtpaths in Qt path: {}",
47+
path_qt.display()
48+
));
49+
};
50+
51+
// Determine the Qt version from qtpaths
52+
let version = semver::Version::parse(
53+
&crate::QtToolQtPaths::from_path_buf(qtpaths)
54+
.query("QT_VERSION")
55+
.expect("Could not query qtpaths for QT_VERSION"),
56+
)
57+
.expect("Could not parse Qt version");
58+
59+
Ok(Self { path_qt, version })
60+
}
61+
}
62+
2163
impl TryFrom<semver::Version> for QtInstallationQtMinimal {
2264
type Error = anyhow::Error;
2365

@@ -73,10 +115,7 @@ impl TryFrom<semver::Version> for QtInstallationQtMinimal {
73115
artifact_include.download_and_extract(&extract_target_dir);
74116
}
75117

76-
Ok(Self {
77-
path_qt: extract_target_dir.join("qt"),
78-
version,
79-
})
118+
Self::try_from(extract_target_dir.join("qt"))
80119
}
81120
}
82121

@@ -111,13 +150,11 @@ impl QtInstallation for QtInstallationQtMinimal {
111150

112151
fn try_find_tool(&self, tool: crate::QtTool) -> anyhow::Result<std::path::PathBuf> {
113152
// Tools could be either in libexec or bin
114-
let path_bin = self.path_qt.join("bin").join(tool.binary_name());
115-
let path_libexec = self.path_qt.join("libexec").join(tool.binary_name());
116-
117-
if path_bin.exists() {
118-
return Ok(path_bin);
119-
} else if path_libexec.exists() {
120-
return Ok(path_libexec);
153+
for folder in ["bin", "libexec"] {
154+
let path = self.path_qt.join(folder).join(tool.binary_name());
155+
if path.exists() {
156+
return Ok(path);
157+
}
121158
}
122159

123160
Err(anyhow::anyhow!(

crates/qt-build-utils/src/tool/qtpaths.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,11 @@ impl QtToolQtPaths {
8181
Some(String::from_utf8_lossy(&output).trim().to_owned())
8282
}
8383
}
84+
85+
#[cfg(feature = "qt_minimal")]
86+
impl QtToolQtPaths {
87+
/// Construct from an executable path this is used interally to allow for querying for the Qt version
88+
pub(crate) fn from_path_buf(executable: PathBuf) -> Self {
89+
Self { executable }
90+
}
91+
}

0 commit comments

Comments
 (0)