From add9f9c50d0233219d0fcfa190260f591e73af2b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 15 Nov 2018 17:04:04 -0500 Subject: [PATCH] save configured environment construction flags for separate build step - close #60 --- lib/rscons/application.rb | 4 +++ lib/rscons/cache.rb | 56 +++++++++++++++++++++++++++++++++++++++ lib/rscons/environment.rb | 16 +++++++++-- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index c36acd8..4353940 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -134,6 +134,10 @@ module Rscons rv = 1 end co.close + cache = Cache.instance + cache.set_configured(rv == 0) + cache.set_default_environment_vars(@default_environment.to_h) + cache.write rv end diff --git a/lib/rscons/cache.rb b/lib/rscons/cache.rb index f89a7b4..d254a37 100644 --- a/lib/rscons/cache.rb +++ b/lib/rscons/cache.rb @@ -85,6 +85,45 @@ module Rscons @lookup_checksums = {} end + # Return whether the project has been configured. + # + # @return [Boolean] + # Whether the project has been configured. + def configured? + @cache["configured"] + end + + # Set whether the project has been configured. + # + # @param configured [Boolean] + # Whether the project has been configured. + # + # @return [void] + def set_configured(configured) + @cache["configured"] = configured + @dirty = true + end + + # Get the default environment construction variables. + # + # @return [Hash] + # Default environment construction variables. + def get_default_environment_vars + @cache["default_environment_vars"] + end + + # Set the default environment construction variables. + # + # @param vars [Hash] + # Default environment construction variables. + # + # @return [void] + def set_default_environment_vars(vars) + validate_json_object(vars) + @cache["default_environment_vars"] = vars + @dirty = true + end + # Write the cache to disk to be loaded next time. # # @return [void] @@ -314,6 +353,7 @@ module Rscons end @cache["targets"] ||= {} @cache["directories"] ||= {} + @cache["default_environment_vars"] ||= {} @lookup_checksums = {} @dirty = false end @@ -336,5 +376,21 @@ module Rscons def calculate_checksum(file) @lookup_checksums[file] = Digest::MD5.hexdigest(File.read(file, mode: "rb")) rescue "" end + + # Validate that an object is one of a known set of values that can be + # serialized properly with JSON. + # + # @param o [Object] + # Object to validate. + def validate_json_object(o) + if o.is_a?(Array) + o.each {|v| validate_json_object(v)} + elsif o.is_a?(Hash) + o.each {|*kv| validate_json_object(kv)} + elsif [NilClass, TrueClass, FalseClass, String].none? {|c| o.is_a?(c)} + raise "Unexpected cache value for serialization: #{o.inspect}" + end + end + end end diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index f301429..ccd905b 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -278,14 +278,26 @@ module Rscons # # @see VarSet#[] def [](*args) - @varset.send(:[], *args) + @varset.__send__(:[], *args) end # Set a construction variable's value. # # @see VarSet#[]= def []=(*args) - @varset.send(:[]=, *args) + @varset.__send__(:[]=, *args) + end + + # Return the Environment construction variables as a Hash. + # + # @since 2.0.0 + # + # @api private + # + # @return [Hash] + # Environment construction variables. + def to_h + @varset.to_h end # Add a set of construction variables to the Environment.