Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

chore: simplify backend_source_path setting #357

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions packages/toolchain/src/backend/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::path::PathBuf;
use tokio::fs;

const BACKEND_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../backend");
const DENO_CONFIG: &'static str = include_str!("../../../../deno.jsonc");
const DENO_LOCKFILE: &'static str = include_str!("../../../../deno.lock");

/// Return a path for the backend. If one does not exist, the backend dir will automatically be
/// extracted.
Expand All @@ -28,8 +26,6 @@ pub async fn backend_dir() -> Result<PathBuf> {
if !backend_dir.exists() {
fs::create_dir_all(&backend_dir).await?;
tokio::task::block_in_place(|| BACKEND_DIR.extract(&backend_dir))?;
fs::write(backend_dir.join("deno.jsonc"), DENO_CONFIG).await?;
fs::write(backend_dir.join("deno.lock"), DENO_LOCKFILE).await?;
}

Ok(backend_dir)
Expand Down
42 changes: 4 additions & 38 deletions packages/toolchain/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ use crate::{
ToolchainCtx,
};

const OPENGB_DENO_CONFIG_PATH: &'static str = "/deno.jsonc";
const OPENGB_DENO_LOCK_PATH: &'static str = "/deno.lock";
const OPENGB_CLI_MAIN_PATH: &'static str = "/cli/main.ts";

pub struct BackendCommandOpts {
pub command: &'static str,
pub opts: serde_json::Value,
Expand All @@ -29,7 +25,7 @@ pub struct BackendCommandOpts {
async fn base_url() -> Result<String> {
// Attempt to read from user or default
let base_url = if let Some(url) =
config::settings::try_read(|x| Ok(x.backend.opengb_url.clone())).await?
config::settings::try_read(|x| Ok(x.backend.backend_source_path.clone())).await?
{
url
} else {
Expand All @@ -43,36 +39,6 @@ async fn base_url() -> Result<String> {
pub async fn build_opengb_command(opts: BackendCommandOpts) -> Result<Command> {
let base_url = base_url().await?;

// Download config from remote if needed.
//
// Deno does not support pulling config from URL.
let (deno_config_path, deno_lock_path) =
if base_url.starts_with("http://") || base_url.starts_with("https://") {
let temp_dir = tempfile::tempdir()?.into_path();

let deno_config_path = temp_dir.join("deno.jsonc");
let deno_config_url = format!("{base_url}{OPENGB_DENO_CONFIG_PATH}");
let response = reqwest::get(&deno_config_url).await?.error_for_status()?;
let deno_config_content = response.text().await?;
tokio::fs::write(&deno_config_path, deno_config_content).await?;

let deno_lock_path = temp_dir.join("deno.jsonc");
let deno_lock_url = format!("{base_url}{OPENGB_DENO_LOCK_PATH}");
let response = reqwest::get(&deno_lock_url).await?.error_for_status()?;
let deno_lock_content = response.text().await?;
tokio::fs::write(&deno_lock_path, deno_lock_content).await?;

(
deno_config_path.to_str().unwrap().to_string(),
deno_lock_path.to_str().unwrap().to_string(),
)
} else {
(
format!("{base_url}{OPENGB_DENO_CONFIG_PATH}"),
format!("{base_url}{OPENGB_DENO_LOCK_PATH}"),
)
};

// Get Deno executable
let deno = crate::util::deno::get_or_download_executable(
crate::util::deno::DEFAULT_VERSION,
Expand All @@ -98,10 +64,10 @@ pub async fn build_opengb_command(opts: BackendCommandOpts) -> Result<Command> {
"--allow-write",
"--allow-sys",
"--config",
&deno_config_path,
&format!("{base_url}/deno.jsonc"),
"--lock",
&deno_lock_path,
&format!("{base_url}{OPENGB_CLI_MAIN_PATH}"),
&format!("{base_url}/deno.lock"),
&format!("{base_url}/cli/main.ts"),
"--command",
backend_cmd,
]);
Expand Down
10 changes: 5 additions & 5 deletions packages/toolchain/src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub struct Settings {
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BackendConfig {
/// Env vars to pass to all OpenGB commands.
/// Env vars to pass to all backend commands.
#[serde(default)]
pub command_environment: HashMap<String, String>,
#[serde(default)]
pub opengb_url: Option<String>,
pub backend_source_path: Option<String>,

#[serde(default)]
pub sdk: BackendSdkConfig,
Expand All @@ -39,7 +39,7 @@ pub struct BackendConfig {
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BackendSdkConfig {
/// Env vars to pass to the deploy OpenGB commands.
/// Env vars to pass to the deploy backend commands.
#[serde(default)]
pub command_environment: HashMap<String, String>,
#[serde(default)]
Expand All @@ -49,7 +49,7 @@ pub struct BackendSdkConfig {
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BackendDevConfig {
/// Env vars to pass to the deploy OpenGB commands.
/// Env vars to pass to the deploy backend commands.
#[serde(default)]
pub command_environment: HashMap<String, String>,
/// Backend ocnfig to use when running backend config.
Expand All @@ -75,7 +75,7 @@ impl BackendDevConfig {
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BackendDeployConfig {
/// Env vars to pass to the deploy OpenGB commands.
/// Env vars to pass to the deploy backend commands.
#[serde(default)]
pub command_environment: HashMap<String, String>,
/// Backend ocnfig to use when running backend config.
Expand Down
Loading