use crate::assemble_core::lazy_evaluation::ProviderExt;
use assemble_core::lazy_evaluation::{Prop, Provider};
use assemble_core::project::error::ProjectResult;
use assemble_core::task::initialize_task::InitializeTask;
use assemble_core::task::up_to_date::UpToDate;
use assemble_core::{BuildResult, Executable, Project, Task};
use std::path::PathBuf;
use url::Url;
#[derive(Debug, Clone, CreateTask, TaskIO)]
pub struct DownloadFile {
#[input]
pub url: Prop<Url>,
#[output]
pub fname: Prop<PathBuf>,
}
impl InitializeTask for DownloadFile {
fn initialize(task: &mut Executable<Self>, project: &Project) -> ProjectResult {
let build_dir = project.build_dir();
let map = task
.url
.clone()
.zip(build_dir, |url: Url, build_dir: PathBuf| {
build_dir.join("downloads").join(
url.path_segments()
.and_then(|segs| segs.last())
.and_then(|name| if name.is_empty() { None } else { Some(name) })
.unwrap_or("tmp.bin"),
)
});
task.fname.set_with(map)?;
Ok(())
}
}
impl UpToDate for DownloadFile {}
impl Task for DownloadFile {
fn task_action(task: &mut Executable<Self>, _project: &Project) -> BuildResult {
let url = task.url.get();
println!("url = {}", url);
println!("fname = {:?}", task.fname.try_get().unwrap_or_default());
Ok(())
}
}