Class: Tmuxinator::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/tmuxinator/config.rb

Constant Summary collapse

LOCAL_DEFAULTS =
["./.tmuxinator.yml", "./.tmuxinator.yaml"].freeze
NO_LOCAL_FILE_MSG =
"Project file at ./.tmuxinator.yml doesn't exist."
NO_PROJECT_FOUND_MSG =
"Project could not be found."
TMUX_MASTER_VERSION =
Float::INFINITY

Class Method Summary collapse

Class Method Details

.active_sessionsObject

List of all active tmux sessions



132
133
134
# File 'lib/tmuxinator/config.rb', line 132

def active_sessions
  `tmux list-sessions -F "#S"`.split("\n")
end

.config_file_basenamesArray<String] a list of config file names

List the names of all config files relative to the config directory.

If sub-folders are used, those are part of the name too.

Example:

$CONFIG_DIR/project.yml -> project
$CONFIG_DIR/sub/project.yml -> sub/project
$HOME_CONFIG_DIR/project.yml -> project

Returns:

  • (Array<String] a list of config file names)

    Array<String] a list of config file names



162
163
164
165
166
167
168
# File 'lib/tmuxinator/config.rb', line 162

def config_file_basenames
  directories.flat_map do |directory|
    Dir["#{directory}/**/*.yml"].map do |path|
      path.gsub("#{directory}/", "").gsub(".yml", "")
    end
  end.sort
end

.configs(active: nil) ⇒ Array<String>

Sorted list of all project file basenames, including duplicates.

Parameters:

  • active (defaults to: nil)

    filter configs by active project sessions

Returns:

  • (Array<String>)

    list of project names



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tmuxinator/config.rb', line 140

def configs(active: nil)
  configs = config_file_basenames

  if active == true
    configs &= active_sessions
  elsif active == false
    configs -= active_sessions
  end

  configs
end

.defaultObject



64
65
66
# File 'lib/tmuxinator/config.rb', line 64

def default
  "#{directory}/default.yml"
end

.default?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/tmuxinator/config.rb', line 68

def default?
  exist?(name: "default")
end

.default_or_sampleObject



56
57
58
# File 'lib/tmuxinator/config.rb', line 56

def default_or_sample
  default? ? default : sample
end

.default_path_optionObject



84
85
86
# File 'lib/tmuxinator/config.rb', line 84

def default_path_option
  version && version < 1.8 ? "default-path" : "-c"
end

.default_project(name) ⇒ Object



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

def default_project(name)
  "#{directory}/#{name}.yml"
end

.directoriesObject

Existent directories which may contain project files Listed in search order Used by ‘implode` and `list` commands



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

def directories
  if environment?
    [environment]
  else
    [xdg, home].select { |d| File.directory? d }
  end
end

.directoryObject Also known as: root

The directory (created if needed) in which to store new projects



12
13
14
15
16
17
18
19
20
# File 'lib/tmuxinator/config.rb', line 12

def directory
  return environment if environment?
  return xdg if xdg?
  return home if home?

  # No project directory specified or existent, default to XDG:
  FileUtils::mkdir_p(xdg)
  xdg
end

.environmentObject

$TMUXINATOR_CONFIG (and create directory) or “”.



44
45
46
47
48
49
50
# File 'lib/tmuxinator/config.rb', line 44

def environment
  environment = ENV["TMUXINATOR_CONFIG"]
  return "" if environment.to_s.empty? # variable is unset (nil) or blank

  FileUtils::mkdir_p(environment) unless File.directory?(environment)
  environment
end

.environment?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/tmuxinator/config.rb', line 52

def environment?
  File.directory?(environment)
end

.exist?(name: nil, path: nil) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/tmuxinator/config.rb', line 88

def exist?(name: nil, path: nil)
  return File.exist?(path) if path
  return File.exist?(project(name)) if name

  false
end

.global_project(name) ⇒ Object Also known as: project_in_root

Pathname of given project searching only global directories



100
101
102
103
104
# File 'lib/tmuxinator/config.rb', line 100

def global_project(name)
  project_in(environment, name) ||
    project_in(xdg, name) ||
    project_in(home, name)
end

.homeObject



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

def home
  "#{ENV['HOME']}/.tmuxinator"
end

.home?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tmuxinator/config.rb', line 26

def home?
  File.directory?(home)
end

.local?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/tmuxinator/config.rb', line 95

def local?
  local_project
end

.local_projectObject Also known as: project_in_local



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

def local_project
  LOCAL_DEFAULTS.detect { |f| File.exist?(f) }
end

.project(name) ⇒ Object

Pathname of the given project



115
116
117
# File 'lib/tmuxinator/config.rb', line 115

def project(name)
  global_project(name) || local_project || default_project(name)
end

.sampleObject



60
61
62
# File 'lib/tmuxinator/config.rb', line 60

def sample
  asset_path "sample.yml"
end

.stop_templateObject



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

def stop_template
  asset_path "template-stop.erb"
end

.templateObject



119
120
121
# File 'lib/tmuxinator/config.rb', line 119

def template
  asset_path "template.erb"
end

.valid_local_project?(name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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

def valid_local_project?(name)
  return false if name
  raise NO_LOCAL_FILE_MSG unless local?

  true
end

.valid_project_config?(project_config) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
# File 'lib/tmuxinator/config.rb', line 181

def valid_project_config?(project_config)
  return false unless project_config
  unless exist?(path: project_config)
    raise "Project config (#{project_config}) doesn't exist."
  end

  true
end

.valid_standard_project?(name) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
# File 'lib/tmuxinator/config.rb', line 197

def valid_standard_project?(name)
  return false unless name
  raise "Project #{name} doesn't exist." unless exist?(name: name)

  true
end

.validate(options = {}) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/tmuxinator/config.rb', line 204

def validate(options = {})
  name = options[:name]
  options[:force_attach] ||= false
  options[:force_detach] ||= false
  project_config = options.fetch(:project_config, false)
  project_file = if valid_project_config?(project_config)
                   project_config
                 elsif valid_local_project?(name)
                   local_project
                 elsif valid_standard_project?(name)
                   project(name)
                 else
                   # This branch should never be reached,
                   # but just in case ...
                   raise NO_PROJECT_FOUND_MSG
                 end

  Tmuxinator::Project.load(project_file, options).validate!
end

.versionObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tmuxinator/config.rb', line 72

def version
  if Tmuxinator::Doctor.installed?
    tmux_version = `tmux -V`.split(" ")[1]

    if tmux_version == "master"
      TMUX_MASTER_VERSION
    else
      tmux_version.to_s[/\d+(?:\.\d+)?/, 0].to_f
    end
  end
end

.wemux_templateObject



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

def wemux_template
  asset_path "wemux_template.erb"
end

.xdgObject

~/.config/tmuxinator unless $XDG_CONFIG_HOME has been configured to use a custom value. (e.g. if $XDG_CONFIG_HOME is set to ~/my-config, the return value will be ~/my-config/tmuxinator)



33
34
35
36
37
# File 'lib/tmuxinator/config.rb', line 33

def xdg
  xdg_config_directory = ENV.fetch("XDG_CONFIG_HOME", "~/.config")
  config_home = File.expand_path(xdg_config_directory)
  File.join(config_home, "tmuxinator")
end

.xdg?Boolean

Returns:

  • (Boolean)


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

def xdg?
  File.directory?(xdg)
end