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

refactor: make more forward compatible #2931

Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions crates/pixi_build_frontend/src/protocols/builders/pixi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct ProtocolBuilder {
manifest_path: PathBuf,
workspace_manifest: WorkspaceManifest,
package_manifest: PackageManifest,
configuration: Option<serde_json::Value>,
backend_override: Option<BackendOverride>,
channel_config: Option<ChannelConfig>,
cache_dir: Option<PathBuf>,
Expand Down Expand Up @@ -84,12 +85,21 @@ impl ProtocolBuilder {
manifest_path,
workspace_manifest,
package_manifest,
configuration: None,
backend_override: None,
channel_config: None,
cache_dir: None,
}
}

/// Sets the configuration of the build backend
pub fn with_configuration(self, config: serde_json::Value) -> Self {
Self {
configuration: Some(config),
..self
}
}

/// Sets an optional backend override.
pub fn with_backend_override(self, backend_override: BackendOverride) -> Self {
Self {
Expand Down Expand Up @@ -210,6 +220,7 @@ impl ProtocolBuilder {
self.source_dir,
self.manifest_path,
Some(&self.package_manifest),
self.configuration,
&channel_config,
build_id,
self.cache_dir,
Expand All @@ -235,6 +246,7 @@ impl ProtocolBuilder {
self.source_dir,
self.manifest_path,
Some(&self.package_manifest),
self.configuration,
&channel_config,
build_id,
self.cache_dir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl ProtocolBuilder {
self.source_dir,
self.recipe_dir.join("recipe.yaml"),
None,
None,
&channel_config,
build_id,
self.cache_dir,
Expand Down
8 changes: 7 additions & 1 deletion crates/pixi_build_frontend/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ impl JsonRPCBuildProtocol {

/// Set up a new protocol instance.
/// This will spawn a new backend process and establish a JSON-RPC connection.
#[allow(clippy::too_many_arguments)]
async fn setup(
source_dir: PathBuf,
manifest_path: PathBuf,
package_manifest: Option<&'_ PackageManifest>,
configuration: Option<serde_json::Value>,
channel_config: &ChannelConfig,
build_id: usize,
cache_dir: Option<PathBuf>,
Expand Down Expand Up @@ -187,6 +189,7 @@ impl JsonRPCBuildProtocol {
source_dir,
manifest_path,
package_manifest,
configuration,
channel_config,
build_id,
cache_dir,
Expand All @@ -205,6 +208,7 @@ impl JsonRPCBuildProtocol {
// In case of rattler-build it's recipe.yaml
manifest_path: PathBuf,
package_manifest: Option<&'_ PackageManifest>,
configuration: Option<serde_json::Value>,
channel_config: &ChannelConfig,
build_id: usize,
cache_dir: Option<PathBuf>,
Expand Down Expand Up @@ -238,13 +242,15 @@ impl JsonRPCBuildProtocol {
let project_model = package_manifest
.map(|p| to_project_model_v1(p, channel_config))
.transpose()
.map_err(ProtocolError::from)?;
.map_err(ProtocolError::from)?
.map(Into::into);
// Invoke the initialize method on the backend to establish the connection.
let _result: InitializeResult = client
.request(
procedures::initialize::METHOD_NAME,
RpcParams::from(InitializeParams {
project_model,
configuration,
manifest_path: manifest_path.clone(),
cache_directory: cache_dir,
}),
Expand Down
47 changes: 26 additions & 21 deletions crates/pixi_build_type_conversions/src/project_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,27 @@ fn to_target_v1(
// Difference for us is that [`pbt::TargetV1`] has split the host, run and build dependencies
// into separate fields, so we need to split them up here
Ok(pbt::TargetV1 {
host_dependencies: target
.host_dependencies()
.map(|deps| to_pbt_dependencies(deps.iter(), channel_config))
.transpose()?
.unwrap_or_default(),
build_dependencies: target
.build_dependencies()
.map(|deps| to_pbt_dependencies(deps.iter(), channel_config))
.transpose()?
.unwrap_or_default(),
run_dependencies: target
.run_dependencies()
.map(|deps| to_pbt_dependencies(deps.iter(), channel_config))
.transpose()?
.unwrap_or_default(),
host_dependencies: Some(
target
.host_dependencies()
.map(|deps| to_pbt_dependencies(deps.iter(), channel_config))
.transpose()?
.unwrap_or_default(),
),
build_dependencies: Some(
target
.build_dependencies()
.map(|deps| to_pbt_dependencies(deps.iter(), channel_config))
.transpose()?
.unwrap_or_default(),
),
run_dependencies: Some(
target
.run_dependencies()
.map(|deps| to_pbt_dependencies(deps.iter(), channel_config))
.transpose()?
.unwrap_or_default(),
),
})
}

Expand Down Expand Up @@ -131,16 +137,16 @@ fn to_targets_v1(
.collect::<Result<HashMap<pbt::TargetSelectorV1, pbt::TargetV1>, _>>()?;

Ok(pbt::TargetsV1 {
default_target: to_target_v1(targets.default(), channel_config)?,
targets: selected_targets,
default_target: Some(to_target_v1(targets.default(), channel_config)?),
targets: Some(selected_targets),
})
}

/// Converts a [`PackageManifest`] to a [`pbt::ProjectModelV1`].
pub fn to_project_model_v1(
manifest: &PackageManifest,
channel_config: &ChannelConfig,
) -> Result<pbt::VersionedProjectModel, SpecConversionError> {
) -> Result<pbt::ProjectModelV1, SpecConversionError> {
let project = pbt::ProjectModelV1 {
name: manifest.package.name.clone(),
version: manifest.package.version.clone(),
Expand All @@ -152,10 +158,9 @@ pub fn to_project_model_v1(
homepage: manifest.package.homepage.clone(),
repository: manifest.package.repository.clone(),
documentation: manifest.package.documentation.clone(),
configuration: serde_json::Value::Null,
targets: to_targets_v1(&manifest.targets, channel_config)?,
targets: Some(to_targets_v1(&manifest.targets, channel_config)?),
};
Ok(pbt::VersionedProjectModel::V1(project))
Ok(project)
}

#[cfg(test)]
Expand Down
9 changes: 6 additions & 3 deletions crates/pixi_build_types/src/procedures/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ pub struct InitializeParams {
/// Optionally the cache directory to use for any caching activity.
pub cache_directory: Option<PathBuf>,

/// Project model that the backend should use
/// even though it is an option it is highly recommended to use
/// this field. Otherwise it will be very easy to break backwards compatibility.
/// Project model that the backend should use even though it is an option
/// it is highly recommended to use this field. Otherwise, it will be very
/// easy to break backwards compatibility.
pub project_model: Option<VersionedProjectModel>,

/// Backend specific configuration passed from the frontend to the backend.
pub configuration: Option<serde_json::Value>,
}

/// The result of the initialize request.
Expand Down
25 changes: 14 additions & 11 deletions crates/pixi_build_types/src/project_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ pub struct ProjectModelV1 {
/// URL of the project documentation
pub documentation: Option<Url>,

/// Configuration for this specific project model
pub configuration: serde_json::Value,

/// The target of the project, this may contain
/// platform specific configurations.
pub targets: TargetsV1,
pub targets: Option<TargetsV1>,
}

impl From<ProjectModelV1> for VersionedProjectModel {
fn from(value: ProjectModelV1) -> Self {
VersionedProjectModel::V1(value)
}
}

/// Represents a target selector. Currently we only support explicit platform
Expand All @@ -122,27 +125,27 @@ pub enum TargetSelectorV1 {
}

/// A collect of targets including a default target.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TargetsV1 {
pub default_target: TargetV1,
pub default_target: Option<TargetV1>,

/// We use an [`IndexMap`] to preserve the order in which the items where
/// defined in the manifest.
pub targets: HashMap<TargetSelectorV1, TargetV1>,
pub targets: Option<HashMap<TargetSelectorV1, TargetV1>>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TargetV1 {
/// Host dependencies of the project
pub host_dependencies: IndexMap<SourcePackageName, PackageSpecV1>,
pub host_dependencies: Option<IndexMap<SourcePackageName, PackageSpecV1>>,

/// Build dependencies of the project
pub build_dependencies: IndexMap<SourcePackageName, PackageSpecV1>,
pub build_dependencies: Option<IndexMap<SourcePackageName, PackageSpecV1>>,

/// Run dependencies of the project
pub run_dependencies: IndexMap<SourcePackageName, PackageSpecV1>,
pub run_dependencies: Option<IndexMap<SourcePackageName, PackageSpecV1>>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
Loading