Class: Tmuxinator::Project

Inherits:
Object
  • Object
show all
Includes:
Deprecations, Hooks::Project, Util
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
SYNC_DEP_MSG =
<<-M
DEPRECATION: The `synchronize` option's current default behaviour is to
enable pane synchronization before running commands. In a future release,
the default synchronization option will be `after`, i.e. synchronization of
panes will occur after the commands described in each of the panes
have run. At that time, the current behavior will need to be explicitly
enabled, using the `synchronize: before` option. To use this behaviour
now, use the 'synchronize: after' option.
M
PRE_DEP_MSG =
<<-M
DEPRECATION: The `pre` option has been replaced by project hooks (`on_project_start` and
`on_project_restart`) and will be removed in a future release.
M
POST_DEP_MSG =
<<-M
DEPRECATION: The `post` option has been replaced by project hooks (`on_project_stop` and
`on_project_exit`) and will be removed in a future release.
M

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks::Project

hook_on_project_exit, hook_on_project_first_start, hook_on_project_restart, hook_on_project_start, hook_on_project_stop

Methods included from Deprecations

#pre_tab?

Methods included from Util

#exit!, #yes_no

Constructor Details

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

Returns a new instance of Project.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tmuxinator/project.rb', line 79

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

  extend Tmuxinator::WemuxSupport if wemux?
end

Instance Attribute Details

#custom_nameObject (readonly)

Returns the value of attribute custom_name.



40
41
42
# File 'lib/tmuxinator/project.rb', line 40

def custom_name
  @custom_name
end

#force_attachObject (readonly)

Returns the value of attribute force_attach.



38
39
40
# File 'lib/tmuxinator/project.rb', line 38

def force_attach
  @force_attach
end

#force_detachObject (readonly)

Returns the value of attribute force_detach.



39
40
41
# File 'lib/tmuxinator/project.rb', line 39

def force_detach
  @force_detach
end

#yamlObject (readonly)

Returns the value of attribute yaml.



37
38
39
# File 'lib/tmuxinator/project.rb', line 37

def yaml
  @yaml
end

Class Method Details

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



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

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.safe_load(content, [], [], true)
  rescue SyntaxError, StandardError => error
    raise "Failed to parse config file: #{error.message}"
  end

  new(yaml, options)
end

.parse_settings(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tmuxinator/project.rb', line 59

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

.render_template(template, bndg) ⇒ Object



104
105
106
107
# File 'lib/tmuxinator/project.rb', line 104

def self.render_template(template, bndg)
  content = File.read(template)
  Erubis::Eruby.new(content).result(bndg)
end

Instance Method Details

#attach?Boolean

Returns:

  • (Boolean)


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

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

#base_indexObject



210
211
212
# File 'lib/tmuxinator/project.rb', line 210

def base_index
  get_base_index.to_i
end

#cli_args?Boolean

Returns:

  • (Boolean)


304
305
306
# File 'lib/tmuxinator/project.rb', line 304

def cli_args?
  yaml["cli_args"]
end

#deprecation_checksObject



266
267
268
269
270
271
272
273
274
275
# File 'lib/tmuxinator/project.rb', line 266

def deprecation_checks
  [
    rvm_or_rbenv?,
    tabs?,
    cli_args?,
    legacy_synchronize?,
    pre?,
    post?
  ]
end

#deprecation_messagesObject



277
278
279
280
281
282
283
284
285
286
# File 'lib/tmuxinator/project.rb', line 277

def deprecation_messages
  [
    RBENVRVM_DEP_MSG,
    TABS_DEP_MSG,
    CLIARGS_DEP_MSG,
    SYNC_DEP_MSG,
    PRE_DEP_MSG,
    POST_DEP_MSG
  ]
end

#deprecationsObject



258
259
260
261
262
263
264
# File 'lib/tmuxinator/project.rb', line 258

def deprecations
  deprecation_checks.zip(deprecation_messages).
    inject([]) do |deps, (chk, msg)|
    deps << msg if chk
    deps
  end
end

#get_base_indexObject



320
321
322
# File 'lib/tmuxinator/project.rb', line 320

def get_base_index
  tmux_config["base-index"]
end

#get_pane_base_indexObject



316
317
318
# File 'lib/tmuxinator/project.rb', line 316

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

#killObject



100
101
102
# File 'lib/tmuxinator/project.rb', line 100

def kill
  self.class.render_template(Tmuxinator::Config.stop_template, binding)
end

#nameObject



122
123
124
125
# File 'lib/tmuxinator/project.rb', line 122

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

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  !name.nil?
end

#pane_base_indexObject



214
215
216
# File 'lib/tmuxinator/project.rb', line 214

def pane_base_index
  get_pane_base_index.to_i
end

#postObject



155
156
157
158
# File 'lib/tmuxinator/project.rb', line 155

def post
  post_config = yaml["post"]
  parsed_parameters(post_config)
end

#post?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/tmuxinator/project.rb', line 312

def post?
  yaml["post"]
end

#preObject



127
128
129
130
# File 'lib/tmuxinator/project.rb', line 127

def pre
  pre_config = yaml["pre"]
  parsed_parameters(pre_config)
end

#pre?Boolean

Returns:

  • (Boolean)


308
309
310
# File 'lib/tmuxinator/project.rb', line 308

def pre?
  yaml["pre"]
end

#pre_windowObject



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tmuxinator/project.rb', line 142

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

#rbenv?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/tmuxinator/project.rb', line 288

def rbenv?
  yaml["rbenv"]
end

#renderObject



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

def render
  self.class.render_template(Tmuxinator::Config.template, binding)
end

#rootObject



117
118
119
120
# File 'lib/tmuxinator/project.rb', line 117

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

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  !root.nil?
end

#rvm?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/tmuxinator/project.rb', line 292

def rvm?
  yaml["rvm"]
end

#rvm_or_rbenv?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/tmuxinator/project.rb', line 296

def rvm_or_rbenv?
  rvm? || rbenv?
end

#send_keys(cmd, window_index) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/tmuxinator/project.rb', line 250

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



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

def send_pane_command(cmd, window_index, _pane_index)
  send_keys(cmd, window_index)
end

#show_tmux_optionsObject



324
325
326
327
328
# File 'lib/tmuxinator/project.rb', line 324

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

#socketObject



184
185
186
187
188
189
190
# File 'lib/tmuxinator/project.rb', line 184

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

#socket_nameObject



192
193
194
# File 'lib/tmuxinator/project.rb', line 192

def socket_name
  yaml["socket_name"]
end

#socket_pathObject



196
197
198
# File 'lib/tmuxinator/project.rb', line 196

def socket_path
  yaml["socket_path"]
end

#startup_paneObject



222
223
224
# File 'lib/tmuxinator/project.rb', line 222

def startup_pane
  "#{startup_window}.#{yaml['startup_pane'] || pane_base_index}"
end

#startup_windowObject



218
219
220
# File 'lib/tmuxinator/project.rb', line 218

def startup_window
  "#{name}:#{yaml['startup_window'] || base_index}"
end

#tabs?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/tmuxinator/project.rb', line 300

def tabs?
  yaml["tabs"]
end

#tmuxObject



160
161
162
# File 'lib/tmuxinator/project.rb', line 160

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

#tmux_commandObject



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

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

#tmux_has_session?(name) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/tmuxinator/project.rb', line 168

def tmux_has_session?(name)
  # Redirect stderr to /dev/null in order to prevent "failed to connect
  # to server: Connection refused" error message and non-zero exit status
  # if no tmux sessions exist.
  # Please see issues #402 and #414.
  sessions = `#{tmux} ls 2> /dev/null`

  # Remove any escape sequences added by `shellescape` in Project#name.
  # Escapes can result in: "ArgumentError: invalid multibyte character"
  # when attempting to match `name` against `sessions`.
  # Please see issue #564.
  unescaped_name = name.shellsplit.join("")

  !(sessions !~ /^#{unescaped_name}:/)
end

#tmux_kill_session_commandObject



335
336
337
# File 'lib/tmuxinator/project.rb', line 335

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

#tmux_new_session_commandObject



330
331
332
333
# File 'lib/tmuxinator/project.rb', line 330

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

#tmux_optionsObject



200
201
202
203
204
205
206
207
208
# File 'lib/tmuxinator/project.rb', line 200

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)


226
227
228
# File 'lib/tmuxinator/project.rb', line 226

def tmux_options?
  yaml["tmux_options"]
end

#validate!Object



71
72
73
74
75
76
77
# File 'lib/tmuxinator/project.rb', line 71

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

#window(i) ⇒ Object



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

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

#windowsObject



109
110
111
112
113
114
115
# File 'lib/tmuxinator/project.rb', line 109

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)


230
231
232
# File 'lib/tmuxinator/project.rb', line 230

def windows?
  windows.any?
end