Class: Tmuxinator::Project

Inherits:
Object
  • Object
show all
Includes:
Deprecations, Hooks::Project, 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
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 and will
not be supported anymore.
M
POST_DEP_MSG =
<<-M
DEPRECATION: The `post` option has been replaced by project hooks and will
not be supported anymore.
M
TMUX_MASTER_DEP_MSG =
<<-M
DEPRECATION: You are running tmuxinator with an unsupported version of tmux.
Please consider using a supported version:
(#{Tmuxinator::SUPPORTED_TMUX_VERSIONS.join(', ')})
M

Constants included from WemuxSupport

WemuxSupport::COMMAND

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WemuxSupport

#load_wemux_overrides, #override_commands!, #override_render!, #wemux?

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tmuxinator/project.rb', line 85

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.



46
47
48
# File 'lib/tmuxinator/project.rb', line 46

def custom_name
  @custom_name
end

#force_attachObject (readonly)

Returns the value of attribute force_attach.



44
45
46
# File 'lib/tmuxinator/project.rb', line 44

def force_attach
  @force_attach
end

#force_detachObject (readonly)

Returns the value of attribute force_detach.



45
46
47
# File 'lib/tmuxinator/project.rb', line 45

def force_detach
  @force_detach
end

#yamlObject (readonly)

Returns the value of attribute yaml.



43
44
45
# File 'lib/tmuxinator/project.rb', line 43

def yaml
  @yaml
end

Class Method Details

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tmuxinator/project.rb', line 48

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



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

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



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

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

Instance Method Details

#attach?Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
# File 'lib/tmuxinator/project.rb', line 138

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

#base_indexObject



216
217
218
# File 'lib/tmuxinator/project.rb', line 216

def base_index
  get_base_index.to_i
end

#cli_args?Boolean

Returns:

  • (Boolean)


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

def cli_args?
  yaml["cli_args"]
end

#deprecation_checksObject



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tmuxinator/project.rb', line 272

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

#deprecation_messagesObject



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/tmuxinator/project.rb', line 284

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

#deprecationsObject



264
265
266
267
268
269
270
# File 'lib/tmuxinator/project.rb', line 264

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

#get_base_indexObject



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

def get_base_index
  tmux_config["base-index"]
end

#get_pane_base_indexObject



328
329
330
# File 'lib/tmuxinator/project.rb', line 328

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

#killObject



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

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

#nameObject



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

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

#name?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/tmuxinator/project.rb', line 244

def name?
  !name.nil?
end

#pane_base_indexObject



220
221
222
# File 'lib/tmuxinator/project.rb', line 220

def pane_base_index
  get_pane_base_index.to_i
end

#postObject



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

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

#post?Boolean

Returns:

  • (Boolean)


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

def post?
  yaml["post"]
end

#preObject



133
134
135
136
# File 'lib/tmuxinator/project.rb', line 133

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

#pre?Boolean

Returns:

  • (Boolean)


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

def pre?
  yaml["pre"]
end

#pre_windowObject



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tmuxinator/project.rb', line 148

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)


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

def rbenv?
  yaml["rbenv"]
end

#renderObject



102
103
104
# File 'lib/tmuxinator/project.rb', line 102

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

#rootObject



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

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

#root?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/tmuxinator/project.rb', line 240

def root?
  !root.nil?
end

#rvm?Boolean

Returns:

  • (Boolean)


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

def rvm?
  yaml["rvm"]
end

#rvm_or_rbenv?Boolean

Returns:

  • (Boolean)


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

def rvm_or_rbenv?
  rvm? || rbenv?
end

#send_keys(cmd, window_index) ⇒ Object



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

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



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

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

#show_tmux_optionsObject



336
337
338
339
340
# File 'lib/tmuxinator/project.rb', line 336

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

#socketObject



190
191
192
193
194
195
196
# File 'lib/tmuxinator/project.rb', line 190

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

#socket_nameObject



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

def socket_name
  yaml["socket_name"]
end

#socket_pathObject



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

def socket_path
  yaml["socket_path"]
end

#startup_paneObject



228
229
230
# File 'lib/tmuxinator/project.rb', line 228

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

#startup_windowObject



224
225
226
# File 'lib/tmuxinator/project.rb', line 224

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

#tabs?Boolean

Returns:

  • (Boolean)


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

def tabs?
  yaml["tabs"]
end

#tmuxObject



166
167
168
# File 'lib/tmuxinator/project.rb', line 166

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

#tmux_commandObject



170
171
172
# File 'lib/tmuxinator/project.rb', line 170

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

#tmux_has_session?(name) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/tmuxinator/project.rb', line 174

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_command} 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



347
348
349
# File 'lib/tmuxinator/project.rb', line 347

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

#tmux_new_session_commandObject



342
343
344
345
# File 'lib/tmuxinator/project.rb', line 342

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

#tmux_optionsObject



206
207
208
209
210
211
212
213
214
# File 'lib/tmuxinator/project.rb', line 206

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)


232
233
234
# File 'lib/tmuxinator/project.rb', line 232

def tmux_options?
  yaml["tmux_options"]
end

#unsupported_version?Boolean

Returns:

  • (Boolean)


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

def unsupported_version?
  !Tmuxinator::SUPPORTED_TMUX_VERSIONS.include?(Tmuxinator::Config.version)
end

#validate!Object



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

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



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

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

#windowsObject



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

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)


236
237
238
# File 'lib/tmuxinator/project.rb', line 236

def windows?
  windows.any?
end