Skip to content

Commit d6fd0a8

Browse files
committed
style: minor function naming changes
1 parent ef68dc7 commit d6fd0a8

File tree

6 files changed

+98
-26
lines changed

6 files changed

+98
-26
lines changed

src/commands/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use flintpkg::{
99
build::build,
1010
chunks::{utils::clean_unused, verify_all_chunks},
1111
repo::{PackageManifest, get_package, read_manifest},
12-
run::{install, start},
12+
run::{install_package, start},
1313
utils::{resolve_package, resolve_repo},
1414
};
1515

@@ -48,7 +48,7 @@ pub async fn install_cmd(
4848
}
4949
};
5050

51-
install(&target_repo_path, package_id, chunk_store_path).await?;
51+
install_package(&target_repo_path, package_id, chunk_store_path).await?;
5252

5353
Ok(())
5454
}
@@ -139,7 +139,7 @@ pub async fn run_cmd(
139139
.join("install.meta")
140140
.exists()
141141
{
142-
install(&target_repo_path, &package, chunk_store_path)
142+
install_package(&target_repo_path, &package, chunk_store_path)
143143
.await
144144
.with_context(|| "Failed to install package.")?;
145145
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ async fn update_all_repos(base_path: &Path, chunk_store_path: &Path) -> Result<(
203203
get_all_installed_packages, get_package, network::update_repository, read_manifest,
204204
remove_package,
205205
};
206-
use flintpkg::run::install;
206+
use flintpkg::run::install_package;
207207

208208
for entry in base_path.read_dir()? {
209209
let repo = entry?;
@@ -225,7 +225,7 @@ async fn update_all_repos(base_path: &Path, chunk_store_path: &Path) -> Result<(
225225
if installed_package != repo_package {
226226
updated_package(&repo_package);
227227

228-
install(&repo_path, &repo_package.id, chunk_store_path).await?;
228+
install_package(&repo_path, &repo_package.id, chunk_store_path).await?;
229229
}
230230
} else {
231231
remove_package(&installed_package.id, &repo_path, None)?;

src/repo/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod manifest;
22
mod manifest_io;
33
#[cfg(feature = "network")]
44
pub mod network;
5+
pub mod versions;
56
pub use manifest::*;
67
pub use manifest_io::{read_manifest, update_manifest};
78

src/repo/versions.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use anyhow::{Context, Result};
2+
use std::{fs, os::unix::fs::symlink, path::Path};
3+
4+
use crate::{
5+
chunks::{HashKind, hash::hash, load_tree},
6+
repo::{PackageManifest, get_package, read_manifest},
7+
};
8+
9+
fn hash_package(package_manifest: &PackageManifest, hash_kind: HashKind) -> Result<String> {
10+
let hash_str = serde_yaml::to_string(package_manifest)?;
11+
12+
Ok(hash(hash_kind, hash_str.as_bytes()))
13+
}
14+
15+
/// Installs the latest version of a package, assumes all chunks are available.
16+
/// It is recommended you call `autoclean_versions` after.
17+
///
18+
/// # Errors
19+
///
20+
/// - Filesystem errors (Out of space, Permissions)
21+
/// - Invalid Repository/Package manifest
22+
pub fn install_version(repo_path: &Path, package_id: &str, chunk_store_path: &Path) -> Result<()> {
23+
let repo_manifest = read_manifest(repo_path)?;
24+
25+
let package_manifest = get_package(&repo_manifest, package_id)
26+
.with_context(|| "Failed to get package from Repository.")?;
27+
let package_hash = hash_package(&package_manifest, repo_manifest.hash_kind)?;
28+
let installed_path = &repo_path
29+
.join("versions")
30+
.join(format!("{}-{}", package_manifest.id, package_hash));
31+
32+
load_tree(installed_path, chunk_store_path, &package_manifest.chunks)
33+
.with_context(|| "Failed to rebuild the tree.")?;
34+
35+
fs::write(
36+
installed_path.join("install.meta"),
37+
serde_yaml::to_string(&package_manifest)?,
38+
)?;
39+
40+
Ok(())
41+
}
42+
43+
pub fn switch_version(repo_path: &Path, hash: String, package_id: &str) -> Result<()> {
44+
let target_parent_path = repo_path.join("installed");
45+
let target_path = target_parent_path.join(package_id);
46+
symlink(format!("../versions/{package_id}-{hash}"), target_path)?;
47+
48+
Ok(())
49+
}
50+
51+
/// Gets all versions for the `package_id`
52+
pub fn get_versions(repo_path: &Path, package_id: &str) -> Result<Vec<String>> {
53+
let mut versions = Vec::new();
54+
55+
for entry in repo_path.join("versions").read_dir()? {
56+
if let Ok(entry) = entry
57+
&& let Ok(file_name) = entry.file_name().into_string()
58+
{
59+
let split: Vec<&str> = file_name.split('-').collect();
60+
61+
if let Some((version_hash, file_name_split)) = split.split_last()
62+
&& file_name_split.join("-") == package_id
63+
{
64+
versions.push((*version_hash).to_string());
65+
}
66+
}
67+
}
68+
69+
Ok(versions)
70+
}
71+
72+
/// Removes a version of a package.
73+
pub fn remove_version(repo_path: &Path, hash: String, package_id: &str) -> Result<()> {
74+
let path = repo_path.join(format!("versions/{}-{}", package_id, hash));
75+
fs::remove_dir_all(path)?;
76+
77+
Ok(())
78+
}

src/run/mod.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ use anyhow::{Context, Result, bail};
44
use std::{
55
collections::HashMap,
66
ffi::OsStr,
7-
fs,
87
path::{Path, PathBuf},
98
process::{Command, ExitStatus},
109
};
1110

1211
#[cfg(feature = "network")]
1312
use crate::chunks::install_tree;
14-
use crate::{
15-
chunks::load_tree,
16-
repo::{self, PackageManifest, read_manifest},
17-
};
13+
use crate::repo::{PackageManifest, get_package, read_manifest, versions::install_version};
1814

1915
/// Starts a package from an entrypoint
2016
///
@@ -72,18 +68,23 @@ pub fn start<S: AsRef<OsStr>>(
7268
}
7369
}
7470

75-
/// Installs or Updates a Package.
71+
/// Installs the latest version of a package, assumes all chunks are available.
72+
/// Will automatically autoclean.
7673
///
7774
/// # Errors
7875
///
7976
/// - Filesystem errors (Out of space, Permissions)
8077
/// - Invalid Repository/Package manifest
81-
pub async fn install(repo_path: &Path, package_id: &str, chunk_store_path: &Path) -> Result<()> {
78+
/// - Network Errors (If network is enabled)
79+
pub async fn install_package(
80+
repo_path: &Path,
81+
package_id: &str,
82+
chunk_store_path: &Path,
83+
) -> Result<()> {
8284
let repo_manifest = read_manifest(repo_path)?;
8385

84-
let package_manifest = repo::get_package(&repo_manifest, package_id)
86+
let package_manifest = get_package(&repo_manifest, package_id)
8587
.with_context(|| "Failed to get package from Repository.")?;
86-
let installed_path = &repo_path.join("installed").join(&package_manifest.id);
8788

8889
// Get any chunks that are not installed
8990
#[cfg(feature = "network")]
@@ -96,15 +97,7 @@ pub async fn install(repo_path: &Path, package_id: &str, chunk_store_path: &Path
9697
.await
9798
.with_context(|| "Failed to install package.")?;
9899

99-
load_tree(installed_path, chunk_store_path, &package_manifest.chunks)
100-
.with_context(|| "Failed to rebuild the tree.")?;
101-
102-
fs::write(
103-
installed_path.join("install.meta"),
104-
serde_yaml::to_string(&package_manifest)?,
105-
)?;
106-
107-
Ok(())
100+
install_version(repo_path, package_id, chunk_store_path)
108101
}
109102

110103
#[cfg(test)]
@@ -154,7 +147,7 @@ mod tests {
154147
insert_package(&package, repo_path, Some(repo_path))?;
155148

156149
// Now install
157-
install(repo_path, "testpkg", chunks_path).await?;
150+
install_package(repo_path, "testpkg", chunks_path).await?;
158151

159152
// Check installed
160153
let installed_path = repo_path.join("installed/testpkg");

tests/full_workflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use temp_dir::TempDir;
55
use flintpkg::{
66
build::build,
77
repo::{self, get_installed_package},
8-
run::{install, start},
8+
run::{install_package, start},
99
};
1010

1111
#[tokio::test]
@@ -20,7 +20,7 @@ async fn full_workflow_test() -> Result<()> {
2020
let build_manifest_path = Path::new("build_manifest.yml");
2121
build(build_manifest_path, repo_path, None, chunks_path).await?;
2222

23-
install(repo_path, "example", chunks_path).await?;
23+
install_package(repo_path, "example", chunks_path).await?;
2424

2525
let manifest = get_installed_package(repo_path, "example")?;
2626

0 commit comments

Comments
 (0)