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

Append Option #2

Closed
drujensen opened this issue Jan 7, 2017 · 9 comments
Closed

Append Option #2

drujensen opened this issue Jan 7, 2017 · 9 comments

Comments

@drujensen
Copy link
Contributor

Add the ability to append to the end of a file instead of skip/overwrite it.

@drujensen
Copy link
Contributor Author

I need this for only specific files in the templates. Specifically, I need to append to the config/routes.cr file.

maybe a file naming convention? If the file name ends with a + then it will append instead of overwrite?

@mosop
Copy link
Owner

mosop commented Jan 8, 2017

I think it's a rare case. Instead of "append", how is "custom rendering" like this?

class Template < Teeplate::FileTree
  directory "/path/to/template"

  def overwrite_file(out_file, ...)
    if out_file == "config/routes.cr"
      overwrite_routes out_file, ...
    else
      super
    end
  end

  def overwrite_routes(out_file, ...)
    # ...
  end
end

@mosop
Copy link
Owner

mosop commented Jan 8, 2017

Or, tell me more details about your story if you can.

@drujensen
Copy link
Contributor Author

@mosop Thanks for your response!

We are using two of your libraries to build a rails like generator for Kemalyst. They have been a huge help to this project. https://github.com/TechMagister/kemalyst-generator

To generate a resource, we need to inject the routes into the /config/routes.cr file. Your suggestion of overwriting the method should work fine. You are correct that this is a rare case and something we can handle in the Template class.

Thanks for the suggestion.

@drujensen drujensen reopened this Jan 8, 2017
@mosop
Copy link
Owner

mosop commented Jan 9, 2017

Thank you.

  • How does the generator's user append a new route into the routes.cr?

  • Can I see the routes.cr samples? I want to see:

    • a typical state before appending
    • a typical state after appending

@drujensen
Copy link
Contributor Author

Here is the routes.cr template when you create a new Kemalyst project: https://github.com/TechMagister/kemalyst-generator/blob/master/src/templates/app/config/routes.cr

Here is the scaffolding to add a new resource to the routes.cr: https://github.com/TechMagister/kemalyst-generator/blob/master/src/templates/scaffold/config/%2Broutes.cr.ecr

I solved this problem by re-opening the Entry.cr and looking for a + in the filename. If there is a +, then I strip the + and open the file as append: https://github.com/TechMagister/kemalyst-generator/blob/master/src/templates/entry.cr

Let me know if you have other suggestions on how to solve this.

@mosop
Copy link
Owner

mosop commented Jan 10, 2017

Thank you. I'll think it over. Wait for a while.

@mosop
Copy link
Owner

mosop commented Jan 21, 2017

Hi @drujensen, I added this feature into v0.3.0. It's almost the same as you want; You add a "+" sign into the head of filename and the file is written with "append mode". Try it!

@mosop mosop closed this as completed Jan 21, 2017
@drujensen
Copy link
Contributor Author

Awesome. Thanks!

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

No branches or pull requests

2 participants