amberframework / amber

A Crystal web framework that makes building applications fast, simple, and enjoyable. Get started with quick prototyping, less bugs, and blazing fast performance.
https://amberframework.org
MIT License
2.57k stars 206 forks source link

Plugin: Add support for a config file #1235

Closed AndiLavera closed 3 years ago

AndiLavera commented 3 years ago

Description of the Change

This adds the ability for plugin authors to create a config.yml file. The file is should be located in the plugin folder. Currently, I have only added the ability to inject routes into the users route.cr file but I have no problem adding extra functionality if people bring up ideas.

The code expects the YAML file to be in the following format:

routes:
  pipelines:
    web:
      - get "/", HomeController, :index
      - post "/", HomeController, :index

Benefits

Right now, the benefit is routes can be auto-generated.

Possible Drawbacks

More code.

A current limitation is it breaks if you don't have the full information. The cli will not read the file if the file doesn't exist. It doesn't break if the file is empty. It does break when the file is just:

routes:
  pipelines:

or

routes:
  pipelines:
    web:

But plugin authors shouldn't be doing that anyways.

AndiLavera commented 3 years ago

@drujensen A spec has been added.

I had to read the routes file my self. I tried doing what routes_spec does with:

output = ""
Amber::CLI::MainCommand.run %w(routes) { |cmd| output = cmd.out.gets_to_end }
output_lines = route_table_rows(output)

But it would hang indefinitely.