Skip to content

Commit

Permalink
WIP in describe API
Browse files Browse the repository at this point in the history
  • Loading branch information
Virviil committed Apr 18, 2022
1 parent a7867be commit 26cbc4c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
5 changes: 3 additions & 2 deletions API.md
Expand Up @@ -5,13 +5,14 @@
Usage:

```
pyflow new <project_name>
pyflow new <path>
```


Arguments:

* [] `--src` - Creates `src` folder instead of `<project_name>` folder
* [x] `--name` - Creates project with the given `<name>` inside `<path>` folder, so these identifiers can be different.
* [] `--src` - Creates `src` folder instead of `<path>` folder.

* [] `init` - Create a `pyproject.toml` from [] requirements.txt, [] pipfile, [] setup.py, etc.
To be run inside project folder with these files.
Expand Down
23 changes: 12 additions & 11 deletions src/actions/new.rs
Expand Up @@ -32,27 +32,28 @@ Problem creating the project. This may be due to a permissions problem.
If on linux, please try again with `sudo`.
"#};

pub fn new(name: &str) {
if new_internal(name).is_err() {
pub fn new(path: &str, name: &str) {
if new_internal(path, name).is_err() {
abort(NEW_ERROR_MESSAGE);
}
success(&format!("Created a new Python project named {}", name))
}

// TODO: Join this function after refactoring
/// Create a template directory for a python project.
fn new_internal(name: &str) -> Result<(), Box<dyn Error>> {
fn new_internal(path: &str, name: &str) -> Result<(), Box<dyn Error>> {
let normalized_name = name.replace("-", "_");
if !PathBuf::from(name).exists() {
fs::create_dir_all(&format!("{}/{}", name, name.replace("-", "_")))?;
fs::File::create(&format!("{}/{}/__init__.py", name, name.replace("-", "_")))?;
fs::File::create(&format!("{}/README.md", name))?;
fs::File::create(&format!("{}/.gitignore", name))?;
fs::create_dir_all(&format!("{}/{}", path, normalized_name))?;
fs::File::create(&format!("{}/{}/__init__.py", path, normalized_name))?;
fs::File::create(&format!("{}/README.md", path))?;
fs::File::create(&format!("{}/.gitignore", path))?;
}

let readme_init = &format!("# {}\n\n{}", name, "(A description)");

fs::write(&format!("{}/.gitignore", name), GITIGNORE_INIT)?;
fs::write(&format!("{}/README.md", name), readme_init)?;
fs::write(&format!("{}/.gitignore", path), GITIGNORE_INIT)?;
fs::write(&format!("{}/README.md", path), readme_init)?;

let cfg = Config {
name: Some(name.to_string()),
Expand All @@ -61,9 +62,9 @@ fn new_internal(name: &str) -> Result<(), Box<dyn Error>> {
..Default::default()
};

cfg.write_file(&PathBuf::from(format!("{}/pyproject.toml", name)));
cfg.write_file(&PathBuf::from(format!("{}/pyproject.toml", path)));

if commands::git_init(Path::new(name)).is_err() {
if commands::git_init(Path::new(path)).is_err() {
util::print_color(
"Unable to initialize a git repo for your project",
Color::Yellow, // Dark
Expand Down
8 changes: 5 additions & 3 deletions src/cli_options.rs
Expand Up @@ -15,11 +15,13 @@ pub struct Opt {

#[derive(StructOpt, Debug)]
pub enum SubCommand {
/// Create a project folder with the basics
/// Create a project folder with the basics file structure
#[structopt(name = "new")]
New {
#[structopt(name = "name")]
name: String, // holds the project name.
#[structopt(name = "path")]
path: String, // holds the project folder.
#[structopt(long)]
name: Option<String>, // holds the project name.
},

/// Add packages to `pyproject.toml` and sync an environment
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Expand Up @@ -94,7 +94,13 @@ fn main() {

match &subcmd {
// Actions requires nothing to know about the project
SubCommand::New { name } => actions::new(name),
SubCommand::New { path, name } => {
// if name is not provided, use the directory name
match name {
Some(name) => actions::new(path, name),
None => actions::new(path, path)
}
},
SubCommand::Init => actions::init(CFG_FILENAME),
SubCommand::Reset {} => actions::reset(),
SubCommand::Clear {} => actions::clear(&pyflow_path, &dep_cache_path, &script_env_path),
Expand Down
Empty file added src/repositories/mod.rs
Empty file.

0 comments on commit 26cbc4c

Please sign in to comment.