From 8dcb7db9c03610802b59a90e973c86f19c985099 Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Mon, 13 Nov 2023 12:44:04 -0500 Subject: [PATCH] fix(playground): fix playground vrl version and link (#19119) * fix(playground): fix playground vrl version and link * Ran cargo vdev fmt * remove redundant line * replace clone() with as_ref() whenever possible * Ran cargo vdev fmt --- lib/vector-vrl/web-playground/build.rs | 72 +++++++++++++------ lib/vector-vrl/web-playground/public/index.js | 16 +++-- lib/vector-vrl/web-playground/src/lib.rs | 9 +++ 3 files changed, 68 insertions(+), 29 deletions(-) diff --git a/lib/vector-vrl/web-playground/build.rs b/lib/vector-vrl/web-playground/build.rs index f6d42756f1057..d6abdbbd3090e 100644 --- a/lib/vector-vrl/web-playground/build.rs +++ b/lib/vector-vrl/web-playground/build.rs @@ -24,40 +24,68 @@ fn get_git_hash() -> String { .args(["rev-parse", "HEAD"]) .output() .expect("Failed to get git HEAD sha"); - let mut git_hash = String::from_utf8(output.stdout).unwrap(); - git_hash.truncate(8); - git_hash + String::from_utf8(output.stdout).unwrap().trim().to_string() } -fn write_build_constants(manifest: &Manifest, dest_path: &Path) -> io::Result<()> { - let mut output_file = File::create(dest_path)?; - output_file.write_all( - "// AUTOGENERATED CONSTANTS. SEE BUILD.RS AT REPOSITORY ROOT. DO NOT MODIFY.\n".as_ref(), - )?; - - let create_const_statement = - |name, value| format!("pub const {}: &str = \"{}\";\n", name, value); +fn create_const_statement(name: &str, value: &str) -> String { + format!("pub const {name}: &str = \"{value}\";\n") +} +fn write_vector_constants(output_file: &mut File) { // TODO: For releases, we should use the manifest.package().version(). // https://github.com/vectordotdev/vector/issues/18425 - let vector_version_const = create_const_statement("VECTOR_VERSION", get_git_hash()); + let vector_git_sha = get_git_hash(); + let vector_version_statement = create_const_statement("VECTOR_VERSION", &vector_git_sha); + output_file + .write_all(vector_version_statement.as_bytes()) + .expect("Failed to write Vector version constant"); + + let vector_link = format!("https://github.com/vectordotdev/vector/tree/{vector_git_sha}"); + let vector_link_statement = create_const_statement("VECTOR_LINK", &vector_link); output_file - .write_all(vector_version_const.as_bytes()) + .write_all(vector_link_statement.as_bytes()) .expect("Failed to write Vector version constant"); +} - let vrl_version = manifest +fn write_vrl_constants(manifest: &Manifest, output_file: &mut File) { + let vrl_dep = manifest .dependencies .get("vrl") - .unwrap() + .expect("missing VRL dependency") .detail() - .unwrap() - .version - .clone() - .unwrap_or_else(|| "FIXME".into()); - let vrl_version_const = create_const_statement("VRL_VERSION", vrl_version); + .expect("expected detail dependency format"); + + let (version, link) = match &vrl_dep.version { + None => { + let repo = vrl_dep + .git + .as_ref() + .expect("VRL dependency should use 'version' or 'git'"); + let version = vrl_dep + .rev + .as_ref() + .expect("VRL git revision not specified"); + (version.clone(), format!("{repo}/tree/{version}")) + } + Some(v) => (v.clone(), format!("https://crates.io/crates/vrl/{v}")), + }; + output_file - .write_all(vrl_version_const.as_bytes()) - .expect("Failed to write Vector version constant"); + .write_all(create_const_statement("VRL_VERSION", &version).as_bytes()) + .expect("Failed to write VRL version constant"); + + output_file + .write_all(create_const_statement("VRL_LINK", &link).as_bytes()) + .expect("Failed to write VRL_LINK constant"); +} + +fn write_build_constants(manifest: &Manifest, dest_path: &Path) -> io::Result<()> { + let mut output_file = File::create(dest_path)?; + output_file.write_all( + "// AUTOGENERATED CONSTANTS. SEE BUILD.RS AT REPOSITORY ROOT. DO NOT MODIFY.\n".as_ref(), + )?; + write_vector_constants(&mut output_file); + write_vrl_constants(manifest, &mut output_file); Ok(()) } diff --git a/lib/vector-vrl/web-playground/public/index.js b/lib/vector-vrl/web-playground/public/index.js index 377167f8f6cc9..aaf0c87007253 100644 --- a/lib/vector-vrl/web-playground/public/index.js +++ b/lib/vector-vrl/web-playground/public/index.js @@ -1,4 +1,4 @@ -import init, { run_vrl, vrl_version, vector_version } from "./pkg/vector_vrl_web_playground.js"; +import init, { run_vrl, vrl_version, vrl_link, vector_version, vector_link } from "./pkg/vector_vrl_web_playground.js"; import { vrlLanguageDefinition, vrlThemeDefinition } from "./vrl-highlighter.js"; const PROGRAM_EDITOR_DEFAULT_VALUE = `# Remove some fields @@ -39,8 +39,12 @@ export class VrlWebPlayground { constructor() { let temp = init().then(() => { this.run_vrl = run_vrl; + this.vector_version = vector_version(); + this.vector_link = vector_link(); + this.vrl_version = vrl_version(); + this.vrl_link = vrl_link(); // require is provided by loader.min.js. require.config({ @@ -92,14 +96,12 @@ export class VrlWebPlayground { addVersions() { let vectorLinkElement = document.getElementById('vector-version-link'); - let vectorVersion = vector_version(); - vectorLinkElement.text = vectorVersion; - vectorLinkElement.href = `https://github.com/vectordotdev/vector/tree/${vectorVersion}`; + vectorLinkElement.text = this.vector_version.substring(0, 8); + vectorLinkElement.href = this.vector_link; let vrlLinkElement = document.getElementById('vrl-version-link'); - let vrlVersion = vrl_version(); - vrlLinkElement.text = vrlVersion; - vrlLinkElement.href = `https://crates.io/crates/vrl/${vrlVersion}`; + vrlLinkElement.text = this.vrl_version.substring(0, 8); + vrlLinkElement.href = this.vrl_link; } createDefaultEditor(elementId, value, language, theme) { diff --git a/lib/vector-vrl/web-playground/src/lib.rs b/lib/vector-vrl/web-playground/src/lib.rs index 51a23cacfbd9b..a434050aefa3c 100644 --- a/lib/vector-vrl/web-playground/src/lib.rs +++ b/lib/vector-vrl/web-playground/src/lib.rs @@ -120,7 +120,16 @@ pub fn vector_version() -> String { built_info::VECTOR_VERSION.to_string() } +#[wasm_bindgen] +pub fn vector_link() -> String { + built_info::VECTOR_LINK.to_string() +} + #[wasm_bindgen] pub fn vrl_version() -> String { built_info::VRL_VERSION.to_string() } +#[wasm_bindgen] +pub fn vrl_link() -> String { + built_info::VRL_LINK.to_string() +}