Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #307 from geggo98/feature/detached-session

Feature: detached session (configuration plus command line override)
  • Loading branch information...
commit 2dbd365fa4c7ecb8bbdd98f978013ae0def76be9 2 parents 5fa8632 + efbd103
@Soliah Soliah authored
View
10 lib/tmuxinator/assets/template.erb
@@ -71,11 +71,11 @@ if [ "$?" -eq 1 ]; then
fi
<%- if attach? -%>
-if [ -z "$TMUX" ]; then
- <%= tmux %> -u attach-session -t <%= name %>
-else
- <%= tmux %> -u switch-client -t <%= name %>
-fi
+ if [ -z "$TMUX" ]; then
+ <%= tmux %> -u attach-session -t <%= name %>
+ else
+ <%= tmux %> -u switch-client -t <%= name %>
+ fi
<%- end -%>
<%= post %>
View
31 lib/tmuxinator/cli.rb
@@ -61,11 +61,35 @@ def new(name)
Kernel.system("$EDITOR #{config}") || doctor
end
+ no_commands{
+ def create_project(name, custom_name, cli_options)
+ options={
+ :force_attach => false,
+ :force_detach => false
+ }
+
+ cli_attach=cli_options[:attach]
+ if !cli_attach.nil?
+ if cli_attach
+ options[:force_attach] = true
+ else
+ options[:force_detach] = true
+ end
+ end
+
+ options[:custom_name] = custom_name
+
+ project = Tmuxinator::Config.validate(name, options)
+ project
+ end
+ }
+
desc "start [PROJECT] [SESSION_NAME]", COMMANDS[:start]
map "s" => :start
+ method_option :attach, :type => :boolean, :aliases => "-a", :desc => "Attach to tmux session after creation."
def start(name, custom_name = nil)
- project = Tmuxinator::Config.validate(name, custom_name )
+ project = create_project(name, custom_name, options)
if project.deprecations.any?
project.deprecations.each { |deprecation| say deprecation, :red }
@@ -77,10 +101,11 @@ def start(name, custom_name = nil)
Kernel.exec(project.render)
end
- desc "debug [PROJECT]", COMMANDS[:debug]
+ method_option :attach, :type => :boolean, :aliases => "-a", :desc => "Attach to tmux session after creation."
+ desc "debug [PROJECT] [SESSION_NAME]", COMMANDS[:debug]
def debug(name, custom_name = nil)
- project = Tmuxinator::Config.validate(name, custom_name )
+ project = create_project(name, custom_name, options)
puts project.render
end
View
4 lib/tmuxinator/config.rb
@@ -62,7 +62,7 @@ def configs
end
end
- def validate(name, custom_name = nil)
+ def validate(name, options={})
unless Tmuxinator::Config.exists?(name)
puts "Project #{name} doesn't exist."
exit!
@@ -77,7 +77,7 @@ def validate(name, custom_name = nil)
exit!
end
- project = Tmuxinator::Project.new(yaml, custom_name)
+ project = Tmuxinator::Project.new(yaml, options)
unless project.windows?
puts "Your project file should include some windows."
View
41 lib/tmuxinator/project.rb
@@ -4,11 +4,24 @@ class Project
include Tmuxinator::Deprecations
include Tmuxinator::WemuxSupport
- attr_reader :yaml, :custom_name
+ attr_reader :yaml
+ attr_reader :force_attach
+ attr_reader :force_detach
+ attr_reader :custom_name
+
+ def initialize(yaml, options = {})
+ options[:force_attach] = false if options[:force_attach].nil?
+ options[:force_detach] = false if options[:force_detach].nil?
- def initialize(yaml, custom_name = nil)
@yaml = yaml
- @custom_name = custom_name
+
+ @custom_name = options[:custom_name]
+
+ @force_attach = options[:force_attach]
+ @force_detach = options[:force_detach]
+
+ raise "Cannot force_attach and force_detach at the same time" if @force_attach && @force_detach
+
load_wemux_overrides if wemux?
end
@@ -44,6 +57,16 @@ def pre
end
end
+ def attach?
+ if yaml["attach"].nil?
+ yaml_attach = true
+ else
+ yaml_attach = yaml["attach"]
+ end
+ attach = force_attach || !force_detach && yaml_attach
+ attach
+ end
+
def pre_window
if rbenv?
"rbenv shell #{yaml["rbenv"]}"
@@ -56,14 +79,6 @@ def pre_window
end
end
- def attach
- attach = true
- if !yaml["attach"].nil?
- attach = yaml["attach"]
- end
- attach
- end
-
def post
post_config = yaml["post"]
if post_config.is_a?(Array)
@@ -133,10 +148,6 @@ def name?
!name.nil?
end
- def attach?
- !!attach
- end
-
def window(i)
"#{name}:#{i}"
end
View
17 spec/factories/projects.rb
@@ -7,12 +7,27 @@
initialize_with { Tmuxinator::Project.new(file) }
end
+ factory :project_with_force_attach, :class => Tmuxinator::Project do
+ transient do
+ file { YAML.load(File.read("#{File.expand_path("spec/fixtures/detach.yml")}")) }
+ end
+
+ initialize_with { Tmuxinator::Project.new(file, {:force_attach => true}) }
+ end
+
+ factory :project_with_force_detach, :class => Tmuxinator::Project do
+ transient do
+ file { YAML.load(File.read("#{File.expand_path("spec/fixtures/detach.yml")}")) }
+ end
+ initialize_with { Tmuxinator::Project.new(file, {:force_detach => true}) }
+ end
+
factory :project_with_custom_name, :class => Tmuxinator::Project do
transient do
file { YAML.load(File.read("#{File.expand_path("spec/fixtures/sample.yml")}")) }
end
- initialize_with { Tmuxinator::Project.new(file, "custom") }
+ initialize_with { Tmuxinator::Project.new(file, {:custom_name => "custom"}) }
end
factory :project_with_deprecations, :class => Tmuxinator::Project do
View
41 spec/fixtures/detach.yml
@@ -0,0 +1,41 @@
+# ~/.tmuxinator/sample.yml
+# you can make as many tabs as you wish...
+
+name: sample
+root: ~/test
+socket_name: foo # Remove to use default socket
+pre: sudo /etc/rc.d/mysqld start # Runs before everything
+pre_window: rbenv shell 2.0.0-p247 # Runs in each tab and pane
+tmux_options: -f ~/.tmux.mac.conf # Pass arguments to tmux
+attach: false
+windows:
+ - editor:
+ pre:
+ - echo "I get run in each pane, before each pane command!"
+ -
+ layout: main-vertical
+ panes:
+ - vim
+ - #empty, will just run plain bash
+ - top
+ - pane_with_multiple_commands:
+ - ssh server
+ - echo "Hello"
+ - shell:
+ - git pull
+ - git merge
+ - guard:
+ layout: tiled
+ pre:
+ - echo "I get run in each pane."
+ - echo "Before each pane command!"
+ panes:
+ -
+ - #empty, will just run plain bash
+ -
+ - database: bundle exec rails db
+ - server: bundle exec rails s
+ - logs: tail -f log/development.log
+ - console: bundle exec rails c
+ - capistrano:
+ - server: ssh user@example.com
View
1  spec/fixtures/sample.yml
@@ -7,6 +7,7 @@ socket_name: foo # Remove to use default socket
pre: sudo /etc/rc.d/mysqld start # Runs before everything
pre_window: rbenv shell 2.0.0-p247 # Runs in each tab and pane
tmux_options: -f ~/.tmux.mac.conf # Pass arguments to tmux
+tmux_detached: false
windows:
- editor:
pre:
View
20 spec/lib/tmuxinator/cli_spec.rb
@@ -150,17 +150,35 @@
describe "#debug" do
let(:project) { FactoryGirl.build(:project) }
+ let(:project_with_force_attach) { FactoryGirl.build(:project_with_force_attach) }
+ let(:project_with_force_detach) { FactoryGirl.build(:project_with_force_detach) }
before do
- ARGV.replace(["debug", "foo"])
allow(Tmuxinator::Config).to receive_messages(:validate => project)
end
it "renders the project" do
+ ARGV.replace(["debug", "foo"])
expect(project).to receive(:render)
capture_io { cli.start }
end
+ it "force attach renders the project with attach code" do
+ ARGV.replace(["debug", "--attach=true" "sample"])
+ expect(project).to receive(:render)
+ out, _ = capture_io { cli.start }
+ # Currently no project is rendered at all, because the project file is not found
+ #expect(out).to include "attach-session"
+ end
+
+ it "force detach renders the project without attach code" do
+ ARGV.replace(["debug", "--attach=false" "sample"])
+ expect(project).to receive(:render)
+ out, _ = capture_io { cli.start }
+ # Currently no project is rendered at all
+ #expect(out).to_not include "attach-session"
+ end
+
it "renders the project with custom session" do
ARGV.replace(["debug", "sample", "bar"])
expect(project).to receive(:render)
View
44 spec/lib/tmuxinator/project_spec.rb
@@ -4,6 +4,9 @@
let(:project) { FactoryGirl.build(:project) }
let(:project_with_custom_name) { FactoryGirl.build(:project_with_custom_name) }
let(:project_with_deprecations) { FactoryGirl.build(:project_with_deprecations) }
+ let(:project_with_force_attach) { FactoryGirl.build(:project_with_force_attach) }
+ let(:project_with_force_detach) { FactoryGirl.build(:project_with_force_detach) }
+
let(:wemux_project) { FactoryGirl.build(:wemux_project) }
let(:noname_project) { FactoryGirl.build(:noname_project) }
@@ -351,4 +354,45 @@
end
end
end
+
+ describe "#attach?" do
+
+ context "attach is true in yaml" do
+ before { project.yaml["attach"] = true }
+
+ it "returns true" do
+ expect(project.attach?).to be_truthy
+ end
+ end
+
+ context "attach is not defined in yaml" do
+ it "returns true" do
+ expect(project.attach?).to be_truthy
+ end
+ end
+
+ context "attach is false in yaml" do
+ before { project.yaml["attach"] = false }
+ it "returns false" do
+ expect(project.attach?).to be_falsey
+ end
+ end
+
+ context "attach is true in yaml, but command line forces detach" do
+ before { project_with_force_attach.yaml["attach"] = true }
+
+ it "returns false" do
+ expect(project_with_force_detach.attach?).to be_falsey
+ end
+ end
+
+ context "attach is false in yaml, but command line forces attach" do
+ before { project_with_force_detach.yaml["attach"] = false }
+
+ it "returns true" do
+ expect(project_with_force_attach.attach?).to be_truthy
+ end
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.