1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::result::Result;
use std::{fs, process::Command};
const NAME: &str = "$NAME";
const USER: &str = "$USER";
pub const CARTO_TOML: &str = r#"
[package]
name = "$NAME"
version = "0.1.0"
authors = ["$USER"]
edition = "2021"
license = "GPL-3.0"
[dependencies]
gstd = { git = "https://github.com/gear-tech/gear.git", features = ["debug"] }
[build-dependencies]
gear-wasm-builder = { git = "https://github.com/gear-tech/gear.git" }
[dev-dependencies]
gtest = { git = "https://github.com/gear-tech/gear.git" }
"#;
pub const BUILD_RS: &str = r#"
fn main() {
gear_wasm_builder::build();
}
"#;
pub const LIB_RS: &str = r#"
#![no_std]
use gstd::{debug, msg, prelude::*};
static mut MESSAGE_LOG: Vec<String> = vec![];
#[no_mangle]
pub unsafe extern "C" fn handle() {
let new_msg = String::from_utf8(msg::load_bytes()).expect("Invalid message");
if new_msg == "PING" {
msg::reply_bytes("PONG", 0).unwrap();
}
MESSAGE_LOG.push(new_msg);
debug!("{:?} total message(s) stored: ", MESSAGE_LOG.len());
}
"#;
pub fn create(name: &str) -> Result<()> {
let user_bytes = Command::new("git")
.args(&["config", "--global", "--get", "user.name"])
.output()?
.stdout;
let user = String::from_utf8_lossy(&user_bytes);
let email_bytes = Command::new("git")
.args(&["config", "--global", "--get", "user.email"])
.output()?
.stdout;
let email = String::from_utf8_lossy(&email_bytes);
fs::create_dir_all(format!("{}/src", name))?;
fs::write(
format!("{}/Cargo.toml", name),
CARTO_TOML
.replace(NAME, name)
.replace(USER, &format!("{} <{}>", user.trim(), email.trim())),
)?;
fs::write(format!("{}/build.rs", name), BUILD_RS)?;
fs::write(format!("{}/src/lib.rs", name), LIB_RS)?;
Ok(())
}