Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specified config directory and XDG Base Dirs Spec (redux) #511

Merged
merged 6 commits into from May 19, 2017
Merged
Diff settings

Always

Just for now

salvage haletom commits from PR 479

  • Loading branch information...
HaleTom authored and ethagnawl committed Oct 13, 2016
commit 263692349f1142c0edcacfbefae541cbc0e7b44e
@@ -3,6 +3,7 @@
- Removed support for Ruby 1.9.3
- Move gem dependencies from Gemfile to tmuxinator.gemspec
- Add tmux 2.2 and 2.3 the TravisCI test matrix
- Support user-specified and XDG Base Dirs configuration directories

## 0.9.0
### Misc
@@ -1,8 +1,10 @@
require "yaml"
require "erubis"
require "fileutils"
require "shellwords"
require "thor"
require "thor/version"
require "xdg"
require "yaml"

require "tmuxinator/util"
require "tmuxinator/deprecations"
@@ -231,7 +231,9 @@ def delete(*projects)

def implode
if yes?("Are you sure you want to delete all tmuxinator configs?", :red)
FileUtils.remove_dir(Tmuxinator::Config.root)
Tmuxinator::Config.directories.each do |directory|
FileUtils.remove_dir(directory)
end
say "Deleted all tmuxinator projects."
end
end
@@ -4,18 +4,38 @@ class Config
NO_LOCAL_FILE_MSG = "Project file at ./.tmuxinator.yml doesn't exist."

class << self
def root
root_dir = File.expand_path("#{ENV['HOME']}/.tmuxinator")
Dir.mkdir(root_dir) unless File.directory?(root_dir)
root_dir
# The directory (created if needed) in which to store new projects
def directory
if !environment.nil? && !environment.empty?
FileUtils::mkpath(environment) unless File.directory?(environment)
return environment
end
return xdg if File.directory?(xdg)
return home if File.directory?(home)
# No project directory specified or existant, default to XDG:
FileUtils::mkpath(xdg)
xdg
end

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

# Is ~/.config/tmuxinator unless $XDG_CONFIG_DIR is set
def xdg
XDG["CONFIG"].to_s + "/tmuxinator"
end

def environment
ENV["TMUXINATOR_CONFIG"]
end

def sample
asset_path "sample.yml"
end

def default
"#{ENV['HOME']}/.tmuxinator/default.yml"
"#{directory}/default.yml"
end

def default?
@@ -46,25 +66,28 @@ def exists?(name)
File.exist?(project(name))
end

def project_in_root(name)
projects = Dir.glob("#{root}/**/*.yml")
projects.detect { |project| File.basename(project, ".yml") == name }
def local?
local_project
end

def local?
project_in_local
# 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 project_in_local
def local_project
[LOCAL_DEFAULT].detect { |f| File.exist?(f) }
end

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

# Pathname of the given project
def project(name)
project_in_root(name) || project_in_local || default_project(name)
global_project(name) || local_project || default_project(name)
end

def template
@@ -75,9 +98,23 @@ def wemux_template
asset_path "wemux_template.erb"
end

# Sorted list of all .yml files, including duplicates
def configs
Dir["#{Tmuxinator::Config.root}/**/*.yml"].sort.map do |path|
path.gsub("#{Tmuxinator::Config.root}/", "").gsub(".yml", "")
directories.map do |directory|
Dir["#{directory}/**/*.yml"].map do |path|
path.gsub("#{directory}/", "").gsub(".yml", "")
end
end.flatten.sort
end

# Existant directories which may contain project files
# Listed in search order
# Used by `implode` and `list` commands
def directories
if !environment.nil? && !environment.empty?
[environment]
else
[xdg, home].select { |d| File.directory? d }
end
end

@@ -89,7 +126,7 @@ def validate(options = {})
project_file = if name.nil?
raise NO_LOCAL_FILE_MSG \
unless Tmuxinator::Config.local?
project_in_local
local_project
else
raise "Project #{name} doesn't exist." \
unless Tmuxinator::Config.exists?(name)
@@ -98,11 +135,24 @@ def validate(options = {})
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").sort
projects.detect { |project| File.basename(project, ".yml") == name }
end
end
end
end
No changes.
No changes.
No changes.
No changes.
No changes.
No changes.
No changes.
@@ -1,4 +1,5 @@
require "spec_helper"

describe Tmuxinator::Cli do
let(:cli) { Tmuxinator::Cli }

@@ -231,16 +232,16 @@
end
end

context "files exists" do
let(:root_path) { "#{ENV['HOME']}\/\.tmuxinator\/#{name}\.yml" }
context "file exists" do
let(:project_path) { "#{Tmuxinator::Config.project(name)}" }

before do
allow(File).to receive(:exist?).with(anything).and_return(false)
expect(File).to receive(:exist?).with(root_path).and_return(true)
expect(File).to receive(:exist?).with(project_path).and_return(true)
end

it "just opens the file" do
expect(Kernel).to receive(:system).with(%r{#{root_path}})
expect(Kernel).to receive(:system).with(%r{#{project_path}})
capture_io { cli.start }
end
end
@@ -264,7 +265,7 @@
end
end

context "files exists" do
context "file exists" do
let(:path) { Tmuxinator::Config::LOCAL_DEFAULT }
before do
expect(File).to receive(:exist?).with(path) { true }
@@ -444,10 +445,27 @@
capture_io { cli.start }
end

it "deletes all projects" do
expect(FileUtils).to receive(:remove_dir)
it "deletes the configuration directory(s)" do
allow(Tmuxinator::Config).to receive(:directories) \
{ [Tmuxinator::Config.xdg, Tmuxinator::Config.home] }
expect(FileUtils).to receive(:remove_dir).once.
with(Tmuxinator::Config.xdg)
expect(FileUtils).to receive(:remove_dir).once.
with(Tmuxinator::Config.home)
expect(FileUtils).to receive(:remove_dir).never
capture_io { cli.start }
end

context "$TMUXINATOR_CONFIG specified" do
it "only deletes projects in that directory" do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("TMUXINATOR_CONFIG").and_return "dir"
allow(File).to receive(:directory?).with("dir").and_return true
expect(FileUtils).to receive(:remove_dir).once.with("dir")
expect(FileUtils).to receive(:remove_dir).never
capture_io { cli.start }
end
end
end

describe "#list" do
@@ -571,7 +589,7 @@
subject { described_class.new.create_project(params) }

before do
allow(Tmuxinator::Config).to receive_messages(root: path)
allow(Tmuxinator::Config).to receive_messages(directory: path)
end

it_should_behave_like :a_proper_project
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.