-
Notifications
You must be signed in to change notification settings - Fork 626
/
config.rb
213 lines (171 loc) · 5.54 KB
/
config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# frozen_string_literal: true
module Tmuxinator
class Config
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 << self
# The directory (created if needed) in which to store new projects
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
def home
ENV["HOME"] + "/.tmuxinator"
end
def home?
File.directory?(home)
end
# ~/.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)
def xdg
XDG["CONFIG"].to_s + "/tmuxinator"
end
def xdg?
File.directory?(xdg)
end
# $TMUXINATOR_CONFIG (and create directory) or "".
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
def environment?
File.directory?(environment)
end
def default_or_sample
default? ? default : sample
end
def sample
asset_path "sample.yml"
end
def default
"#{directory}/default.yml"
end
def default?
exist?(name: "default")
end
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
def default_path_option
version && version < 1.8 ? "default-path" : "-c"
end
def exist?(name: nil, path: nil)
return File.exist?(path) if path
return File.exist?(project(name)) if name
false
end
def local?
local_project
end
# Pathname of given project searching only global directories
def global_project(name)
project_in(environment, name) ||
project_in(xdg, name) ||
project_in(home, name)
end
def local_project
LOCAL_DEFAULTS.detect { |f| File.exist?(f) }
end
def default_project(name)
"#{directory}/#{name}.yml"
end
# Pathname of the given project
def project(name)
global_project(name) || local_project || default_project(name)
end
def template
asset_path "template.erb"
end
def stop_template
asset_path "template-stop.erb"
end
def wemux_template
asset_path "wemux_template.erb"
end
# Sorted list of all .yml files, including duplicates
def configs
directories.map do |directory|
Dir["#{directory}/**/*.yml"].map do |path|
path.gsub("#{directory}/", "").gsub(".yml", "")
end
end.flatten.sort
end
# Existent directories which may contain project files
# Listed in search order
# Used by `implode` and `list` commands
def directories
if environment?
[environment]
else
[xdg, home].select { |d| File.directory? d }
end
end
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
def valid_local_project?(name)
return false if name
raise NO_LOCAL_FILE_MSG unless local?
true
end
def valid_standard_project?(name)
return false unless name
raise "Project #{name} doesn't exist." unless exist?(name: name)
true
end
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
# Deprecated methods: ignore the 1st, use the 2nd
alias :root :directory
alias :project_in_root :global_project
alias :project_in_local :local_project
private
def asset_path(asset)
"#{File.dirname(__FILE__)}/assets/#{asset}"
end
# The first pathname of the project named 'name' found while
# recursively searching 'directory'
def project_in(directory, name)
return nil if String(directory).empty?
projects = Dir.glob("#{directory}/**/*.{yml,yaml}").sort
projects.detect { |project| File.basename(project, ".*") == name }
end
end
end
end