This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sidekick): add backend gen command (#273)
- Loading branch information
1 parent
5fc1c89
commit 2f1358e
Showing
3 changed files
with
138 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use std::{collections::HashMap, io::Write, path::PathBuf}; | ||
use std::{collections::HashMap, io::Write, path::PathBuf, str::FromStr}; | ||
|
||
use clap::Parser; | ||
use cli_core::rivet_api::{apis, models}; | ||
|
@@ -131,13 +131,38 @@ pub struct OpenGbCommandOpts { | |
pub cwd: PathBuf, | ||
} | ||
|
||
pub async fn run_opengb_command(opts: OpenGbCommandOpts) -> GlobalResult<std::process::ExitStatus> { | ||
let run_native = std::env::var("_RIVET_NATIVE_OPENGB") | ||
.ok() | ||
.map_or(false, |x| &x == "1"); | ||
#[derive(PartialEq)] | ||
pub enum OpenGbTarget { | ||
Native, | ||
Docker, | ||
} | ||
|
||
impl Default for OpenGbTarget { | ||
fn default() -> Self { | ||
Self::Docker | ||
} | ||
} | ||
impl FromStr for OpenGbTarget { | ||
type Err = GlobalError; | ||
|
||
fn from_str(s: &str) -> GlobalResult<Self> { | ||
match s { | ||
"native" => Ok(Self::Native), | ||
"docker" => Ok(Self::Docker), | ||
_ => bail!("unknown opengb target: {s}"), | ||
} | ||
} | ||
} | ||
|
||
pub fn build_opengb_command(opts: OpenGbCommandOpts) -> GlobalResult<Command> { | ||
let opengb_target = if let Ok(x) = std::env::var("RIVET_OPENGB_TARGET") { | ||
OpenGbTarget::from_str(&x)? | ||
} else { | ||
OpenGbTarget::default() | ||
}; | ||
|
||
// Check OpenGB installed | ||
if run_native { | ||
if opengb_target == OpenGbTarget::Native { | ||
ensure!( | ||
which::which("opengb").is_ok(), | ||
"OpenGB is not installed. Install it from {}.", | ||
|
@@ -146,39 +171,46 @@ pub async fn run_opengb_command(opts: OpenGbCommandOpts) -> GlobalResult<std::pr | |
} | ||
|
||
// Build command | ||
if run_native { | ||
let mut cmd = Command::new("opengb"); | ||
cmd.envs(opts.env); | ||
cmd.current_dir(opts.cwd); | ||
cmd.args(&opts.args); | ||
Ok(cmd.status().await?) | ||
} else { | ||
let image_tag = std::env::var("_RIVET_OPENGB_IMAGE") | ||
.ok() | ||
.unwrap_or_else(|| DEFAULT_OPENGB_DOCKER_TAG.to_string()); | ||
|
||
// Build env file | ||
let mut env_file = NamedTempFile::new().expect("Failed to create temp file"); | ||
for (k, v) in std::env::vars() { | ||
writeln!(env_file, "{k}={v}")?; | ||
} | ||
if std::env::var("DATABASE_URL").is_err() { | ||
writeln!(env_file, "DATABASE_URL=postgres://postgres:[email protected]:5432/postgres?sslmode=disable")?; | ||
match opengb_target { | ||
OpenGbTarget::Native => { | ||
let mut cmd = Command::new("opengb"); | ||
cmd.envs(opts.env); | ||
cmd.current_dir(opts.cwd); | ||
cmd.args(&opts.args); | ||
Ok(cmd) | ||
} | ||
for (k, v) in opts.env { | ||
writeln!(env_file, "{k}={v}")?; | ||
OpenGbTarget::Docker => { | ||
let image_tag = std::env::var("RIVET_OPENGB_DOCKER_IMAGE") | ||
.ok() | ||
.unwrap_or_else(|| DEFAULT_OPENGB_DOCKER_TAG.to_string()); | ||
|
||
// Build env file | ||
let mut env_file = NamedTempFile::new().expect("Failed to create temp file"); | ||
for (k, v) in std::env::vars() { | ||
writeln!(env_file, "{k}={v}")?; | ||
} | ||
if std::env::var("DATABASE_URL").is_err() { | ||
writeln!(env_file, "DATABASE_URL=postgres://postgres:[email protected]:5432/postgres?sslmode=disable")?; | ||
} | ||
for (k, v) in opts.env { | ||
writeln!(env_file, "{k}={v}")?; | ||
} | ||
|
||
let mut cmd = Command::new("docker"); | ||
cmd.arg("run").arg("-it"); | ||
cmd.arg("--init"); | ||
cmd.arg("--env-file").arg(env_file.path()); | ||
cmd.arg("--add-host=host.docker.internal:host-gateway"); | ||
cmd.arg("--publish=6420:6420"); | ||
cmd.arg(format!("--volume={}:/backend", opts.cwd.display())); | ||
cmd.arg("--workdir=/backend"); | ||
cmd.arg(image_tag); | ||
cmd.args(&opts.args); | ||
Ok(cmd) | ||
} | ||
|
||
let mut cmd = Command::new("docker"); | ||
cmd.arg("run").arg("-it"); | ||
cmd.arg("--init"); | ||
cmd.arg("--env-file").arg(env_file.path()); | ||
cmd.arg("--add-host=host.docker.internal:host-gateway"); | ||
cmd.arg("--publish=6420:6420"); | ||
cmd.arg(format!("--volume={}:/backend", opts.cwd.display())); | ||
cmd.arg("--workdir=/backend"); | ||
cmd.arg(image_tag); | ||
cmd.args(&opts.args); | ||
Ok(cmd.status().await?) | ||
} | ||
} | ||
|
||
pub async fn run_opengb_command(opts: OpenGbCommandOpts) -> GlobalResult<std::process::ExitStatus> { | ||
Ok(build_opengb_command(opts)?.status().await?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use clap::Parser; | ||
use global_error::prelude::*; | ||
use serde::Serialize; | ||
use std::collections::HashMap; | ||
|
||
use crate::commands::backend; | ||
|
||
use super::SideKickHandler; | ||
|
||
#[derive(Parser)] | ||
pub struct Opts { | ||
#[clap(long)] | ||
output_path: String, | ||
#[clap(long)] | ||
unity: bool, | ||
#[clap(long)] | ||
godot: bool, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct Output { | ||
pub exit_code: i32, | ||
pub stdout: String, | ||
pub stderr: String, | ||
} | ||
|
||
impl SideKickHandler for Output {} | ||
|
||
impl Opts { | ||
pub async fn execute(&self) -> GlobalResult<Output> { | ||
// Run command | ||
let target = if self.unity { | ||
"unity" | ||
} else if self.godot { | ||
"godot" | ||
} else { | ||
bail!("no target selected") | ||
}; | ||
let args = vec![ | ||
"sdk".into(), | ||
"generate".into(), | ||
"--output".into(), | ||
self.output_path.clone(), | ||
target.into(), | ||
]; | ||
let mut env = HashMap::new(); | ||
env.insert("OPENGB_TERM_COLOR".into(), "never".into()); | ||
let opengb_output = backend::build_opengb_command(backend::OpenGbCommandOpts { | ||
args, | ||
env, | ||
cwd: std::env::current_dir()?, | ||
})? | ||
.output() | ||
.await?; | ||
|
||
let output = Output { | ||
exit_code: opengb_output.status.code().unwrap_or(1), | ||
stdout: String::from_utf8(opengb_output.stdout)?, | ||
stderr: String::from_utf8(opengb_output.stderr)?, | ||
}; | ||
|
||
Ok(output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters