Skip to content

Have generators handle multi-word names (#316)#330

Merged
drujensen merged 8 commits into
amberframework:masterfrom
marksiemers:fix-name-generator
Oct 30, 2017
Merged

Have generators handle multi-word names (#316)#330
drujensen merged 8 commits into
amberframework:masterfrom
marksiemers:fix-name-generator

Conversation

@marksiemers

@marksiemers marksiemers commented Oct 26, 2017

Copy link
Copy Markdown
Contributor

Description of the Change

This changes how the generators handle file names and class names. It is related to #316 .
That issue was originally about controllers, but it applies to all the generators, so this fix addresses all of them.

I ended up added a lot of LOC to the test suite. Part of this is due to the before_each and after_each hooks in crystal being global (so I couldn't use them here), another reason is that the generators create a lot of files, checking each one takes a bit of code.

That said, I'm opening this PR for initial feedback to see if this approach is acceptable - both to testing and coding this fix. If there is a better way to structure the test suite, I'm all ears.

Alternate Designs

I thought about DRY'ing the code up a bit so that camelcase wasn't used in so many places, but it would have required adding at least an instance variable and a getter to the Template - in the end that may be the best solution, in case the generators need to deal with other naming considerations (singular and plural).

I went with this solution to get something working, and provided a (hopefully) comprehensive set of tests if a refactor happens in the future.

Benefits

It fixes issue #316

Possible Drawbacks

This change touches a lot of files and has an effect on every generator. Based on the test suite, everything is still generating as normal, but it is possible something breaks in the actual generation of files.

On my machine, one of the pre-existing tests is either not passing, or hanging indefinitely. I checked out the master branch and got the same results.

It hangs here: https://github.com/amberframework/amber/blob/master/spec/amber/cli/commands/generator_spec.cr#L14

Or fails here: https://github.com/amberframework/amber/blob/master/spec/amber/cli/commands/generator_spec.cr#L16

File.delete("./src/views/#{snake_case}/edit.slang")
File.delete("./src/views/#{snake_case}/index.slang")
File.delete("./src/views/#{snake_case}/new.slang")
File.delete("./src/views/#{snake_case}/show.slang")

@veelenga veelenga Oct 26, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If one expectation above fails these files will not be deleted. Or absence of this files is guaranteed by Amber::CLI::Spec.cleanup ? If not, you may need to use begin ... ensure block for this:

begin
  # expectations
ensure
  # delete files
end

But if it is guaranteed, you don't need to delete them manually.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The calls to Amber::CLI::Spec.cleanup should ensure cleanup, but admittedly, I didn't explicitly test for or look for it.

I did run a bunch of failing test suites though, and there were never any leftovers (according to git).

The entire spec in this file is wrapped in a begin ... ensure ... end that executes Amber::CLI::Spec.cleanup which is more comprehensive than my file deletion.

I can rerun some failing tests and explicitly look for leftover files (as they may be gitignored).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made it fail one of the should tests on generating a scaffold, then did an ls tmp and it was empty. It looks like we are good to count on Amber::CLI::Spec.cleanup

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So you don't need to delete it manually, right ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When the test suite is complete, Amber::CLI::Spec.cleanup makes sure everything is cleaned up.

The test itself tests two cases of possible CLI input (in a loop) - PostComment and post_comment

The reason for the manual delete is to avoid false green tests with the second iteration of the loop.

@veelenga veelenga Oct 26, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, sorry, i haven't noticed the loop above. Thanks for the explanation!

@marksiemers

marksiemers commented Oct 27, 2017

Copy link
Copy Markdown
Contributor Author

What's the preferred course of action here? Resolve the conflicts here, or rebase (off master) my changes locally and push with the new commits?

@eliasjpr

Copy link
Copy Markdown
Contributor

Rebase off master

@marksiemers

Copy link
Copy Markdown
Contributor Author

Rebased, refactored, and add a new concept of display_name
LMKWYT

@marksiemers

Copy link
Copy Markdown
Contributor Author

Hold off on this, I found a bug while manually testing - I didn't touch the routes generator.

@marksiemers

Copy link
Copy Markdown
Contributor Author

Manually tested with both Crecto and Granite, and added spec tests for both as well.

LMK if anything else is needed for this PR.

@drujensen drujensen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm. Nice work. Love all the test coverage. 💯 🥇

@drujensen

Copy link
Copy Markdown
Member

@marksiemers I found a couple issues testing the branch:

  1. The name of the project having dashes:
amber new my-cool-blog
cd my-cool-blog
amber g scaffold post title:string
amber db drop create migrate

produces:
syntax error at or near "-"

The config/database.yml needs to be changes to replace the dashes in the db name.

pg:
  database: postgres://root:@localhost:5432/my-cool-blog_development
  1. The field name having dashes:
amber g scaffold post my-title:string

An error ocurred executing migration 20171028085330. Error message is: syntax error at or near "-"

@marksiemers

Copy link
Copy Markdown
Contributor Author

@drujensen - The issue with the app name having dashes is a pre-existing bug
Reported here: #329
Pending fix here: #332

@marksiemers

marksiemers commented Oct 28, 2017

Copy link
Copy Markdown
Contributor Author

@drujensen @veelenga
Any chance of this getting merged soon? The work I'm doing on #294 requires code that will need to be reworked after this change is incorporated.

@elorest elorest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall I I like this. Don't like all the seemingly repetitive code in generator_spec though.

File.read("./config/routes.cr").should contain "#{camel_case}Controller"
File.read("./config/routes.cr").should_not contain "#{incorrect_case}Controller"

File.delete("./spec/models/#{snake_case}_spec.cr")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are we deleting all of these one by one? We already delete he folder after the test. At least we should be.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like you sort of answered my question below. Even if you do have a reason for deleting these we shouldn't be deleting them one by one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can clean it up, the two (not mutually exclusive) ideas that come to mind first are:

  • Use rm -rf ./spec/* rm -rf ./src/*
  • Refactor to put the file handling in a module

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can clean this up, two thoughts come to mind (not mutually exclusive), LMKWYT:

  • Use rm -rf ./src/* rm -rf ./spec/*
  • Have a module take care of the deletion of files (and potentially other file handling operations)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The first option seems clean and easy to read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For the scaffold, I changed it so that a new app is created for each iteration of the loop, and the spec cleanup is also run. This is because the routes file is touched and that can't be deleted safely without generating a new app.

When applicable, I switched to rm -rf ./[dir]/*

@veelenga veelenga left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I haven't tried to run generators and test it locally, but the change looks good to me 👍

@elorest elorest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Still approved! Thanks.

@drujensen drujensen merged commit 33a6eb9 into amberframework:master Oct 30, 2017
@marksiemers marksiemers deleted the fix-name-generator branch October 31, 2017 01:23
elorest pushed a commit that referenced this pull request Nov 17, 2017
* Add tests for generating file names and class names correctly (#316)

* No longer downcase name when instantiating a Template (#316)

* Switch all cases of `@name.capitalize` to `@name.camelcase` (#316)

* Switch from `@name.camelcase` everywhere to `class_name` instance method (#316)

* Add `display_name` method and use it in generators

* Incorporate class naming convention in route generators (#316)

* Incorporate table naming convention in crecto and granite generators (#316)

* Delete files in a better way in generator_spec
elorest pushed a commit that referenced this pull request Nov 17, 2017
* Add tests for generating file names and class names correctly (#316)

* No longer downcase name when instantiating a Template (#316)

* Switch all cases of `@name.capitalize` to `@name.camelcase` (#316)

* Switch from `@name.camelcase` everywhere to `class_name` instance method (#316)

* Add `display_name` method and use it in generators

* Incorporate class naming convention in route generators (#316)

* Incorporate table naming convention in crecto and granite generators (#316)

* Delete files in a better way in generator_spec


Former-commit-id: 7e422db
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.

6 participants