Skip to content

Commit

Permalink
feat: Enhance custom template generation (#404)
Browse files Browse the repository at this point in the history
* feat: Add a new template subcommand to create new custom template bases
from existing templates

* Remove call to wrapCustoTemplate in scaffolding nix flake

* Update custom template docs
  • Loading branch information
c12i authored Oct 30, 2024
1 parent ba018be commit 21387f7
Show file tree
Hide file tree
Showing 104 changed files with 72 additions and 2,331 deletions.
12 changes: 1 addition & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
flake-parts.lib.mkFlake { inherit inputs; }
rec {
flake = {
templates.default = {
path = ./templates/custom-template;
description = "Custom template for the scaffolding tool";
};

lib.wrapCustomTemplate = { system, pkgs, customTemplatePath }:
let
scaffolding = inputs.holonix.packages.${system}.hc-scaffold;
Expand Down Expand Up @@ -70,7 +65,7 @@

# source filtering to ensure builds using include_str! or include_bytes! succeed
# https://crane.dev/faq/building-with-non-rust-includes.html
nonCargoBuildFiles = path: _type: builtins.match ".*(gitignore|md|hbs)$" path != null;
nonCargoBuildFiles = path: _type: builtins.match ".*(gitignore|md|hbs|nix|sh)$" path != null;
includeFilesFilter = path: type:
(craneLib.filterCargoSources path type) || (nonCargoBuildFiles path type);
in
Expand All @@ -95,11 +90,6 @@
nativeBuildInputs = [ pkgs.perl ];
};

checks.custom-template = flake.lib.wrapCustomTemplate {
inherit pkgs system;
customTemplatePath = ./templates/custom-template/custom-template;
};

devShells.default = pkgs.mkShell {
packages = (with inputs'.holonix.packages; [
holochain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ Custom template for the [scaffolding tool](https://github.com/holochain/scaffold

1. To scaffold a new project with this template, run this:

`nix run github:<TODO:REPLACE_ME_WITH_THE_APPROPRIATE_GIT_URL>#hc-scaffold-custom-template -- web-app`
```bash
nix run github:<TODO:REPLACE_ME_WITH_CUSTOM_TEMPLATE_GIT_URL>#app -- web-app
```

2. If you already have an existing project, add the `<TODO:REPLACE_ME_WITH_THE_APPROPRIATE_GIT_URL>` repository as input to your flake, and use it in the packages or your `devShell`:
2. If you already have an existing project, add the `<TODO:REPLACE_ME_WITH_CUSTOM_TEMPLATE_GIT_URL>` repository as input to your flake, and use it in the packages or your `devShell`:

```diff
{
description = "Template for Holochain app development";
description = "Flake for Holochain app development";

inputs = {
versions.url = "github:holochain/holochain?dir=versions/weekly";

holochain-flake.url = "github:holochain/holochain";
holochain-flake.inputs.versions.follows = "versions";

nixpkgs.follows = "holochain-flake/nixpkgs";
flake-parts.follows = "holochain-flake/flake-parts";
holonix.url = "github:holochain/holonix?ref=main";
nixpkgs.follows = "holonix/nixpkgs";
flake-parts.follows = "holonix/flake-parts";

+ scaffolding.url = "github:<TODO:REPLACE_ME_WITH_THE_APPROPRIATE_GIT_URL>";
+ scaffolding.url = "github:<TODO:REPLACE_ME_WITH_CUSTOM_TEMPLATE_GIT_URL>";
};

outputs = inputs:
Expand All @@ -32,7 +30,7 @@ Custom template for the [scaffolding tool](https://github.com/holochain/scaffold
inherit inputs;
}
{
systems = builtins.attrNames inputs.holochain-flake.devShells;
systems = builtins.attrNames inputs.holonix.devShells;
perSystem =
{ inputs'
, config
Expand All @@ -41,24 +39,24 @@ Custom template for the [scaffolding tool](https://github.com/holochain/scaffold
, ...
}: {
devShells.default = pkgs.mkShell {
inputsFrom = [ inputs'.holochain-flake.devShells.holonix ];
inputsFrom = [ inputs'.holonix.devShells.default ];
packages = [
pkgs.nodejs_20
# more packages go here
+ ] ++ [
+ inputs'.scaffolding.packages.hc-scaffold-custom-template
+ inputs'.scaffolding.packages.app
];
};
};
};
}
}
```

---

After this set up, you will be able to `nix develop` from inside your repository, and use the scaffolding tool as normal:

```
```bash
hc scaffold dna
hc scaffold zome
...
Expand All @@ -72,4 +70,4 @@ To run the tests for this custom template, simply run the `run_test.sh` script:

```bash
sh run_test.sh
```
```
File renamed without changes.
File renamed without changes.
67 changes: 43 additions & 24 deletions src/cli/template.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::{ffi::OsString, path::PathBuf};

use build_fs_tree::{dir, Build, MergeableFileSystemTree};
use dialoguer::{theme::ColorfulTheme, Input};
use build_fs_tree::{dir, file, Build, MergeableFileSystemTree};
use structopt::StructOpt;

use crate::scaffold::web_app::template_type::TemplateType;
use crate::{scaffold::web_app::template_type::TemplateType, utils::input_with_case};

#[derive(Debug, StructOpt)]
#[structopt(setting = structopt::clap::AppSettings::InferSubcommands)]
/// Manage custom templates
pub enum Template {
/// Create a new template from an existing scaffolding template
New,
/// Clone the template in use into a new custom template
Clone {
#[structopt(long)]
Expand All @@ -20,14 +21,44 @@ pub enum Template {

impl Template {
pub fn run(self, template_type: &TemplateType) -> anyhow::Result<()> {
let target_template = match self.target_template() {
match self {
Template::New => Template::new_template(template_type),
Template::Clone { to_template } => Template::clone_template(to_template, template_type),
}
}

fn new_template(from_template: &TemplateType) -> anyhow::Result<()> {
let name = input_with_case(
"Enter new template name (kebab-case):",
Some(&from_template.name()),
convert_case::Case::Kebab,
)?;

let template_file_tree = dir! {
name.clone() => dir!{
"template" => from_template.file_tree()?,
"README.md" => file!(include_str!("custom-template/README.md")),
"flake.nix" => file!(include_str!("custom-template/flake.nix")),
"run_test.sh" => file!(include_str!("custom-template/run_test.sh"))
},
};

let file_tree = MergeableFileSystemTree::<OsString, String>::from(template_file_tree);

file_tree.build(&PathBuf::from("."))?;

println!(r#"Template initialized in path: ./{} "#, name);

Ok(())
}

fn clone_template(
to_template: Option<String>,
template_type: &TemplateType,
) -> anyhow::Result<()> {
let target_template = match to_template {
Some(t) => t,
None => {
// Enter template name
Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter new template name:")
.interact()?
}
None => input_with_case("Enter new template name:", None, convert_case::Case::Kebab)?,
};

let template_file_tree = dir! {
Expand All @@ -38,20 +69,8 @@ impl Template {

file_tree.build(&PathBuf::from("."))?;

match self {
Template::Clone { .. } => {
println!(r#"Template initialized to folder {:?} "#, target_template);
}
}
Ok(())
}
println!(r#"Template initialized in path: ./{} "#, target_template);

pub fn target_template(&self) -> Option<String> {
match self {
Template::Clone {
to_template: target_template,
..
} => target_template.clone(),
}
Ok(())
}
}
17 changes: 13 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,19 @@
//! To create a custom template, execute these steps:
//!
//! 1. Run this command:
//! `nix flake init -t github:holochain/scaffolding`
//! 2. A new dir `custom-template` will be created in the current directory. Check this new folder in a version control system like git.
//! 3. Replace all instances of `<TODO:REPLACE_ME_WITH_THE_APPROPRIATE_GIT_URL>` in its `README.md` file with the appropriate git URL (eg. "github:holochain-open-dev/templates").
//! 4. Replace all instances of `<TODO:REPLACE_ME_WITH_THE_APPROPRIATE_GIT_URL>` in its `template/web-app/flake.nix.hbs` file with the appropriate git URL (eg. "github:holochain-open-dev/templates").
//! - Through holonix:
//!
//! ```bash
//! nix run github:holochain/holonix#hc-scaffold -- template new
//! ```
//! - Outside holonix if the cli was installed via `cargo install holochain_scaffolding_cli`
//!
//! ```bash
//! hc-scaffold template new
//! ```
//! 2. A new dir will be created in the current directory. Check this new folder in a version control system like git.
//! 3. Replace all instances of `<TODO:REPLACE_ME_WITH_CUSTOM_TEMPLATE_GIT_URL>` in its `README.md` file with the appropriate git URL (eg. "github:holochain-open-dev/templates").
//! 4. Replace all instances of `<TODO:REPLACE_ME_WITH_CUSTOM_TEMPLATE_GIT_URL>` in its `template/web-app/flake.nix.hbs` file with the appropriate git URL (eg. "github:holochain-open-dev/templates").
//!
//! That's it! At this point you will have a correctly functioning custom template repository with tests, a `README.md` documenting how to use it, and a `template` folder. That's where your custom template lives.
//!
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 21387f7

Please sign in to comment.