mkvtoolnix/rake.d/compilation_database.rb
Moritz Bunkus ab6455f68c
build system: fix compatibility with Ruby 3.2.0
Several deprecated functions were removed: `Dir.exists?`,
`File.exists?` & `FileTest.exists?`.  Instead of those
`FileTest.exist?` must be used (without the `s`). See the release
notes for Ruby 3.2.0 as well as the issue for removing the functions:

https://www.ruby-lang.org/en/news/2022/12/25/ruby-3-2-0-released/
https://bugs.ruby-lang.org/issues/17391
2023-01-02 22:57:21 +01:00

37 lines
946 B
Ruby

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?
return if !FileTest.exist?(@file_name) && !c?(:BUILD_COMPILATION_DATABASE)
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 }