Class: RubyBindgen::Generators::CMake

Inherits:
Generator
  • Object
show all
Defined in:
lib/ruby-bindgen/generators/cmake/cmake.rb,
lib/ruby-bindgen/generators/cmake/guard.rb

Defined Under Namespace

Classes: Guard, GuardedEntry

Instance Attribute Summary

Attributes inherited from Generator

#config, #inputter, #outputter

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

#initialize, #project, #render_template, #translation_unit_file?

Constructor Details

This class inherits a constructor from RubyBindgen::Generators::Generator

Class Method Details

.template_dirObject



6
7
8
# File 'lib/ruby-bindgen/generators/cmake/cmake.rb', line 6

def self.template_dir
  __dir__
end

Instance Method Details

#generateObject



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby-bindgen/generators/cmake/cmake.rb', line 23

def generate
  base = Pathname.new(@inputter.base_path)

  # Collect files from inputter, grouped by relative directory
  files_by_dir = Hash.new do |hash, key|
    hash[key] = []
  end

  @inputter.each do |path, relative_path|
    dir = File.dirname(relative_path)
    files_by_dir[dir] << Pathname.new(path)
  end

  # Collect all directories that need CMakeLists.txt files
  # (directories containing files, plus all ancestor directories)
  all_dirs = Set.new
  files_by_dir.each_key do |dir|
    next if dir == "."
    parts = Pathname.new(dir).each_filename.to_a
    parts.length.times do |i|
      all_dirs << parts[0..i].join("/")
    end
  end

  # Build parent -> immediate child directories mapping
  child_dirs_of = Hash.new do |hash, key|
    hash[key] = []
  end
  all_dirs.each do |dir|
    parent = File.dirname(dir)
    parent = "." if parent == dir
    child_dirs_of[parent] << base.join(dir)
  end
  child_dirs_of.each_value(&:sort!)

  file_guards, directory_guards = build_guard_maps(files_by_dir, all_dirs.to_a)

  if @project
    # Root CMakeLists.txt
    content = render_template("project",
                              :project => self.project,
                              :directories => unguarded_paths(child_dirs_of["."], directory_guards, base),
                              :files => unguarded_paths(files_by_dir["."].sort, file_guards, base),
                              :guarded_entries => guarded_entries(child_dirs_of["."],
                                                                  directory_guards,
                                                                  files_by_dir["."].sort,
                                                                  file_guards,
                                                                  base),
                              :include_dirs => self.include_dirs)
    self.outputter.write("CMakeLists.txt", content)

    # Presets
    content = render_template("presets")
    self.outputter.write("CMakePresets.json", content)
  end

  # Subdirectory CMakeLists.txt files
  all_dirs.sort.each do |dir|
    content = render_template("directory",
                              :project => self.project,
                              :directories => unguarded_paths(child_dirs_of[dir], directory_guards, base),
                              :files => unguarded_paths((files_by_dir[dir] || []).sort, file_guards, base),
                              :guarded_entries => guarded_entries(child_dirs_of[dir],
                                                                  directory_guards,
                                                                  (files_by_dir[dir] || []).sort,
                                                                  file_guards,
                                                                  base))
    self.outputter.write(File.join(dir, "CMakeLists.txt"), content)
  end
end

#guardsObject



14
15
16
17
18
19
20
21
# File 'lib/ruby-bindgen/generators/cmake/cmake.rb', line 14

def guards
  @guards ||= begin
    config_guards = config[:guards] || {}
    config_guards.map do |condition, patterns|
      Guard.new(condition: condition, patterns: patterns, base_path: @inputter.base_path)
    end
  end
end

#include_dirsObject



10
11
12
# File 'lib/ruby-bindgen/generators/cmake/cmake.rb', line 10

def include_dirs
  config[:include_dirs] || []
end