Class: Pakyow::Task

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Support::DeepFreeze
Includes:
Rake::DSL
Defined in:
lib/pakyow/task.rb

Overview

Base task class that extends rake with additional functionality.

Defined Under Namespace

Classes: Loader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace: [], description: nil, arguments: {}, options: {}, flags: {}, task_args: [], global: false, &block) ⇒ Task

Returns a new instance of Task.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pakyow/task.rb', line 26

def initialize(namespace: [], description: nil, arguments: {}, options: {}, flags: {}, task_args: [], global: false, &block)
  @description, @arguments, @options, @flags, @global = description, arguments, options, flags, global
  @short_names = determine_short_names

  if namespace.any?
    send(:namespace, namespace.join(":")) do
      define_task(task_args, block)
    end
  else
    define_task(task_args, block)
  end
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



24
25
26
# File 'lib/pakyow/task.rb', line 24

def description
  @description
end

Instance Method Details

#app?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/pakyow/task.rb', line 121

def app?
  args.include?(:app)
end

#call(options = {}, argv = []) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/pakyow/task.rb', line 39

def call(options = {}, argv = [])
  parse_options(argv, options)
  parse_arguments(argv, options)
  check_options(options)

  @rake.invoke(*args.map { |arg|
    options[arg]
  })
end

#global?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/pakyow/task.rb', line 125

def global?
  @global == true
end

#help(describe: true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pakyow/task.rb', line 49

def help(describe: true)
  required_arguments = sorted_arguments.select { |_, argument|
    argument[:required]
  }.map { |key, _|
    "[#{key.to_s.upcase}]"
  }.join(" ")

  required_options = sorted_options.select { |_, option|
    option[:required]
  }.map { |key, _|
    "--#{key}=#{key}"
  }.join(" ")

  text = String.new

  if describe
    text << Support::CLI.style.blue.bold(@description) + "\n"
  end

  text += <<~HELP

    #{Support::CLI.style.bold("USAGE")}
      $ pakyow #{[name, required_arguments, required_options].reject(&:empty?).join(" ")}
  HELP

  if @arguments.any?
    text += <<~HELP

      #{Support::CLI.style.bold("ARGUMENTS")}
    HELP

    longest_length = @arguments.keys.map(&:to_s).max_by(&:length).length
    sorted_arguments.each do |key, argument|
      description = Support::CLI.style.yellow(argument[:description])
      if argument[:required]
        description += Support::CLI.style.red(" (required)")
      end
      text += "  #{key.upcase}".ljust(longest_length + 4) + description + "\n"
    end
  end

  if @options.any?
    text += <<~HELP

      #{Support::CLI.style.bold("OPTIONS")}
    HELP

    longest_length = (@options.keys + @flags.keys).map(&:to_s).max_by(&:length).length
    sorted_options_and_flags.each do |key, option|
      description = Support::CLI.style.yellow(option[:description])

      if option[:required]
        description += Support::CLI.style.red(" (required)")
      end

      prefix = if @flags.key?(key)
        "      --#{key}"
      else
        if @short_names.key?(key)
          "  -#{key.to_s[0]}, --#{key}=#{key}"
        else
          "      --#{key}=#{key}"
        end
      end

      text += prefix.ljust(longest_length * 2 + 11) + description + "\n"
    end
  end

  text
end