class RBS::Template

Public Class Methods

render(out_file) click to toggle source
# File rbs-3.7.0/templates/template.rb, line 61
      def render(out_file)
        filepath = "templates/#{out_file}.erb"
        template = File.expand_path("../#{filepath}", __dir__)

        erb = read_template(template)
        extension = File.extname(filepath.gsub(".erb", ""))

        heading = <<~HEADING
          /*----------------------------------------------------------------------------*/
          /* This file is generated by the templates/template.rb script and should not  */
          /* be modified manually.                                                      */
          /* To change the template see                                                 */
          /* #{filepath + " " * (74 - filepath.size) } */
          /*----------------------------------------------------------------------------*/
        HEADING

        write_to = File.expand_path("../#{out_file}", __dir__)
        contents = heading + "\n" + erb.result_with_hash(locals)

        if (extension == ".c" || extension == ".h") && !contents.ascii_only?
          # Enforce that we only have ASCII characters here. This is necessary
          # for non-UTF-8 locales that only allow ASCII characters in C source
          # files.
          contents.each_line.with_index(1) do |line, line_number|
            raise "Non-ASCII character on line #{line_number} of #{write_to}" unless line.ascii_only?
          end
        end

        FileUtils.mkdir_p(File.dirname(write_to))
        File.write(write_to, contents)
      end

Private Class Methods

erb(template) click to toggle source
# File rbs-3.7.0/templates/template.rb, line 102
def erb(template)
  ERB.new(template, trim_mode: "-")
end
locals() click to toggle source
# File rbs-3.7.0/templates/template.rb, line 106
def locals
  config = YAML.load_file(File.expand_path("../config.yml", __dir__))
  {
    nodes: config.fetch("nodes").map { |node| Type.new(node) }.sort_by(&:ruby_full_name),
  }
end
read_template(filepath) click to toggle source
# File rbs-3.7.0/templates/template.rb, line 95
def read_template(filepath)
  template = File.read(filepath, encoding: Encoding::UTF_8)
  erb = erb(template)
  erb.filename = filepath
  erb
end