Class: Consular::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/consular/dsl.rb

Overview

The DSL class provides the DSL for the Consular scripting file. It provides the basic commands such as:

#setup  - commands to run when invoked by 'consular setup'
#window - commands to run in the context of a window
#tab    - commands to run in the context of tab
#before - commands to run in the context before every tab context

The DSL class can be extended to provide additional API’s for core specific DSL.

Defined Under Namespace

Modules: Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DSL

Initializes the DSL library and stores the commands.

Examples:

Consular::DSL.new 'foo/bar.term'

Parameters:

  • path (String)

    path to Consular script/ Termfile



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/consular/dsl.rb', line 29

def initialize(path)
  @_setup              = []
  @_windows            = ActiveSupport::OrderedHash.new
  @_windows['default'] = window_hash
  @_context            = @_windows['default']
  file = File.read(path)
  if path =~ /\.yml$/
    @_file = YAML.load file
    extend Yaml
  else
    instance_eval file, __FILE__, __LINE__
  end
end

Instance Attribute Details

#_contextObject (readonly)

Returns the value of attribute _context.



18
19
20
# File 'lib/consular/dsl.rb', line 18

def _context
  @_context
end

#_setupObject (readonly)

Returns the value of attribute _setup.



18
19
20
# File 'lib/consular/dsl.rb', line 18

def _setup
  @_setup
end

#_windowsObject (readonly)

Returns the value of attribute _windows.



18
19
20
# File 'lib/consular/dsl.rb', line 18

def _windows
  @_windows
end

Instance Method Details

#before(*commands, &block) ⇒ Object

Run commands prior to each tab context.

Examples:

# Executes `whoami` before tab with `ls` and `gitx`
window do
  before { run 'whoami' }
  tab 'ls'
  tab 'gitx'
end

Parameters:

  • commands (Array<String>)

    Commands to be executed.

  • block (Proc)

    Proc of commands to run



77
78
79
80
# File 'lib/consular/dsl.rb', line 77

def before(*commands, &block)
  context = (@_context[:before] ||= [])
  block_given? ? run_context(context, &block) : context.concat(commands)
end

#run(*commands) ⇒ Object

Store commands to run in context.

Examples:

run 'brew update', 'gitx'

Parameters:

  • commands (Array<String>)

    Array of commands to be executed.



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/consular/dsl.rb', line 145

def run(*commands)
  context = case
            when @_context.is_a?(Hash) && @_context[:tabs]
              @_context[:tabs]['default'][:commands]
            when @_context.is_a?(Hash)
              @_context[:commands]
            else
              @_context
            end
  context << commands.map { |c| c =~ /&$/ ? "(#{c})" : c }.join(" && ")
end

#setup(*commands, &block) ⇒ Object

Run commands using prior to the workflow using the command ‘consular setup`. This allows you to perform any command that needs to be ran prior to setup a particular project/script.

Examples:

setup 'bundle install', 'brew update'
setup { run 'bundle install' }

Parameters:

  • commands (Array<String>)

    Commands to be executed.

  • block (Proc)

    Proc of commands to run.



57
58
59
# File 'lib/consular/dsl.rb', line 57

def setup(*commands, &block)
  block_given? ? run_context(@_setup, &block) : @_setup.concat(commands)
end

#tab(*args, &block) ⇒ Object

Run commands in the context of a tab.

Examples:

tab 'first tab', :settings => 'Grass' do
  run 'ps aux'
end

tab 'ls', 'gitx'

Parameters:

  • args (Array)

    Accepts either:

    - an array of string commands
    - a hash containing options for the tab.
    
  • block (Proc)


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/consular/dsl.rb', line 122

def tab(*args, &block)
  tabs = @_context[:tabs]
  key  = "tab#{tabs.keys.size}"
  return (tabs[key] = { :commands => args }) unless block_given?

  context           = (tabs[key] = {:commands => []})
  options           = args.extract_options!
  options[:name]    = args.first unless args.empty?
  context[:options] = options

  run_context context, &block
  @_context = @_windows[@_windows.keys.last] # Jump back out into the context of the last window.
end

#to_hashHash

Returns yaml file as Consular formmatted hash

Returns:

  • (Hash)

    Return hash format of Termfile



163
164
165
# File 'lib/consular/dsl.rb', line 163

def to_hash
  { :setup => @_setup, :windows => @_windows }
end

#window(*args, &block) ⇒ Object

Run commands in the conext of a window.

Examples:

window 'my project', :size => [80, 30] do
  run 'ps aux'
end

Parameters:

  • args (Array)

    Hash to pass options to each context of a window. Each core can implement the desired behavior for the window based on the options set here. Can also pass a string as first parameter which will be set as the :name

  • block (Proc)

    block of commands to run in window context.



98
99
100
101
102
103
104
# File 'lib/consular/dsl.rb', line 98

def window(*args, &block)
  key            = "window#{@_windows.keys.size}"
  options        = args.extract_options!
  options[:name] = args.first unless args.empty?
  context = (@_windows[key] = window_hash.merge(:options => options))
  run_context context, &block
end