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, ProblemCode};
9use workyard_state::InitOptions;
10
11/// Initialize a workspace with the requested tasks directory.
12pub async fn init(
13	input: InitInput,
14	context: InvocationContext<Progress>,
15) -> Result<InitOutcome, ProblemOutcome> {
16	blocking_action(async move {
17		let cancellation = context.cancellation().clone();
18		let control = Control::new({
19			let cancellation = cancellation.clone();
20			move || cancellation.is_cancelled()
21		});
22
23		let directory = input
24			.directory
25			.as_ref()
26			.map(NativePath::as_path)
27			.unwrap_or(Path::new("."));
28
29		let workspace = workyard_state::canonical_future(directory)?;
30		if input.tasks.is_some() && input.detached_tasks.is_some() {
31			return Err(Problem::new(
32				ProblemCode::InvalidInput,
33				"Use either --tasks or --detached-tasks.",
34			)
35			.into());
36		}
37		let requested_tasks = input
38			.tasks
39			.as_ref()
40			.or(input.detached_tasks.as_ref())
41			.map(NativePath::as_path);
42		if let Some(tasks) = requested_tasks {
43			skill::check_tasks(&workspace, tasks)?;
44		}
45
46		let track: Vec<_> = input.track.iter().map(NativePath::as_path).collect();
47		let tasks = workyard_state::initialize_workspace_with_options(
48			&workspace,
49			InitOptions {
50				tasks: requested_tasks,
51				detached_tasks: input.detached_tasks.is_some(),
52				tracked_only: input.tracked_only,
53				track: &track,
54			},
55			&control,
56		)
57		.await?;
58
59		skill::install(&workspace).map_err(|error| {
60			Problem::io(
61				format!("Workspace is ready. Could not install {}", skill::LOCATION),
62				error,
63			)
64			.remedy("Check the skill path and rerun workyard init for the same workspace.")
65		})?;
66
67		Ok(InitOutcome {
68			cancellation,
69			workspace: NativePath::new(workspace).expect("workspace path"),
70			tasks_root: NativePath::new(tasks).expect("tasks root path"),
71		})
72	})
73	.await
74}