Skip to content

Commit

Permalink
feat: add get and set name commands to project (prefix-dev#2649)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruben Arts <[email protected]>
  • Loading branch information
LiamConnors and ruben-arts authored Dec 9, 2024
1 parent da38075 commit 6da07e2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crates/pixi_manifest/src/manifests/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,14 @@ impl Manifest {
Ok(())
}

/// Set the project name
pub fn set_name(&mut self, name: &str) -> miette::Result<()> {
self.workspace.workspace.name = name.to_string();
self.document.set_name(name);

Ok(())
}

/// Set the project description
pub fn set_description(&mut self, description: &str) -> miette::Result<()> {
// Update in both the manifest and the toml
Expand Down
5 changes: 5 additions & 0 deletions crates/pixi_manifest/src/manifests/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ impl ManifestSource {
.is_some())
}

/// Sets the name of the project
pub fn set_name(&mut self, name: &str) {
self.as_table_mut()["project"]["name"] = value(name);
}

/// Sets the description of the project
pub fn set_description(&mut self, description: &str) {
self.as_table_mut()["project"]["description"] = value(description);
Expand Down
20 changes: 20 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,26 @@ pixi project export conda-explicit-spec output
pixi project export conda-explicit-spec -e default -e test -p linux-64 output
```

### `project name get`

Get the project name.

```sh
$ pixi project name get
my project name
```

### `project name set`

Set the project name.

##### Arguments

1. `<NAME>`: The name to set.

```sh
pixi project name set "my new project name"
```

### `project platform add`

Expand Down
3 changes: 3 additions & 0 deletions src/cli/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod channel;
pub mod description;
pub mod environment;
pub mod export;
pub mod name;
pub mod platform;
pub mod version;

Expand All @@ -16,6 +17,7 @@ pub enum Command {
Version(version::Args),
Environment(environment::Args),
Export(export::Args),
Name(name::Args),
}

/// Modify the project configuration file through the command line.
Expand All @@ -36,6 +38,7 @@ pub async fn execute(cmd: Args) -> miette::Result<()> {
Command::Version(args) => version::execute(args).await?,
Command::Environment(args) => environment::execute(args).await?,
Command::Export(cmd) => export::execute(cmd).await?,
Command::Name(args) => name::execute(args).await?,
};
Ok(())
}
6 changes: 6 additions & 0 deletions src/cli/project/name/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::Project;

pub async fn execute(project: Project) -> miette::Result<()> {
println!("{}", project.name());
Ok(())
}
36 changes: 36 additions & 0 deletions src/cli/project/name/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub mod get;
pub mod set;

use crate::cli::cli_config::ProjectConfig;
use crate::Project;
use clap::Parser;

/// Commands to manage project name.
#[derive(Parser, Debug)]
pub struct Args {
#[clap(flatten)]
pub project_config: ProjectConfig,

/// The subcommand to execute
#[clap(subcommand)]
pub command: Command,
}

#[derive(Parser, Debug)]
pub enum Command {
/// Get the project name.
Get,
/// Set the project name
Set(set::Args),
}

pub async fn execute(args: Args) -> miette::Result<()> {
let project = Project::load_or_else_discover(args.project_config.manifest_path.as_deref())?;

match args.command {
Command::Get => get::execute(project).await?,
Command::Set(args) => set::execute(project, args).await?,
}

Ok(())
}
26 changes: 26 additions & 0 deletions src/cli/project/name/set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::Project;
use clap::Parser;

#[derive(Parser, Debug)]
pub struct Args {
/// The project name
#[clap(required = true, num_args = 1)]
pub name: String,
}

pub async fn execute(mut project: Project, args: Args) -> miette::Result<()> {
// Set the new project name
project.manifest.set_name(&args.name)?;

// Save project
project.save()?;

// Report back to the user
eprintln!(
"{}Updated project name to '{}'.",
console::style(console::Emoji("✔ ", "")).green(),
project.manifest.workspace.workspace.name
);

Ok(())
}
10 changes: 10 additions & 0 deletions tests/integration_python/test_main_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ def test_project_commands(pixi: Path, tmp_pixi_workspace: Path) -> None:
ExitCode.SUCCESS,
stderr_contains="2.1.1",
)
verify_cli_command(
[pixi, "project", "--manifest-path", manifest_path, "name", "get"],
ExitCode.SUCCESS,
stdout_contains="test_project_commands",
)
verify_cli_command(
[pixi, "project", "--manifest-path", manifest_path, "name", "set", "new-name"],
ExitCode.SUCCESS,
stderr_contains="new-name",
)


def test_broken_config(pixi: Path, tmp_pixi_workspace: Path) -> None:
Expand Down

0 comments on commit 6da07e2

Please sign in to comment.