Class: Tmuxinator::Project

Inherits:
Object show all
Includes:
Deprecations, Util, WemuxSupport
Defined in:
lib/tmuxinator/project.rb

Constant Summary collapse

RBENVRVM_DEP_MSG =
<<-M
DEPRECATION: rbenv/rvm-specific options have been replaced by the
pre_tab option and will not be supported in 0.8.0.
M
TABS_DEP_MSG =
<<-M
DEPRECATION: The tabs option has been replaced by the windows option
and will not be supported in 0.8.0.
M
CLIARGS_DEP_MSG =
<<-M
DEPRECATION: The cli_args option has been replaced by the tmux_options
option and will not be supported in 0.8.0.
M

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WemuxSupport

#load_wemux_overrides, #wemux?

Methods included from Deprecations

#cli_args?, #pre_tab?, #rbenv?, #rvm?

Methods included from Util

#exit!, #yes_no

Constructor Details

#initialize(yaml, options = {}) ⇒ Project

Returns a new instance of Project.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tmuxinator/project.rb', line 62

def initialize(yaml, options = {})
  options[:force_attach] = false if options[:force_attach].nil?
  options[:force_detach] = false if options[:force_detach].nil?

  @yaml = yaml

  @custom_name = options[:custom_name]

  @force_attach = options[:force_attach]
  @force_detach = options[:force_detach]

  raise "Cannot force_attach and force_detach at the same time" \
    if @force_attach && @force_detach

  load_wemux_overrides if wemux?
end

Instance Attribute Details

#custom_nameObject (readonly)

Returns the value of attribute custom_name.



23
24
25
# File 'lib/tmuxinator/project.rb', line 23

def custom_name
  @custom_name
end

#force_attachObject (readonly)

Returns the value of attribute force_attach.



21
22
23
# File 'lib/tmuxinator/project.rb', line 21

def force_attach
  @force_attach
end

#force_detachObject (readonly)

Returns the value of attribute force_detach.



22
23
24
# File 'lib/tmuxinator/project.rb', line 22

def force_detach
  @force_detach
end

#yamlObject (readonly)

Returns the value of attribute yaml.



20
21
22
# File 'lib/tmuxinator/project.rb', line 20

def yaml
  @yaml
end

Class Method Details

.load(path, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tmuxinator/project.rb', line 25

def self.load(path, options = {})
  yaml = begin
    raw_content = File.read(path)

    args = options[:args] || []
    @settings = parse_settings(args)
    @args = args

    content = Erubis::Eruby.new(raw_content).result(binding)
    YAML.load(content)
  rescue SyntaxError, StandardError
    raise "Failed to parse config file. Please check your formatting."
  end

  new(yaml, options)
end

.parse_settings(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tmuxinator/project.rb', line 42

def self.parse_settings(args)
  settings = args.select { |x| x.match(/.*=.*/) }
  args.reject! { |x| x.match(/.*=.*/) }

  settings.map! do |setting|
    parts = setting.split("=")
    [parts[0], parts[1]]
  end

  Hash[settings]
end

Instance Method Details

#attach?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
# File 'lib/tmuxinator/project.rb', line 111

def attach?
  if yaml["attach"].nil?
    yaml_attach = true
  else
    yaml_attach = yaml["attach"]
  end
  attach = force_attach || !force_detach && yaml_attach
  attach
end

#base_indexObject



182
183
184
# File 'lib/tmuxinator/project.rb', line 182

def base_index
  get_pane_base_index ? get_pane_base_index.to_i : get_base_index.to_i
end

#deprecationsObject



226
227
228
229
230
231
232
# File 'lib/tmuxinator/project.rb', line 226

def deprecations
  deprecations = []
  deprecations << RBENVRVM_DEP_MSG if yaml["rbenv"] || yaml["rvm"]
  deprecations << TABS_DEP_MSG if yaml["tabs"]
  deprecations << CLIARGS_DEP_MSG if yaml["cli_args"]
  deprecations
end

#get_base_indexObject



238
239
240
# File 'lib/tmuxinator/project.rb', line 238

def get_base_index
  tmux_config["base-index"]
end

#get_pane_base_indexObject



234
235
236
# File 'lib/tmuxinator/project.rb', line 234

def get_pane_base_index
  tmux_config["pane-base-index"]
end

#nameObject



97
98
99
100
# File 'lib/tmuxinator/project.rb', line 97

def name
  name = custom_name || yaml["project_name"] || yaml["name"]
  name.blank? ? nil : name.to_s.shellescape
end

#name?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/tmuxinator/project.rb', line 202

def name?
  !name.nil?
end

#postObject



133
134
135
136
137
138
139
140
# File 'lib/tmuxinator/project.rb', line 133

def post
  post_config = yaml["post"]
  if post_config.is_a?(Array)
    post_config.join("; ")
  else
    post_config
  end
end

#preObject



102
103
104
105
106
107
108
109
# File 'lib/tmuxinator/project.rb', line 102

def pre
  pre_config = yaml["pre"]
  if pre_config.is_a?(Array)
    pre_config.join("; ")
  else
    pre_config
  end
end

#pre_windowObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tmuxinator/project.rb', line 121

def pre_window
  if rbenv?
    "rbenv shell #{yaml['rbenv']}"
  elsif rvm?
    "rvm use #{yaml['rvm']}"
  elsif pre_tab?
    yaml["pre_tab"]
  else
    yaml["pre_window"]
  end
end

#renderObject



79
80
81
82
# File 'lib/tmuxinator/project.rb', line 79

def render
  template = File.read(Tmuxinator::Config.template)
  Erubis::Eruby.new(template).result(binding)
end

#rootObject



92
93
94
95
# File 'lib/tmuxinator/project.rb', line 92

def root
  root = yaml["project_root"] || yaml["root"]
  root.blank? ? nil : File.expand_path(root).shellescape
end

#root?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/tmuxinator/project.rb', line 198

def root?
  !root.nil?
end

#send_keys(cmd, window_index) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/tmuxinator/project.rb', line 218

def send_keys(cmd, window_index)
  if cmd.empty?
    ""
  else
    "#{tmux} send-keys -t #{window(window_index)} #{cmd.shellescape} C-m"
  end
end

#send_pane_command(cmd, window_index, _pane_index) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/tmuxinator/project.rb', line 210

def send_pane_command(cmd, window_index, _pane_index)
  if cmd.empty?
    ""
  else
    "#{tmux} send-keys -t #{window(window_index)} #{cmd.shellescape} C-m"
  end
end

#show_tmux_optionsObject



242
243
244
# File 'lib/tmuxinator/project.rb', line 242

def show_tmux_options
  "#{tmux} start-server\\; show-option -g"
end

#socketObject



156
157
158
159
160
161
162
# File 'lib/tmuxinator/project.rb', line 156

def socket
  if socket_path
    " -S #{socket_path}"
  elsif socket_name
    " -L #{socket_name}"
  end
end

#socket_nameObject



164
165
166
# File 'lib/tmuxinator/project.rb', line 164

def socket_name
  yaml["socket_name"]
end

#socket_pathObject



168
169
170
# File 'lib/tmuxinator/project.rb', line 168

def socket_path
  yaml["socket_path"]
end

#startup_windowObject



186
187
188
# File 'lib/tmuxinator/project.rb', line 186

def startup_window
  yaml["startup_window"] || base_index
end

#tmuxObject



142
143
144
# File 'lib/tmuxinator/project.rb', line 142

def tmux
  "#{tmux_command}#{tmux_options}#{socket}"
end

#tmux_commandObject



146
147
148
# File 'lib/tmuxinator/project.rb', line 146

def tmux_command
  yaml["tmux_command"] || "tmux"
end

#tmux_has_session?(name) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
# File 'lib/tmuxinator/project.rb', line 150

def tmux_has_session?(name)
  sessions = `#{tmux_command} ls`

  !!sessions.match("^#{name}:")
end

#tmux_kill_session_commandObject



251
252
253
# File 'lib/tmuxinator/project.rb', line 251

def tmux_kill_session_command
  "#{tmux} kill-session -t #{name}"
end

#tmux_new_session_commandObject



246
247
248
249
# File 'lib/tmuxinator/project.rb', line 246

def tmux_new_session_command
  window = windows.first.tmux_window_name_option
  "#{tmux} new-session -d -s #{name} #{window}"
end

#tmux_optionsObject



172
173
174
175
176
177
178
179
180
# File 'lib/tmuxinator/project.rb', line 172

def tmux_options
  if cli_args?
    " #{yaml['cli_args'].to_s.strip}"
  elsif tmux_options?
    " #{yaml['tmux_options'].to_s.strip}"
  else
    ""
  end
end

#tmux_options?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/tmuxinator/project.rb', line 190

def tmux_options?
  yaml["tmux_options"]
end

#validate!Object



54
55
56
57
58
59
60
# File 'lib/tmuxinator/project.rb', line 54

def validate!
  raise "Your project file should include some windows." \
    unless self.windows?
  raise "Your project file didn't specify a 'project_name'" \
    unless self.name?
  self
end

#window(i) ⇒ Object



206
207
208
# File 'lib/tmuxinator/project.rb', line 206

def window(i)
  "#{name}:#{i}"
end

#windowsObject



84
85
86
87
88
89
90
# File 'lib/tmuxinator/project.rb', line 84

def windows
  windows_yml = yaml["tabs"] || yaml["windows"]

  @windows ||= (windows_yml || {}).map.with_index do |window_yml, index|
    Tmuxinator::Window.new(window_yml, index, self)
  end
end

#windows?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/tmuxinator/project.rb', line 194

def windows?
  windows.any?
end