Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Fixes #58 - Auto increment for default name #469

Merged
merged 5 commits into from
May 1, 2020

Conversation

xprazak2
Copy link
Contributor

@xprazak2 xprazak2 commented Aug 25, 2019

Fixes #58

Stores the name of the last default project
in global config and increments it
when user generates a project without a name.

@xprazak2
Copy link
Contributor Author

xprazak2 commented Dec 4, 2019

All tests are finally passing. Apologies it took me so long to make them green.

@ashleymichal
Copy link
Contributor

hey @xprazak2 !

Sorry this has been sitting on the stack for awhile. I reviewed the code, and actually I don't think we want to cache the directory name every time wrangler runs; rather, we want to have Wrangler respond to an "already exists" OS error by reading the contents of the current working directory, and adding a number if the default name already exists, similar to how filenames are updated in a downloads folder when you download the same asset twice.

So, if worker already exists in the CWD, create worker2; if worker2 already exists, create worker3, etc.

We really try to minimize the amount of local state Wrangler has to keep track of to keep our footprint relatively small.

This does have a small disadvantage, wherein one could accidentally overwrite a worker because of naming collisions, but we would rather solve that with #330 .

@xprazak2
Copy link
Contributor Author

xprazak2 commented Jan 2, 2020

Thank you for your review, I have made changes as suggested - wrangler no longer keeps additional state and scans current dir for the next available name.

Copy link
Contributor

@ashleymichal ashleymichal left a comment

Choose a reason for hiding this comment

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

This gets most of the way there! however i think it is still a little "smart"er than we strictly need, and often "smart"er leads to unwanted side effects.

I'm happy to work with you on making some of these changes, or to pick this up and finish it off if you don't have the bandwidth to implement the changes. Either way you will be credited in the changelog, this work is very much appreciated!

@@ -33,14 +35,80 @@ pub fn run_generate(name: &str, template: &str) -> Result<(), failure::Error> {
let tool_name = "cargo-generate";
let binary_path = install::install(tool_name, "ashleygwilliams")?.binary(tool_name)?;

let args = ["generate", "--git", template, "--name", name, "--force"];
let new_name = match generate_name(name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

i'd like all this logic to live in the top level "generate" function; run generate should only be responsible for running the cargo generate command, it should not also be in charge of naming.

also, i would suggest an early return: first check should be "does a directory by this name already exist in the current working directory?" if it does not, we can skip updating the name. i think i would put that check directly in the generate function above the log::info on line 18.

commands::run(command, &command_name)
}

fn generate_name(name: &str) -> Result<String, failure::Error> {
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 it is worth adding some comments here to establish the convention.


for entry in fs::read_dir(current_dir)? {
let entry = entry?;
entry_names.push(entry.file_name());
Copy link
Contributor

Choose a reason for hiding this comment

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

this will return a value whether the entry in question is a file or a directory. we should only be worried about directory collisions.

Ok(entry_names)
}

fn construct_name(bare_name: &str, num: Option<usize>) -> String {
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 this function could likely be replaced by a call to format! given the other comments in this review.

Comment on lines 59 to 68
let digits = detect_num(name);
let mut new_name = String::from(name);
let mut chars = new_name.chars().collect::<Vec<char>>();
let split = chars.split_off(new_name.len() - digits);
let bare_name = chars.into_iter().collect::<String>();

let mut num = match split.into_iter().collect::<String>().parse::<usize>() {
Ok(val) => Some(val),
Err(_) => None,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should try to split the given name this way. if it makes it easier to parse, we can use a - or _ separator for the digits.

try using a regex like /^name-\d+$/ against the directory names in the current working directory. if someone passes worker-2 and that already exists, we'll end up with worker-2-1, which is fine.

Ok(new_name)
}

fn read_current_dir() -> Result<Vec<OsString>, failure::Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

i would also suggest passing the name in and only returning colliding directories.

@ashleymichal
Copy link
Contributor

hey @xprazak2 , just bumping this, sorry i forgot to mention you in my review last week. what do you think about these comments? if you have the bandwidth to implement that would be lovely, if not please let me know and i can pick this up and finish it up, you'll of course be credited in the changelog. thanks again for this PR, it is very much appreciated!

@xprazak2
Copy link
Contributor Author

I simplified the logic for generating a new name. The project name is no longer scanned for digits at the end but -\d+ is appended when we detect the dir with the same name already exists.

@xprazak2
Copy link
Contributor Author

xprazak2 commented Feb 3, 2020

@ashleymichal, any chance you could do a re-review please?

Copy link
Contributor

@ashleymichal ashleymichal left a comment

Choose a reason for hiding this comment

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

this looks good. i may have to wait to merge it until we tag 1.7.0 but it'll go in asap.

@ashleymichal ashleymichal added this to the 1.8.0 milestone Feb 6, 2020
@ashleygwilliams ashleygwilliams modified the milestones: 1.8.0, 1.9.0 Feb 25, 2020
@ashleymichal ashleymichal merged commit bae61d8 into cloudflare:master May 1, 2020
@EverlastingBugstopper EverlastingBugstopper mentioned this pull request May 6, 2020
2 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

default name should auto increment
4 participants