summaryrefslogtreecommitdiff
path: root/src/pkgtoolkit
diff options
context:
space:
mode:
authorNamilskyy <alive6863@gmail.com>2025-12-06 20:20:16 +0300
committerNamilskyy <alive6863@gmail.com>2025-12-06 20:20:16 +0300
commite7c49d685efe768c0c1d9c66da7b93b6e118b305 (patch)
treee1bfb33a34c3fe4cb84436e73b48463f5deb8f08 /src/pkgtoolkit
parentc8520a5cf071eedc966e57abaf4ba2a334df83ee (diff)
Fixed logical errors, tryed to fix tests
Diffstat (limited to 'src/pkgtoolkit')
-rw-r--r--src/pkgtoolkit/git_source.rs86
-rw-r--r--src/pkgtoolkit/index.rs1
-rw-r--r--src/pkgtoolkit/mod.rs5
-rw-r--r--src/pkgtoolkit/types.rs3
4 files changed, 95 insertions, 0 deletions
diff --git a/src/pkgtoolkit/git_source.rs b/src/pkgtoolkit/git_source.rs
new file mode 100644
index 0000000..2f33c14
--- /dev/null
+++ b/src/pkgtoolkit/git_source.rs
@@ -0,0 +1,86 @@
+use std::fs;
+use std::path::Path;
+use git2::Repository;
+use crate::pkgtoolkit::types::Package;
+use crate::cfg::config::Config;
+use toml;
+
+pub struct GitSource;
+
+impl GitSource {
+ /// Clone a git repository to a local directory
+ pub fn clone_repo(git_url: &str, target_dir: &Path) -> Result<(), git2::Error> {
+ Repository::clone(git_url, target_dir)?;
+ Ok(())
+ }
+
+ /// Get source code for a package from its git repository
+ pub fn get_package_source(package: &Package, config: &Config) -> Result<String, Box<dyn std::error::Error>> {
+ let git_repo = package.git_repo.as_ref()
+ .ok_or("Package does not have a git repository specified")?;
+
+ let source_dir = Path::new(&config.paths.cache_dir)
+ .join("sources")
+ .join(&package.name)
+ .join(&package.version);
+
+ fs::create_dir_all(&source_dir)?;
+
+ println!("Cloning {} from {}", package.name, git_repo);
+ Self::clone_repo(git_repo, &source_dir)?;
+
+ let source_path = source_dir.to_string_lossy().to_string();
+ println!("Source code downloaded to: {}", source_path);
+
+ Ok(source_path)
+ }
+
+ /// Get source code for a package by name, first checking INSTALL file, then falling back to index
+ pub fn get_source_by_name(pkg_name: &str, config: &Config) -> Result<String, Box<dyn std::error::Error>> {
+ // First try to get git_repo from INSTALL file in cache
+ let install_path = Path::new(&config.paths.cache_dir).join(format!("{}/INSTALL", pkg_name));
+
+ if install_path.exists() {
+ let install_content = fs::read_to_string(&install_path)?;
+ let install_data: crate::pkgtoolkit::types::Install = toml::from_str(&install_content)
+ .map_err(|e| format!("Failed to parse INSTALL file: {}", e))?;
+
+ // Check if InstallMeta has git_repo
+ if let Some(git_repo) = install_data.install.git_repo {
+ println!("Found git repository in INSTALL file: {}", git_repo);
+
+ let source_dir = Path::new(&config.paths.cache_dir)
+ .join("sources")
+ .join(&install_data.package.name)
+ .join(&install_data.package.version);
+
+ fs::create_dir_all(&source_dir)?;
+
+ println!("Cloning {} from {}", install_data.package.name, git_repo);
+ Self::clone_repo(&git_repo, &source_dir)?;
+
+ let source_path = source_dir.to_string_lossy().to_string();
+ println!("Source code downloaded to: {}", source_path);
+
+ return Ok(source_path);
+ }
+ }
+
+ // Fall back to index if INSTALL file doesn't exist or doesn't have git_repo
+ let index_path = Path::new(&config.paths.cache_dir).join("INDEX.toml");
+
+ if !index_path.exists() {
+ return Err("Index file not found. Please run 'mesk update' first.".into());
+ }
+
+ let index_content = fs::read_to_string(&index_path)?;
+ let index: crate::pkgtoolkit::types::Index = toml::from_str(&index_content)
+ .map_err(|e| format!("Failed to parse index: {}", e))?;
+
+ let package = index.packages.iter()
+ .find(|pkg| pkg.name == pkg_name)
+ .ok_or(format!("Package '{}' not found in index", pkg_name))?;
+
+ Self::get_package_source(package, config)
+ }
+}
diff --git a/src/pkgtoolkit/index.rs b/src/pkgtoolkit/index.rs
index 7b13533..78c37aa 100644
--- a/src/pkgtoolkit/index.rs
+++ b/src/pkgtoolkit/index.rs
@@ -95,6 +95,7 @@ impl IndexOperations for Package {
descr: Some(install_data.package.descr.unwrap_or_default()),
license: Some(install_data.package.license.unwrap_or_default()),
url: install_data.package.url,
+ git_repo: install_data.package.git_repo,
};
all_packages.push(pkg_for_index);
diff --git a/src/pkgtoolkit/mod.rs b/src/pkgtoolkit/mod.rs
index 2fc316a..1cadee3 100644
--- a/src/pkgtoolkit/mod.rs
+++ b/src/pkgtoolkit/mod.rs
@@ -1,6 +1,7 @@
// Core package toolkit modules
pub mod archive;
pub mod build;
+pub mod git_source;
pub mod index;
pub mod install;
pub mod types;
@@ -52,3 +53,7 @@ pub use install::InstallOperations;
// Index operations for package repository management
#[allow(unused_imports)]
pub use index::IndexOperations;
+
+// Git source operations for downloading package source code
+#[allow(unused_imports)]
+pub use git_source::GitSource;
diff --git a/src/pkgtoolkit/types.rs b/src/pkgtoolkit/types.rs
index 01a807a..83f47a3 100644
--- a/src/pkgtoolkit/types.rs
+++ b/src/pkgtoolkit/types.rs
@@ -17,6 +17,7 @@ pub struct Package {
pub descr: Option<String>,
pub license: Option<String>,
pub url: String,
+ pub git_repo: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
@@ -27,6 +28,8 @@ pub struct InstallMeta {
pub mode: String,
// Cancels the previous fields and installs them using the shell script
pub custom_script: Option<String>,
+ // Git repository URL for source code if needed
+ pub git_repo: Option<String>,
// pub files: Option<Vec<String>>,
}