Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe api #163

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions API.md
@@ -0,0 +1,20 @@
# Pyflow commands API

* [] `new` - Create a project folder with the basics file structure

Usage:

```
pyflow new <path>
```


Arguments:

* [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.

* [] `add` - the
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.