FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Introduce "partials" to support config inheritance by ethagnawl · Pull Request #987 · tmuxinator/tmuxinator · GitHub

Closed
50 changes: 48 additions & 2 deletions lib/tmuxinator/project.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,61 @@ def load(path, options = {})
@settings = parse_settings(args)
@args = args

content = render_template(path, binding)
YAML.safe_load(content, aliases: true)
project_content = render_template(path, binding)
project_yaml = YAML.safe_load(project_content, aliases: true)

load_and_merge_partials(project_yaml)
rescue SyntaxError, StandardError => e
raise "Failed to parse config file: #{e.message}"
end

new(yaml, options)
end

def load_partials(project_yaml)
partials = project_yaml.fetch("partials", []) || []
windows_ = []
partial_config_paths = partials.map do |p|
if FileTest.exist?(p)
p
else
Tmuxinator::Config.valid_project_config?(p) && Tmuxinator::Config.global_project(p)
end
end
partial_yamls = partial_config_paths.inject({}) do |memo, partial|
partial_content = render_template(partial, binding)
partial_yaml_ = YAML.safe_load(partial_content, aliases: true)
if partial_yaml_.key?("windows")
windows__ = partial_yaml_.delete("windows")
windows_ += windows__
end
memo.merge(partial_yaml_)
end
partial_yamls["windows"] = windows_
partial_yamls
end

def load_and_merge_partials(project_yaml)
partial_yamls = load_partials(project_yaml)
# partial_yamls should always be safe to merge against
# The merge block currently exists to ensure that windows arrays are
# composed instead of clobbered. This first version is naive and
# doesn't attempt to merge windows contents and accumulates them. The
# result is that you may wind up with multiple windows with the same
# name and, perhaps, the same content. This is better than clobbering
# windows as partials are processed but the ideal is probably to merge
# and dedupe like-windows. We'll see ...
partial_yamls.merge(project_yaml) do |key, partial_val, project_val|
if key == "windows" &&
partial_val.is_a?(Array) &&
project_val.is_a?(Array)
partial_val + project_val
else
project_val
end
end
end

def parse_settings(args)
settings = args.select { |x| x.match(/.*=.*/) }
args.reject! { |x| x.match(/.*=.*/) }
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/projects.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def yaml_load(file)
initialize_with { Tmuxinator::Project.new(file) }
end

factory :project_with_partials, class: Tmuxinator::Project do
transient do
file { "spec/fixtures/sample_partials.yml" }
end

initialize_with { Tmuxinator::Project.load(file) }
end

factory :project_with_alias, class: Tmuxinator::Project do
transient do
file { "spec/fixtures/sample_alias.yml" }
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/sample_partial.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pre:
- echo "partial_is_working"
18 changes: 18 additions & 0 deletions spec/fixtures/sample_partials.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ~/.tmuxinator/sample.yml
# you can make as many tabs as you wish...

partials:
- spec/fixtures/sample_partial.yml
name: sample_partials
root: ~/test
windows:
- editor:
layout: main-vertical
panes:
- vim
- #empty, will just run plain bash
- top
- pane_with_multiple_commands:
- ssh server
- echo "Hello"
- guard:
12 changes: 12 additions & 0 deletions spec/lib/tmuxinator/project_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
FactoryBot.build(:nameless_window_project)
end

let(:project_with_partials) do
FactoryBot.build(:project_with_partials)
end

let(:project_with_alias) do
FactoryBot.build(:project_with_alias)
end
Expand Down Expand Up @@ -118,6 +122,14 @@
end
end

context "with partials" do
it "renders the tmux config" do
rendered = project_with_partials.render
expect(rendered).to_not be_empty
expect(rendered).to include("partial_is_working")
end
end

context "with alias" do
it "renders the tmux config" do
rendered = project_with_alias.render
Expand Down

Back | FazBrowse Home | New Git URL