Skip to content

Commit

Permalink
ShellVariables module for parser
Browse files Browse the repository at this point in the history
Find all shell variables in script into array and accessor `variables`
  • Loading branch information
dmytro committed Mar 28, 2013
1 parent 5a40f24 commit 7e517c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/banalize/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Banalize

require_relative 'parser/pod_comments'
require_relative 'parser/variables'

# Instance attributes
# -----------
Expand All @@ -14,14 +15,16 @@ module Banalize

class Parser

include Banalize::Parser::PodStyleComments
include PodStyleComments
include ShellVariables

def initialize path
@lines = IO.read(path).force_encoding("utf-8").split($/)
@shebang = Numbered.new
@comments = Numbered.new
@code = Numbered.new

@lines = IO.read(path).force_encoding("utf-8").split($/)
@shebang = Numbered.new
@comments = Numbered.new
@code = Numbered.new
@variables = []

@shebang.add @lines.shift if @lines.first =~ /^#!/

@lines.each_index do |idx|
Expand All @@ -37,9 +40,13 @@ def initialize path
end
end
pod_comments
shell_variables

p variables
end

# attr_accessor :variables

# Lines of the tested bash file, split by \n's
attr_accessor :lines

Expand Down
29 changes: 29 additions & 0 deletions lib/banalize/parser/variables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Banalize
class Parser

##
# Parse and detect all shell variables used in script. Set
# instance level accessor `variables`.
#
module ShellVariables

##
# Parse and detect all shell variables used in script.
#
# @return [Array]
def shell_variables
ln = code.grep(/\$\{?\w+\}?/).map(&:last).join " "

vars = ln.scan(/\$\{?\w+\}?/)
vars.map! { |x| x.gsub(/[${}]/,'') }
vars.reject! { |x| x =~ /^\d$/}

@variables = vars.uniq!
end

# All variables used in shell script
attr_accessor :variables

end # ShellVariables
end # Parser
end # Banalize

0 comments on commit 7e517c2

Please sign in to comment.