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: Additional Features #1236

Closed AndiLavera closed 3 years ago

AndiLavera commented 3 years ago

Description of the Change

Small changes to expand the functionality of plugins.

One feature allow passing arguments to liquid template files. The syntax is:

amber plugin mochi granite

With granite being the single argument.

I added support for auto-generating plugs. The final commit ensures the config.yml file is deleted after rendering. This should of made it into the previous PR.

A spec has been added for all additional features.

Benefits

Plugin templates can be more dynamic. For example, my shard Mochi supports both Granite and Jennifer ORM's. Rather than having shards "mochi-granite-plugin" and "mochi-jennifer-plugin" that would be almost duplicates of each other, I can use simple if statements in my plugin templates.

if args.orm == "granite"

Possible Drawbacks

It's possible users don't pass in the proper arguments and the template doesn't come out right.

Example Config File

I have a branch open on my authentication shard waiting for this final PR. In case anyone else wanted to take a look, here is what the config file currently supports.

args: 
  - orm
  - extension
routes:
  plugs:
    web:
      - "# Mochi Authenticable"
      - plug CurrentUser.new
    auth:
      - "# Mochi Authenticable"
      - plug Authenticate.new
  pipelines:
    web:
      - "# Mochi Authenticable"
      - get "/signin", SessionController, :new
      - post "/session", SessionController, :create
      - get "/signup", UserController, :new
      - post "/signup", UserController, :create
      - ""
      - "# Mochi Confirmable"
      - "# get \"/registration/update\", RegistrationController, :update"
      - ""
      - "# Mochi Invitable"
      - "# get \"/invite/new\", InvitableController, :new"
      - "# get \"/invite/edit\", InvitableController, :edit"
      - "# post \"/invite\", InvitableController, :create"
      - "# patch \"/invite\", InvitableController, :update"
      - ""
      - "# Mochi Lockable"
      - "# get \"/unlock\", UnlockableController, :update"
      - ""
      - "# Mochi Omniauthable"
      - "# get \"/omniauth/user/:provider\", Omniauthable::UserController, :create"
      - "# get \"/omniauth/user/:provider/callback\", Omniauthable::UserController, :callback"
      - "# get \"/omniauth/:provider\", Omniauthable::SessionController, :create"
      - "# get \"/omniauth/:provider/callback\", Omniauthable::SessionController, :callback"
      - ""
      - "# Mochi Recoverable"
      - "# get \"/reset/password\", RecoverableController, :new"
      - "# get \"/reset/password/edit\", RecoverableController, :edit"
      - "# post \"/reset/password\", RecoverableController, :create"
      - "# patch \"/reset/password/:id\", RecoverableController, :update"
    auth:
      - "# Mochi Authenticable"
      - get "/profile", UserController, :show
      - get "/profile/edit", UserController, :edit
      - patch "/profile", UserController, :update
      - get "/signout", SessionController, :destroy

We decided not to go with submodules since the crystal binary doesn't include unused code. Because of that, I wanted only my "first" module (basic authentication) to have routes and the rest are commented out. Users can uncomment what they need and delete the rest.

Routes & plugs get auto-generated and in my liquid templates, i can reference args.orm and args.extension.

AndiLavera commented 3 years ago

A spec has been added.

I also changed the naming scheme for arguments. Plugin authors define argument names in their config.yml.

args:
  - name
  - age

Then these can be accessed in liquid templates with args.name & args.age. Let me know what you think.

AndiLavera commented 3 years ago

@drujensen I updated the first comment of this PR to include an explanation of all changes. Please let me know if any additional changes need to be made.