File: compilation_database.rb

package info (click to toggle)
mkvtoolnix 92.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 58,620 kB
  • sloc: cpp: 216,810; ruby: 11,403; xml: 8,058; ansic: 6,885; sh: 4,884; python: 1,041; perl: 191; makefile: 113; awk: 16; javascript: 4
file content (35 lines) | stat: -rw-r--r-- 865 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Mtx
  module CompilationDatabase
    @file_name            = "#{$build_dir}/compile_commands.json"
    @compilation_commands = {}

    def self.database_file_name
      @file_name
    end

    def self.read
      return {} unless FileTest.exist?(@file_name)

      Hash[
        *JSON.parse(IO.readlines(@file_name).join("")).
          map { |entry| [ entry["file"], entry ] }.
          flatten
      ]
    end

    def self.write
      return if @compilation_commands.empty?

      entries = self.read.merge(@compilation_commands).values.sort_by { |e| e["file"] }
      File.open(@file_name, "w") do |f|
        f.write(JSON.generate(entries, :indent => "  ", :object_nl => "\n", :array_nl => "\n"))
      end
    end

    def self.add entry
      @compilation_commands[entry["file"]] = entry
    end
  end
end

at_exit { Mtx::CompilationDatabase.write }