assemble_std/tasks/
web.rs

1//! Contains web-based tasks, like getting
2
3use crate::assemble_core::lazy_evaluation::ProviderExt;
4
5use assemble_core::lazy_evaluation::{Prop, Provider};
6use assemble_core::project::error::ProjectResult;
7
8use assemble_core::task::initialize_task::InitializeTask;
9use assemble_core::task::up_to_date::UpToDate;
10use assemble_core::{BuildResult, Executable, Project, Task};
11use std::path::PathBuf;
12use url::Url;
13
14/// Downloads a file
15#[derive(Debug, Clone, CreateTask, TaskIO)]
16pub struct DownloadFile {
17    /// The url to download from
18    #[input]
19    pub url: Prop<Url>,
20    /// The file name to download into
21    #[output]
22    pub fname: Prop<PathBuf>,
23}
24
25impl InitializeTask for DownloadFile {
26    fn initialize(task: &mut Executable<Self>, project: &Project) -> ProjectResult {
27        let build_dir = project.build_dir();
28
29        let map = task
30            .url
31            .clone()
32            .zip(build_dir, |url: Url, build_dir: PathBuf| {
33                build_dir.join("downloads").join(
34                    url.path_segments()
35                        .and_then(|segs| segs.last())
36                        .and_then(|name| if name.is_empty() { None } else { Some(name) })
37                        .unwrap_or("tmp.bin"),
38                )
39            });
40        task.fname.set_with(map)?;
41        Ok(())
42    }
43}
44
45impl UpToDate for DownloadFile {}
46
47impl Task for DownloadFile {
48    fn task_action(task: &mut Executable<Self>, _project: &Project) -> BuildResult {
49        let url = task.url.get();
50        println!("url = {}", url);
51        println!("fname = {:?}", task.fname.try_get().unwrap_or_default());
52
53        Ok(())
54    }
55}
56//
57// impl CreateTask for DownloadFile {
58//     fn new(id: &TaskId, project: &Project) -> ProjectResult<Self> {
59//         let url = id.prop("url").unwrap();
60//         let mut fname: Prop<PathBuf> = id.prop("file_name").unwrap();
61//
62//         let build_dir = project.build_dir();
63//
64//         let map = url.zip(&build_dir, |url: Url, build_dir: PathBuf| {
65//             println!("Calculating fname");
66//             build_dir.join("downloads").join(
67//                 url.path_segments()
68//                     .and_then(|segs| segs.last())
69//                     .and_then(|name| if name.is_empty() { None } else { Some(name) })
70//                     .unwrap_or("tmp.bin"),
71//             )
72//         });
73//         fname.set_with(map).unwrap();
74//         Ok(Self { url, fname })
75//     }
76//
77//
78// }