Class: Woro::TaskHelper

Inherits:
Object
  • Object
show all
Includes:
Commander::UI
Defined in:
lib/woro/task_helper.rb

Overview

Set of helper methods used in the woro executable

Class Method Summary collapse

Class Method Details

.check_environmentObject

Check if woro environment is available in project



9
10
11
12
# File 'lib/woro/task_helper.rb', line 9

def check_environment
  return if woro_environment_setup?
  abort 'Woro environment is not set up. Call `woro init` to do so.'
end

.choose_and_build_adapter_config(available_adapters) ⇒ Hash

Choose adapter and return its settings.

Parameters:

  • task_name (String)

    sanitized name of the task, used

Returns:

  • (Hash)

    Hash with adapter name and its settings



73
74
75
76
77
# File 'lib/woro/task_helper.rb', line 73

def choose_and_build_adapter_config(available_adapters)
  adapter_name = select_choice available_adapters, 'Please choose a service to use with Woro:'
  adapter = Object.const_get "Woro::Adapters::#{adapter_name}"
  { adapter_name.downcase => adapter.setup }
end

.create_directory_unless_existing(directory) ⇒ Object

Create the given directory, unless it already exists.

Parameters:

  • directory (String)

    directory to create



16
17
18
19
20
# File 'lib/woro/task_helper.rb', line 16

def create_directory_unless_existing(directory)
  return if File.exist? directory
  FileUtils.mkdir_p directory
  say "Created `#{directory}`"
end

.create_file_unless_existing(file_path, content) ⇒ Object

Write a file at a given path with the given content, unless it already exists.

Parameters:

  • file_path (String)

    save the new file here

  • content (String)

    write this into the file



26
27
28
29
30
31
32
# File 'lib/woro/task_helper.rb', line 26

def create_file_unless_existing(file_path, content)
  return if File.exist? file_path
  File.open(file_path, 'w') do |f|
    f.puts content
  end
  say "Created `#{file_path}`"
end

.create_required_filesObject

Creates all files required for a setup woro environment.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/woro/task_helper.rb', line 35

def create_required_files
  create_directory_unless_existing Woro::Configuration.woro_task_dir
  create_file_unless_existing File.join(Woro::Configuration.woro_task_dir, '.gitignore'), '*.rake'
  create_directory_unless_existing Woro::Configuration.rake_task_dir
  create_directory_unless_existing File.dirname(Woro::Configuration.config_file)

  return if File.exist? File.join(Woro::Configuration.rake_task_dir, 'woro.rake')

  woro_task_file = File.join(File.dirname(__FILE__),
                             'templates', 'woro.rake')
  FileUtils.cp(woro_task_file, Woro::Configuration.rake_task_dir)
  say "Created `woro.rake` in `#{Woro::Configuration.rake_task_dir}`"
end

.read_template_fileString

Read the rake task template

Returns:

  • (String)

    Content of template file



95
96
97
# File 'lib/woro/task_helper.rb', line 95

def read_template_file
  File.read File.join(File.dirname(__FILE__), 'templates', 'task.rake.erb')
end

.select_choice(choices, message) ⇒ String

Display choice of adapter and return name of the chosen one.

Parameters:

  • choices (Array)

    list of choices

Returns:

  • (String)

    Name of chosen adapter



61
62
63
64
65
66
67
68
# File 'lib/woro/task_helper.rb', line 61

def select_choice(choices, message)
  choose do |menu|
    menu.prompt = message
    menu.choices(*choices) do |choice|
      return choice.to_s.strip
    end
  end
end

.woro_environment_setup?boolean

Returns true, if all requirements of a woro setup are met.

Returns:

  • (boolean)

    all configurations, all directories exist



51
52
53
54
55
56
# File 'lib/woro/task_helper.rb', line 51

def woro_environment_setup?
  File.exist?(Woro::Configuration.woro_task_dir) &&
    File.exist?(File.join('config', 'woro.yml')) &&
    File.exist?(Woro::Configuration.rake_task_dir) &&
    File.exist?(File.join(Woro::Configuration.rake_task_dir, 'woro.rake'))
end

.woro_task_files(directory) ⇒ Array

Perform an action over all files within the woro task directory

Parameters:

  • directory (String)

    directory

Returns:

  • (Array)

    List of rake tasks in the directory



82
83
84
85
86
87
88
89
90
91
# File 'lib/woro/task_helper.rb', line 82

def woro_task_files(directory)
  tasks = []
  Dir.foreach(directory) do |file_name|
    if file_name.include? '.rake'
      data = File.read(File.join(directory, file_name))
      tasks << yield(file_name, data)
    end
  end
  tasks
end