-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rye init option to set mercurial VCS for new project.
Usage: ``` rye init --vcs [git | mercurial] ``` Default can be configured in ~/.rye/config.toml ``` [default] vcs = "mercurial" ``` When not specified, or when git specified, current behavior is preserved. When mercurial is specified, a mercurial VCS is initialized unless the WD is already in a mercurial VCS, and a hgignore file is created.
- Loading branch information
Showing
10 changed files
with
475 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,9 @@ toolchain = "[email protected]" | |
# This is the default build system that is used | ||
build-system = "hatchling" | ||
|
||
# This is the default version control system that is used | ||
vcs = "git" | ||
|
||
# This is the default license that is used | ||
license = "MIT" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,15 @@ use tempfile::tempdir; | |
use crate::bootstrap::ensure_self_venv; | ||
use crate::config::Config; | ||
use crate::platform::{ | ||
get_default_author_with_fallback, get_latest_cpython_version, get_pinnable_version, | ||
get_python_version_request_from_pyenv_pin, | ||
get_latest_cpython_version, get_pinnable_version, get_python_version_request_from_pyenv_pin, | ||
}; | ||
use crate::pyproject::BuildSystem; | ||
use crate::sources::py::PythonVersionRequest; | ||
use crate::utils::{ | ||
copy_dir, escape_string, format_requirement, get_venv_python_bin, is_inside_git_work_tree, | ||
CommandOutput, CopyDirOptions, IoPathContext, | ||
copy_dir, escape_string, format_requirement, get_venv_python_bin, CommandOutput, | ||
CopyDirOptions, IoPathContext, | ||
}; | ||
use crate::vcs::ProjectVCS; | ||
|
||
/// Initialize a new or existing Python project with Rye. | ||
#[derive(Parser, Debug)] | ||
|
@@ -83,6 +83,9 @@ pub struct Args { | |
/// Turns off all output. | ||
#[arg(short, long, conflicts_with = "verbose")] | ||
quiet: bool, | ||
/// Which VCS should be used? (defaults to git) | ||
#[arg(long)] | ||
vcs: Option<ProjectVCS>, | ||
} | ||
|
||
#[derive(Parser, Debug)] | ||
|
@@ -128,9 +131,6 @@ const RUST_INIT_PY_TEMPLATE: &str = include_str!("../templates/lib/maturin/__ini | |
/// Template for the Cargo.toml. | ||
const CARGO_TOML_TEMPLATE: &str = include_str!("../templates/lib/maturin/Cargo.toml.j2"); | ||
|
||
/// Template for fresh gitignore files. | ||
const GITIGNORE_TEMPLATE: &str = include_str!("../templates/gitignore.j2"); | ||
|
||
/// Script used for setup.py setup proxy. | ||
const SETUP_PY_PROXY_SCRIPT: &str = r#" | ||
import json, sys | ||
|
@@ -199,8 +199,13 @@ pub fn execute(cmd: Args) -> Result<(), Error> { | |
.unwrap_or_else(|| "unknown".into()) | ||
})); | ||
|
||
let project_vcs = match cmd.vcs { | ||
Some(project_vcs) => project_vcs, | ||
None => cfg.default_vcs().unwrap_or(ProjectVCS::Git), | ||
}; | ||
|
||
let version = "0.1.0"; | ||
let author = get_default_author_with_fallback(&dir); | ||
let author = flat_author(project_vcs.get_author(&dir, cfg.default_author())); | ||
let license = match cmd.license { | ||
Some(license) => Some(license), | ||
None => cfg.default_license(), | ||
|
@@ -326,36 +331,10 @@ pub fn execute(cmd: Args) -> Result<(), Error> { | |
name_safe.insert(0, '_'); | ||
} | ||
|
||
// if git init is successful prepare the local git repository | ||
if !is_inside_git_work_tree(&dir) | ||
&& Command::new("git") | ||
.arg("init") | ||
.current_dir(&dir) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.status() | ||
.map(|status| status.success()) | ||
.unwrap_or(false) | ||
&& is_metadata_author_none | ||
if !project_vcs.inside_work_tree(&dir) && project_vcs.init_dir(&dir) && is_metadata_author_none | ||
{ | ||
let new_author = get_default_author_with_fallback(&dir); | ||
if author != new_author { | ||
metadata.author = new_author; | ||
} | ||
} | ||
|
||
let gitignore = dir.join(".gitignore"); | ||
|
||
// create a .gitignore if one is missing | ||
if !gitignore.is_file() { | ||
let rv = env.render_named_str( | ||
"gitignore.txt", | ||
GITIGNORE_TEMPLATE, | ||
context! { | ||
is_rust => matches!(build_system, BuildSystem::Maturin) | ||
}, | ||
)?; | ||
fs::write(&gitignore, rv).path_context(&gitignore, "failed to write .gitignore")?; | ||
metadata.author = flat_author(project_vcs.get_author(&dir, cfg.default_author())); | ||
project_vcs.render_templates(&dir, &env, build_system)?; | ||
} | ||
|
||
let rv = env.render_named_str( | ||
|
@@ -457,6 +436,15 @@ pub fn execute(cmd: Args) -> Result<(), Error> { | |
Ok(()) | ||
} | ||
|
||
fn flat_author(author: (Option<String>, Option<String>)) -> Option<(String, String)> { | ||
match author { | ||
(Some(name), Some(email)) => Some((name, email)), | ||
(Some(name), None) => Some((name, "[email protected]".to_string())), | ||
(None, Some(email)) => Some(("Unknown".to_string(), email)), | ||
_ => None, | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct Metadata { | ||
name: Option<String>, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
use std::path::{Path, PathBuf}; | ||
use std::process::{Command, Stdio}; | ||
use std::sync::Mutex; | ||
use std::{env, fs}; | ||
|
||
use anyhow::{anyhow, Context, Error}; | ||
|
||
use crate::config::Config; | ||
use crate::pyproject::latest_available_python_version; | ||
use crate::sources::py::{PythonVersion, PythonVersionRequest}; | ||
use crate::utils::IoPathContext; | ||
|
@@ -177,39 +175,6 @@ pub fn list_known_toolchains() -> Result<Vec<(PythonVersion, PathBuf)>, Error> { | |
Ok(rv) | ||
} | ||
|
||
/// Returns the default author from git or the config. | ||
pub fn get_default_author_with_fallback(dir: &PathBuf) -> Option<(String, String)> { | ||
let (mut name, mut email) = Config::current().default_author(); | ||
let is_name_none = name.is_none(); | ||
let is_email_none = email.is_none(); | ||
|
||
if let Ok(rv) = Command::new("git") | ||
.arg("config") | ||
.arg("--get-regexp") | ||
.current_dir(dir) | ||
.arg("^user.(name|email)$") | ||
.stdout(Stdio::piped()) | ||
.output() | ||
{ | ||
for line in std::str::from_utf8(&rv.stdout).ok()?.lines() { | ||
match line.split_once(' ') { | ||
Some((key, value)) if key == "user.email" && is_email_none => { | ||
email = Some(value.to_string()); | ||
} | ||
Some((key, value)) if key == "user.name" && is_name_none => { | ||
name = Some(value.to_string()); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
Some(( | ||
name?, | ||
email.unwrap_or_else(|| "[email protected]".into()), | ||
)) | ||
} | ||
|
||
/// Reads the current `.python-version` file. | ||
pub fn get_python_version_request_from_pyenv_pin(root: &Path) -> Option<PythonVersionRequest> { | ||
let mut here = root.to_owned(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# python generated files | ||
__pycache__/ | ||
.*\.py[oc] | ||
build/ | ||
dist/ | ||
wheels/ | ||
.*\.egg-info | ||
|
||
{%- if is_rust %} | ||
# Rust | ||
target/ | ||
{%- endif %} | ||
|
||
# venv | ||
\.venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.