-
Notifications
You must be signed in to change notification settings - Fork 147
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
Add the correct crate name (as in crates.io) to toml and notify user #98
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,35 +4,40 @@ use rustc_serialize::json; | |
use rustc_serialize::json::{BuilderError, Json}; | ||
use curl::{ErrCode, http}; | ||
use curl::http::handle::{Method, Request}; | ||
use cargo_edit::Manifest; | ||
use cargo_edit::{Dependency, Manifest}; | ||
use regex::Regex; | ||
|
||
const REGISTRY_HOST: &'static str = "https://crates.io"; | ||
|
||
/// Query latest version from crates.io | ||
/// | ||
/// The latest version will be returned as a string. This will fail, when | ||
/// The latest version will be returned as a `Dependency`. This will fail, when | ||
/// | ||
/// - there is no Internet connection, | ||
/// - the response from crates.io is an error or in an incorrect format, | ||
/// - or when a crate with the given name does not exist on crates.io. | ||
pub fn get_latest_version(crate_name: &str) -> Result<String, FetchVersionError> { | ||
pub fn get_latest_dependency(crate_name: &str) -> Result<Dependency, FetchVersionError> { | ||
if env::var("CARGO_IS_TEST").is_ok() { | ||
// We are in a simulated reality. Nothing is real here. | ||
// FIXME: Use actual test handling code. | ||
return Ok("CURRENT_VERSION_TEST".into()); | ||
return Ok(Dependency::new(crate_name).set_version("CURRENT_VERSION_TEST".into())); | ||
} | ||
|
||
let crate_data = try!(fetch_cratesio(&format!("/crates/{}", crate_name))); | ||
let crate_json = try!(Json::from_str(&crate_data)); | ||
|
||
crate_json.as_object() | ||
.and_then(|c| c.get("crate")) | ||
.and_then(|c| c.as_object()) | ||
.and_then(|c| c.get("max_version")) | ||
let name = try!(crate_json.find_path(&["crate", "name"]) | ||
.and_then(|n| n.as_string()) | ||
.ok_or(FetchVersionError::CratesIo(CratesIoError::NotFound))); | ||
|
||
if name != crate_name { | ||
println!("WARN: Added {} instead of {}", name, crate_name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good reminder to add an actual logging facility some time soon (but not in this PR) :) |
||
} | ||
|
||
let version = try!(crate_json.find_path(&["crate", "max_version"]) | ||
.and_then(|v| v.as_string()) | ||
.map(|v| v.to_owned()) | ||
.ok_or(FetchVersionError::GetVersion) | ||
.ok_or(FetchVersionError::GetVersion)); | ||
|
||
Ok(Dependency::new(name).set_version(&version)) | ||
} | ||
|
||
quick_error! { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment why you are sure this is actually always present and unreachable is fine here?