From 2e429a034d8a96748987559561240de96230623a Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 9 May 2026 15:10:58 +0900 Subject: [PATCH] gem.rb: add post_user_config hook for gem-author finalization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lets a gem author register a block that runs after the user's `build.gem` block has been processed. Intended for filling in defaults that depend on user-supplied configuration — for example, library auto-detection that should only kick in when the user hasn't explicitly chosen which library to use. Initialization order is now: 1. block in MRuby::Gem::Specification.new (gem author defaults) 2. block in build.gem (user override) 3. block in post_user_config (gem author finalize, new) Closes #6212, picked from PR by dearblue with the method renamed from last_initializer to post_user_config (per matz's review). Co-authored-by: Claude --- lib/mruby/gem.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/mruby/gem.rb b/lib/mruby/gem.rb index b6d95dd5b..c0fe806b4 100644 --- a/lib/mruby/gem.rb +++ b/lib/mruby/gem.rb @@ -43,6 +43,7 @@ module MRuby def initialize(name, &block) @name = name @initializer = block + @post_user_config = nil @version = "0.0.0" @dependencies = [] @conflicts = [] @@ -91,6 +92,7 @@ module MRuby build.libmruby_objs << @objs instance_eval(&@build_config_initializer) if @build_config_initializer + instance_eval(&@post_user_config) if @post_user_config repo_url = build.gem_dir_to_repo_url[dir] build.locks[repo_url]['version'] = version if repo_url @@ -200,6 +202,19 @@ module MRuby end end + # Register a block that runs after the user's `build.gem` block has + # been processed. Intended for gem authors to fill in defaults that + # depend on user-supplied configuration (e.g. auto-detect a library + # only if the user didn't specify which to use). + # + # Initialization order: + # 1. block in `MRuby::Gem::Specification.new` (gem author) + # 2. block in `build.gem` (user's build_config) + # 3. block in `post_user_config` (gem author, this hook) + def post_user_config(&block) + @post_user_config = block + end + def build_settings(&blk) @build_settings = blk end