Class: Tmuxinator::Config

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.configsObject

Sorted list of all .yml files, including duplicates



122
123
124
125
126
127
128
# File 'lib/tmuxinator/config.rb', line 122

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

.defaultObject



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

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

.default?Boolean

Returns:

  • (Boolean)


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

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

.default_path_optionObject



75
76
77
# File 'lib/tmuxinator/config.rb', line 75

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

.default_project(name) ⇒ Object



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

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

.directoriesObject

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



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

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



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

def directory
  return environment if environment?
  return xdg if xdg?
  return home if home?
  # No project directory specified or existant, default to XDG:
  FileUtils::mkdir_p(xdg)
  xdg
end

.environmentObject

$TMUXINATOR_CONFIG (and create directory) or “”.



40
41
42
43
44
45
# File 'lib/tmuxinator/config.rb', line 40

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)


47
48
49
# File 'lib/tmuxinator/config.rb', line 47

def environment?
  File.directory?(environment)
end

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

Returns:

  • (Boolean)


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

def exists?(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



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

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

.homeObject



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

def home
  ENV["HOME"] + "/.tmuxinator"
end

.home?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/tmuxinator/config.rb', line 24

def home?
  File.directory?(home)
end

.local?Boolean

Returns:

  • (Boolean)


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

def local?
  local_project
end

.local_projectObject Also known as: project_in_local



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

def local_project
  [LOCAL_DEFAULT].detect { |f| File.exist?(f) }
end

.project(name) ⇒ Object

Pathname of the given project



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

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

.sampleObject



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

def sample
  asset_path "sample.yml"
end

.stop_templateObject



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

def stop_template
  asset_path "template-stop.erb"
end

.templateObject



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

def template
  asset_path "template.erb"
end

.valid_local_project?(name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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

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)


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

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

.valid_standard_project?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.validate(options = {}) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tmuxinator/config.rb', line 161

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



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tmuxinator/config.rb', line 63

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

    if tmux_version == "master"
      TMUX_MASTER_VERSION
    else
      tmux_version.to_f
    end
  end
end

.wemux_templateObject



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

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)



31
32
33
# File 'lib/tmuxinator/config.rb', line 31

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

.xdg?Boolean

Returns:

  • (Boolean)


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

def xdg?
  File.directory?(xdg)
end