Skip to content

Commit fef7117

Browse files
Move CargoProfile into cargo_pgrx::cargo (pgcentralfoundation#2188)
Consolidates cargo-command-building code in its now-obvious home.
1 parent 96ab845 commit fef7117

8 files changed

Lines changed: 55 additions & 68 deletions

File tree

cargo-pgrx/src/cargo.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//LICENSE All rights reserved.
88
//LICENSE
99
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10-
use crate::profile::CargoProfile;
1110
use std::collections::BTreeMap;
1211
use std::path::PathBuf;
1312
use std::{env, process};
@@ -213,3 +212,52 @@ pub(crate) fn initialize() {
213212
(_, Err(_)) => {}
214213
}
215214
}
215+
216+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
217+
pub enum CargoProfile {
218+
/// The default non-release profile, `[profile.dev]`
219+
#[default]
220+
Dev,
221+
/// The default release profile, `[profile.release]`
222+
Release,
223+
/// Some other profile, specified by name.
224+
Profile(String),
225+
}
226+
227+
impl CargoProfile {
228+
pub fn from_flags(profile: Option<&str>, default: CargoProfile) -> eyre::Result<Self> {
229+
match profile {
230+
// Cargo treats `--profile release` the same as `--release`.
231+
Some("release") => Ok(Self::Release),
232+
// Cargo has two names for the debug profile, due to legacy
233+
// reasons...
234+
Some("debug") | Some("dev") => Ok(Self::Dev),
235+
Some(profile) => Ok(Self::Profile(profile.into())),
236+
None => Ok(default),
237+
}
238+
}
239+
240+
pub fn cargo_args(&self) -> Vec<String> {
241+
match self {
242+
Self::Dev => vec![],
243+
Self::Release => vec!["--release".into()],
244+
Self::Profile(p) => vec!["--profile".into(), p.into()],
245+
}
246+
}
247+
248+
pub fn name(&self) -> &str {
249+
match self {
250+
Self::Dev => "dev",
251+
Self::Release => "release",
252+
Self::Profile(p) => p,
253+
}
254+
}
255+
256+
pub fn target_subdir(&self) -> &str {
257+
match self {
258+
Self::Dev => "debug",
259+
Self::Release => "release",
260+
Self::Profile(p) => p,
261+
}
262+
}
263+
}

cargo-pgrx/src/command/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
//LICENSE
99
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
1010
use crate::CommandExecute;
11+
use crate::cargo::CargoProfile;
1112
use crate::command::get::{find_control_file, get_property};
1213
use crate::command::sudo_install::SudoInstall;
1314
use crate::manifest::{PgVersionSource, display_version_info};
14-
use crate::profile::CargoProfile;
1515
use cargo_metadata::Message as CargoMessage;
1616
use cargo_toml::Manifest;
1717
use eyre::{WrapErr, eyre};

cargo-pgrx/src/command/package.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
//LICENSE
99
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
1010
use crate::CommandExecute;
11+
use crate::cargo::CargoProfile;
12+
use crate::command::get::get_property;
1113
use crate::command::install::install_extension;
1214
use crate::manifest::{PgVersionSource, display_version_info};
13-
use crate::{command::get::get_property, profile::CargoProfile};
1415
use cargo_toml::Manifest;
1516
use eyre::{WrapErr, eyre};
1617
use pgrx_pg_config::{PgConfig, Pgrx, get_target_dir};

cargo-pgrx/src/command/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use std::collections::HashMap;
99
//LICENSE
1010
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
1111
use crate::CommandExecute;
12+
use crate::cargo::CargoProfile;
1213
use crate::command::get::get_property;
1314
use crate::command::install::install_extension;
1415
use crate::command::regress::Regress;
1516
use crate::command::start::start_postgres;
1617
use crate::command::stop::stop_postgres;
1718
use crate::manifest::{get_package_manifest, pg_config_and_version};
18-
use crate::profile::CargoProfile;
1919
use eyre::eyre;
2020
use owo_colors::OwoColorize;
2121
use pgrx_pg_config::{PgConfig, Pgrx, createdb};

cargo-pgrx/src/command/schema.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
//LICENSE
99
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
1010
use crate::CommandExecute;
11-
use crate::cargo::{self, Cargo};
11+
use crate::cargo::{self, Cargo, CargoProfile};
1212
use crate::command::get::{find_control_file, get_property};
1313
use crate::manifest::{get_package_manifest, pg_config_and_version};
14-
use crate::profile::CargoProfile;
1514
use cargo_toml::Manifest;
1615
use eyre::{WrapErr, eyre};
1716
use object::read::macho::MachOFatFile32;

cargo-pgrx/src/command/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::path::{Path, PathBuf};
1313
use std::process::Stdio;
1414

1515
use crate::CommandExecute;
16+
use crate::cargo::CargoProfile;
1617
use crate::manifest::{get_package_manifest, pg_config_and_version};
17-
use crate::profile::CargoProfile;
1818

1919
/// Run the test suite for this crate
2020
#[derive(clap::Args, Debug, Clone)]

cargo-pgrx/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ mod manifest;
1212
mod metadata;
1313

1414
pub(crate) mod cargo;
15-
pub(crate) mod profile;
1615

1716
use clap::Parser;
1817
use std::io::{self, IsTerminal};

cargo-pgrx/src/profile.rs

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)