Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
Kevin O'Sullivan edited this page Jul 8, 2021 · 6 revisions

Tasks are grouped sets of functionality that can be called from your commands. There can be several reasons to use a task.

  • Prerequisite Tasks A prerequisite_task declaration on a command will run that task before a command is called so that you can group environment validations before your commands so that when your command is invoked, you can be certain of the state of the environment.
  • Shared Functionality often, there is shared code between commands and this can be captured in a task to reduce this complexity.
  • Capture Business Process If may be easier to test your business logic, completely away from presentation logic. Tasks can better support this.

A task is a very simple class that just implements a call method

module Rails
  module Tasks
    class Build < ShopifyCli::Task
      def call(ctx)
        # coordinate large task here.
      end
    end
  end
end

Setup your project to load it

# frozen_string_literal: true
module Rails
  class Project < ShopifyCli::ProjectType
  end
  module Tasks
    # autoload the class so ruby knows where to load it from
    autoload :Build, Project.project_filepath('tasks/build')
  end
end

And then this can be called like Rails::Tasks::Build.call(@ctx).

Prerequisite Task

If you want to use the task as a prerequisite task, then it can be registered in your project

# frozen_string_literal: true
module Rails
  class Project < ShopifyCli::ProjectType
    # register the task so that it can be found by name
    register_task Rails::Tasks::Build, 'build'
  end
end

and added to a command

# frozen_string_literal: true
module Rails
  class Command
    class Run < ShopifyCli::SubCommand
      prerequisite_task :build
Clone this wiki locally