Skip to content

Commit

Permalink
feat(cli): created very basic cli scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Dec 27, 2021
1 parent a3402f2 commit 6b836ef
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Cargo.lock
node_modules/
.perseus/
tailwind.css
# This is still included in the Cargo package
.tribble/
12 changes: 12 additions & 0 deletions bonnie.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ check.cmd = [
"cargo watch"
]
check.desc = "watches the project for changes and checks code validity"

prep-wasm.cmd = [
"rm -rf packages/tribble-cli/.tribble",
"mkdir packages/tribble-cli/.tribble",
"mkdir packages/tribble-cli/.tribble/pkg",
"mkdir packages/tribble-cli/.tribble/dist",
"cd packages/tribble/.perseus",
"wasm-pack build --target web --release",
"mv pkg/perseus_engine_bg.wasm ../../tribble-cli/.tribble/pkg/bundle.wasm",
"mv pkg/perseus_engine.js ../../tribble-cli/.tribble/pkg/bundle.js"
]
prep-wasm.desc = "builds the wasm artifacts and prepares them for the cli"
13 changes: 13 additions & 0 deletions packages/tribble-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@
name = "tribble-cli"
version = "0.1.0"
edition = "2021"
include = [
"src/",
"Cargo.toml",
".perseus/"
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
thiserror = "1"
fmterr = "0.1"
clap = { version = "=3.0.0-beta.5", features = [ "color" ] }
include_dir = "0.6"

[[bin]]
name = "tribble"
path = "src/main.rs"
16 changes: 16 additions & 0 deletions packages/tribble-cli/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum PrepError {
#[error("couldn't extract internal `.tribble/` directory, please ensure you have write permissions here")]
ExtractionFailed {
target_dir: Option<String>,
#[source]
source: std::io::Error
},
#[error("couldn't update your `.gitignore`, you should add `.tribble/` to it manually")]
GitignoreUpdateFailed {
#[source]
source: std::io::Error
}
}
7 changes: 7 additions & 0 deletions packages/tribble-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
mod options;
mod prep;
mod errors;

/// The current version of the CLI, extracted from the crate version.
pub const TRIBBLE_VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() {
println!("Hello, world!");
}
24 changes: 24 additions & 0 deletions packages/tribble-cli/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![allow(missing_docs)] // Prevents double-documenting some things

use clap::Parser;
use crate::TRIBBLE_VERSION;

// The documentation for the `Opts` struct will appear in the help page, hence the lack of puncutation and the lowercasing in places

/// Tribble creates seamless contribution experiences for your project.
#[derive(Parser)]
#[clap(version = TRIBBLE_VERSION)]
pub struct Opts {
#[clap(subcommand)]
pub subcmd: Subcommand,
}

#[derive(Parser)]
pub enum Subcommand {
/// Builds your Tribble workflows. This is called by `tribble serve` automatically.
Build,
/// Serves your Tribble workflows locally for development.
Serve,
/// Builds your Tribble workflows for release deployment.
Deploy
}
55 changes: 55 additions & 0 deletions packages/tribble-cli/src/prep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::path::PathBuf;

use include_dir::{include_dir, Dir};
use crate::errors::*;
use std::fs::{self, OpenOptions};
use std::io::Write;

// We include the `.tribble/` directory because it contains pre-built Wasm artifacts
static TRIBBLE_DIR: Dir = include_dir!("./.tribble");

/// Prepares the directory for running Tribble by writing the directory. Tribble has no prerequisites (not even `cargo`), so
/// this doesn't need to do any prerequisite checking.
pub fn prep(dir: PathBuf) -> Result<(), PrepError> {
let tribble_dir = dir.join("./.tribble");
if tribble_dir.exists() {
// The directory already exists, so we'll assume we don't need to do anything
// This will run before every command, so we don't want to be needlessly recreating things
Ok(())
} else {
// Write the stored directory to that location, creating the directory first
if let Err(err) = fs::create_dir(&tribble_dir) {
return Err(PrepError::ExtractionFailed {
target_dir: tribble_dir.to_str().map(|s| s.to_string()),
source: err,
});
}
// Notably, this function will not do anything or tell us if the directory already exists...
if let Err(err) = TRIBBLE_DIR.extract(&tribble_dir) {
return Err(PrepError::ExtractionFailed {
target_dir: tribble_dir.to_str().map(|s| s.to_string()),
source: err,
});
}

// If we aren't already gitignoring the Tribble directory, update `.gitignore` to do so
if let Ok(contents) = fs::read_to_string(".gitignore") {
if contents.contains(".tribble/") {
return Ok(());
}
}
let file = OpenOptions::new()
.append(true)
.create(true) // If it doesn't exist, create it
.open(".gitignore");
let mut file = match file {
Ok(file) => file,
Err(err) => return Err(PrepError::GitignoreUpdateFailed { source: err }),
};
// Check for errors with appending to the file
if let Err(err) = file.write_all(b".tribble/") {
return Err(PrepError::GitignoreUpdateFailed { source: err });
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion packages/tribble/src/templates/workflow/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn workflow_inner(
HistoryBreadcrumbs()
// We want to alert screenreaders that this entire section can be swapped out for new content
main(class = "section-content", aria-live = "assertive", aria-atomic = true) {
(*active_page.get())
(*page.get())
}
}
}
Expand Down

0 comments on commit 6b836ef

Please sign in to comment.