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

WIP: Implement basic add command #177

Closed
wants to merge 0 commits into from
Closed

Conversation

0101coding
Copy link
Contributor

@0101coding 0101coding commented Sep 21, 2022

This is draft PR for Issue #9

@cnpryer
Copy link
Owner

cnpryer commented Sep 21, 2022

Thanks! I should have more time later today to check this out.

Copy link
Owner

@cnpryer cnpryer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments related to how were implementing this and potentially some final steps. Besides what would be the remaining cleanup, this looks like a good first pass!

Thanks for helping out!

Cargo.toml Outdated

serde_json = "1.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
typer = "0.1.1"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not using typer. but we are using serde_json and reqwests

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay cool. Then we can remove this.

Comment on lines 30 to 49
let path = format!("https://pypi.org/pypi/{}/json", dependency.unwrap());

//info!("Requesting data from {}", path);
let resp: reqwest::blocking::Response = reqwest::blocking::get(path)?;

let json: PyPi = resp.json()?;
println!("{:#?}", json);

// Get the version

let version = json.info.version;
let name = json.info.name;
let dep = PythonPackage{
name,
version,
};

//let cwd = cwd_buff.as_path();
let venv = Venv::default();
venv.install_package(&dep)?;
Copy link
Owner

@cnpryer cnpryer Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments on this part.

  1. Can we move this to huak::ops::add? In bin/huak/commands/ we're only adding a thin CLI layer and implementing all the operation logic as part of the lib. So you'd want to
Suggested change
let path = format!("https://pypi.org/pypi/{}/json", dependency.unwrap());
//info!("Requesting data from {}", path);
let resp: reqwest::blocking::Response = reqwest::blocking::get(path)?;
let json: PyPi = resp.json()?;
println!("{:#?}", json);
// Get the version
let version = json.info.version;
let name = json.info.name;
let dep = PythonPackage{
name,
version,
};
//let cwd = cwd_buff.as_path();
let venv = Venv::default();
venv.install_package(&dep)?;
let project = Project::from(&cwd);
ops::add::add_dependency_to_project(&project)?;
  1. If we don't need certain annotations because the compiler will imply them, then maybe we remove those too. I'm not sure if PyPI can be implied but I wonder if the Response can.
  2. From the ops space, we want to standardize operations to execute on Projects vs Venvs. So you should be able to do the same thing here.
  3. Last thing would be adding to the Toml.

Also let's make sure to remove and prints/debug code before a merge.

src/huak/errors.rs Outdated Show resolved Hide resolved
@cnpryer
Copy link
Owner

cnpryer commented Sep 21, 2022

Looks like we also need to make sure this passes the ci-rust action.

@cnpryer cnpryer changed the title Draft PR for Add Implementation WIP: Implement basic add command Sep 21, 2022
@0101coding
Copy link
Contributor Author

I will update my branch from remote so that the repo is in sync with mainstream. Will look at the ci-rust actions too

@0101coding
Copy link
Contributor Author

Hi @cnpryer after making the changes, i ran cargo run add typer, and it shows what it is in the screenshot, however I cannot see any file tracking installed dependency
image

Any hint will be appreciated

@cnpryer
Copy link
Owner

cnpryer commented Sep 23, 2022

Hmm I'm not sure what you mean. Can you give me something to reproduce this with?

Cargo.toml Outdated Show resolved Hide resolved
@cnpryer
Copy link
Owner

cnpryer commented Sep 24, 2022

Hi! I'm hoping to get to PyPI this week or next weekend the latest. How are you doing here? I see that you're on your master, so unfortunately I don't think I can help out unless I PR your master. Correct me if I'm wrong.

It looks like you need a rebase. I would rebase when you can. I want to update PythonPackage to include something like

pub struct PythonPackage {
    string: String, // TODO: More like a view maybe.
    pub name: String,
    pub op: Option<String>,
    pub version: Option<String>,
}

impl PythonPackage {
    pub fn new(name: String, op: Option<String>, version: Option<String>) -> PythonPackage {
        let string = py_package_string_from_parts(&name, &op, &version);

        PythonPackage { string, name, op, version }
    }

    pub fn from(string: String) -> PythonPackage {
       let (name, op, version) = py_package_from_string(string);

       PythonPackage { string, name, op, version }
    }

    pub fn string(&self) -> &String {
        &self.string
    }

    pub fn set_string(&mut self, name: &str, op: &str, version: &str) {
        self.string = format!("{name}{op}{version}");
    }
}

fn py_package_from_string(string: String) -> (String, Option<String>, Option<String>) {
    ("".to_string(), None, None)
}

fn py_package_string_from_parts(name: &str, op: &Option<String>, version: &Option<String>) -> String {
    "".to_string()
}

If we move to that I think it'd be straightfoward to wrap up your PR, unless I'm missing something. The PythonPackage change would take a little bit of rework. So I can try to get that in today. From there you'd just need to do the following:

  1. Move to huak::ops::add::add_project_dependency, only leave something like this in the bin space
let dependency = match args.get_one::<String>("dependency") {
        Some(d) => d,
        None => {
            return Err(CliError::new(
                anyhow::format_err!("no dependency was provided"),
                2,
            ))
        }
    };
  1. Add a test
  2. Update for anything added from my PythonPackage change

I'll update this comment throughout the day if something changes.

@0101coding
Copy link
Contributor Author

I have made an initial push of the changes made, though test implementation is still outstanding

@@ -1,6 +1,7 @@
use super::utils::subcommand;
use clap::{value_parser, Arg, ArgMatches, Command};
use huak::errors::CliResult;
use huak::ops::add::add_project_dependency;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use huak::ops::add::add_project_dependency;
use huak::ops;


pub fn add_project_dependency(
package: String,
_dependency_is_dev: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_dependency_is_dev: bool,
_is_dev: bool,

Copy link
Owner

@cnpryer cnpryer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I added the new error pattern here. It's probably not correct yet, but at least gives you an idea how we can handle errors in the bin space as well (post rebase).

Let me know if you need any help. Maybe I'll have the PythonPackage changes done for your test.

// Get the version
let version = json.info.version;
let name = json.info.name;
let mut dep = PythonPackage::new(name.clone());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be able to do

let dep = PythonPackage::new(name, None, version);

after the PythonPackage rewrite.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be good to go. It'll default the string to "name==version".

Comment on lines 19 to 20
let dependency = args.get_one::<String>("dependency");
add_project_dependency(dependency.unwrap().to_string(), false)?;
Copy link
Owner

@cnpryer cnpryer Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let dependency = args.get_one::<String>("dependency");
add_project_dependency(dependency.unwrap().to_string(), false)?;
let dependency = match args.get_one::<String>("dependency") {
Some(d) => d,
None => {
return Err(CliError::new(HuakError::MissingArguments))
}
};
ops::add::add_project_dependency(dependency, false)?;

I'm like 60% sure i got this right. I'll come back.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only change I think is adding , 1) to just return a basic exit code. A rebase will add status codes to CliError.

@cnpryer
Copy link
Owner

cnpryer commented Sep 25, 2022

Btw if you need more interactive communication I set up a discord https://discord.gg/St3menxFZT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants