Skip to content

Commit

Permalink
Merge pull request #72 from azriel91/feature/35/profile-init
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 authored Jan 19, 2023
2 parents 2e385c0 + e1846f2 commit 696be70
Show file tree
Hide file tree
Showing 70 changed files with 1,149 additions and 214 deletions.
29 changes: 29 additions & 0 deletions crate/core/src/app_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::borrow::Cow;

use serde::{Deserialize, Serialize};

/// Name of the application that is run by end users.
///
/// This is usually the crate name of the final binary. It needs to be passed in
/// from the executable crate, as it is not possible for Peace to detect it
/// within the library itself.
///
/// Must begin with a letter or underscore, and contain only letters, numbers,
/// and underscores.
///
/// # Examples
///
/// The following are all examples of valid `AppName`s:
///
/// ```rust
/// # use peace_core::{app_name, AppName};
/// #
/// let _default = app_name!(); // defaults to calling crate name
/// let _snake = app_name!("snake_case");
/// let _camel = app_name!("camelCase");
/// let _pascal = app_name!("PascalCase");
/// ```
#[derive(Clone, Debug, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct AppName(Cow<'static, str>);

crate::id_newtype!(AppName, AppNameInvalidFmt, app_name);
15 changes: 15 additions & 0 deletions crate/core/src/flow_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use peace_static_check_macros::flow_id;
use serde::{Deserialize, Serialize};

/// Identifier or name of a process flow.
Expand Down Expand Up @@ -35,3 +36,17 @@ use serde::{Deserialize, Serialize};
pub struct FlowId(Cow<'static, str>);

crate::id_newtype!(FlowId, FlowIdInvalidFmt, flow_id);

impl FlowId {
/// Flow ID used by the Peace framework when a command is for initializing a
/// workspace.
pub const fn workspace_init() -> Self {
flow_id!("workspace_init")
}

/// Flow ID used by the Peace framework when a command is for initializing a
/// profile.
pub const fn profile_init() -> Self {
flow_id!("profile_init")
}
}
4 changes: 3 additions & 1 deletion crate/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
//! [peace#67]: https://github.com/azriel91/peace/issues/67
// Re-exports
pub use peace_static_check_macros::{flow_id, item_spec_id, profile};
pub use peace_static_check_macros::{app_name, flow_id, item_spec_id, profile};

pub use crate::{
app_name::{AppName, AppNameInvalidFmt},
flow_id::{FlowId, FlowIdInvalidFmt},
item_spec_id::{ItemSpecId, ItemSpecIdInvalidFmt},
op_check_status::OpCheckStatus,
Expand All @@ -27,6 +28,7 @@ pub use crate::{
#[cfg(feature = "output_progress")]
pub mod progress;

mod app_name;
mod flow_id;
mod item_spec_id;
mod op_check_status;
Expand Down
9 changes: 9 additions & 0 deletions crate/core/src/profile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use peace_static_check_macros::profile;
use serde::{Deserialize, Serialize};

/// Identifier or namespace to distinguish execution environments.
Expand Down Expand Up @@ -28,3 +29,11 @@ use serde::{Deserialize, Serialize};
pub struct Profile(Cow<'static, str>);

crate::id_newtype!(Profile, ProfileInvalidFmt, profile);

impl Profile {
/// Profile used by the Peace framework when a command is for initializing
/// the workspace.
pub const fn workspace_init() -> Self {
profile!("workspace_init")
}
}
2 changes: 1 addition & 1 deletion crate/data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ doctest = false
test = false

[dependencies]
fn_graph = { version = "0.8.0", features = ["resman"] }
fn_graph = { version = "0.8.1", features = ["resman"] }
peace_core = { path = "../core", version = "0.0.5" }
peace_data_derive = { path = "../data_derive", version = "0.0.5" }
14 changes: 13 additions & 1 deletion crate/resources/src/internal/workspace_dirs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::paths::{FlowDir, PeaceDir, ProfileDir, ProfileHistoryDir, WorkspaceDir};
use crate::paths::{FlowDir, PeaceAppDir, PeaceDir, ProfileDir, ProfileHistoryDir, WorkspaceDir};

/// Directories used during `peace` execution.
///
Expand All @@ -12,6 +12,8 @@ pub struct WorkspaceDirs {
workspace_dir: WorkspaceDir,
/// Peace directory,
peace_dir: PeaceDir,
/// Peace app directory,
peace_app_dir: PeaceAppDir,
/// Directory to store data for the current profile.
profile_dir: ProfileDir,
/// Directory to store profile executions' summaries.
Expand All @@ -25,13 +27,15 @@ impl WorkspaceDirs {
pub fn new(
workspace_dir: WorkspaceDir,
peace_dir: PeaceDir,
peace_app_dir: PeaceAppDir,
profile_dir: ProfileDir,
profile_history_dir: ProfileHistoryDir,
flow_dir: FlowDir,
) -> Self {
Self {
workspace_dir,
peace_dir,
peace_app_dir,
profile_dir,
profile_history_dir,
flow_dir,
Expand All @@ -44,13 +48,15 @@ impl WorkspaceDirs {
) -> (
WorkspaceDir,
PeaceDir,
PeaceAppDir,
ProfileDir,
ProfileHistoryDir,
FlowDir,
) {
let Self {
workspace_dir,
peace_dir,
peace_app_dir,
profile_dir,
profile_history_dir,
flow_dir,
Expand All @@ -59,6 +65,7 @@ impl WorkspaceDirs {
(
workspace_dir,
peace_dir,
peace_app_dir,
profile_dir,
profile_history_dir,
flow_dir,
Expand All @@ -75,6 +82,11 @@ impl WorkspaceDirs {
&self.peace_dir
}

/// Returns a reference to the `.peace/$app` directory.
pub fn peace_app_dir(&self) -> &PeaceAppDir {
&self.peace_app_dir
}

/// Returns a reference to the profile directory.
pub fn profile_dir(&self) -> &ProfileDir {
&self.profile_dir
Expand Down
12 changes: 6 additions & 6 deletions crate/resources/src/internal/workspace_params_file.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::path::PathBuf;

use crate::paths::PeaceDir;
use crate::paths::PeaceAppDir;

/// Path to the file that stores the workspace initialization parameters.
///
/// Typically `$workspace_dir/.peace/init.yaml`.
/// Typically `$workspace_dir/.peace/$app/init.yaml`.
///
/// See `WorkspaceParamsFile::from<&PeaceDir>` if you want to construct a
/// `WorkspaceParamsFile` with the conventional `$peace_dir/init.yaml`
/// See `WorkspaceParamsFile::from<&PeaceAppDir>` if you want to construct a
/// `WorkspaceParamsFile` with the conventional `$peace_dir/$app/init.yaml`
/// path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkspaceParamsFile(PathBuf);
Expand All @@ -19,8 +19,8 @@ impl WorkspaceParamsFile {
pub const NAME: &'static str = "init.yaml";
}

impl From<&PeaceDir> for WorkspaceParamsFile {
fn from(flow_dir: &PeaceDir) -> Self {
impl From<&PeaceAppDir> for WorkspaceParamsFile {
fn from(flow_dir: &PeaceAppDir) -> Self {
let path = flow_dir.join(Self::NAME);

Self(path)
Expand Down
3 changes: 2 additions & 1 deletion crate/resources/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@
//! ```
pub use self::{
flow_dir::FlowDir, peace_dir::PeaceDir, profile_dir::ProfileDir,
flow_dir::FlowDir, peace_app_dir::PeaceAppDir, peace_dir::PeaceDir, profile_dir::ProfileDir,
profile_history_dir::ProfileHistoryDir, states_desired_file::StatesDesiredFile,
states_saved_file::StatesSavedFile, workspace_dir::WorkspaceDir,
};

mod flow_dir;
mod peace_app_dir;
mod peace_dir;
mod profile_dir;
mod profile_history_dir;
Expand Down
2 changes: 1 addition & 1 deletion crate/resources/src/paths/flow_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::paths::ProfileDir;

/// Directory to store all data produced by the current flow's execution.
///
/// Typically `$workspace_dir/.peace/$profile/$flow_id`.
/// Typically `$workspace_dir/.peace/$app/$profile/$flow_id`.
///
/// This is the directory that contains the information produced and used during
/// a `peace` tool invocation for a particular flow.
Expand Down
30 changes: 30 additions & 0 deletions crate/resources/src/paths/peace_app_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::path::PathBuf;

use peace_core::AppName;

use crate::paths::PeaceDir;

/// Directory to store all data produced by the current application's execution.
///
/// Typically `$workspace_dir/.peace/$app`.
///
/// This is the directory that contains all information produced and used during
/// a `peace` tool invocation. This directory layer is created to accommodate
/// different `peace` tools being run in the same workspace.
///
/// # Implementors
///
/// This type is constructed by the Peace framework when a Workspace's
/// directories are created.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PeaceAppDir(PathBuf);

crate::paths::pathbuf_newtype!(PeaceAppDir);

impl From<(&PeaceDir, &AppName)> for PeaceAppDir {
fn from((peace_dir, app_name): (&PeaceDir, &AppName)) -> Self {
let path = peace_dir.join(app_name.as_ref());

Self(path)
}
}
2 changes: 1 addition & 1 deletion crate/resources/src/paths/peace_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use crate::paths::WorkspaceDir;

/// Directory to store all data produced by `peace` tool execution.
/// Directory to store all data produced by `peace` tools' executions.
///
/// Typically `$workspace_dir/.peace`.
///
Expand Down
14 changes: 7 additions & 7 deletions crate/resources/src/paths/profile_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ use std::path::PathBuf;

use peace_core::Profile;

use crate::paths::PeaceDir;
use crate::paths::PeaceAppDir;

/// Directory to store all data produced by the current profile's execution.
///
/// Typically `$workspace_dir/.peace/$profile`.
/// Typically `$workspace_dir/.peace/$app/$profile`.
///
/// This is the directory that contains all information produced and used during
/// a `peace` tool invocation. Exceptions include authentication information
/// stored in their respective directories on the file system, such as
/// application credentials stored in `~/${app}/credentials`.
///
/// See `ProfileDir::from<(&PeaceDir, &Profile)>` if you want to construct a
/// `ProfileDir` with the conventional `$peace_dir/.peace/$profile` path.
/// See `ProfileDir::from<(&PeaceAppDir, &Profile)>` if you want to construct a
/// `ProfileDir` with the conventional `$peace_dir/.peace/$app/$profile` path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProfileDir(PathBuf);

crate::paths::pathbuf_newtype!(ProfileDir);

impl From<(&PeaceDir, &Profile)> for ProfileDir {
fn from((peace_dir, profile): (&PeaceDir, &Profile)) -> Self {
let path = peace_dir.join(profile.as_ref());
impl From<(&PeaceAppDir, &Profile)> for ProfileDir {
fn from((peace_app_dir, profile): (&PeaceAppDir, &Profile)) -> Self {
let path = peace_app_dir.join(profile.as_ref());

Self(path)
}
Expand Down
2 changes: 1 addition & 1 deletion crate/resources/src/paths/profile_history_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::paths::ProfileDir;

/// Directory to store all data produced by the current profile's execution.
///
/// Typically `$workspace_dir/.peace/$profile/.history`.
/// Typically `$workspace_dir/.peace/$app/$profile/.history`.
///
/// This directory contains significant command execution summaries.
///
Expand Down
2 changes: 1 addition & 1 deletion crate/rt_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test = false
cfg-if = { workspace = true }
dyn-clone = "1.0.10"
erased-serde = "0.3.24"
fn_graph = { version = "0.8.0", features = ["resman"] }
fn_graph = { version = "0.8.1", features = ["resman"] }
futures = "0.3.25"
indicatif = { workspace = true, features = ["tokio"] }
miette = { workspace = true, optional = true }
Expand Down
9 changes: 5 additions & 4 deletions crate/rt_model/src/cmd_context_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
pub async fn build(mut self) -> Result<CmdContext<'ctx, E, O, SetUp>, E> {
let dirs = self.workspace.dirs();
let storage = self.workspace.storage();
let workspace_params_file = WorkspaceParamsFile::from(dirs.peace_dir());
let workspace_params_file = WorkspaceParamsFile::from(dirs.peace_app_dir());
let profile_params_file = ProfileParamsFile::from(dirs.profile_dir());
let flow_params_file = FlowParamsFile::from(dirs.flow_dir());
let states_saved_file = StatesSavedFile::from(dirs.flow_dir());
Expand Down Expand Up @@ -270,24 +270,25 @@ where

/// Inserts workspace directory resources into the `Resources` map.
fn workspace_dirs_insert(resources: &mut Resources<Empty>, workspace: &Workspace) {
let (workspace_dirs, profile, flow_id, storage) = workspace.clone().into_inner();
let (workspace_dir, peace_dir, profile_dir, profile_history_dir, flow_dir) =
let (app_name, workspace_dirs, profile, flow_id, storage) = workspace.clone().into_inner();
let (workspace_dir, peace_dir, peace_app_dir, profile_dir, profile_history_dir, flow_dir) =
workspace_dirs.into_inner();

resources.insert(workspace_dir);
resources.insert(peace_dir);
resources.insert(peace_app_dir);
resources.insert(profile_dir);
resources.insert(profile_history_dir);
resources.insert(flow_dir);

resources.insert(app_name);
resources.insert(profile);
resources.insert(flow_id);
resources.insert(storage);
}

/// Inserts init params into the `Resources` map.
fn init_params_insert(&mut self, resources: &mut Resources<Empty>) {
// TODO: we need to insert the raw type, right now we're inserting the box
if let Some(workspace_params) = self.workspace_params.as_mut() {
workspace_params
.drain(..)
Expand Down
12 changes: 11 additions & 1 deletion crate/rt_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! `peace_rt_model_web` depending on the compilation target architecture.
// Re-exports
pub use fn_graph::{self, FnRef, FnRefMut};
pub use fn_graph::{self, FnRef};
pub use peace_rt_model_core::cmd_context_params;
#[cfg(feature = "output_progress")]
pub use peace_rt_model_core::*;
Expand All @@ -16,6 +16,16 @@ pub mod output {
pub use peace_rt_model_native::output::*;
}

pub mod workspace {
pub use peace_rt_model_core::workspace::*;

#[cfg(not(target_arch = "wasm32"))]
pub use peace_rt_model_native::workspace::*;

#[cfg(target_arch = "wasm32")]
pub use peace_rt_model_web::workspace::*;
}

#[cfg(not(target_arch = "wasm32"))]
pub use peace_rt_model_native::*;

Expand Down
1 change: 1 addition & 0 deletions crate/rt_model_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use indicatif;

pub mod cmd_context_params;
pub mod output;
pub mod workspace;

cfg_if::cfg_if! {
if #[cfg(feature = "output_progress")] {
Expand Down
3 changes: 3 additions & 0 deletions crate/rt_model_core/src/workspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Common workspace types for native and web targets.
pub mod ts;
Loading

0 comments on commit 696be70

Please sign in to comment.