Skip to content

Commit

Permalink
add new command
Browse files Browse the repository at this point in the history
  • Loading branch information
eduadiez committed Jul 5, 2024
1 parent ebbf30c commit 75b533f
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ tokio = { version = "1", features = ["full"] }
serde_json = "1.0.120"
indicatif = "0.17.8"
futures = "0.3.30"
yansi = "1.0.1"
7 changes: 6 additions & 1 deletion src/bin/cargo-zisk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{Context, Result};
use cargo_zisk::commands::build_toolchain::BuildToolchainCmd;
use cargo_zisk::commands::install_toolchain::InstallToolchainCmd;

use cargo_zisk::commands::new::NewCmd;
use cargo_zisk::ZISK_VERSION_MESSAGE;
use clap::{Parser, Subcommand};

Expand All @@ -25,6 +25,7 @@ pub struct ZiskSdk {
pub enum ZiskSdkCommands {
BuildToolchain(BuildToolchainCmd),
InstallToolchain(InstallToolchainCmd),
New(NewCmd),
}

fn main() -> Result<()> {
Expand All @@ -42,6 +43,10 @@ fn main() -> Result<()> {
cmd.run()
.context("Error executing BuildToolchain command")?;
}
ZiskSdkCommands::New(cmd) => {
cmd.run()
.context("Error executing BuildToolchain command")?;
}
}
} else {
println!("No command provided");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/install_toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
#[derive(Parser)]
#[command(
name = "install-toolchain",
about = "Install the cargo-prove toolchain."
about = "Install the cargo-zisk toolchain."
)]
pub struct InstallToolchainCmd {}

Expand Down
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod build_toolchain;
pub mod install_toolchain;
pub mod install_toolchain;
pub mod new;
60 changes: 60 additions & 0 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use anyhow::Result;
use clap::Parser;
use std::{fs, path::Path, process::Command};
use yansi::Paint;

#[derive(Parser)]
#[command(name = "new", about = "Setup a new project that runs inside the ZisK.")]
pub struct NewCmd {
name: String,
}

const TEMPLATE_REPOSITORY_URL: &str = "https://github.com/0xPolygonHermez/hellozisk_rust";

impl NewCmd {
pub fn run(&self) -> Result<()> {
let root = Path::new(&self.name);

// Create the root directory if it doesn't exist.
if !root.exists() {
fs::create_dir(&self.name)?;
}

// Clone the repository.
let output = Command::new("git")
.arg("clone")
.arg(TEMPLATE_REPOSITORY_URL)
.arg(root.as_os_str())
.arg("--recurse-submodules")
.arg("--depth=1")
.output()
.expect("failed to execute command");
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(anyhow::anyhow!("failed to clone repository: {}", stderr));
}

// Remove the .git directory.
fs::remove_dir_all(root.join(".git"))?;

// Check if the user has `foundry` installed.
if Command::new("foundry").arg("--version").output().is_err() {
println!(
" \x1b[1m{}\x1b[0m Make sure to install Foundry to use contracts: https://book.getfoundry.sh/getting-started/installation.",
Paint::yellow("Warning:"),
);
}

println!(
" \x1b[1m{}\x1b[0m {} ({})",
Paint::green("Initialized"),
self.name,
std::fs::canonicalize(root)
.expect("failed to canonicalize")
.to_str()
.unwrap()
);

Ok(())
}
}
62 changes: 62 additions & 0 deletions ziskup/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash

# Reference: https://github.com/foundry-rs/foundry/blob/master/foundryup/install

set -e

echo Installing ziskup...

BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
ZISK_DIR=${ZISK_DIR-"$BASE_DIR/.zisk"}
ZISK_BIN_DIR="$ZISK_DIR/bin"

BIN_URL="https://raw.githubusercontent.com/0xPolygonHermez/zisk/main/ziskup/ziskup"
BIN_PATH="$ZISK_BIN_DIR/ziskup"

# Create the .zisk bin directory and ziskup binary if it doesn't exist.
mkdir -p $ZISK_BIN_DIR
curl -H "Authorization: token ${ZISK_TOKEN}" -# -L $BIN_URL -o $BIN_PATH
chmod +x $BIN_PATH

# Store the correct profile file (i.e. .profile for bash or .zshenv for ZSH).
case $SHELL in
*/zsh)
PROFILE=${ZDOTDIR-"$HOME"}/.zshenv
PREF_SHELL=zsh
;;
*/bash)
PROFILE=$HOME/.bashrc
PREF_SHELL=bash
;;
*/fish)
PROFILE=$HOME/.config/fish/config.fish
PREF_SHELL=fish
;;
*/ash)
PROFILE=$HOME/.profile
PREF_SHELL=ash
;;
*)
echo "ziskup: could not detect shell, manually add ${ZISK_BIN_DIR} to your PATH."
exit 1
;;
esac

# Only add ziskup if it isn't already in PATH.
if [[ ":$PATH:" != *":${ZISK_BIN_DIR}:"* ]]; then
# Add the ziskup directory to the path and ensure the old PATH variables remain.
echo >>$PROFILE && echo "export PATH=\"\$PATH:$ZISK_BIN_DIR\"" >>$PROFILE
fi

# Warn MacOS users that they may need to manually install libusb via Homebrew:
if [[ "$OSTYPE" =~ ^darwin ]] && [[ ! -f /usr/local/opt/libusb/lib/libusb-1.0.0.dylib && ! -f /opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib ]]; then
echo && echo "warning: libusb not found. You may need to install it manually on MacOS via Homebrew (brew install libusb)."
fi

# Warn MacOS users that they may need to manually install opensll via Homebrew:
if [[ "$OSTYPE" =~ ^darwin ]] && [[ ! -f /usr/local/opt/openssl/lib/libssl.3.dylib && ! -f /opt/homebrew/opt/openssl/lib/libssl.3.dylib ]]; then
echo && echo "warning: libusb not found. You may need to install it manually on MacOS via Homebrew (brew install openssl)."
fi

echo && echo "Detected your preferred shell is ${PREF_SHELL} and added ziskup to PATH. Run 'source ${PROFILE}' or start a new terminal session to use ziskup."
echo "Then, simply run 'ziskup' to install ZISK."
Loading

0 comments on commit 75b533f

Please sign in to comment.