Skip to content
Merged
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
26 changes: 25 additions & 1 deletion glean-core/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const GLEAN_PARSER_VERSION: &str = "19.0.0";
pub struct Builder {
files: Vec<String>,
out_dir: String,
env_dir: Option<PathBuf>,
format: String,
}

impl Default for Builder {
Expand All @@ -57,6 +59,8 @@ impl Default for Builder {
Self {
files: vec![],
out_dir,
env_dir: None,
format: String::from("rust"),
}
}
}
Expand All @@ -70,6 +74,8 @@ impl Builder {
Self {
files: vec![],
out_dir: out_dir.into(),
env_dir: None,
format: String::from("rust"),
}
}

Expand All @@ -81,6 +87,12 @@ impl Builder {
self
}

/// Change output format. Defaults to `rust`.
pub fn format<S: Into<String>>(&mut self, format: S) -> &mut Self {
self.format = format.into();
self
}

/// Add multiple definition files, e.g. `metrics.yaml` or `pings.yaml`.
pub fn files<P>(&mut self, files: P) -> &mut Self
where
Expand All @@ -93,6 +105,15 @@ impl Builder {
self
}

/// Set environment path to use.
pub fn env_dir<P>(&mut self, path: P) -> &mut Self
where
P: Into<PathBuf>,
{
self.env_dir = Some(path.into());
self
}

/// Generate the Rust bindings.
///
/// The consumer must include the generated `glean_metrics.rs` file, e.g.:
Expand All @@ -111,6 +132,8 @@ impl Builder {
eprintln!("got env dir: {env_dir}");
let env_path = PathBuf::from(env_dir);
VirtualEnv::with_path(&sh, &env_path)?
} else if let Some(env_dir) = &self.env_dir {
VirtualEnv::with_path(&sh, env_dir)?
} else {
let venv = VirtualEnv::new(&sh, "py3-glean_parser")?;

Expand All @@ -125,7 +148,8 @@ impl Builder {
println!("cargo:rerun-if-changed={file}");
}

let mut args = vec!["translate", "--format", "rust", "--output", out_dir];
let mut args = vec!["translate", "--output", out_dir];
args.extend(["--format", &self.format]);
args.extend(self.files.iter().map(|s| s.as_str()));
venv.run_module("glean_parser", &args)?;

Expand Down
Loading