From 02e3e1f77c1669818abcbc63a1fe97617c2345da Mon Sep 17 00:00:00 2001 From: Mike Karlesky Date: Thu, 2 May 2024 15:21:02 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20No=20more=20double=20insta?= =?UTF-8?q?ntiation=20of=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Objects that are in common between bin/ and lib/ are handed off from former to latter in dependency injection. This also preserves state in any handoff objects. --- bin/ceedling | 15 +++++++++++++++ bin/objects.yml | 11 +++++++++-- lib/ceedling/rakefile.rb | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/bin/ceedling b/bin/ceedling index 13eebf07..0e43d4d3 100755 --- a/bin/ceedling +++ b/bin/ceedling @@ -76,6 +76,21 @@ begin objects = {} # Empty initial hash to be redefined (fingers crossed) objects = DIY::Context.from_yaml( File.read( bin_objects_filepath ) ) objects.build_everything() + + # Extract objects shared between bootloader and application + # This prevents double instantiation and preserves object state in handoff + handoff_objects = {} + handoff = [ + :loginator, + :file_wrapper, + :yaml_wrapper, + :config_walkinator, + :stream_wrapper, + :system_wrapper, + :verbosinator + ] + CEEDLING_HANDOFF_OBJECTS = handoff_objects + handoff.each {|name| handoff_objects[name] = objects[name] } # Remove all load paths we've relied on (main application will set up load paths again) $LOAD_PATH.delete( ceedling_bin_path ) diff --git a/bin/objects.yml b/bin/objects.yml index 4cecf178..2365864a 100644 --- a/bin/objects.yml +++ b/bin/objects.yml @@ -5,7 +5,11 @@ # SPDX-License-Identifier: MIT # ========================================================================= -# Loaded from ceedling/lib +# +# Loaded from lib/ +# ---------------- +# + file_wrapper: yaml_wrapper: @@ -25,7 +29,10 @@ loginator: - system_wrapper - stream_wrapper -# Loaded from bin (here) +# +# Loaded from bin/ +# ---------------- +# actions_wrapper: diff --git a/lib/ceedling/rakefile.rb b/lib/ceedling/rakefile.rb index d289cb73..e97a55e5 100644 --- a/lib/ceedling/rakefile.rb +++ b/lib/ceedling/rakefile.rb @@ -46,9 +46,18 @@ def log_runtime(run, start_time_s, end_time_s, enabled) # 3. Remove full path from $LOAD_PATH $LOAD_PATH.unshift( CEEDLING_APPCFG[:ceedling_lib_path] ) objects_filepath = File.join( CEEDLING_APPCFG[:ceedling_lib_path], 'objects.yml' ) + + # Create object hash and dependency injection context @ceedling = {} # Empty hash to be redefined if all goes well @ceedling = DIY::Context.from_yaml( File.read( objects_filepath ) ) + + # Inject objects already insatantiated from bin/ bootloader before building the rest + CEEDLING_HANDOFF_OBJECTS.each_pair {|name,obj| @ceedling.set_object( name.to_s, obj )} + + # Build Ceedling application's objects @ceedling.build_everything() + + # Simplify load path after construction $LOAD_PATH.delete( CEEDLING_APPCFG[:ceedling_lib_path] ) # One-stop shopping for all our setup and such after construction