use crate::generator::{Generators, Options};
use crate::snap::{File, SNAPCRAFT_YAML};
use core::result;
use std::env;
use std::error::Error;
use std::path::{Path, PathBuf};
use url::Url;
pub mod generator;
pub mod snap;
type Result<T> = result::Result<T, Box<dyn Error>>;
pub fn fetch_source(source_url: &Url) -> Result<PathBuf> {
let cwd = env::current_dir()?;
let source_name = source_url
.path_segments()
.unwrap()
.last()
.unwrap()
.replace(".git", "");
let path = cwd.join(source_name);
git2::Repository::clone(source_url.as_str(), &path)?;
Ok(path)
}
pub fn package_source<P: AsRef<Path>>(source_path: P, options: &Options) -> Result<File> {
let source_path = if source_path.as_ref().eq(Path::new(".")) {
env::current_dir()?
} else {
source_path.as_ref().to_path_buf()
};
let mut options = options.clone();
let source_name = source_path.file_name().unwrap().to_str().unwrap();
options.source_name = source_name.to_string();
if source_path.join(SNAPCRAFT_YAML).exists()
|| source_path.join("snap").join(SNAPCRAFT_YAML).exists()
{
return Err(format!("{} is already packaged", source_name).into());
}
Generators::generate(&source_path, &options)
}