From c71c7fb022b176052880be6bfb2a2aa0ed6adfbd Mon Sep 17 00:00:00 2001 From: Tung Nguyen Date: Sun, 23 Apr 2023 16:14:33 +0000 Subject: [PATCH 1/2] speed boast: copy_modules option --- lib/terraspace/app.rb | 1 + lib/terraspace/builder.rb | 2 +- lib/terraspace/compiler/perform.rb | 7 ++++--- lib/terraspace/compiler/strategy/abstract_base.rb | 5 +++-- lib/terraspace/compiler/strategy/mod.rb | 3 +++ 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/terraspace/app.rb b/lib/terraspace/app.rb index 46ea8ed4..97370879 100644 --- a/lib/terraspace/app.rb +++ b/lib/terraspace/app.rb @@ -43,6 +43,7 @@ def defaults config.build.default_pass_files = ["/files/"] config.build.pass_files = [] config.build.dependency_words = [] + config.build.copy_modules = true # speed improvement config.bundle = ActiveSupport::OrderedOptions.new config.bundle.logger = ts_logger diff --git a/lib/terraspace/builder.rb b/lib/terraspace/builder.rb index 0de9d012..8051258e 100644 --- a/lib/terraspace/builder.rb +++ b/lib/terraspace/builder.rb @@ -69,7 +69,7 @@ def build_dir(type_dir) with_each_mod(type_dir) do |mod| is_root_module = mod.cache_dir == @mod.cache_dir next if is_root_module # handled by build_stacks - Compiler::Perform.new(mod).compile + Compiler::Perform.new(mod, type_dir: type_dir).compile end end diff --git a/lib/terraspace/compiler/perform.rb b/lib/terraspace/compiler/perform.rb index 2559b53a..b8f1d1f4 100644 --- a/lib/terraspace/compiler/perform.rb +++ b/lib/terraspace/compiler/perform.rb @@ -3,8 +3,9 @@ class Perform include CommandsConcern include Basename - def initialize(mod) - @mod = mod + def initialize(mod, options={}) + # options for type_dir + @mod, @options = mod, options end def compile @@ -60,7 +61,7 @@ def compile_config_file(file) end def compile_mod_file(src_path) - content = Strategy::Mod.new(@mod, src_path).run + content = Strategy::Mod.new(@mod, src_path, @options).run Writer.new(@mod, src_path: src_path).write(content) end diff --git a/lib/terraspace/compiler/strategy/abstract_base.rb b/lib/terraspace/compiler/strategy/abstract_base.rb index 85536ef1..22c38a66 100644 --- a/lib/terraspace/compiler/strategy/abstract_base.rb +++ b/lib/terraspace/compiler/strategy/abstract_base.rb @@ -1,7 +1,8 @@ module Terraspace::Compiler::Strategy class AbstractBase - def initialize(mod, src_path) - @mod, @src_path = mod, src_path + def initialize(mod, src_path, options={}) + # options for type_dir + @mod, @src_path, @options = mod, src_path, options end end end diff --git a/lib/terraspace/compiler/strategy/mod.rb b/lib/terraspace/compiler/strategy/mod.rb index 09ef1d24..b7ebf877 100644 --- a/lib/terraspace/compiler/strategy/mod.rb +++ b/lib/terraspace/compiler/strategy/mod.rb @@ -7,6 +7,9 @@ def run end def strategy_class(path) + # Significant speed improvement + return Mod::Pass if Terraspace.config.build.copy_modules && @options[:type_dir] == "modules" + ext = File.extname(path).sub('.','') return Mod::Pass if ext.empty? # infinite loop without this return Mod::Pass if Terraspace.pass_file?(path) or !text_file?(path) From dd4d408985e78eaae50c414b1c9c5d2f95b9acdd Mon Sep 17 00:00:00 2001 From: Tung Nguyen Date: Tue, 2 May 2023 19:28:26 +0000 Subject: [PATCH 2/2] warn about config.build.copy_modules behavior --- lib/terraspace/app.rb | 4 ++- lib/terraspace/compiler/strategy/mod.rb | 37 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/terraspace/app.rb b/lib/terraspace/app.rb index 97370879..e8b05838 100644 --- a/lib/terraspace/app.rb +++ b/lib/terraspace/app.rb @@ -43,7 +43,9 @@ def defaults config.build.default_pass_files = ["/files/"] config.build.pass_files = [] config.build.dependency_words = [] - config.build.copy_modules = true # speed improvement + # copy_modules = nil # => Will show a warning and default to true + # copy_modules = true # => Will be default in next major version + config.build.copy_modules = nil # speed improvement config.bundle = ActiveSupport::OrderedOptions.new config.bundle.logger = ts_logger diff --git a/lib/terraspace/compiler/strategy/mod.rb b/lib/terraspace/compiler/strategy/mod.rb index b7ebf877..a763180b 100644 --- a/lib/terraspace/compiler/strategy/mod.rb +++ b/lib/terraspace/compiler/strategy/mod.rb @@ -1,5 +1,7 @@ module Terraspace::Compiler::Strategy class Mod < AbstractBase + include Terraspace::Util::Logging + def run klass = strategy_class(@src_path) strategy = klass.new(@mod, @src_path) # IE: Terraspace::Compiler::Strategy::Mod::Rb.new @@ -8,7 +10,7 @@ def run def strategy_class(path) # Significant speed improvement - return Mod::Pass if Terraspace.config.build.copy_modules && @options[:type_dir] == "modules" + return Mod::Pass if copy_modules? ext = File.extname(path).sub('.','') return Mod::Pass if ext.empty? # infinite loop without this @@ -17,6 +19,39 @@ def strategy_class(path) "Terraspace::Compiler::Strategy::Mod::#{ext.camelize}".constantize rescue Mod::Pass end + @@copy_modules_warned = false + def copy_modules? + return false unless @options[:type_dir] == "modules" + + copy_modules = Terraspace.config.build.copy_modules + if copy_modules.nil? && @@copy_modules_warned == false + logger.info "WARN: config.build.copy_modules is not set. Defaulting to true.".color(:yellow) + logger.info <<~EOL + The terraspace building behavior is to copy modules from + the app/modules folder to the .terraspace-cache for speed. + Most do not need app/modules to be compiled. + Other files like app/stacks and tfvars files are still compiled. + This is a change from previous versions of Terraspace, where + all files were compiled. + + You can turn this warning off by setting: + + .terraspace/config.rb + + Terraspace.configure do |config| + config.build.copy_modules = true + end + + In future Terraspace versions, the default will be true. + There will be no warning message, but it will still be configurable. + EOL + copy_modules = true + @@copy_modules_warned = true + end + + copy_modules + end + private def text_file?(filename) TextFile.new(filename).check