[FIX] Added a OHCOUNT_ROOT global so that included libs always work. We can now run...
[ohcount] / lib / ohcount / parser.rb
1 require 'yaml'
2
3 class Ohcount::Parser
4         # Parses a file, and store the results in files on disk.
5         #
6         # This method primarily exists for the benefit of the Ohloh analysis engine,
7         # which prefers to store interim results in files on disk. It is unlikely to be
8         # useful to the general public.
9         #
10         # A subdirectory is created for each language found in the target file.
11         # Within each language subdirectory, separate files are created for code, comments, and blanks.
12         #
13         # The 'code' file contains code lines, the 'comments' file contains comment lines, and the 'blanks'
14         # file contains a count of the number of blank lines in the file.
15         def self.parse_to_dir(args)
16                 required_arg_keys = [
17                         :dir,       # directory to parse to
18                         :buffer,    # buffer contents of the what we're parsing
19                         :polyglot   # the polyglot name of what we're parsing
20                 ]
21                 raise ArgumentError.new('Missing required args') unless (required_arg_keys - args.keys).empty?
22
23                 polyglot = args[:polyglot].to_s
24                 dir = args[:dir]
25                 buffer = args[:buffer].to_s
26                 find_licenses = args[:find_licenses].to_s
27
28     licenses = []
29                 Ohcount::parse(buffer, polyglot).each do |lb|
30                         lb_dest_dir = dir + "/" + lb.name
31                         Dir.mkdir(lb_dest_dir)
32                         if (lb.code)
33                                 File.open(lb_dest_dir + "/code", "w") do |io|
34                                         code = lb.code
35                                         io.write code
36                                         io.write "\n" unless (code.size == 0 || code[-1,1] == "\n")
37                                 end
38                         end
39                         if (lb.comment)
40                                 File.open(lb_dest_dir + "/comment", "w") do  |io|
41                                         comment = lb.comment
42                                         io.write comment
43                                         io.write "\n" unless (comment.size == 0 || comment[-1,1] == "\n")
44                                 end
45
46         # find licenses if required
47         licenses += LicenseSniffer.sniff(lb.comment) if find_licenses
48                         end
49                         File.open(lb_dest_dir + "/blanks", "w") do |io|
50                                 io.write lb.blanks.to_s
51                         end
52     end
53
54     if licenses.any?
55       licenses.uniq!
56       File.open(dir + "/licenses.yaml", "w") do |io|
57         io.write licenses.to_yaml
58       end
59     end
60
61   end
62 end