Skip to content

Commit

Permalink
develop: Accept flake with attrs as arg
Browse files Browse the repository at this point in the history
dir is only used for Markdown printing.

We need this to be able to pass custom devShell to `use flake`
  • Loading branch information
srid committed Oct 29, 2024
1 parent 59bee20 commit 3a82640
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
13 changes: 5 additions & 8 deletions crates/omnix-cli/src/command/develop.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::path::PathBuf;

use clap::Parser;
use nix_rs::flake::url::FlakeUrl;

/// Prepare to develop on a flake project
#[derive(Parser, Debug)]
pub struct DevelopCommand {
/// Directory of the project
#[arg(name = "DIR", default_value = ".")]
dir: PathBuf,
flake_shell: FlakeUrl,

/// The stage to run in. If not provided, runs all stages.
#[arg(long, value_enum)]
Expand All @@ -26,11 +25,9 @@ enum Stage {

impl DevelopCommand {
pub async fn run(&self) -> anyhow::Result<()> {
tracing::info!(
"⌨️ Preparing to develop project at {:}",
self.dir.display()
);
let prj = omnix_develop::core::Project::new(&self.dir).await?;
let flake = self.flake_shell.without_attr();
tracing::info!("⌨️ Preparing to develop project: {:}", &flake);
let prj = omnix_develop::core::Project::new(flake).await?;
match self.stage {
Some(Stage::PreShell) => omnix_develop::core::develop_on_pre_shell(&prj).await?,
Some(Stage::PostShell) => omnix_develop::core::develop_on_post_shell(&prj).await?,
Expand Down
16 changes: 10 additions & 6 deletions crates/omnix-develop/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use std::path::{Path, PathBuf};
use std::{env::current_dir, path::PathBuf};

use nix_rs::{flake::url::FlakeUrl, info::NixInfo};
use omnix_common::markdown::print_markdown;
Expand All @@ -10,17 +10,19 @@ use crate::config::DevelopConfig;
/// A project that an be developed on locally.
pub struct Project {
/// The directory of the project.
pub dir: PathBuf,
pub dir: Option<PathBuf>,
/// [FlakeUrl] corresponding to the project.
pub flake: FlakeUrl,
/// The develop configuration
pub cfg: DevelopConfig,
}

impl Project {
pub async fn new(dir: &Path) -> anyhow::Result<Self> {
let dir = dir.canonicalize()?;
let flake: FlakeUrl = Into::<FlakeUrl>::into(dir.as_ref());
pub async fn new(flake: FlakeUrl) -> anyhow::Result<Self> {
let dir = match flake.as_local_path() {
Some(path) => Some(path.canonicalize()?),
None => None,
};
let cfg = DevelopConfig::from_flake(&flake).await?;
Ok(Self { dir, flake, cfg })
}
Expand Down Expand Up @@ -85,7 +87,9 @@ pub async fn develop_on_pre_shell(prj: &Project) -> anyhow::Result<()> {

pub async fn develop_on_post_shell(prj: &Project) -> anyhow::Result<()> {
eprintln!();
print_markdown(&prj.dir, prj.cfg.readme.get_markdown()).await?;
let pwd = current_dir()?;
let dir = prj.dir.as_ref().unwrap_or(&pwd);
print_markdown(dir, prj.cfg.readme.get_markdown()).await?;
Ok(())
}

Expand Down

0 comments on commit 3a82640

Please sign in to comment.