tmux-erb-parser 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1609219d3c43082d5419e05ce2b64430b0524f0b6b09acb1e447e1efd4a5f243
4
+ data.tar.gz: 841d87ef8ccf1c9d618a301df1de3248cff2bac12d790f4ce8025c679fbcc626
5
+ SHA512:
6
+ metadata.gz: ab1579f7d6bd609b12cfc58423e612595dbd534af28335ac7574766234fd6faa40dcf1f00695fb5afcfd8ae00bdc6e7f28077d960f87e7f91172cf527be7632f
7
+ data.tar.gz: e52c39ca365fc048fbdf0b4b2547ab76196b9c634c6de3b3b6f473512c11519fb9549ef4d52f3aee646f3f710a1f6e91ccdf13bfbedc66d070a1af4fbbab3d32
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Tmux ERB Parser
2
+ [![Build Status](https://travis-ci.org/epaew/tmux-erb-parser.svg?branch=master)](https://travis-ci.org/epaew/tmux-erb-parser)
3
+
4
+ A Tmux plugin to load tmux.conf.erb
5
+ [What's ERB?](https://ruby-doc.org/stdlib-2.5.1/libdoc/erb/rdoc/ERB.html)
6
+
7
+ ## Requirements
8
+ * bash
9
+ * For script execution.
10
+ * You can use whatever you like as the default shell.
11
+ * git
12
+ * ruby:
13
+ * 2.3 or higher is required.
14
+ * tmux
15
+
16
+ ## How to use
17
+ 1. Create your `tmux.conf.erb` and place it.
18
+ * By default, tmux-erb-parser loads all `*.erb` files in `~/.config/tmux/`
19
+ * Or you can change the load path. (Please see below.)
20
+ 2. Install tmux-erb-parser and run tmux!
21
+
22
+ ### Install with tpm (Tmux Plugin Manager)
23
+ * Put this at the bottom of `~/.tmux.conf` (**Not your tmux.conf.erb !**):
24
+ ```tmux
25
+ setenv -g TMUX_CONF_EXT_PATH "path/to/tmux.conf.erb" # set your tmux.conf.erb's path
26
+ # Note: You can specify multiple files using glob expressions. This is parsed by bash.
27
+
28
+ set -g @plugin 'epaew/tmux-erb-parser'
29
+ set -g @plugin 'tmux-plugins/tpm'
30
+ # and list other plugins you want to install
31
+
32
+ # Initialize Tmux plugin manager (keep this line at the very bottom of tmux.conf)
33
+ run '~/.tmux/plugins/tpm/tpm'
34
+ ```
35
+
36
+ * Run tmux and press `Prefix + I` to install plugins!
37
+
38
+ ### Install and configure with git
39
+ * Install:
40
+ ```bash
41
+ git clone https://github.com/epaew/tmux-erb-parser ~/.tmux/plugins/tmux-erb-parser
42
+ ```
43
+
44
+ * Configure:
45
+ * Put this in `~/.tmux.conf`:
46
+ ```tmux
47
+ run '~/.tmux/plugins/tmux-erb-parser/bin/tmux-erb-parser --inline path/to/tmux.conf.erb'
48
+ ```
49
+
50
+ ## License
51
+ [MIT](LICENSE)
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # vim:set ft=ruby:
5
+
6
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
7
+ require 'tmux-erb-parser'
8
+
9
+ cmd = TmuxERBParser::Command.new(ARGV)
10
+ cmd.run
data/erb-parser.tmux ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ # vim:set ft=sh:
3
+
4
+ CURRENT_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}) && pwd)
5
+ cmd="${CURRENT_DIR}/bin/tmux-erb-parser"
6
+ opts="--inline"
7
+
8
+ TMUX_CONF_EXT_DEFAULT="${HOME}/.config/tmux/*.erb"
9
+ tmux run-shell "${cmd} ${opts} ${TMUX_CONF_EXT_PATH:-$TMUX_CONF_EXT_DEFAULT}"
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ module TmuxERBParser
6
+ class Command
7
+ def initialize(args = [])
8
+ @command_name = File.basename($PROGRAM_NAME)
9
+ @args = args.empty? ? ['--help'] : args
10
+ @options = {}
11
+ @logger = Logger.instance
12
+ end
13
+
14
+ def run
15
+ @option_parser = OptionParser.new(&method(:command_opts))
16
+ @option_parser.parse!(@args)
17
+ process
18
+ exit 0
19
+ rescue StandardError => e
20
+ err_msg = "#{e.message}\n#{e.backtrace.join("\n\t")}"
21
+ warn err_msg unless @options[:quiet]
22
+ @logger.error err_msg
23
+ exit 1
24
+ end
25
+
26
+ private
27
+
28
+ def check_args
29
+ msg = 'INPUT_ERB_FILES are required.' if @args.empty?
30
+ unless @options[:inline] ^ @options[:output]
31
+ msg = 'Please specify either --inline or --output option.'
32
+ end
33
+
34
+ raise ArgumentError, msg if msg
35
+ end
36
+
37
+ def process
38
+ check_args
39
+
40
+ args = @args.dup
41
+ args.each do |arg|
42
+ @logger.info "open #{arg}."
43
+ File.open(arg, 'r') do |input|
44
+ p = Parser.new(input,
45
+ @options[:output],
46
+ File.extname(arg.downcase)[1..-1].to_sym,
47
+ @options)
48
+ p.parse
49
+ end
50
+ end
51
+ end
52
+
53
+ def command_opts(opts) # rubocop:disable Metrics/MethodLength
54
+ opts.banner = "Usage: #{@command_name} INPUT_FILES [options]"
55
+
56
+ opts.on('-i',
57
+ '--inline',
58
+ 'Exec tmux subcommands to the current tmux-server.') do
59
+ @options[:inline] = true
60
+ end
61
+
62
+ opts.on('-o',
63
+ '--output [OUTPUT_FILE_NAME]',
64
+ String,
65
+ 'Output the configuration to a file with the specified name.'\
66
+ ' If the file name is omitted, output to stdout') do |fname|
67
+ @options[:output] = File.open(fname, 'w') if fname
68
+ @options[:output] ||= $stdout
69
+ end
70
+
71
+ opts.on('-h', '--help', 'Print this help message') do
72
+ puts opts
73
+ exit
74
+ end
75
+
76
+ opts.on('-v', '--version', 'Print version information') do
77
+ puts "#{@command_name} #{TmuxERBParser::VERSION}"
78
+ exit
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TmuxERBParser
4
+ module Helpers
5
+ module_function
6
+
7
+ def prefix_key
8
+ `tmux show-option -g prefix`.split[1]
9
+ end
10
+
11
+ def server_started?
12
+ !ENV['TMUX'].nil?
13
+ end
14
+
15
+ def tmux_version
16
+ (ENV['TMUX_VERSION'] || `tmux -V`.split[1]).to_f
17
+ end
18
+
19
+ def uname
20
+ ENV['UNAME'] || `uname`.chomp
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'singleton'
5
+
6
+ module TmuxERBParser
7
+ class Logger < ::Logger
8
+ include Singleton
9
+
10
+ def initialize
11
+ super(File.join(File.dirname(__dir__),
12
+ "../log/#{File.basename($PROGRAM_NAME)}.log"))
13
+ self.level = ENV['DEBUG'] ? DEBUG : WARN
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+ require 'json'
5
+ require 'yaml'
6
+
7
+ module TmuxERBParser
8
+ PARSER_CMD = File.expand_path('../../bin/tmux-erb-parser', __dir__)
9
+
10
+ class ParseError < StandardError; end
11
+
12
+ class Parser
13
+ def initialize(input, output, type = :erb, options = {})
14
+ @input = input
15
+ @output = output
16
+ @type = type
17
+ @options = options
18
+ @logger = Logger.instance
19
+ end
20
+
21
+ def parse
22
+ conf_lines = parse_file(@input, @type)
23
+ converted_lines = conf_lines.map(&method(:replace_source_file))
24
+ exec(converted_lines, @output)
25
+ end
26
+
27
+ private
28
+
29
+ def exec(commands, output = nil)
30
+ if output
31
+ commands.each(&output.method(:puts))
32
+ else
33
+ commands.inject(+'', &method(:exec_tmux))
34
+ end
35
+ end
36
+
37
+ def exec_tmux(buf, line)
38
+ buf = +buf if buf.frozen?
39
+ buf << line
40
+ buf = strip_comments(buf)
41
+ return buf if buf.empty?
42
+
43
+ if buf.end_with?('\\')
44
+ buf.chop!
45
+ else
46
+ command = "tmux #{buf.gsub(%(\\;), %( '\\;'))}"
47
+ @logger.debug "exec: #{command}"
48
+
49
+ `#{command}` unless ENV['DEBUG']
50
+ buf.clear
51
+ end
52
+ end
53
+
54
+ def generate_conf(_structured)
55
+ # TODO
56
+ parse_result = []
57
+ end
58
+
59
+ def parse_file(input, type)
60
+ erb_result = ERB.new(input.read).result
61
+
62
+ case type
63
+ when :json
64
+ generate_conf(JSON.parse(erb_result))
65
+ when :yml, :yaml
66
+ generate_conf(YAML.load_stream(erb_result))
67
+ else
68
+ # rubocop:disable Metrics/LineLength
69
+ erb_result
70
+ .gsub(/(\R){3,}/) { Regexp.last_match(1) * 2 } # reduce continuity blanklines
71
+ .gsub(/(\R)+\z/) { Regexp.last_match(1) } # remove blankline at EOF
72
+ .each_line
73
+ .map(&:chomp)
74
+ # rubocop:enable Metrics/LineLength
75
+ end
76
+ end
77
+
78
+ def replace_source_file(line)
79
+ # source file -> run-shell "parser --inline file"
80
+ if line =~ /source/ && line !~ /run(-shell)?/
81
+ line = line.gsub(/"source(-file)?( -q)?\s([^\s\\;]+)"/) do
82
+ %("run-shell \\"#{PARSER_CMD} --inline #{Regexp.last_match(3)}\\"")
83
+ end
84
+ line = line.gsub(/source(-file)?( -q)?\s([^\s\\;]+)/) do
85
+ %(run-shell "#{PARSER_CMD} --inline #{Regexp.last_match(3)}")
86
+ end
87
+ end
88
+ line
89
+ end
90
+
91
+ def strip_comments(str)
92
+ return '' if str.empty? || str.lstrip.start_with?('#')
93
+
94
+ strip_eol_comments(str)
95
+ end
96
+
97
+ # TODO: refactoring
98
+ def strip_eol_comments(str)
99
+ flags = {}
100
+ str = str.each_char.inject(+'') do |result, char|
101
+ case char
102
+ when '\''
103
+ if !flags[:double] || (flags[:single] && result[-1] != '\\')
104
+ flags[:single] = !flags[:single]
105
+ end
106
+ when '"'
107
+ if !flags[:single] || (flags[:double] && result[-1] != '\\')
108
+ flags[:double] = !flags[:double]
109
+ end
110
+ when '#'
111
+ break result if flags.values.none?
112
+ end
113
+
114
+ result << char
115
+ end
116
+ str.rstrip
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TmuxERBParser
4
+ VERSION = '0.1.1'
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'tmux-erb-parser/command'
4
+ require_relative 'tmux-erb-parser/helpers'
5
+ require_relative 'tmux-erb-parser/logger'
6
+ require_relative 'tmux-erb-parser/parser'
7
+ require_relative 'tmux-erb-parser/version'
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmux-erb-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - epaew
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov-console
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: test-unit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.2.8
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.2.8
111
+ - !ruby/object:Gem::Dependency
112
+ name: test-unit-rr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.0.5
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.0.5
125
+ description: A Tmux plugin to load tmux.conf.erb
126
+ email:
127
+ - epaew.333@gmail.com
128
+ executables:
129
+ - tmux-erb-parser
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - LICENSE
134
+ - README.md
135
+ - bin/tmux-erb-parser
136
+ - erb-parser.tmux
137
+ - lib/tmux-erb-parser.rb
138
+ - lib/tmux-erb-parser/command.rb
139
+ - lib/tmux-erb-parser/helpers.rb
140
+ - lib/tmux-erb-parser/logger.rb
141
+ - lib/tmux-erb-parser/parser.rb
142
+ - lib/tmux-erb-parser/version.rb
143
+ homepage: https://github.com/epaew/tmux-erb-parser
144
+ licenses:
145
+ - MIT
146
+ metadata:
147
+ allowed_push_host: https://rubygems.org
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: 2.3.0
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements:
163
+ - bash
164
+ - tmux
165
+ rubyforge_project:
166
+ rubygems_version: 2.7.6
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: A Tmux plugin to load tmux.conf.erb
170
+ test_files: []