Skip to content

Commit

Permalink
fix(playground): fix playground vrl version and link (#19119)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pront authored Nov 13, 2023
1 parent 678605c commit 8dcb7db
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 29 deletions.
72 changes: 50 additions & 22 deletions lib/vector-vrl/web-playground/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
16 changes: 9 additions & 7 deletions lib/vector-vrl/web-playground/public/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions lib/vector-vrl/web-playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 8dcb7db

Please sign in to comment.