Skip to content

Commit b8690ef

Browse files
committed
Extract to function cargo_manifest_and_lock
1 parent 202a782 commit b8690ef

File tree

3 files changed

+25
-48
lines changed

3 files changed

+25
-48
lines changed

crates/tauri-cli/src/helpers/cargo_manifest.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::{
1010
path::{Path, PathBuf},
1111
};
1212

13+
use crate::interface::rust::get_workspace_dir;
14+
1315
#[derive(Clone, Deserialize)]
1416
pub struct CargoLockPackage {
1517
pub name: String,
@@ -49,6 +51,18 @@ pub struct CargoManifest {
4951
pub dependencies: HashMap<String, CargoManifestDependency>,
5052
}
5153

54+
pub fn cargo_manifest_and_lock(tauri_dir: &Path) -> (Option<CargoManifest>, Option<CargoLock>) {
55+
let manifest: Option<CargoManifest> = fs::read_to_string(tauri_dir.join("Cargo.toml"))
56+
.ok()
57+
.and_then(|manifest_contents| toml::from_str(&manifest_contents).ok());
58+
59+
let lock: Option<CargoLock> = get_workspace_dir()
60+
.ok()
61+
.and_then(|p| fs::read_to_string(p.join("Cargo.lock")).ok())
62+
.and_then(|s| toml::from_str(&s).ok());
63+
64+
(manifest, lock)
65+
}
5266
#[derive(Default)]
5367
pub struct CrateVersion {
5468
pub version: Option<String>,

crates/tauri-cli/src/info/packages_rust.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,18 @@
33
// SPDX-License-Identifier: MIT
44

55
use super::{ActionResult, SectionItem};
6-
use crate::{
7-
helpers::cargo_manifest::{
8-
crate_latest_version, crate_version, CargoLock, CargoManifest, CrateVersion,
9-
},
10-
interface::rust::get_workspace_dir,
6+
use crate::helpers::cargo_manifest::{
7+
cargo_manifest_and_lock, crate_latest_version, crate_version, CrateVersion,
118
};
129
use colored::Colorize;
13-
use std::fs::read_to_string;
1410
use std::path::{Path, PathBuf};
1511

1612
pub fn items(frontend_dir: Option<&PathBuf>, tauri_dir: Option<&Path>) -> Vec<SectionItem> {
1713
let mut items = Vec::new();
1814

1915
if tauri_dir.is_some() || frontend_dir.is_some() {
2016
if let Some(tauri_dir) = tauri_dir {
21-
let manifest: Option<CargoManifest> =
22-
if let Ok(manifest_contents) = read_to_string(tauri_dir.join("Cargo.toml")) {
23-
toml::from_str(&manifest_contents).ok()
24-
} else {
25-
None
26-
};
27-
let lock: Option<CargoLock> = get_workspace_dir()
28-
.ok()
29-
.and_then(|p| read_to_string(p.join("Cargo.lock")).ok())
30-
.and_then(|s| toml::from_str(&s).ok());
31-
17+
let (manifest, lock) = cargo_manifest_and_lock(tauri_dir);
3218
for dep in ["tauri", "tauri-build", "wry", "tao"] {
3319
let crate_version = crate_version(tauri_dir, manifest.as_ref(), lock.as_ref(), dep);
3420
let item = rust_section_item(dep, crate_version);

crates/tauri-cli/src/info/plugins.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
use std::{
66
collections::HashMap,
7-
fs, iter,
7+
iter,
88
path::{Path, PathBuf},
99
};
1010

11-
use crate::{
12-
helpers::{
13-
self,
14-
cargo_manifest::{crate_version, CargoLock, CargoManifest},
15-
npm::PackageManager,
16-
},
17-
interface::rust::get_workspace_dir,
11+
use crate::helpers::{
12+
self,
13+
cargo_manifest::{cargo_manifest_and_lock, crate_version},
14+
npm::PackageManager,
1815
};
1916

2017
use super::{packages_nodejs, packages_rust, SectionItem};
@@ -48,18 +45,6 @@ pub fn installed_tauri_packages(
4845
tauri_dir: &Path,
4946
package_manager: PackageManager,
5047
) -> InstalledPackages {
51-
let manifest: Option<CargoManifest> =
52-
if let Ok(manifest_contents) = fs::read_to_string(tauri_dir.join("Cargo.toml")) {
53-
toml::from_str(&manifest_contents).ok()
54-
} else {
55-
None
56-
};
57-
58-
let lock: Option<CargoLock> = get_workspace_dir()
59-
.ok()
60-
.and_then(|p| fs::read_to_string(p.join("Cargo.lock")).ok())
61-
.and_then(|s| toml::from_str(&s).ok());
62-
6348
let know_plugins = helpers::plugins::known_plugins();
6449
let crate_names: Vec<String> = iter::once("tauri".to_owned())
6550
.chain(
@@ -76,6 +61,8 @@ pub fn installed_tauri_packages(
7661
)
7762
.collect();
7863

64+
let (manifest, lock) = cargo_manifest_and_lock(tauri_dir);
65+
7966
let mut rust_plugins: HashMap<String, semver::Version> = crate_names
8067
.iter()
8168
.filter_map(|crate_name| {
@@ -121,17 +108,7 @@ pub fn items(
121108

122109
if tauri_dir.is_some() || frontend_dir.is_some() {
123110
if let Some(tauri_dir) = tauri_dir {
124-
let manifest: Option<CargoManifest> =
125-
if let Ok(manifest_contents) = fs::read_to_string(tauri_dir.join("Cargo.toml")) {
126-
toml::from_str(&manifest_contents).ok()
127-
} else {
128-
None
129-
};
130-
131-
let lock: Option<CargoLock> = get_workspace_dir()
132-
.ok()
133-
.and_then(|p| fs::read_to_string(p.join("Cargo.lock")).ok())
134-
.and_then(|s| toml::from_str(&s).ok());
111+
let (manifest, lock) = cargo_manifest_and_lock(tauri_dir);
135112

136113
for p in helpers::plugins::known_plugins().keys() {
137114
let dep = format!("tauri-plugin-{p}");

0 commit comments

Comments
 (0)