Implement install package

This commit is contained in:
Federico Terzi 2019-09-26 18:21:27 +02:00
parent 76439e28fe
commit 6765702d4a
5 changed files with 127 additions and 12 deletions

View File

@ -47,6 +47,7 @@ use std::io::{BufReader, BufRead};
mod ui; mod ui;
mod event; mod event;
mod check; mod check;
mod utils;
mod bridge; mod bridge;
mod engine; mod engine;
mod config; mod config;

View File

@ -25,7 +25,7 @@ use std::io::BufReader;
use chrono::{NaiveDateTime, Timelike}; use chrono::{NaiveDateTime, Timelike};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use crate::package::UpdateResult::{NotOutdated, Updated}; use crate::package::UpdateResult::{NotOutdated, Updated};
use crate::package::InstallResult::{NotFound, AlreadyInstalled}; use crate::package::InstallResult::{NotFoundInIndex, AlreadyInstalled};
use std::fs; use std::fs;
use tempfile::TempDir; use tempfile::TempDir;
use git2::Repository; use git2::Repository;
@ -165,7 +165,7 @@ impl super::PackageManager for DefaultPackageManager {
self.install_package_from_repo(name, &package.repo) self.install_package_from_repo(name, &package.repo)
}, },
None => { None => {
Ok(NotFound) Ok(NotFoundInIndex)
}, },
} }
} }
@ -177,8 +177,16 @@ impl super::PackageManager for DefaultPackageManager {
return Ok(AlreadyInstalled); return Ok(AlreadyInstalled);
} }
// TODO: clone the repo, and copy the folder to the packages dir let temp_dir = Self::clone_repo_to_temp(repo_url)?;
unimplemented!()
let temp_package_dir = temp_dir.path().join(name);
if !temp_package_dir.exists() {
return Ok(InstallResult::NotFoundInRepo);
}
crate::utils::copy_dir_into(&temp_package_dir, &self.package_dir)?;
Ok(InstallResult::Installed)
} }
} }
@ -189,6 +197,7 @@ mod tests {
use std::path::Path; use std::path::Path;
use crate::package::PackageManager; use crate::package::PackageManager;
use std::fs::create_dir; use std::fs::create_dir;
use crate::package::InstallResult::{Installed, NotFoundInRepo};
const OUTDATED_INDEX_CONTENT : &str = include_str!("../res/test/outdated_index.json"); const OUTDATED_INDEX_CONTENT : &str = include_str!("../res/test/outdated_index.json");
const INDEX_CONTENT_WITHOUT_UPDATE: &str = include_str!("../res/test/index_without_update.json"); const INDEX_CONTENT_WITHOUT_UPDATE: &str = include_str!("../res/test/index_without_update.json");
@ -315,7 +324,7 @@ mod tests {
std::fs::write(index_file, INSTALL_PACKAGE_INDEX); std::fs::write(index_file, INSTALL_PACKAGE_INDEX);
}); });
assert_eq!(temp.package_manager.install_package("not-existing").unwrap(), NotFound); assert_eq!(temp.package_manager.install_package("doesnotexist").unwrap(), NotFoundInIndex);
} }
#[test] #[test]
@ -334,4 +343,27 @@ mod tests {
let cloned_dir = DefaultPackageManager::clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core").unwrap(); let cloned_dir = DefaultPackageManager::clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core").unwrap();
assert!(cloned_dir.path().join("LICENSE").exists()); assert!(cloned_dir.path().join("LICENSE").exists());
} }
#[test]
fn test_install_package() {
let mut temp = create_temp_package_manager(|_, data_dir| {
let index_file = data_dir.join(DEFAULT_PACKAGE_INDEX_FILE);
std::fs::write(index_file, INSTALL_PACKAGE_INDEX);
});
assert_eq!(temp.package_manager.install_package("dummy-package").unwrap(), Installed);
assert!(temp.package_dir.path().join("dummy-package").exists());
assert!(temp.package_dir.path().join("dummy-package/README.md").exists());
assert!(temp.package_dir.path().join("dummy-package/package.yml").exists());
}
#[test]
fn test_install_package_does_not_exist_in_repo() {
let mut temp = create_temp_package_manager(|_, data_dir| {
let index_file = data_dir.join(DEFAULT_PACKAGE_INDEX_FILE);
std::fs::write(index_file, INSTALL_PACKAGE_INDEX);
});
assert_eq!(temp.package_manager.install_package("not-existing").unwrap(), NotFoundInRepo);
}
} }

View File

@ -58,7 +58,8 @@ pub enum UpdateResult {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum InstallResult { pub enum InstallResult {
NotFound, NotFoundInIndex,
NotFoundInRepo,
AlreadyInstalled, AlreadyInstalled,
Installed Installed
} }

View File

@ -3,11 +3,11 @@
"packages": [ "packages": [
{ {
"name": "basic-emojis", "name": "dummy-package",
"title": "Basic Emojis", "title": "Dummy Package",
"version": "0.1.0", "version": "0.1.0",
"repo": "https://github.com/federico-terzi/espanso-hub-core", "repo": "https://github.com/federico-terzi/espanso-hub-core",
"desc": "A package to include some basic emojis in espanso.", "desc": "Dummy package",
"author": "Federico Terzi" "author": "Federico Terzi"
}, },
@ -21,7 +21,15 @@
"desc": "Include Italian accents substitutions to espanso.", "desc": "Include Italian accents substitutions to espanso.",
"author": "Federico Terzi" "author": "Federico Terzi"
},
{
"name": "not-existing",
"title": "Not Existing",
"version": "0.1.0",
"repo": "https://github.com/federico-terzi/espanso-hub-core",
"desc": "Package that does not exist in the repo",
"author": "Federico Terzi"
} }
]} ]}

73
src/utils.rs Normal file
View File

@ -0,0 +1,73 @@
use std::path::Path;
use std::error::Error;
use walkdir::WalkDir;
use std::fs::create_dir;
pub fn copy_dir_into(source_dir: &Path, dest_dir: &Path) -> Result<(), Box<dyn Error>> {
// Create source directory in dest dir
let name = source_dir.file_name().expect("Error obtaining the filename");
let target_dir = dest_dir.join(name);
create_dir(&target_dir)?;
for entry in WalkDir::new(source_dir) {
let entry = entry?;
let entry = entry.path();
if entry.is_dir() {
copy_dir_into(entry, &target_dir);
}else if entry.is_file() {
let target_entry = target_dir.join(entry.file_name().expect("Error obtaining the filename"));
std::fs::copy(entry, target_entry);
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
use std::path::Path;
use std::fs::create_dir;
#[test]
fn test_copy_dir_into() {
let source_tmp_dir = TempDir::new().expect("Error creating temp directory");
let dest_tmp_dir = TempDir::new().expect("Error creating temp directory");
let source_dir = source_tmp_dir.path().join("source");
create_dir(&source_dir);
std::fs::write(source_dir.join("file1.txt"), "file1");
std::fs::write(source_dir.join("file2.txt"), "file2");
copy_dir_into(&source_dir, dest_tmp_dir.path());
assert!(dest_tmp_dir.path().join("source").exists());
assert!(dest_tmp_dir.path().join("source/file1.txt").exists());
assert!(dest_tmp_dir.path().join("source/file2.txt").exists());
}
#[test]
fn test_copy_dir_into_recursive() {
let source_tmp_dir = TempDir::new().expect("Error creating temp directory");
let dest_tmp_dir = TempDir::new().expect("Error creating temp directory");
let source_dir = source_tmp_dir.path().join("source");
create_dir(&source_dir);
std::fs::write(source_dir.join("file1.txt"), "file1");
std::fs::write(source_dir.join("file2.txt"), "file2");
let nested_dir = source_dir.join("nested");
create_dir(&nested_dir);
std::fs::write(nested_dir.join("nestedfile.txt"), "nestedfile1");
copy_dir_into(&source_dir, dest_tmp_dir.path());
assert!(dest_tmp_dir.path().join("source").exists());
assert!(dest_tmp_dir.path().join("source/file1.txt").exists());
assert!(dest_tmp_dir.path().join("source/file2.txt").exists());
assert!(dest_tmp_dir.path().join("source/nested").exists());
assert!(dest_tmp_dir.path().join("source/nested/nestedfile.txt").exists());
}
}