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" => nil, # Auto-detect from current Ruby version
    "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



135
136
137
# File 'lib/t_ruby/config.rb', line 135

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



141
142
143
# File 'lib/t_ruby/config.rb', line 141

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



147
148
149
# File 'lib/t_ruby/config.rb', line 147

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



229
230
231
# File 'lib/t_ruby/config.rb', line 229

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



255
256
257
258
# File 'lib/t_ruby/config.rb', line 255

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



129
130
131
# File 'lib/t_ruby/config.rb', line 129

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



122
123
124
# File 'lib/t_ruby/config.rb', line 122

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/t_ruby/config.rb', line 235

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



223
224
225
226
# File 'lib/t_ruby/config.rb', line 223

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

#out_dirObject

Backwards compatible: alias for ruby_dir



195
196
197
# File 'lib/t_ruby/config.rb', line 195

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



212
213
214
# File 'lib/t_ruby/config.rb', line 212

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

#source_extensionsArray<String>

Get source file extensions

Returns:

  • (Array<String>)

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



218
219
220
# File 'lib/t_ruby/config.rb', line 218

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

#source_includeArray<String>

Get source include directories

Returns:

  • (Array<String>)

    list of include directories



206
207
208
# File 'lib/t_ruby/config.rb', line 206

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

#src_dirObject

Backwards compatible: first source.include directory



200
201
202
# File 'lib/t_ruby/config.rb', line 200

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 If not specified in config, auto-detects from current Ruby environment

Returns:

  • (String)

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

Raises:



103
104
105
106
107
108
109
110
111
112
# File 'lib/t_ruby/config.rb', line 103

def target_ruby
  configured = @compiler["target_ruby"]
  if configured
    RubyVersion.parse(configured).validate!
    configured.to_s
  else
    version = RubyVersion.current.validate!
    "#{version.major}.#{version.minor}"
  end
end

#target_ruby_versionRubyVersion

Get target Ruby version as RubyVersion object

Returns:



116
117
118
# File 'lib/t_ruby/config.rb', line 116

def target_ruby_version
  RubyVersion.parse(target_ruby)
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:



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

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



177
178
179
180
181
182
183
184
185
# File 'lib/t_ruby/config.rb', line 177

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



165
166
167
# File 'lib/t_ruby/config.rb', line 165

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

#watch_debounceInteger

Get watch debounce delay in milliseconds

Returns:

  • (Integer)

    debounce delay in milliseconds



159
160
161
# File 'lib/t_ruby/config.rb', line 159

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



171
172
173
# File 'lib/t_ruby/config.rb', line 171

def watch_on_success
  @watch["on_success"]
end

#watch_pathsArray<String>

Get additional watch paths

Returns:

  • (Array<String>)

    list of additional paths to watch



153
154
155
# File 'lib/t_ruby/config.rb', line 153

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