Skip to content

Commit

Permalink
Merge pull request #301 from boltops-tools/speed-boast-copy-modules
Browse files Browse the repository at this point in the history
Speed boast copy modules
  • Loading branch information
tongueroo authored May 2, 2023
2 parents e405648 + dd4d408 commit d755757
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/terraspace/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def defaults
config.build.default_pass_files = ["/files/"]
config.build.pass_files = []
config.build.dependency_words = []
# 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
Expand Down
2 changes: 1 addition & 1 deletion lib/terraspace/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions lib/terraspace/compiler/perform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/terraspace/compiler/strategy/abstract_base.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions lib/terraspace/compiler/strategy/mod.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
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
strategy.run
end

def strategy_class(path)
# Significant speed improvement
return Mod::Pass if copy_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)
# Fallback to Mod::Tf for all other files. ERB useful for terraform.tfvars
"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
Expand Down

0 comments on commit d755757

Please sign in to comment.