Skip to content

Commit

Permalink
Add holo-dev-server to the dev shell when --holo is passed
Browse files Browse the repository at this point in the history
We add a --holo flag to the example subcommand.
When this flag is passed, we add holo-nixpkgs as a flake input and we
add holo-dev-server to the dev shell's packages.
  • Loading branch information
r-vdp committed Mar 20, 2024
1 parent 36d0b30 commit 06dccae
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 65 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
scope: [ hello_world ]
scope:
- hello_world
- holo_integration
steps:
- uses: actions/checkout@v4

Expand Down
147 changes: 96 additions & 51 deletions run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,138 @@ SCOPE=
# parse args
while getopts ":t:s:" opt; do
case $opt in
t) TEMPLATE_NAME="$OPTARG";;
s) SCOPE="$OPTARG";;
\?) echo "Invalid option: -$OPTARG" >&2
exit 1;;
:) echo "Option -$OPTARG requires an argument." >&2
exit 1;;
t) TEMPLATE_NAME="$OPTARG" ;;
s) SCOPE="$OPTARG" ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done

cleanup_tmp() {
rm -rf $TEMPLATE_PATH/$1
rm -rf "${TEMPLATE_PATH:?}/$1"
}

print_version() {
echo $(hc-scaffold --version)
hc-scaffold --version
}

setup_and_build_happ() {
print_version
cleanup_tmp $1

cd $TEMPLATE_PATH
hc-scaffold --template $2 web-app $1 --setup-nix true
cd $1

hc-scaffold dna forum
hc-scaffold zome posts --integrity dnas/forum/zomes/integrity/ --coordinator dnas/forum/zomes/coordinator/
hc-scaffold entry-type post --reference-entry-hash false --crud crud --link-from-original-to-each-update true --fields title:String:TextField,content:String:TextArea
hc-scaffold entry-type comment --reference-entry-hash false --crud crud --link-from-original-to-each-update false --fields post_hash:ActionHash::Post
hc-scaffold entry-type like --reference-entry-hash false --crud crd --fields like_hash:Option\<ActionHash\>::Like,string_list:Vec\<String\>
hc-scaffold entry-type certificate --reference-entry-hash true --crud cr --fields post_hash:ActionHash::Post,agent:AgentPubKey::certified,certifications_hashes:Vec\<EntryHash\>::Certificate,certificate_type:Enum::CertificateType:TypeOne.TypeTwo,dna_hash:DnaHash

hc-scaffold collection global all_posts post
hc-scaffold collection by-author posts_by_author post
hc-scaffold collection global all_likes like
hc-scaffold collection global all_posts_entry_hash post:EntryHash
hc-scaffold collection by-author posts_by_author_entry_hash post:EntryHash

hc-scaffold link-type post like --delete true --bidirectional false
hc-scaffold link-type comment like:EntryHash --delete true --bidirectional true
hc-scaffold link-type certificate:EntryHash like --delete false --bidirectional false
hc-scaffold link-type agent:creator post:EntryHash --delete false --bidirectional true

nix develop --command bash -c "
print_version
cleanup_tmp "$1"

cd $TEMPLATE_PATH
hc-scaffold --template $2 web-app $1 --setup-nix true
cd $1

hc-scaffold dna forum
hc-scaffold zome posts --integrity dnas/forum/zomes/integrity/ --coordinator dnas/forum/zomes/coordinator/
hc-scaffold entry-type post --reference-entry-hash false --crud crud --link-from-original-to-each-update true --fields title:String:TextField,content:String:TextArea
hc-scaffold entry-type comment --reference-entry-hash false --crud crud --link-from-original-to-each-update false --fields post_hash:ActionHash::Post
hc-scaffold entry-type like --reference-entry-hash false --crud crd --fields like_hash:Option\<ActionHash\>::Like,string_list:Vec\<String\>
hc-scaffold entry-type certificate --reference-entry-hash true --crud cr --fields post_hash:ActionHash::Post,agent:AgentPubKey::certified,certifications_hashes:Vec\<EntryHash\>::Certificate,certificate_type:Enum::CertificateType:TypeOne.TypeTwo,dna_hash:DnaHash

hc-scaffold collection global all_posts post
hc-scaffold collection by-author posts_by_author post
hc-scaffold collection global all_likes like
hc-scaffold collection global all_posts_entry_hash post:EntryHash
hc-scaffold collection by-author posts_by_author_entry_hash post:EntryHash

hc-scaffold link-type post like --delete true --bidirectional false
hc-scaffold link-type comment like:EntryHash --delete true --bidirectional true
hc-scaffold link-type certificate:EntryHash like --delete false --bidirectional false
hc-scaffold link-type agent:creator post:EntryHash --delete false --bidirectional true

nix develop --command bash -c "
set -e
npm i
npm run build -w ui
npm t
npm run package
"
cd ..
cd ..
}

setup_and_build_hello_world() {
print_version
cleanup_tmp hello-world
print_version
cleanup_tmp hello-world

cd $TEMPLATE_PATH
hc-scaffold --template vanilla example hello-world
cd hello-world
cd $TEMPLATE_PATH
hc-scaffold --template vanilla example hello-world
cd hello-world

nix develop --command bash -c "
nix develop --command bash -c "
set -e
npm i
npm t
"
cd ..
cd ..
}

if [[ -n "$SCOPE" && "$SCOPE" == "hello_world" ]]; then
setup_and_build_hello_world
exit 0 # Exit early
setup_and_build_hello_world
exit 0 # Exit early
fi

if [[ -n "$SCOPE" && "$SCOPE" == "holo_integration" ]]; then
rm -rf /tmp/holo-flake
cd /tmp

hc-scaffold --template vue web-app holo-flake --setup-nix true
cd holo-flake

nix develop --command bash -c "
set -e
# Check if holo-dev-server is in the path
if command -v holo-dev-server >/dev/null 2>&1; then
echo 'holo-dev-server is available in the PATH while it should not'
exit 1
else
echo 'holo-dev-server is NOT available in the PATH'
fi
"

rm -rf /tmp/holo-flake
cd /tmp

hc-scaffold --template vue web-app holo-flake --setup-nix true --holo
cd holo-flake

nix develop --command bash -c "
set -e
# Check if holo-dev-server is in the path
if command -v holo-dev-server >/dev/null 2>&1; then
echo 'holo-dev-server is available in the PATH'
else
echo 'holo-dev-server is NOT available in the PATH'
exit 1
fi
"

rm -rf /tmp/holo-flake
cd /tmp

exit 0 # Exit early
fi

if [[ -z "$APP_NAME" || -z "$TEMPLATE_NAME" ]]; then
echo "Error: APP_NAME and TEMPLATE_NAME environment variables must be set."
exit 1
echo "Error: APP_NAME and TEMPLATE_NAME environment variables must be set."
exit 1
fi

case "$TEMPLATE_NAME" in
"svelte" | "lit" | "vue" | "vanilla")
# Valid template name, proceed
;;
# Valid template name, proceed
;;
*)
echo "Error: TEMPLATE_NAME must be one of 'svelte', 'lit', 'vue', or 'vanilla'."
exit 1
;;
echo "Error: TEMPLATE_NAME must be one of 'svelte', 'lit', 'vue', or 'vanilla'."
exit 1
;;
esac

cleanup_tmp "$APP_NAME"
Expand Down
12 changes: 9 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ pub enum HcScaffoldCommand {
Example {
/// Name of the example to scaffold. One of ['hello-world', 'forum'].
example: Option<Example>,

#[structopt(long = "holo", hidden = true)]
holo_enabled: bool,
},
}

Expand Down Expand Up @@ -674,7 +677,10 @@ Collection "{}" scaffolded!
println!("{}", i);
}
}
HcScaffoldCommand::Example { example } => {
HcScaffoldCommand::Example {
example,
holo_enabled,
} => {
let example = match example {
Some(e) => e,
None => choose_example()?,
Expand All @@ -695,7 +701,7 @@ Collection "{}" scaffolded!
Some(String::from("A simple 'hello world' application.")),
false,
&template_file_tree,
false,
holo_enabled,
)?;

file_tree
Expand All @@ -707,7 +713,7 @@ Collection "{}" scaffolded!
Some(String::from("A simple 'forum' application.")),
false,
&template_file_tree,
false,
holo_enabled,
)?;

// scaffold dna hello_world
Expand Down
34 changes: 25 additions & 9 deletions src/scaffold/app/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ use crate::error::{ScaffoldError, ScaffoldResult};
use crate::file_tree::*;
use crate::versions::holochain_nix_version;

pub fn flake_nix() -> FileTree {
pub fn flake_nix(holo_enabled: bool) -> FileTree {
let holochain_nix_version = holochain_nix_version();

let holo_inputs = holo_enabled
.then_some(
r#"
hds-releases.url = "github:holo-host/hds-releases";
"#,
)
.unwrap_or("");

let holo_packages = holo_enabled
.then_some("inputs'.hds-releases.packages.holo-dev-server-bin")
.unwrap_or("");

file!(format!(
r#"{{
description = "Template for Holochain app development";
Expand All @@ -23,6 +36,7 @@ pub fn flake_nix() -> FileTree {
nixpkgs.follows = "holochain-flake/nixpkgs";
flake-parts.follows = "holochain-flake/flake-parts";
{holo_inputs}
}};
outputs = inputs:
Expand All @@ -43,6 +57,7 @@ pub fn flake_nix() -> FileTree {
inputsFrom = [ inputs'.holochain-flake.devShells.holonix ];
packages = [
pkgs.nodejs_20
{holo_packages}
# more packages go here
];
}};
Expand All @@ -67,14 +82,15 @@ pub fn setup_nix_developer_environment(dir: &PathBuf) -> ScaffoldResult<()> {
.stderr(Stdio::null())
.current_dir(dir)
.args(["rev-parse", "--is-inside-work-tree"])
.output() {
Ok(output) => {
if output.status.success() && output.stdout == b"true\n" {
return Err(ScaffoldError::NixSetupError("- detected that Scaffolding is running inside an existing Git repository, please choose a different location to scaffold".to_string()));
}
},
Err(_) => {} // Ignore errors, Git isn't necessarily available.
.output()
{
Ok(output) => {
if output.status.success() && output.stdout == b"true\n" {
return Err(ScaffoldError::NixSetupError("- detected that Scaffolding is running inside an existing Git repository, please choose a different location to scaffold".to_string()));
}
}
Err(_) => {} // Ignore errors, Git isn't necessarily available.
}

println!("Setting up nix development environment...");

Expand All @@ -84,7 +100,7 @@ pub fn setup_nix_developer_environment(dir: &PathBuf) -> ScaffoldResult<()> {
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.current_dir(dir)
.args(["flake", "update"])
.args(["flake", "update", "--accept-flake-config"])
.output()?;

if !output.status.success() {
Expand Down
2 changes: 1 addition & 1 deletion src/scaffold/web_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn web_app_skeleton(
app_file_tree
.dir_content_mut()
.ok_or(ScaffoldError::PathNotFound(PathBuf::new()))?
.insert(OsString::from("flake.nix"), flake_nix());
.insert(OsString::from("flake.nix"), flake_nix(holo_enabled));
}

let mut scaffold_template_result =
Expand Down

0 comments on commit 06dccae

Please sign in to comment.