Class: Jdepp::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/jdepp/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(locations = {}, feedback = Feedback.new) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
# File 'lib/jdepp/parser.rb', line 9

def initialize(locations={}, feedback=Feedback.new)
   @locations = locations
   @feedback = feedback
end

Instance Method Details

#parse(file_list) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jdepp/parser.rb', line 14

def parse(file_list)

   def recurse(accum, path)
      re = /^\/\/\s*@requires\s*"(([^"\n:]+):)?([^"\n]+)"\s*$/
      lines = File.readlines(path.to_s)
      lines.reduce(accum) do
         |a, ln|
         matches = re.match(ln)
         if matches != nil then
            if matches[1].nil? then
               rel = Pathname.new(path.dirname + matches[3])
            else
               loc_alias = matches[2]
               if @locations.key?(loc_alias) then
                  rel = @locations[loc_alias].join(matches[3])
               else
                  # todo: convert path to relative path.
                  raise "i don't recognize the location alias \"#{loc_alias}\" encountered in <#{path}>."
               end
            end
            if not rel.exist? then
               raise "i can't find #{rel.to_s} (required by #{path})."
            end
            abs = rel.realdirpath
            if not a.key?(abs) then
               @feedback.puts_if(:verbose) {"i matched #{rel} (#{abs})"}
               recurse(a, rel)
               a[abs] = rel
            else
               @feedback.puts_if(:verbose) {"i am ignoring a duplicate directive for #{rel} (#{abs})"}
            end
         end
         a
      end
   end

   # todo: default to returning relative paths; absolute on request.
   (file_list.reduce({}) do
      |accum, s|
      recurse(accum, Pathname.new(s))
      accum
   end).values.map {|p| p.to_s}
end

#recurse(accum, path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jdepp/parser.rb', line 16

def recurse(accum, path)
   re = /^\/\/\s*@requires\s*"(([^"\n:]+):)?([^"\n]+)"\s*$/
   lines = File.readlines(path.to_s)
   lines.reduce(accum) do
      |a, ln|
      matches = re.match(ln)
      if matches != nil then
         if matches[1].nil? then
            rel = Pathname.new(path.dirname + matches[3])
         else
            loc_alias = matches[2]
            if @locations.key?(loc_alias) then
               rel = @locations[loc_alias].join(matches[3])
            else
               # todo: convert path to relative path.
               raise "i don't recognize the location alias \"#{loc_alias}\" encountered in <#{path}>."
            end
         end
         if not rel.exist? then
            raise "i can't find #{rel.to_s} (required by #{path})."
         end
         abs = rel.realdirpath
         if not a.key?(abs) then
            @feedback.puts_if(:verbose) {"i matched #{rel} (#{abs})"}
            recurse(a, rel)
            a[abs] = rel
         else
            @feedback.puts_if(:verbose) {"i am ignoring a duplicate directive for #{rel} (#{abs})"}
         end
      end
      a
   end
end