Skip to content

Commit

Permalink
Added app manifests to holochain_scaffolding_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed May 27, 2024
1 parent 9cc2632 commit 90887bf
Show file tree
Hide file tree
Showing 3 changed files with 759 additions and 704 deletions.
69 changes: 62 additions & 7 deletions crates/holochain_scaffolding_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::{collections::BTreeMap, path::PathBuf};

use dialoguer::{theme::ColorfulTheme, Select};
use file_tree_utils::{find_files_by_name, FileTree, FileTreeError};
use holochain_types::web_app::WebAppManifest;
use holochain_types::{app::AppManifest, web_app::WebAppManifest};
use mr_bundle::Manifest;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum GetOrChooseWebAppManifestError {
pub enum Error {
#[error(transparent)]
FileTreeError(#[from] FileTreeError),

Expand All @@ -19,19 +19,22 @@ pub enum GetOrChooseWebAppManifestError {

#[error("No web-happ.yaml files were found in this repository")]
WebAppManifestNotFound,

#[error("No happ.yaml files were found in this repository")]
AppManifestNotFound,
}

pub fn get_or_choose_web_app_manifest(
file_tree: &FileTree,
) -> Result<(PathBuf, WebAppManifest), GetOrChooseWebAppManifestError> {
) -> Result<(PathBuf, WebAppManifest), Error> {
let web_app_manifests = find_web_app_manifests(&file_tree)?;

let app_manifest = match web_app_manifests.len() {
0 => Err(GetOrChooseWebAppManifestError::WebAppManifestNotFound),
0 => Err(Error::WebAppManifestNotFound),
1 => web_app_manifests
.into_iter()
.last()
.ok_or(GetOrChooseWebAppManifestError::WebAppManifestNotFound),
.ok_or(Error::WebAppManifestNotFound),
_ => choose_web_app(web_app_manifests),
}?;

Expand All @@ -40,7 +43,7 @@ pub fn get_or_choose_web_app_manifest(

pub fn choose_web_app(
app_manifests: BTreeMap<PathBuf, WebAppManifest>,
) -> Result<(PathBuf, WebAppManifest), GetOrChooseWebAppManifestError> {
) -> Result<(PathBuf, WebAppManifest), Error> {
let manifest_vec: Vec<(PathBuf, WebAppManifest)> = app_manifests.into_iter().collect();
let app_names: Vec<String> = manifest_vec
.iter()
Expand All @@ -59,7 +62,7 @@ pub fn choose_web_app(
/// Returns the path to the existing app manifests in the given project structure
pub fn find_web_app_manifests(
app_file_tree: &FileTree,
) -> Result<BTreeMap<PathBuf, WebAppManifest>, GetOrChooseWebAppManifestError> {
) -> Result<BTreeMap<PathBuf, WebAppManifest>, Error> {
let files = find_files_by_name(app_file_tree, &WebAppManifest::path());

let manifests: BTreeMap<PathBuf, WebAppManifest> = files
Expand All @@ -74,3 +77,55 @@ pub fn find_web_app_manifests(

Ok(manifests)
}

pub fn get_or_choose_app_manifest(file_tree: &FileTree) -> Result<(PathBuf, AppManifest), Error> {
let app_manifests = find_app_manifests(&file_tree)?;

let app_manifest = match app_manifests.len() {
0 => Err(Error::AppManifestNotFound),
1 => app_manifests
.into_iter()
.last()
.ok_or(Error::AppManifestNotFound),
_ => choose_app(app_manifests),
}?;

Ok(app_manifest)
}

pub fn choose_app(
app_manifests: BTreeMap<PathBuf, AppManifest>,
) -> Result<(PathBuf, AppManifest), Error> {
let manifest_vec: Vec<(PathBuf, AppManifest)> = app_manifests.into_iter().collect();
let app_names: Vec<String> = manifest_vec
.iter()
.map(|(_, m)| m.app_name().to_string())
.collect();

let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Multiple happs were found in this repository, choose one:")
.default(0)
.items(&app_names[..])
.interact()?;

Ok(manifest_vec[selection].clone())
}

/// Returns the path to the existing app manifests in the given project structure
pub fn find_app_manifests(
app_file_tree: &FileTree,
) -> Result<BTreeMap<PathBuf, AppManifest>, Error> {
let files = find_files_by_name(app_file_tree, &AppManifest::path());

let manifests: BTreeMap<PathBuf, AppManifest> = files
.into_iter()
.map(|(key, manifest_str)| {
let manifest: AppManifest = serde_yaml::from_str(manifest_str.as_str())?;
Ok((key, manifest))
})
.collect::<serde_yaml::Result<Vec<(PathBuf, AppManifest)>>>()?
.into_iter()
.collect();

Ok(manifests)
}
2 changes: 1 addition & 1 deletion packages/signals/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@holochain-open-dev/signals",
"version": "0.300.0-rc.0",
"version": "0.300.0-rc.1",
"description": "Holochain async-signals to build reusable holochain-open-dev modules",
"author": "[email protected]",
"main": "dist/index.js",
Expand Down
Loading

0 comments on commit 90887bf

Please sign in to comment.