Skip to content

Commit

Permalink
commands wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lochlan Wansbrough committed Oct 12, 2024
1 parent 665b81b commit 73a5912
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 60 deletions.
23 changes: 1 addition & 22 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,7 @@ impl App {
rune::runtime::test(input_path.to_path_buf(), binary).await;
}
Some(CliCommand::Run { release }) => {
crate::commands::build::build(release).await?;

let current_dir = env::current_dir()?;
let config = std::fs::read_to_string(current_dir.join("rune.toml"))
.unwrap()
.parse::<Table>()
.unwrap();

let entrypoint_path = match config["build"]["entrypoint"].as_str() {
Some(entrypoint_path) => entrypoint_path,
None => panic!("No build input provided in config!"),
};

match config["build"]["output"].as_str() {
Some(output_path) => {
let output_path = current_dir.join(output_path);
let entrypoint_path = output_path.join(entrypoint_path);
let binary = std::fs::read(entrypoint_path).unwrap();
rune::runtime::run(output_path.to_path_buf(), binary);
}
None => panic!("No build input provided in config!"),
}
crate::commands::run::run(release).await?;
}
Some(CliCommand::Build { release }) => {
crate::commands::build::build(release).await?;
Expand Down
13 changes: 13 additions & 0 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use rust_embed::Embed;

#[derive(Embed)]
#[folder = "crates/rune/wit"]
pub struct RuneWits;

#[derive(Embed)]
#[folder = "crates/rune/wit/runtime"]
pub struct RuneRuntimeWits;

#[derive(Embed)]
#[folder = "src/templates"]
pub struct Templates;
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod build;
pub mod bundle;
pub mod docs;
pub mod new;
pub mod run;
pub mod upgrade;
14 changes: 3 additions & 11 deletions src/commands/docs/docs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rust_embed::Embed;
use std::{
collections::{HashMap, VecDeque},
path::Path,
Expand All @@ -17,16 +16,9 @@ use wit_parser::{
};

use crate::{
action::Action,
components::Component,
config::{Config, KeyBindings},
tui::Event,
action::Action, assets::RuneRuntimeWits, components::Component, config::{Config, KeyBindings}, tui::Event
};

#[derive(Embed)]
#[folder = "crates/rune/wit/runtime"]
struct RuneWit;

#[derive(Clone)]
pub enum CurrentItem {
Package(UnresolvedPackage),
Expand Down Expand Up @@ -388,8 +380,8 @@ impl Docs {
impl Default for Docs {
fn default() -> Self {
let mut source_map = SourceMap::new();
for wit_name in RuneWit::iter() {
let wit = RuneWit::get(&wit_name).unwrap();
for wit_name in RuneRuntimeWits::iter() {
let wit = RuneRuntimeWits::get(&wit_name).unwrap();
source_map.push(
Path::new(wit_name.as_ref()),
std::str::from_utf8(&wit.data).unwrap(),
Expand Down
36 changes: 31 additions & 5 deletions src/commands/new/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ use std::{
path::{Path, PathBuf},
};

use crate::Result;
use crate::{assets::{RuneWits, Templates}, Result};

use liquid::Object;

#[derive(Embed)]
#[folder = "src/templates"]
struct Templates;

pub async fn game(
identifier: &Option<String>,
name: &Option<String>,
Expand Down Expand Up @@ -43,6 +39,8 @@ pub async fn game(
&globals,
)?;

copy_wits(project_root_path.as_path())?;

Ok(())
}

Expand All @@ -59,6 +57,10 @@ pub fn template_files(
.strip_prefix(&format!("{template_type}/{template_key}/"))
.unwrap();
let destination_path = project_root.join(PathBuf::from(relative_path));

let destination_parent = destination_path.as_path().parent().unwrap();
std::fs::create_dir_all(destination_parent).unwrap();

let mut file = fs::OpenOptions::new()
.create(true)
.read(true)
Expand All @@ -78,3 +80,27 @@ pub fn template_files(

Ok(())
}

pub fn copy_wits(
project_root: &Path,
) -> crate::Result<()> {
for wit_path in RuneWits::iter() {
let contents = RuneWits::get(wit_path.as_ref()).unwrap();
let destination_path = project_root
.join(".rune/wit")
.join(PathBuf::from(wit_path.as_ref()));

let destination_parent = destination_path.as_path().parent().unwrap();
std::fs::create_dir_all(destination_parent).unwrap();

let mut file = fs::OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(&destination_path.clone())?;

file.write_all(&contents.data)?;
}

Ok(())
}
35 changes: 35 additions & 0 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;

use toml::Table;

use crate::cli::NewSubcommand;

use crate::Result;


pub async fn run(release: &bool) -> Result<()> {
crate::commands::build::build(release).await?;

let current_dir = env::current_dir()?;
let config = std::fs::read_to_string(current_dir.join("rune.toml"))
.unwrap()
.parse::<Table>()
.unwrap();

let entrypoint_path = match config["build"]["entrypoint"].as_str() {
Some(entrypoint_path) => entrypoint_path,
None => panic!("No build input provided in config!"),
};

match config["build"]["output"].as_str() {
Some(output_path) => {
let output_path = current_dir.join(output_path);
let entrypoint_path = output_path.join(entrypoint_path);
let binary = std::fs::read(entrypoint_path).unwrap();
rune::runtime::run(output_path.to_path_buf(), binary);
}
None => panic!("No build input provided in config!"),
}

Ok(())
}
41 changes: 21 additions & 20 deletions src/commands/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use flate2::bufread::GzDecoder;
use reqwest::{header::{HeaderMap, HeaderValue, USER_AGENT}, Client};
use tar::Archive;
use serde_json::Value;
use tempfile::TempDir;

use crate::Result;

pub async fn upgrade() -> Result<()> {
println!("upgrade");

let latest_version = {
let client = Client::new();

Expand All @@ -32,35 +31,37 @@ pub async fn upgrade() -> Result<()> {
let tarball_name = format!("rune-cli-{latest_version}-{platform}.tar.gz");
let tarball_url = format!("https://github.com/rune-runtime/rune/releases/download/{latest_version}/{tarball_name}");

let tmp_path = download_bin(
let tmp_dir = tempfile::Builder::new()
.prefix(&format!(".update-{latest_version}"))
.tempdir_in(::std::env::current_dir()?)?;

let tmp_path = tmp_dir.path().to_path_buf();

download_tarball(
&latest_version,
&tarball_url,
&tmp_path
).await?;

let mut tar_gz = File::open(tmp_path.clone())?;
let mut tar_bytes = Vec::new();
tar_gz.read_to_end(&mut tar_bytes)?;
let tar = GzDecoder::new(&tar_bytes[..]);
let mut archive = Archive::new(&tar_bytes[..]);
archive.unpack(".")?;

let new_bin = tmp_path.join("rune-cli");

self_replace::self_replace(new_bin)?;

println!("Successfully upgraded to {latest_version}");

Ok(())
}

async fn download_bin(version: &str, url: &str) -> Result<PathBuf> {
async fn download_tarball(version: &str, url: &str, tmp_path: &PathBuf) -> Result<()> {
let response = reqwest::get(url).await?;
let tmp_tarball = tmp_path.join("rune.tar.gz");
let targz_bytes = response.bytes().await?;

let tmp_dir = tempfile::Builder::new()
.prefix(&format!(".update-{version}"))
.tempdir_in(::std::env::current_dir()?)?;

let tmp_path = tmp_dir.path().to_path_buf();
let mut gz = GzDecoder::new(&targz_bytes[..]);
let mut tar_bytes = Vec::<u8>::new();
gz.read_to_end(&mut tar_bytes)?;
let mut archive = Archive::new(&tar_bytes[..]);
archive.unpack(tmp_path)?;

let mut file = std::fs::File::open(tmp_dir)?;
let mut content = Cursor::new(response.bytes().await?);
std::io::copy(&mut content, &mut file)?;
Ok(tmp_path)
Ok(())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(unused_variables)]

pub mod action;
pub mod assets;
pub mod app;
pub mod cli;
pub mod commands;
Expand Down
2 changes: 1 addition & 1 deletion src/templates/game/cube-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
wit_bindgen::generate!({
world: "runtime",
path: "../.rune/wit/runtime",
path: ".rune/wit/runtime",
exports: {
"rune:runtime/guest": Game
},
Expand Down
1 change: 1 addition & 0 deletions src/templates/game/hello-js/rune.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type = "game"
version = "{{ runtime_version }}"

[build]
pre = "npm run build"
input = "./dist" # Your guest code build output, the files you want to package
entrypoint = "game.wasm" # The path to your wasm binary relative to the "input" directory
output = "./bin" # Rune's build output
Expand Down
2 changes: 1 addition & 1 deletion src/templates/game/hello-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
wit_bindgen::generate!({
world: "runtime",
path: "../.rune/wit/runtime",
path: ".rune/wit/runtime",
exports: {
"rune:runtime/guest": Game
},
Expand Down

0 comments on commit 73a5912

Please sign in to comment.