implement leo example command for lottery, tictactoe, token (#2514)

* implement leo example command for lottery, tictactoe, token

* remove unused cli arg

* update github actions

* update github actions
This commit is contained in:
Collin Chin 2023-08-08 09:42:30 -07:00 committed by GitHub
parent af1f7f96fb
commit 013809c076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 174 additions and 8 deletions

View File

@ -169,11 +169,13 @@ jobs:
mkdir tempdir mkdir tempdir
mv target/aarch64-apple-darwin/release/leo tempdir mv target/aarch64-apple-darwin/release/leo tempdir
cd tempdir cd tempdir
zip -r leo-${{ steps.get_version.outputs.version }}-aarch64-apple-darwin.zip leo zip -r leo.zip leo
cp leo.zip leo-${{ steps.get_version.outputs.version }}-aarch64-apple-darwin.zip
cd .. cd ..
mv tempdir/leo-${{ steps.get_version.outputs.version }}-aarch64-apple-darwin.zip . mv tempdir/leo-${{ steps.get_version.outputs.version }}-aarch64-apple-darwin.zip .
mv tempdir/leo.zip .
- name: Release - name: Release macos m1 version
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') if: startsWith(github.ref, 'refs/tags/')
with: with:
@ -182,6 +184,15 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release leo.zip
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
leo.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
windows: windows:
name: Windows name: Windows
runs-on: windows-latest runs-on: windows-latest

View File

@ -171,4 +171,11 @@ create_messages!(
msg: format!("Failed to parse the seed string for account.\nSnarkVM Error: {error}"), msg: format!("Failed to parse the seed string for account.\nSnarkVM Error: {error}"),
help: None, help: None,
} }
@backtraced
failed_to_write_file {
args: (error: impl Display),
msg: format!("Failed to write file.\nIO Error: {error}"),
help: None,
}
); );

View File

@ -34,9 +34,6 @@ pub struct CLI {
#[clap(subcommand)] #[clap(subcommand)]
command: Commands, command: Commands,
#[clap(help = "Custom Aleo PM backend URL", env = "APM_URL")]
api: Option<String>,
#[clap(long, global = true, help = "Optional path to Leo program root folder")] #[clap(long, global = true, help = "Optional path to Leo program root folder")]
path: Option<PathBuf>, path: Option<PathBuf>,
} }
@ -54,6 +51,11 @@ enum Commands {
#[clap(flatten)] #[clap(flatten)]
command: New, command: New,
}, },
#[clap(about = "Create a new Leo example package in a new directory")]
Example {
#[clap(subcommand)]
command: Example,
},
#[clap(about = "Compile the current package as a program")] #[clap(about = "Compile the current package as a program")]
Build { Build {
#[clap(flatten)] #[clap(flatten)]
@ -125,6 +127,7 @@ pub fn run_with_args(cli: CLI) -> Result<()> {
command.try_execute(context) command.try_execute(context)
} }
Commands::Clean { command } => command.try_execute(context), Commands::Clean { command } => command.try_execute(context),
Commands::Example { command } => command.try_execute(context),
Commands::Run { command } => command.try_execute(context), Commands::Run { command } => command.try_execute(context),
Commands::Execute { command } => command.try_execute(context), Commands::Execute { command } => command.try_execute(context),
Commands::Update { command } => command.try_execute(context), Commands::Update { command } => command.try_execute(context),

102
leo/cli/commands/example.rs Normal file
View File

@ -0,0 +1,102 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use std::fs;
/// The example programs that can be generated.
#[derive(Parser, Debug)]
pub enum Example {
#[clap(name = "lottery", about = "A public lottery program")]
Lottery,
#[clap(name = "tictactoe", about = "A standard tic-tac-toe game program")]
TicTacToe,
#[clap(name = "token", about = "A transparent & shielded custom token program")]
Token,
}
impl Command for Example {
type Input = <New as Command>::Output;
type Output = ();
fn prelude(&self, context: Context) -> Result<Self::Input> {
// Run leo new EXAMPLE_NAME
(New { name: self.name() }).execute(context)
}
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output>
where
Self: Sized,
{
let package_dir = context.dir()?;
// Write the main file.
let main_file_path = package_dir.join("src").join("main.leo");
fs::write(main_file_path, self.main_file_string()).map_err(CliError::failed_to_write_file)?;
// Write the input file.
let input_file_path = package_dir.join("inputs").join("input.in");
fs::write(input_file_path, self.input_file_string()).map_err(CliError::failed_to_write_file)?;
// Write the README file.
let readme_file_path = package_dir.join("README.md");
let readme_file_path_string = readme_file_path.display().to_string();
fs::write(readme_file_path, self.readme_file_string()).map_err(CliError::failed_to_write_file)?;
tracing::info!(
"🚀 To run the '{}' program follow the instructions at {}",
self.name().bold(),
readme_file_path_string
);
Ok(())
}
}
impl Example {
fn name(&self) -> String {
match self {
Example::Lottery => "lottery".to_string(),
Example::TicTacToe => "tictactoe".to_string(),
Example::Token => "token".to_string(),
}
}
fn main_file_string(&self) -> String {
match self {
Self::Lottery => include_str!("../../../examples/lottery/src/main.leo").to_string(),
Self::TicTacToe => include_str!("../../../examples/tictactoe/src/main.leo").to_string(),
Self::Token => include_str!("../../../examples/token/src/main.leo").to_string(),
}
}
fn input_file_string(&self) -> String {
match self {
Self::Lottery => include_str!("../../../examples/lottery/inputs/lottery.in").to_string(),
Self::TicTacToe => include_str!("../../../examples/tictactoe/inputs/tictactoe.in").to_string(),
Self::Token => include_str!("../../../examples/token/inputs/token.in").to_string(),
}
}
fn readme_file_string(&self) -> String {
match self {
Self::Lottery => include_str!("../../../examples/lottery/README.md").to_string(),
Self::TicTacToe => include_str!("../../../examples/tictactoe/README.md").to_string(),
Self::Token => include_str!("../../../examples/token/README.md").to_string(),
}
}
}

View File

@ -23,6 +23,9 @@ pub use build::Build;
pub mod clean; pub mod clean;
pub use clean::Clean; pub use clean::Clean;
pub mod example;
pub use example::Example;
pub mod execute; pub mod execute;
pub use execute::Execute; pub use execute::Execute;

View File

@ -22,7 +22,7 @@ use snarkvm::{cli::New as SnarkVMNew, file::AleoFile};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct New { pub struct New {
#[clap(name = "NAME", help = "Set package name")] #[clap(name = "NAME", help = "Set package name")]
name: String, pub(crate) name: String,
} }
impl Command for New { impl Command for New {
@ -43,8 +43,6 @@ impl Command for New {
SnarkVMNew::try_parse_from([SNARKVM_COMMAND, &self.name]).map_err(CliError::failed_to_parse_new)?; SnarkVMNew::try_parse_from([SNARKVM_COMMAND, &self.name]).map_err(CliError::failed_to_parse_new)?;
let result = command.parse().map_err(CliError::failed_to_execute_new)?; let result = command.parse().map_err(CliError::failed_to_execute_new)?;
// todo: modify the readme file to recommend building with `leo build`.
// Log the output of the `aleo new` command. // Log the output of the `aleo new` command.
tracing::info!("{}", result); tracing::info!("{}", result);

View File

@ -0,0 +1,42 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use super::*;
/// The example programs that can be generated.
pub enum Example {
Lottery,
TicTacToe,
Token,
}
impl Example {
pub fn main_file_string(&self, package_name: &str) -> String {
match self {
Self::Lottery => include_str!("../../examples/lottery/src/main.leo").to_string(),
Self::TicTacToe => include_str!("../../examples/tic_tac_toe/src/main.leo").to_string(),
Self::Token => include_str!("../../examples/token/src/main.leo").to_string(),
}
}
pub fn input_file_string(&self, package_name: &str) -> String {
match self {
Self::Lottery => include_str!("../../examples/lottery/inputs/input.in").to_string(),
Self::TicTacToe => include_str!("../../examples/tic_tac_toe/inputs/input.in").to_string(),
Self::Token => include_str!("../../examples/token/inputs/input.in").to_string(),
}
}
}