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

Refactor: Init function cleanup #705

Closed
wants to merge 34 commits into from

Conversation

jmwill86
Copy link
Contributor

@jmwill86 jmwill86 commented Mar 13, 2023

Adding a new framework to Shuttle currently takes a lot of duplicating existing code, and that code is defined in various files making it very error prone. I see this commit as step 1 or 2 (the second being a refactor of cargo-shuttle/args.rs) to making the process easier and cleaner for people wanting to add a new framework.

This commit includes a clean up of the cargo-shuttle init.rs functionality with an addition of a cargo builder to make any future Cargo.toml changes easier. This is largely separation of concerns and cutting out duplicated code to improve usability and testability.

Testing has remained largely the same or improved from the original.

I've added a few #[allow(dead_code)] elements in there too, the reason is that although a couple of functions aren't used at the moment I think they may be useful in future. There may be a better way to ignore these warnings.

@jmwill86
Copy link
Contributor Author

Let me know if there's anything you need me to update or do with this @oddgrd . I know a few things have changed since it was requested so if I can help ease the load with anything just let me know.

Copy link
Contributor

@chesedo chesedo left a comment

Choose a reason for hiding this comment

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

This is a nice update. Thanks @jmwill86!! I left a few suggestions

Comment on lines 64 to 79
match self.dependencies.get_mut(&x.name) {
Some(y) => {
y.insert(attribute_name, dep_value);
}
None => {
self.dependencies.insert(
x.name.to_owned(),
BTreeMap::from([(attribute_name, dep_value)]),
);
}
};

self.dependencies
.get_mut(&x.name)
.unwrap()
.insert("version".to_owned(), Value::from(x.get_latest_version()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
match self.dependencies.get_mut(&x.name) {
Some(y) => {
y.insert(attribute_name, dep_value);
}
None => {
self.dependencies.insert(
x.name.to_owned(),
BTreeMap::from([(attribute_name, dep_value)]),
);
}
};
self.dependencies
.get_mut(&x.name)
.unwrap()
.insert("version".to_owned(), Value::from(x.get_latest_version()));
let mut dependency = self.dependencies.entry(&x.name).or_insert(BTreeMap::new());
dependency.insert(attribute_name, dep_value);
dependency.insert("version".to_owned(), Value::from(x.get_latest_version()));

The entry(..).or_insert(..) makes it easier to have a default entry to work with.

Copy link
Contributor

Choose a reason for hiding this comment

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

I might also be wrong, but I think this add a version the second time - ie add_dependency adds a version too in the attributes

/// values without a version will be calculated automatically
pub fn add_var(
&mut self,
section: CargoSection,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can drop the section enum by moving the match block to the respective functions below.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you look into this @jmwill86?

cargo-shuttle/src/cargo_builder.rs Outdated Show resolved Hide resolved
Comment on lines 51 to 53
fn get_base_dependencies(&self) -> Vec<&str>;
fn get_dependency_attributes(&self) -> HashMap<&str, HashMap<&str, Value>>;
fn get_boilerplate_code_for_framework(&self) -> &'static str;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ohhh, nice 🤩

@jmwill86
Copy link
Contributor Author

I've merged existing changes into this branch, it was a little fiddly but I think I've transferred everything over successfully. I'm not sure if the differences in the examples directory are something I've done and need to correct, or if this is unrelated. I'm not too familiar with git submodules but point me in the right direction to correct this and I'll get it fixed.

Copy link
Contributor

@oddgrd oddgrd left a comment

Choose a reason for hiding this comment

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

Hey @jmwill86, thanks for this, this is great! I love all those red lines. 🥳 I left a few questions and some small comments that we might need to address.

Cargo.lock Outdated Show resolved Hide resolved
examples Outdated Show resolved Hide resolved
/// values without a version will be calculated automatically
pub fn add_var(
&mut self,
section: CargoSection,
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you look into this @jmwill86?

}

fn get_boilerplate_code_for_framework(&self) -> &'static str {
indoc! {r#"
use axum::{routing::get, Router};

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit of a nitpick, but shouldn't we have newlines here?

Comment on lines 114 to 116
fn get_base_dependencies(&self) -> Vec<&str> {
vec!["rocket", "shuttle-rocket", "tokio", "shuttle-runtime"]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is so much simpler, nice!

Comment on lines 38 to 42
crate_ver
.highest_normal_version()
.unwrap()
.version()
.to_string()
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we still covering the case where we want to fetch a pre-release version? For example for rocket we want init to start with rocket 0.5rc-3, not the latest stable release which is 0.4.x.

@oddgrd
Copy link
Contributor

oddgrd commented May 12, 2023

Some of this was implemented in #792, and the init function will soon be refactored to use cargo-generate, so we're closing this. Either way, thank you for working on this @jmwill86! ❤️

@oddgrd oddgrd closed this May 12, 2023
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.

3 participants