Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #25835: Allow passing a target to rudderc build #6003

Draft
wants to merge 5 commits into
base: branches/rudder/8.1
Choose a base branch
from
Draft
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
72 changes: 42 additions & 30 deletions policies/rudderc/src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,56 @@ pub mod metadata;
pub mod unix;
pub mod windows;

/// List resources in directory
///
/// Note: We only support UTF-8 file names.
pub fn list_resources(path: &Path) -> Result<Vec<String>> {
if path.is_dir() {
WalkDir::new(path)
// We need a stable order
.sort_by_file_name()
.into_iter()
// Only select files
.filter(|r| r.as_ref().map(|e| e.file_type().is_file()).unwrap_or(true))
.map(|e| {
e.map(|e| {
e.path()
// relative path
.strip_prefix(path)
.unwrap()
.to_string_lossy()
.to_string()
})
.map_err(|e| e.into())
})
.collect::<Result<Vec<String>, Error>>()
} else {
Ok(vec![])
}
}

/// A backend is something that can generate final code for a given language from an IR
pub trait Backend {
// For now, we only generate one file content
//
// The `standalone` parameter generates wrapping code (hopefully) allowing to run the generated file
// without a Rudder server.
fn generate(&self, policy: Technique, resources: &Path, standalone: bool) -> Result<String>;
}

/// List resources in directory
///
/// Note: We only support UTF-8 file names.
fn list_resources(path: &Path) -> Result<Vec<String>>
where
Self: Sized,
{
if path.is_dir() {
WalkDir::new(path)
// We need a stable order
.sort_by_file_name()
.into_iter()
// Only select files
.filter(|r| r.as_ref().map(|e| e.file_type().is_file()).unwrap_or(true))
.map(|e| {
e.map(|e| {
e.path()
// relative path
.strip_prefix(path)
.unwrap()
.to_string_lossy()
.to_string()
})
.map_err(|e| e.into())
})
.collect::<Result<Vec<String>, Error>>()
} else {
Ok(vec![])
}
}
/// A backend is something that can generate final code for a given language from an IR
pub trait MetadataBackend {
// For now, we only generate one file content
//
// The `standalone` parameter generates wrapping code (hopefully) allowing to run the generated file
// without a Rudder server.
fn generate(
&self,
policy: Technique,
resources: &Path,
standalone: bool,
targets: &[Target],
) -> Result<String>;
}

/// Select the right backend
Expand Down
21 changes: 12 additions & 9 deletions policies/rudderc/src/backends/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@ use std::path::Path;

use anyhow::{Error, Result};
use quick_xml::se::Serializer;
use rudder_commons::{Target, ALL_TARGETS};
use rudder_commons::{Target};
use serde::Serialize;

use crate::backends::MetadataBackend;
use crate::{
backends::{Backend, Windows},
backends,
backends::{Windows},
ir,
ir::technique::{Id, ItemKind, LeafReportingMode, Parameter, ParameterType},
RESOURCES_DIR,
};

pub struct Metadata;

impl Backend for Metadata {
impl MetadataBackend for Metadata {
fn generate(
&self,
technique: ir::Technique,
resources: &Path,
_standalone: bool,
targets: &[Target],
) -> Result<String> {
let technique: Technique = (technique, resources).try_into()?;
let technique: Technique = (technique, resources, targets).try_into()?;
Self::xml(technique)
}
}
Expand Down Expand Up @@ -155,14 +158,14 @@ impl From<Parameter> for SelectOne {
}
}

impl TryFrom<(ir::Technique, &Path)> for Technique {
impl TryFrom<(ir::Technique, &Path, &[Target])> for Technique {
type Error = Error;

fn try_from(data: (ir::Technique, &Path)) -> Result<Self> {
let (src, resources) = data;
let files = Metadata::list_resources(resources)?;
fn try_from(data: (ir::Technique, &Path, &[Target])) -> Result<Self> {
let (src, resources, targets) = data;
let files = backends::list_resources(resources)?;

let agent = ALL_TARGETS
let agent = targets
.iter()
.map(|t| {
Agent::from(
Expand Down
3 changes: 2 additions & 1 deletion policies/rudderc/src/backends/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tracing::trace;

use super::Backend;
use crate::{
backends,
backends::unix::{
cfengine::{bundle::Bundle, promise::Promise},
ncf::{dry_run_mode, method_call::method_call, technique::Technique},
Expand Down Expand Up @@ -117,7 +118,7 @@ impl Backend for Unix {
.parameters(technique.params.iter().map(|p| p.name.clone()).collect());
// separate bundles for each method call
let mut call_bundles = vec![];
if !Unix::list_resources(resources)?.is_empty() {
if !backends::list_resources(resources)?.is_empty() {
main_bundle.add_promise_group(vec![Promise::string(
"resources_dir",
"${this.promise_dirname}/resources",
Expand Down
10 changes: 5 additions & 5 deletions policies/rudderc/src/backends/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ pub mod test;

use std::path::Path;

use anyhow::{bail, Result};
use askama::Template;
use rudder_commons::{methods::method::Agent, Escaping, PolicyMode};

use super::Backend;
use crate::backends;
use crate::ir::{
condition::Condition,
technique::{ItemKind, LeafReportingMode, Method, Parameter},
Technique,
};
use anyhow::{bail, Result};
use askama::Template;
use rudder_commons::{methods::method::Agent, Escaping, PolicyMode};

pub struct Windows;

Expand Down Expand Up @@ -273,7 +273,7 @@ impl Windows {

let technique = TechniqueTemplate {
id: &src.id.to_string(),
has_resources: !Windows::list_resources(resources)?.is_empty(),
has_resources: !backends::list_resources(resources)?.is_empty(),
parameters: src.params,
methods,
};
Expand Down
9 changes: 9 additions & 0 deletions policies/rudderc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clap::{Parser, Subcommand};

use crate::{doc::Format, DEFAULT_AGENT_PATH};
use rudder_cli::logs::OutputFormat;
use rudder_commons::Target;

/// Compile Rudder policies
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -46,6 +47,10 @@ pub enum Command {
/// Load a library from the given path
#[arg(short, long, action = clap::ArgAction::Append)]
library: Vec<PathBuf>,

/// Select a specific target platform
#[arg(long)]
target: Option<Target>,
},

/// Build the technique
Expand All @@ -69,6 +74,10 @@ pub enum Command {
/// Add ids to the source technique. This will also reformat the file.
#[arg(long)]
store_ids: bool,

/// Select a specific target platform
#[arg(long)]
target: Option<Target>,
},

/// Run the provided technique with the provided tests cases
Expand Down
7 changes: 4 additions & 3 deletions policies/rudderc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use rudder_commons::{is_canonified, logs::ok_output, methods::Methods, Target};
use serde_json::Value;
use tracing::{error, warn};

use crate::backends::MetadataBackend;
use crate::{
backends::{backend, metadata::Metadata, Backend},
backends::{backend, metadata::Metadata},
frontends,
ir::{
technique::{
Expand Down Expand Up @@ -118,13 +119,13 @@ pub fn compile(policy: Technique, target: Target, src: &Path, standalone: bool)
}

/// Compile metadata file
pub fn metadata(policy: Technique, src: &Path) -> Result<String> {
pub fn metadata(policy: Technique, src: &Path, targets: &[Target]) -> Result<String> {
ok_output(
"Generating",
format!("{} v{} [Metadata]", policy.name, policy.version,),
);
let resources_path = src.parent().unwrap().join(RESOURCES_DIR);
Metadata.generate(policy, resources_path.as_path(), false)
Metadata.generate(policy, resources_path.as_path(), false, targets)
}

/// Inject metadata information into method calls
Expand Down
Loading