From ac3622cc6a19161b717947c112d6232e9265c3d7 Mon Sep 17 00:00:00 2001 From: Nate Butler Date: Thu, 14 Dec 2023 11:42:08 -0500 Subject: [PATCH] Generate `script/storybook` --- crates/storybook2/Cargo.toml | 7 +- .../src/bin/print_storybook_script.rs | 74 +++++++++++++++++++ crates/storybook2/src/{ => bin}/storybook2.rs | 9 +-- crates/storybook2/src/lib.rs | 4 + script/storybook | 63 +++++++++++++--- 5 files changed, 141 insertions(+), 16 deletions(-) create mode 100644 crates/storybook2/src/bin/print_storybook_script.rs rename crates/storybook2/src/{ => bin}/storybook2.rs (95%) create mode 100644 crates/storybook2/src/lib.rs diff --git a/crates/storybook2/Cargo.toml b/crates/storybook2/Cargo.toml index 16386706cfc70ff1325bf8048c4c4625ba4c907e..949d07b06f83bc4b18aaa7faa6b6c4ebfa2899d0 100644 --- a/crates/storybook2/Cargo.toml +++ b/crates/storybook2/Cargo.toml @@ -3,10 +3,15 @@ name = "storybook2" version = "0.1.0" edition = "2021" publish = false +default-run = "storybook" [[bin]] name = "storybook" -path = "src/storybook2.rs" +path = "src/bin/storybook2.rs" + +[[bin]] +name = "print_storybook_script" +path = "src/bin/print_storybook_script.rs" [dependencies] anyhow.workspace = true diff --git a/crates/storybook2/src/bin/print_storybook_script.rs b/crates/storybook2/src/bin/print_storybook_script.rs new file mode 100644 index 0000000000000000000000000000000000000000..d3409d6121779984a051383ce9ee97842f46625d --- /dev/null +++ b/crates/storybook2/src/bin/print_storybook_script.rs @@ -0,0 +1,74 @@ +use std::fs::File; +use std::io::Write; + +use strum::IntoEnumIterator; + +use storybook2::story_selector::ComponentStory; + +// TOOD: Ideally we actually create a more full featured CLI, +// but for the moment I just wanted a easier way to run the stories + +fn main() -> std::io::Result<()> { + let path = std::env::current_dir()?; + let out_file = path.join("script").join("storybook"); + + // the script output file + let mut file = File::create(out_file)?; + + // generate the list of components, in `snake_case` + let components = ComponentStory::iter() + .map(|c| c.to_string()) // Converts enum to string in `snake_case` + .collect::>(); + + // write the bash script + writeln!(file, "#!/bin/bash")?; + writeln!(file, "")?; + writeln!(file, "options=(")?; + for component in &components { + writeln!(file, " \"{}\"", component)?; + } + writeln!(file, ")")?; + writeln!(file, "")?; + + // Check if an argument is provided and if it matches a valid option + writeln!(file, "run_story() {{")?; + writeln!(file, " echo \"Running story: $1\"")?; + writeln!(file, " cargo run -p storybook2 -- \"$1\"")?; + writeln!(file, "}}")?; + writeln!(file, "")?; + + writeln!(file, "if [ \"$#\" -gt 0 ]; then")?; + writeln!(file, " story_arg=\"$1\"")?; + writeln!(file, " # Add prefix 'components/' if not present")?; + writeln!(file, " if [[ $story_arg != components/* ]]; then")?; + writeln!(file, " story_arg=\"components/$story_arg\"")?; + writeln!(file, " fi")?; + writeln!(file, " # Check if the provided story is a valid option")?; + writeln!(file, " for opt in \"${{options[@]}}\"; do")?; + writeln!( + file, + " if [[ \"components/$opt\" == \"$story_arg\" ]]; then" + )?; + writeln!(file, " run_story \"$story_arg\"")?; + writeln!(file, " exit")?; + writeln!(file, " fi")?; + writeln!(file, " done")?; + writeln!(file, " echo \"Invalid story name: $1\"")?; + writeln!(file, " exit 1")?; + writeln!(file, "fi")?; + writeln!(file, "")?; + + // Existing selection prompt + writeln!(file, "prompt=\"Please select a story:\"")?; + writeln!(file, "PS3=\"$prompt \"")?; + writeln!(file, "select story in \"${{options[@]}}\"; do")?; + writeln!(file, " if [[ -n $story ]]; then")?; + writeln!(file, " run_story \"components/$story\"")?; + writeln!(file, " break")?; + writeln!(file, " else")?; + writeln!(file, " echo \"Invalid option\"")?; + writeln!(file, " fi")?; + writeln!(file, "done")?; + + Ok(()) +} diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/bin/storybook2.rs similarity index 95% rename from crates/storybook2/src/storybook2.rs rename to crates/storybook2/src/bin/storybook2.rs index e1bb4ef3f46255381f919f789c9ec832911b30af..43f91db1a471f01d5d48e47161f6163f84dde62b 100644 --- a/crates/storybook2/src/storybook2.rs +++ b/crates/storybook2/src/bin/storybook2.rs @@ -1,7 +1,3 @@ -mod assets; -mod stories; -mod story_selector; - use std::sync::Arc; use clap::Parser; @@ -15,8 +11,9 @@ use simplelog::SimpleLogger; use theme2::{ThemeRegistry, ThemeSettings}; use ui::prelude::*; -use crate::assets::Assets; -use crate::story_selector::StorySelector; +use storybook2::assets::Assets; +pub use storybook2::story_selector::*; +// pub use crate::story_selector::{ComponentStory, StorySelector}; // gpui::actions! { // storybook, diff --git a/crates/storybook2/src/lib.rs b/crates/storybook2/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..f9c945f06ab73c0b314f78997cb9dafc94624118 --- /dev/null +++ b/crates/storybook2/src/lib.rs @@ -0,0 +1,4 @@ +pub mod assets; + +pub mod stories; +pub mod story_selector; diff --git a/script/storybook b/script/storybook index bcabdef0afa8f52e55673eeb652014edcae440da..2978a3d8f46c162a0108f0ef29cb7931ae6afd21 100755 --- a/script/storybook +++ b/script/storybook @@ -1,15 +1,60 @@ #!/bin/bash -# This script takes a single text input and replaces 'list_item' with the input in a cargo run command +options=( + "auto_height_editor" + "avatar" + "button" + "checkbox" + "context_menu" + "cursor" + "disclosure" + "focus" + "icon" + "icon_button" + "keybinding" + "label" + "list" + "list_header" + "list_item" + "overflow_scroll" + "scroll" + "tab" + "tab_bar" + "text" + "viewport_units" + "z_index" + "picker" +) -# Check if an argument is provided -if [ "$#" -ne 1 ]; then - echo "Usage: $0 " +run_story() { + echo "Running story: $1" + cargo run -p storybook2 -- "$1" +} + +if [ "$#" -gt 0 ]; then + story_arg="$1" + # Add prefix 'components/' if not present + if [[ $story_arg != components/* ]]; then + story_arg="components/$story_arg" + fi + # Check if the provided story is a valid option + for opt in "${options[@]}"; do + if [[ "components/$opt" == "$story_arg" ]]; then + run_story "$story_arg" + exit + fi + done + echo "Invalid story name: $1" exit 1 fi -# Assign the argument to a variable -COMPONENT_NAME="$1" - -# Run the cargo command with the provided component name -cargo run -p storybook2 -- components/"$COMPONENT_NAME" +prompt="Please select a story:" +PS3="$prompt " +select story in "${options[@]}"; do + if [[ -n $story ]]; then + run_story "components/$story" + break + else + echo "Invalid option" + fi +done