Skip to content

Commit

Permalink
cargo-ci -> Use repo_path!() to specify paths verbatim.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfm committed Jan 15, 2025
1 parent 395fd44 commit 7bac2b9
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 55 deletions.
1 change: 1 addition & 0 deletions .ci/cargo-ci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ flate2 = { workspace = true }
fs-err = { workspace = true }
fs_extra = { workspace = true }
predicates = { workspace = true }
repo_path = { workspace = true }
reqwest = { workspace = true, features = ["blocking"] }
shadow-rs = { workspace = true }
serde_json = { workspace = true }
Expand Down
33 changes: 3 additions & 30 deletions .ci/cargo-ci/src/core/constants.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

static WORKSPACE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
let output = std::process::Command::new(env!("CARGO"))
.arg("locate-project")
.arg("--workspace")
.arg("--message-format=plain")
.output()
.unwrap()
.stdout;

let cargo_path = Path::new(std::str::from_utf8(&output).unwrap().trim());

cargo_path.parent().unwrap().to_path_buf()
});

/// The root of the Cargo Workspace. Should be the repository root.
pub fn workspace_dir() -> PathBuf {
WORKSPACE_DIR.to_owned()
}

/// Where CI-related code is in the repository.
pub fn ci_code_dir() -> PathBuf { workspace_dir().join(".ci") }

/// The generic Cargo target directory.
pub fn cargo_target_dir() -> PathBuf {
workspace_dir().join("target")
}
use std::path::PathBuf;
use repo_path::repo_path;

/// Sub-directory of the Cargo target directory, which we use for any of our own build artifacts.
pub fn target_dir() -> PathBuf {
cargo_target_dir().join("ci")
repo_path!("target/").join("ci")
}
3 changes: 2 additions & 1 deletion .ci/cargo-ci/src/core/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::sync::LazyLock;
use cargo_metadata::Package;
use repo_path::repo_path;

static CARGO_METADATA: LazyLock<cargo_metadata::Metadata> = LazyLock::new(||
cargo_metadata::MetadataCommand::new()
.manifest_path(crate::constants::workspace_dir().join("Cargo.toml"))
.manifest_path(repo_path!("Cargo.toml"))
.exec()
.expect("Failed to gather Cargo metadata.")
);
Expand Down
4 changes: 2 additions & 2 deletions .ci/cargo-ci/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;

pub use fs_err as fs;

use repo_path::repo_path;
pub(crate) use core::constants;
pub(crate) use core::metadata;
pub(crate) use core::types::{self, Arch, Package, Result};
Expand Down Expand Up @@ -40,7 +40,7 @@ enum TaskCli {
fn main() -> crate::Result {
util::init_tracing()?;

std::env::set_current_dir(constants::workspace_dir())?;
std::env::set_current_dir(repo_path!())?;

let cli = RootCli::parse();
match cli.task {
Expand Down
8 changes: 2 additions & 6 deletions .ci/cargo-ci/src/packages/carl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub mod distribution {

pub mod diesel {
use std::process::Command;

use repo_path::repo_path;
use crate::core::dependency::Crate;
use crate::core::util::RunRequiringSuccess;
use crate::util;
Expand Down Expand Up @@ -381,10 +381,6 @@ pub mod diesel {
}

fn carl_database_dir() -> PathBuf {
crate::constants::workspace_dir()
.join(SELF_PACKAGE.ident())
.join("src")
.join("persistence")
.join("database")
repo_path!("opendut-carl/src/persistence/database/")
}
}
4 changes: 2 additions & 2 deletions .ci/cargo-ci/src/packages/lea.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::fs;
use std::path::PathBuf;
use std::process::Command;
use repo_path::repo_path;
use tracing::info;
use crate::Arch;
use crate::core::dependency::Crate;
Expand Down Expand Up @@ -99,8 +100,7 @@ fn install_requirements() -> crate::Result {
}

pub fn self_dir() -> PathBuf {
crate::constants::workspace_dir()
.join(PACKAGE.ident())
repo_path!("opendut-lea/")
}

fn build_impl(out_dir: PathBuf) -> crate::Result {
Expand Down
12 changes: 5 additions & 7 deletions .ci/cargo-ci/src/tasks/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl DocCli {
}

pub mod book {
use repo_path::repo_path;
use super::*;

#[tracing::instrument]
Expand Down Expand Up @@ -97,7 +98,7 @@ pub mod book {
}

fn doc_dir() -> PathBuf {
crate::constants::workspace_dir().join("doc")
repo_path!("doc/")
}

pub fn out_dir() -> PathBuf {
Expand All @@ -106,6 +107,7 @@ pub mod book {
}

pub mod homepage {
use repo_path::repo_path;
use super::*;

#[tracing::instrument]
Expand All @@ -122,7 +124,7 @@ pub mod homepage {
)?;

fs_extra::dir::copy(
homepage_source_dir(),
repo_path!("opendut-homepage/"),
out_dir(),
&fs_extra::dir::CopyOptions::default()
.overwrite(true)
Expand All @@ -136,7 +138,7 @@ pub mod homepage {

for logo in RESOURCES_TO_INCLUDE {
fs_extra::file::copy(
logos_source_dir().join(logo),
repo_path!("resources/logos/").join(logo),
logos_out_dir().join(logo),
&fs_extra::file::CopyOptions::default()
.overwrite(true)
Expand All @@ -146,10 +148,6 @@ pub mod homepage {
Ok(())
}

fn homepage_source_dir() -> PathBuf { crate::constants::workspace_dir().join("opendut-homepage") }

fn logos_source_dir() -> PathBuf { crate::constants::workspace_dir().join("resources").join("logos") }

pub fn out_dir() -> PathBuf { crate::constants::target_dir().join("homepage") }

fn logos_out_dir() -> PathBuf { out_dir().join("resources/logos") }
Expand Down
9 changes: 4 additions & 5 deletions .ci/cargo-ci/src/tasks/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::process::Command;
use std::str::FromStr;

use clap::ArgAction;

use crate::core::constants::workspace_dir;
use repo_path::repo_path;
use crate::core::types::Package;
use crate::core::util::RunRequiringSuccess;

Expand Down Expand Up @@ -57,12 +56,12 @@ pub fn build_carl_docker_image(tag: Option<DockerTag>) -> crate::Result {
let revision = format!("org.opencontainers.image.revision={}", crate::build::COMMIT_HASH);

Command::new("docker")
.current_dir(workspace_dir())
.current_dir(repo_path!())
.args([
"build",
"--no-cache",
"--file",
".ci/docker/carl/Dockerfile",
&repo_path!(".ci/docker/carl/Dockerfile").display().to_string(),
"--build-arg",
&image_version_build_arg,
"--label", &source,
Expand All @@ -80,7 +79,7 @@ pub fn build_carl_docker_image(tag: Option<DockerTag>) -> crate::Result {

pub fn publish_carl_docker_image(tag: Option<DockerTag>) -> crate::Result {
Command::new("docker")
.current_dir(workspace_dir())
.current_dir(repo_path!())
.args(["push", &carl_container_uri(&tag)])
.run_requiring_success()?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions .ci/cargo-ci/src/tasks/licenses/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fs;
use std::path::PathBuf;
use std::process::Command;

use repo_path::repo_path;
use crate::{constants, util};
use crate::core::types::parsing::package::PackageSelection;
use crate::Package;
Expand Down Expand Up @@ -144,5 +144,5 @@ mod texts {


fn cargo_deny_toml() -> PathBuf {
constants::ci_code_dir().join("cargo-deny.toml")
repo_path!(".ci/cargo-deny.toml")
}
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ prost-build = "0.13.1"
prost-types = "0.13.1"
protobuf-src = "1.1.0"
regex = "1.10.2"
repo_path = "1.0.0"
reqwest = { version = "0.12.9", default-features = false, features = ["rustls-tls-native-roots"] }
reqwest-middleware = "0.4.0"
reqwest-retry = "0.7.0"
Expand Down

0 comments on commit 7bac2b9

Please sign in to comment.