Tasks are used to run some code that sits outside of your main application. When you need to add a task, you’ll place it in the tasks folder. To see a list of the available tasks in your application, use lucky --help.

You can think of a Luck Task just like a Rake task in Ruby, npm script for Node, or Mix Task for Elixir.

To run a task, you just run lucky name.of.task [options]

Built-in Tasks

By default, Lucky has a few tasks already built in that you may find really handy while developing your application. These are the ones that you get by default.

# Generated with lucky --help
lucky db.create       - Create the database
lucky db.drop         - Drop the database
lucky db.migrate      - Migrate the database
lucky db.migrate.one  - Run the next pending migration
lucky db.redo         - Rollback then run the last migration
lucky db.rollback     - Rollback the last migration
lucky db.rollback_all - Rollback all migrations
lucky gen.action      - Generate a new action
lucky gen.migration   - Generate a new migration
lucky gen.model       - Generate a model, query, and form
lucky gen.secret_key  - Generate a new secret key
lucky routes          - Show all the routes for the app
lucky watch           - Start and recompile project when files change

Custom Tasks

Custom tasks you need will be placed in the tasks folder of your application. The three main things to creating a custom task is that your class inherits from LuckyCli::Task, it implements a call method, and includes a banner. Be sure to require "lucky_cli" at the top of your new task.

Lucky will infer the name of the task by using the name of your class. This includes using namespaces. (e.g. Db::Migrate becomes lucky db.migrate).

# tasks/generate_sitemaps.cr
require "lucky_cli"

class GenerateSitemaps < LuckCli::Task
  banner "Generate the sitemap.xml for this site"

  def call
    # Implement your task here
$ lucky --help

Usage: lucky name.of.task [options]

Available tasks:

  ▸ generate_sitemaps Generate the sitemap.xml for this site
Next: Logging and Error Handling