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

.configsObject

Sorted list of all .yml files, including duplicates



130
131
132
133
134
135
136
# File 'lib/tmuxinator/config.rb', line 130

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

.defaultObject



62
63
64
# File 'lib/tmuxinator/config.rb', line 62

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

.default?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/tmuxinator/config.rb', line 66

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

.default_or_sampleObject



54
55
56
# File 'lib/tmuxinator/config.rb', line 54

def default_or_sample
  default? ? default : sample
end

.default_path_optionObject



82
83
84
# File 'lib/tmuxinator/config.rb', line 82

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

.default_project(name) ⇒ Object



108
109
110
# File 'lib/tmuxinator/config.rb', line 108

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



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

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 “”.



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

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)


50
51
52
# File 'lib/tmuxinator/config.rb', line 50

def environment?
  File.directory?(environment)
end

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

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/tmuxinator/config.rb', line 86

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



98
99
100
101
102
# File 'lib/tmuxinator/config.rb', line 98

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)


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

def local?
  local_project
end

.local_projectObject Also known as: project_in_local



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

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

.project(name) ⇒ Object

Pathname of the given project



113
114
115
# File 'lib/tmuxinator/config.rb', line 113

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

.sampleObject



58
59
60
# File 'lib/tmuxinator/config.rb', line 58

def sample
  asset_path "sample.yml"
end

.stop_templateObject



121
122
123
# File 'lib/tmuxinator/config.rb', line 121

def stop_template
  asset_path "template-stop.erb"
end

.templateObject



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

def template
  asset_path "template.erb"
end

.valid_local_project?(name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



158
159
160
161
162
163
# File 'lib/tmuxinator/config.rb', line 158

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)


149
150
151
152
153
154
155
156
# File 'lib/tmuxinator/config.rb', line 149

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)


165
166
167
168
169
170
# File 'lib/tmuxinator/config.rb', line 165

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

  true
end

.validate(options = {}) ⇒ Object



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

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



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

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



125
126
127
# File 'lib/tmuxinator/config.rb', line 125

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
# File 'lib/tmuxinator/config.rb', line 33

def xdg
  XDG["CONFIG"].to_s + "/tmuxinator"
end

.xdg?Boolean

Returns:

  • (Boolean)


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

def xdg?
  File.directory?(xdg)
end