Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: "camp" folder to cache users answers to intractions #14

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backpack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ requestty = "0.4.0"
requestty-ui = "0.4.0"
tracing-forest = { version = "^0.1.4" }
tracing-subscriber = { version = "^0.3.11", features = ["env-filter"] }
interactive-actions = "1.0.1"
# interactive-actions = "1.0.1"
# interactive-actions = { path = "../../interactive-actions/interactive-actions" }
interactive-actions = { git = "https://github.com/strowk/interactive-actions.git" }
tera = "^1.17.0"
tera-text-filters = "^1.0.0"
content_inspector = "0.2.4"
Expand Down
76 changes: 76 additions & 0 deletions backpack/src/camp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use anyhow::Result;
use interactive_actions::data::DefaultValue;
use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
};

const RELATIVE_CAMP_FOLDER: &str = ".backpack-camp";
const RELATIVE_INTERACTION_ANSWERS_FILE: &str = "interaction-answers.yaml";

pub(crate) struct CampData {
pub(crate) interaction_answers: HashMap<String, DefaultValue>,
}

impl CampData {
pub fn camp_folder() -> PathBuf {
PathBuf::from(RELATIVE_CAMP_FOLDER)
}

pub fn interactions_config_file() -> PathBuf {
Self::camp_folder().join(RELATIVE_INTERACTION_ANSWERS_FILE)
}

pub fn interactions_answers_from_path(
path: &Path,
) -> Result<Option<HashMap<String, DefaultValue>>> {
if path.exists() {
Ok(Some(Self::interactions_answers_from_text(
&fs::read_to_string(path)?,
)?))
} else {
Ok(None)
}
}

pub fn interactions_answers_from_text(text: &str) -> Result<HashMap<String, DefaultValue>> {
let conf: HashMap<String, DefaultValue> = serde_yaml::from_str(text)?;
Ok(conf)
}

#[tracing::instrument(name = "camp_data_path", skip_all, err)]
pub fn from_path(file: &Path) -> Result<Self> {
Ok(Self {
interaction_answers: Self::interactions_answers_from_path(file)?.unwrap_or_default(),
})
}

#[tracing::instrument(name = "camp_data_load", skip_all, err)]
pub fn load_or_default() -> Result<Self> {
Self::from_path(&Self::interactions_config_file())
}

pub fn save_answers(&self) -> Result<()> {
self.save_answers_to(&Self::interactions_config_file())?;
Ok(())
}

pub fn save_answers_to(&self, path: &Path) -> Result<()> {
fs::write(path, serde_yaml::to_string(&self.interaction_answers)?)?;
Ok(())
}
}

pub(crate) struct Camp {
pub(crate) data: CampData,
}

impl Camp {
pub(crate) fn new() -> Result<Self> {
fs::create_dir_all(RELATIVE_CAMP_FOLDER)?;
Ok(Self {
data: CampData::load_or_default()?,
})
}
}
8 changes: 8 additions & 0 deletions backpack/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ pub struct Project {
#[serde(rename = "swaps")]
#[serde(skip_serializing_if = "Option::is_none")]
pub swaps: Option<Vec<Swap>>,

#[serde(rename = "use_camp")]
#[serde(skip_serializing_if = "Option::is_none")]
pub use_camp: Option<bool>,
}
impl Project {
pub fn from_link(ln: &str) -> Self {
Expand All @@ -247,6 +251,10 @@ pub struct ProjectSetupActions {

#[serde(rename = "swaps")]
pub swaps: Option<Vec<Swap>>,

#[serde(rename = "use_camp")]
#[serde(skip_serializing_if = "Option::is_none")]
pub use_camp: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
53 changes: 45 additions & 8 deletions backpack/src/content.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::camp::Camp;
use crate::config::ProjectSetupActions;
use crate::data::{Location, Opts, Overwrite};
use crate::templates::{CopyResult, Swapper};
use crate::ui::Prompt;
use anyhow::Result;
use interactive_actions::data::Response;
use interactive_actions::{
data::ActionResult,
data::{Action, ActionHook},
Expand Down Expand Up @@ -94,21 +96,56 @@ impl<'a> Deployer<'a> {
coord.to.as_path()
};

let (actions, swaps) = project_setup
.as_ref()
.map_or((None, None), |p| (p.actions.as_ref(), p.swaps.as_ref()));
let (actions, swaps, use_camp) = project_setup.map_or((None, None, false), |p| {
(p.actions, p.swaps, p.use_camp.unwrap_or(false))
});

if let Some(actions) = actions {
self.action_runner.run(
actions,
let (camp, actions) = if let Some(mut actions) = actions {
let camp = if use_camp {
let camp = Camp::new()?;
let interaction_answers = &camp.data.interaction_answers;
for action in actions.iter_mut() {
if let Some(interaction) = action.interaction.as_mut() {
interaction_answers.get(&action.name).map(|answer| {
interaction.default_value = Some(answer.clone());
interaction.ask_if_has_default =
interaction.ask_if_has_default.or(Some(false));
});
}
}
Some(camp)
} else {
None
};
(camp, Some(actions))
} else {
(None, None)
};

if let Some(actions) = actions.as_ref() {
let actions_runner_result = self.action_runner.run(
&actions,
Some(actions_dest),
vars,
ActionHook::Before,
None::<fn(&Action)>,
)?;

if let Some(mut camp) = camp {
for action_runner_result in actions_runner_result {
let interaction_response = action_runner_result.response;
if let Response::Text(interaction_response) = interaction_response {
camp.data.interaction_answers.insert(
action_runner_result.name,
interactive_actions::data::DefaultValue::Input(interaction_response),
);
}
}
camp.data.save_answers()?;
};
}

let swapper = Swapper::with_vars(swaps, vars)?;
let swapper = Swapper::with_vars(swaps.as_ref(), vars)?;

let files = self.copy(
&swapper,
Expand All @@ -131,7 +168,7 @@ impl<'a> Deployer<'a> {
);
}

let after_actions = if let Some(actions) = actions {
let after_actions = if let Some(actions) = actions.as_ref() {
Some(self.action_runner.run(
actions,
Some(actions_dest),
Expand Down
1 change: 1 addition & 0 deletions backpack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![allow(clippy::unused_self)]
#![allow(clippy::missing_const_for_fn)]

mod camp;
pub mod config;
pub mod content;
pub mod data;
Expand Down
1 change: 1 addition & 0 deletions backpack/src/shortlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl<'a> Shortlink<'a> {
.map(|project| ProjectSetupActions {
actions: project.actions.clone(),
swaps: project.swaps.clone(),
use_camp: project.use_camp,
})
}
}
Expand Down