Class: TRuby::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/config.rb

Constant Summary collapse

VALID_STRICTNESS =

Valid strictness levels

%w[strict standard permissive].freeze
DEFAULT_CONFIG =

New schema structure (v0.0.12+)

{
  "source" => {
    "include" => ["src"],
    "exclude" => [],
    "extensions" => [".trb"],
  },
  "output" => {
    "ruby_dir" => "build",
    "rbs_dir" => nil,
    "clean_before_build" => false,
  },
  "compiler" => {
    "strictness" => "standard",
    "generate_rbs" => true,
    "type_check" => true,
    "target_ruby" => "3.0",
    "experimental" => [],
    "checks" => {
      "no_implicit_any" => false,
      "no_unused_vars" => false,
      "strict_nil" => false,
    },
  },
  "watch" => {
    "paths" => [],
    "debounce" => 100,
    "clear_screen" => false,
    "on_success" => nil,
  },
}.freeze
LEGACY_KEYS =

Legacy keys for migration detection

%w[emit paths strict include exclude].freeze
AUTO_EXCLUDE =

Always excluded (not configurable)

[".git"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = nil) ⇒ Config

Returns a new instance of Config.



52
53
54
55
56
57
58
59
60
61
# File 'lib/t_ruby/config.rb', line 52

def initialize(config_path = nil)
  raw_config = load_raw_config(config_path)
  config = process_config(raw_config)

  @source = config["source"]
  @output = config["output"]
  @compiler = config["compiler"]
  @watch = config["watch"]
  @version_requirement = raw_config["version"]
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler.



50
51
52
# File 'lib/t_ruby/config.rb', line 50

def compiler
  @compiler
end

#outputObject (readonly)

Returns the value of attribute output.



50
51
52
# File 'lib/t_ruby/config.rb', line 50

def output
  @output
end

#sourceObject (readonly)

Returns the value of attribute source.



50
51
52
# File 'lib/t_ruby/config.rb', line 50

def source
  @source
end

#version_requirementObject (readonly)

Returns the value of attribute version_requirement.



50
51
52
# File 'lib/t_ruby/config.rb', line 50

def version_requirement
  @version_requirement
end

#watchObject (readonly)

Returns the value of attribute watch.



50
51
52
# File 'lib/t_ruby/config.rb', line 50

def watch
  @watch
end

Instance Method Details

#check_no_implicit_any?Boolean

Check if no_implicit_any check is enabled

Returns:

  • (Boolean)

    true if check is enabled



120
121
122
# File 'lib/t_ruby/config.rb', line 120

def check_no_implicit_any?
  @compiler.dig("checks", "no_implicit_any") == true
end

#check_no_unused_vars?Boolean

Check if no_unused_vars check is enabled

Returns:

  • (Boolean)

    true if check is enabled



126
127
128
# File 'lib/t_ruby/config.rb', line 126

def check_no_unused_vars?
  @compiler.dig("checks", "no_unused_vars") == true
end

#check_strict_nil?Boolean

Check if strict_nil check is enabled

Returns:

  • (Boolean)

    true if check is enabled



132
133
134
# File 'lib/t_ruby/config.rb', line 132

def check_strict_nil?
  @compiler.dig("checks", "strict_nil") == true
end

#clean_before_build?Boolean

Check if output directory should be cleaned before build

Returns:

  • (Boolean)

    true if should clean before build



77
78
79
# File 'lib/t_ruby/config.rb', line 77

def clean_before_build?
  @output["clean_before_build"] == true
end

#exclude_patternsObject

Get exclude patterns



214
215
216
# File 'lib/t_ruby/config.rb', line 214

def exclude_patterns
  @source["exclude"] || []
end

#excluded?(file_path) ⇒ Boolean

Check if a file path should be excluded

Parameters:

  • file_path (String)

    absolute or relative file path

Returns:

  • (Boolean)

    true if file should be excluded



240
241
242
243
# File 'lib/t_ruby/config.rb', line 240

def excluded?(file_path)
  relative_path = relative_to_src(file_path)
  all_exclude_patterns.any? { |pattern| matches_pattern?(relative_path, pattern) }
end

#experimental_enabled?(feature) ⇒ Boolean

Check if a specific experimental feature is enabled

Parameters:

  • feature (String)

    feature name to check

Returns:

  • (Boolean)

    true if feature is enabled



114
115
116
# File 'lib/t_ruby/config.rb', line 114

def experimental_enabled?(feature)
  experimental_features.include?(feature)
end

#experimental_featuresArray<String>

Get list of enabled experimental features

Returns:

  • (Array<String>)

    list of experimental feature names



107
108
109
# File 'lib/t_ruby/config.rb', line 107

def experimental_features
  @compiler["experimental"] || []
end

#find_source_filesArray<String>

Find all source files matching include patterns, excluding exclude patterns

Returns:

  • (Array<String>)

    list of matching file paths



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/t_ruby/config.rb', line 220

def find_source_files
  files = []

  @source["include"].each do |include_dir|
    base_dir = File.expand_path(include_dir)
    next unless Dir.exist?(base_dir)

    include_patterns.each do |pattern|
      full_pattern = File.join(base_dir, pattern)
      files.concat(Dir.glob(full_pattern))
    end
  end

  # Filter out excluded files
  files.reject { |f| excluded?(f) }.uniq.sort
end

#generate_rbs?Boolean

Check if RBS files should be generated

Returns:

  • (Boolean)

    true if RBS files should be generated



89
90
91
# File 'lib/t_ruby/config.rb', line 89

def generate_rbs?
  @compiler["generate_rbs"] != false
end

#include_patternsObject

Get include patterns for file discovery



208
209
210
211
# File 'lib/t_ruby/config.rb', line 208

def include_patterns
  extensions = @source["extensions"] || [".trb"]
  extensions.map { |ext| "**/*#{ext}" }
end

#out_dirObject

Backwards compatible: alias for ruby_dir



180
181
182
# File 'lib/t_ruby/config.rb', line 180

def out_dir
  ruby_dir
end

#rbs_dirString

Get output directory for RBS files

Returns:

  • (String)

    RBS output directory (defaults to ruby_dir if not specified)



71
72
73
# File 'lib/t_ruby/config.rb', line 71

def rbs_dir
  @output["rbs_dir"] || ruby_dir
end

#ruby_dirString

Get output directory for compiled Ruby files

Returns:

  • (String)

    output directory path



65
66
67
# File 'lib/t_ruby/config.rb', line 65

def ruby_dir
  @output["ruby_dir"] || "build"
end

#source_excludeArray<String>

Get source exclude patterns

Returns:

  • (Array<String>)

    list of exclude patterns



197
198
199
# File 'lib/t_ruby/config.rb', line 197

def source_exclude
  @source["exclude"] || []
end

#source_extensionsArray<String>

Get source file extensions

Returns:

  • (Array<String>)

    list of file extensions (e.g., [“.trb”, “.truby”])



203
204
205
# File 'lib/t_ruby/config.rb', line 203

def source_extensions
  @source["extensions"] || [".trb"]
end

#source_includeArray<String>

Get source include directories

Returns:

  • (Array<String>)

    list of include directories



191
192
193
# File 'lib/t_ruby/config.rb', line 191

def source_include
  @source["include"] || ["src"]
end

#src_dirObject

Backwards compatible: first source.include directory



185
186
187
# File 'lib/t_ruby/config.rb', line 185

def src_dir
  @source["include"].first || "src"
end

#strictnessString

Get compiler strictness level

Returns:

  • (String)

    one of: strict, standard, permissive



83
84
85
# File 'lib/t_ruby/config.rb', line 83

def strictness
  @compiler["strictness"] || "standard"
end

#target_rubyString

Get target Ruby version

Returns:

  • (String)

    target Ruby version (e.g., “3.0”, “3.2”)



101
102
103
# File 'lib/t_ruby/config.rb', line 101

def target_ruby
  (@compiler["target_ruby"] || "3.0").to_s
end

#type_check?Boolean

Check if type checking is enabled

Returns:

  • (Boolean)

    true if type checking is enabled (default: true)



95
96
97
# File 'lib/t_ruby/config.rb', line 95

def type_check?
  @compiler["type_check"] != false
end

#validate!Object

Validate the configuration

Raises:



174
175
176
177
# File 'lib/t_ruby/config.rb', line 174

def validate!
  validate_strictness!
  true
end

#version_satisfied?Boolean

Check if current T-Ruby version satisfies the version requirement

Returns:

  • (Boolean)

    true if version is satisfied or no requirement specified



162
163
164
165
166
167
168
169
170
# File 'lib/t_ruby/config.rb', line 162

def version_satisfied?
  return true if @version_requirement.nil?

  requirement = Gem::Requirement.new(@version_requirement)
  current = Gem::Version.new(TRuby::VERSION)
  requirement.satisfied_by?(current)
rescue ArgumentError
  false
end

#watch_clear_screen?Boolean

Check if terminal should be cleared before rebuild

Returns:

  • (Boolean)

    true if terminal should be cleared



150
151
152
# File 'lib/t_ruby/config.rb', line 150

def watch_clear_screen?
  @watch["clear_screen"] == true
end

#watch_debounceInteger

Get watch debounce delay in milliseconds

Returns:

  • (Integer)

    debounce delay in milliseconds



144
145
146
# File 'lib/t_ruby/config.rb', line 144

def watch_debounce
  @watch["debounce"] || 100
end

#watch_on_successString?

Get command to run after successful compilation

Returns:

  • (String, nil)

    command to run on success



156
157
158
# File 'lib/t_ruby/config.rb', line 156

def watch_on_success
  @watch["on_success"]
end

#watch_pathsArray<String>

Get additional watch paths

Returns:

  • (Array<String>)

    list of additional paths to watch



138
139
140
# File 'lib/t_ruby/config.rb', line 138

def watch_paths
  @watch["paths"] || []
end