Skip to main content

workyard_application/actions/init/
action.rs

1use super::{InitInput, InitOutcome, skill};
2use crate::{
3	contracts::{ProblemOutcome, Progress},
4	execution::blocking_action,
5};
6use std::path::Path;
7use trine::{InvocationContext, NativePath};
8use workyard_core::{Control, Problem};
9
10/// Initialize a workspace with the requested tasks directory.
11pub async fn init(
12	input: InitInput,
13	context: InvocationContext<Progress>,
14) -> Result<InitOutcome, ProblemOutcome> {
15	blocking_action(async move {
16		let cancellation = context.cancellation().clone();
17		let control = Control::new({
18			let cancellation = cancellation.clone();
19			move || cancellation.is_cancelled()
20		});
21
22		let directory = input
23			.directory
24			.as_ref()
25			.map(NativePath::as_path)
26			.unwrap_or(Path::new("."));
27
28		let workspace = workyard_state::canonical_future(directory)?;
29		let requested_tasks = input.tasks.as_ref().map(NativePath::as_path);
30		if let Some(tasks) = requested_tasks {
31			skill::check_tasks(&workspace, tasks)?;
32		}
33
34		let tasks = workyard_state::initialize_workspace_with_control(
35			&workspace,
36			requested_tasks,
37			&control,
38		)
39		.await?;
40
41		skill::install(&workspace).map_err(|error| {
42			Problem::io(
43				format!("Workspace is ready. Could not install {}", skill::LOCATION),
44				error,
45			)
46			.remedy("Check the skill path and rerun workyard init for the same workspace.")
47		})?;
48
49		Ok(InitOutcome {
50			cancellation,
51			workspace: NativePath::new(workspace).expect("workspace path"),
52			tasks_root: NativePath::new(tasks).expect("tasks root path"),
53		})
54	})
55	.await
56}