|
| 1 | +use std::{fs, path::PathBuf, sync::mpsc::Receiver}; |
| 2 | + |
| 3 | +use anyhow::{anyhow, bail, Context, Result}; |
| 4 | +use const_format::formatcp; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + app::AppConfig, |
| 8 | + jobs::{ |
| 9 | + objdiff::{run_make, BuildConfig, BuildStatus}, |
| 10 | + start_job, update_status, Job, JobContext, JobResult, JobState, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +#[derive(Debug, Clone)] |
| 15 | +pub struct CreateScratchConfig { |
| 16 | + pub build_config: BuildConfig, |
| 17 | + pub context_path: Option<PathBuf>, |
| 18 | + pub build_context: bool, |
| 19 | + |
| 20 | + // Scratch fields |
| 21 | + pub compiler: String, |
| 22 | + pub platform: String, |
| 23 | + pub compiler_flags: String, |
| 24 | + pub function_name: String, |
| 25 | + pub target_obj: PathBuf, |
| 26 | +} |
| 27 | + |
| 28 | +impl CreateScratchConfig { |
| 29 | + pub(crate) fn from_config(config: &AppConfig, function_name: String) -> Result<Self> { |
| 30 | + let Some(selected_obj) = &config.selected_obj else { |
| 31 | + bail!("No object selected"); |
| 32 | + }; |
| 33 | + let Some(target_path) = &selected_obj.target_path else { |
| 34 | + bail!("No target path for {}", selected_obj.name); |
| 35 | + }; |
| 36 | + let Some(scratch_config) = &selected_obj.scratch else { |
| 37 | + bail!("No scratch configuration for {}", selected_obj.name); |
| 38 | + }; |
| 39 | + Ok(Self { |
| 40 | + build_config: BuildConfig::from_config(config), |
| 41 | + context_path: scratch_config.ctx_path.clone(), |
| 42 | + build_context: scratch_config.build_ctx, |
| 43 | + compiler: scratch_config.compiler.clone().unwrap_or_default(), |
| 44 | + platform: scratch_config.platform.clone().unwrap_or_default(), |
| 45 | + compiler_flags: scratch_config.c_flags.clone().unwrap_or_default(), |
| 46 | + function_name, |
| 47 | + target_obj: target_path.to_path_buf(), |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + pub fn is_available(config: &AppConfig) -> bool { |
| 52 | + let Some(selected_obj) = &config.selected_obj else { |
| 53 | + return false; |
| 54 | + }; |
| 55 | + selected_obj.target_path.is_some() && selected_obj.scratch.is_some() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Default, Debug, Clone)] |
| 60 | +pub struct CreateScratchResult { |
| 61 | + pub scratch_url: String, |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug, Default, Clone, serde::Deserialize)] |
| 65 | +struct CreateScratchResponse { |
| 66 | + pub slug: String, |
| 67 | + pub claim_token: String, |
| 68 | +} |
| 69 | + |
| 70 | +const API_HOST: &str = "http://127.0.0.1:8000"; |
| 71 | +const WEB_HOST: &str = "http://localhost:8080"; |
| 72 | + |
| 73 | +fn run_create_scratch( |
| 74 | + status: &JobContext, |
| 75 | + cancel: Receiver<()>, |
| 76 | + config: CreateScratchConfig, |
| 77 | +) -> Result<Box<CreateScratchResult>> { |
| 78 | + let project_dir = |
| 79 | + config.build_config.project_dir.as_ref().ok_or_else(|| anyhow!("Missing project dir"))?; |
| 80 | + |
| 81 | + let mut context = None; |
| 82 | + if let Some(context_path) = &config.context_path { |
| 83 | + if config.build_context { |
| 84 | + update_status(status, "Building context".to_string(), 0, 2, &cancel)?; |
| 85 | + match run_make(&config.build_config, context_path) { |
| 86 | + BuildStatus { success: true, .. } => {} |
| 87 | + BuildStatus { success: false, stdout, stderr, .. } => { |
| 88 | + bail!("Failed to build context:\n{stdout}\n{stderr}") |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + let context_path = project_dir.join(context_path); |
| 93 | + context = Some( |
| 94 | + fs::read_to_string(&context_path) |
| 95 | + .map_err(|e| anyhow!("Failed to read {}: {}", context_path.display(), e))?, |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + update_status(status, "Creating scratch".to_string(), 1, 2, &cancel)?; |
| 100 | + let diff_flags = [format!("--disassemble={}", config.function_name)]; |
| 101 | + let diff_flags = serde_json::to_string(&diff_flags).unwrap(); |
| 102 | + let obj_path = project_dir.join(&config.target_obj); |
| 103 | + let file = reqwest::blocking::multipart::Part::file(&obj_path) |
| 104 | + .with_context(|| format!("Failed to open {}", obj_path.display()))?; |
| 105 | + let form = reqwest::blocking::multipart::Form::new() |
| 106 | + .text("compiler", config.compiler.clone()) |
| 107 | + .text("platform", config.platform.clone()) |
| 108 | + .text("compiler_flags", config.compiler_flags.clone()) |
| 109 | + .text("diff_label", config.function_name.clone()) |
| 110 | + .text("diff_flags", diff_flags) |
| 111 | + .text("context", context.unwrap_or_default()) |
| 112 | + .text("source_code", "// Move related code from Context tab to here") |
| 113 | + .part("target_obj", file); |
| 114 | + let client = reqwest::blocking::Client::new(); |
| 115 | + let response = client |
| 116 | + .post(formatcp!("{API_HOST}/api/scratch")) |
| 117 | + .multipart(form) |
| 118 | + .send() |
| 119 | + .map_err(|e| anyhow!("Failed to send request: {}", e))?; |
| 120 | + if !response.status().is_success() { |
| 121 | + return Err(anyhow!("Failed to create scratch: {}", response.text()?)); |
| 122 | + } |
| 123 | + let body: CreateScratchResponse = response.json().context("Failed to parse response")?; |
| 124 | + let scratch_url = format!("{WEB_HOST}/scratch/{}/claim?token={}", body.slug, body.claim_token); |
| 125 | + |
| 126 | + update_status(status, "Complete".to_string(), 2, 2, &cancel)?; |
| 127 | + Ok(Box::from(CreateScratchResult { scratch_url })) |
| 128 | +} |
| 129 | + |
| 130 | +pub fn start_create_scratch(ctx: &egui::Context, config: CreateScratchConfig) -> JobState { |
| 131 | + start_job(ctx, "Create scratch", Job::CreateScratch, move |context, cancel| { |
| 132 | + run_create_scratch(&context, cancel, config) |
| 133 | + .map(|result| JobResult::CreateScratch(Some(result))) |
| 134 | + }) |
| 135 | +} |
0 commit comments