Commit 3bcd17c7 authored by Laurent Briais's avatar Laurent Briais

Merge branch 'custom_build_in_plugins' into 'master'

Resolve "Allow plugins to implement their own files generation"

Closes #20

See merge request !24
parents dead5adf fa207b35
Pipeline #95879219 passed with stage
in 1 minute and 2 seconds
......@@ -14,9 +14,15 @@
:processors:
<%= plugin_name %>: <%= plugin_module_name %>::Processor
# This is the name of the method called __instead__ of standard file detemplatization. This method has
# to be a module method in the `plugin_module` module.
:generate_build_files: generate_build_files
# This is the name of the method called after the files are detemplatized. This method has
# to be a module method in the `plugin_module` module.
:build: post_build_hook
:post_build: post_build_hook
# If a dsl module is declared it will be injected in the DSL available in the shell or templates
:dsl:
......
......@@ -5,12 +5,23 @@ require '<%= plugin_name %>/dsl/<%= plugin_name %>_dsl'
module <%= plugin_module_name %>
extend Climatic::Utils::SafeExec
def self.generate_build_files(entity_to_build, target_directory)
# This is an example of what you could do to generate files, here using default detemplatization...
PowerStencil.logger.debug "By default plugin '<%= plugin_name %>' uses standard build..."
PowerStencil.project.engine.generate_build_files entity_to_build, target_directory
end
def self.post_build_hook(built_entity, generated_files_path)
# This is an example of what you could do after files are generated, ie pretty much anything...
case built_entity.type
when :<%= plugin_name %>_entity
generated_file = File.join generated_files_path, 'message.txt'
puts File.readlines(generated_file)
# Enclose in `safely_exec_code` code that is protected by the `--simulate` command-line flag.
safely_exec_code(message: "Displaying content of '#{generated_file}'.") do
puts File.readlines(generated_file)
end
else
raise PowerStencil::Error, 'Plugin <%= plugin_name %> doesnt know how handle the build of a <%= plugin_name %>_entity entity type !'
end
......
......@@ -14,9 +14,15 @@
:processors:
<%= plugin_name %>: <%= plugin_module_name %>::Processor
# This is the name of the method called __instead__ of standard file detemplatization. This method has
# to be a module method in the `plugin_module` module.
:generate_build_files: generate_build_files
# This is the name of the method called after the files are detemplatized. This method has
# to be a module method in the `plugin_module` module.
:build: post_build_hook
:post_build: post_build_hook
# If a dsl module is declared it will be injected in the DSL available in the shell or templates
:dsl:
......
......@@ -5,12 +5,23 @@ require '<%= plugin_name %>/dsl/<%= plugin_name %>_dsl'
module <%= plugin_module_name %>
extend Climatic::Utils::SafeExec
def self.generate_build_files(entity_to_build, target_directory)
# This is an example of what you could do to generate files, here using default detemplatization...
PowerStencil.logger.debug "By default plugin '<%= plugin_name %>' uses standard build..."
PowerStencil.project.engine.generate_build_files entity_to_build, target_directory
end
def self.post_build_hook(built_entity, generated_files_path)
# This is an example of what you could do after files are generated, ie pretty much anything...
case built_entity.type
when :<%= plugin_name %>_entity
generated_file = File.join generated_files_path, 'message.txt'
puts File.readlines(generated_file)
# Enclose in `safely_exec_code` code that is protected by the `--simulate` command-line flag.
safely_exec_code(message: "Displaying content of '#{generated_file}'.") do
puts File.readlines(generated_file)
end
else
raise PowerStencil::Error, 'Plugin <%= plugin_name %> doesnt know how handle the build of a <%= plugin_name %>_entity entity type !'
end
......
......@@ -52,6 +52,28 @@ module PowerStencil
end
end
def generate_build_files(entity_to_build, target_path)
logger.info "Rendering '#{entity_to_build.as_path}' templates in '#{target_path}'"
render_source entity_to_build.templates_path, target_path, main_entry_point: entity_to_build.as_path
logger.info "Detemplatized files generated in '#{target_path}'"
manage_build_links target_path
end
def post_build_hook(entity_to_build, files_path)
case entity_to_build.type
when :simple_exec
script_to_execute = File.expand_path File.join(files_path, entity_to_build.post_process.process)
exec_options = {message: "Running '#{script_to_execute}'"}
exec_options[:show_output] = true if config[:verbose]
process_report = safely_exec_command "'#{script_to_execute}'", exec_options
unless process_report.exit_status.exitstatus == 0
raise PowerStencil::Error, "Process `#{process_report.command}` failed and returned exit code '#{process_report.exit_status.exitstatus}'"
end
process_report
end
end
private
def manage_build_links(last_build_path)
......@@ -97,43 +119,34 @@ module PowerStencil
def build_entity(entity_to_build, target_path)
initial_directory = Dir.pwd
target_plugin_name = entity_to_build.buildable_by.to_s
default_build entity_to_build, target_path
# Files generation
if target_plugin_name.empty?
generate_build_files entity_to_build, target_path
else
target_plugin = project.plugins[target_plugin_name]
raise PowerStencil::Error, "Could not find plugin '#{target_plugin_name}' !" if target_plugin.nil?
raise PowerStencil::Error, "Plugin '#{target_plugin_name}' has no file generation capability !" unless target_plugin.capabilities[:generate_build_files]
target_plugin.generate_build_files entity_to_build, target_path
end
# Post processing executed from generated directory (#11)
Dir.chdir target_path
# Post generation action
if target_plugin_name.empty?
post_build_hook entity_to_build, target_path
else
target_plugin = project.plugins[target_plugin_name]
raise PowerStencil::Error, "Could not find plugin '#{target_plugin_name}' !" if target_plugin.nil?
raise PowerStencil::Error, "Plugin '#{target_plugin_name}' has no build capability !" unless target_plugin.capabilities[:build]
raise PowerStencil::Error, "Plugin '#{target_plugin_name}' has no post build capability !" unless target_plugin.capabilities[:post_build]
target_plugin.post_build_hook entity_to_build, target_path
end
ensure
Dir.chdir initial_directory
end
def default_build(entity_to_build, target_path)
logger.info "Building #{entity_to_build.as_path} in '#{target_path}'"
render_source entity_to_build.templates_path, target_path, main_entry_point: entity_to_build.as_path
logger.info "Detemplatized filed generated in '#{target_path}'"
manage_build_links target_path
end
def post_build_hook(entity_to_build, files_path)
case entity_to_build.type
when :simple_exec
script_to_execute = File.expand_path File.join(files_path, entity_to_build.post_process.process)
exec_options = {message: "Running '#{script_to_execute}'"}
exec_options[:show_output] = true if config[:verbose]
process_report = safely_exec_command "'#{script_to_execute}'", exec_options
unless process_report.exit_status.exitstatus == 0
raise PowerStencil::Error, "Process `#{process_report.command}` failed and returned exit code '#{process_report.exit_status.exitstatus}'"
end
process_report
end
end
end
end
......
......@@ -3,9 +3,16 @@ module PowerStencil
module Build
def generate_build_files(*args)
build_method_name = plugin_definition[:generate_build_files]
msg = "Plugin '#{self.name}' doesn't contain the '#{build_method_name}' method in the '#{plugin_definition[:plugin_module]}' module. Cannot delegate !"
raise PowerStencil::Error, msg unless plugin_module.respond_to? build_method_name
plugin_module.send build_method_name, *args
end
def post_build_hook(*args)
build_method_name = plugin_definition[:build]
msg = "Plugin '#{self.name}' doesn't contain the '#{build_method_name}' method in the '#{plugin_definition[:plugin_module]}' module. Cannot build !"
build_method_name = plugin_definition[:post_build]
msg = "Plugin '#{self.name}' doesn't contain the '#{build_method_name}' method in the '#{plugin_definition[:plugin_module]}' module. Cannot delegate !"
raise PowerStencil::Error, msg unless plugin_module.respond_to? build_method_name
plugin_module.send build_method_name, *args
end
......
......@@ -5,7 +5,7 @@ module PowerStencil
include PowerStencil::Utils::FileHelper
CAPABILITIES = %i(config command_line processors build dsl entity_definitions templates).freeze
CAPABILITIES = %i(config command_line processors generate_build_files post_build dsl entity_definitions templates).freeze
attr_reader :plugin_definition
......@@ -27,7 +27,7 @@ module PowerStencil
logger.debug "Loading plugin '#{self.name}' capabilities..."
begin
@plugin_definition = yaml_file_to_hash yaml_file
%i(processors entity_definitions templates build dsl).each do |capability|
%i(processors entity_definitions templates generate_build_files post_build dsl).each do |capability|
unless plugin_definition[capability].nil? or plugin_definition[capability].empty?
capabilities[capability] = true
end
......
......@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_dependency 'climatic', '~> 0.2.34'
spec.add_dependency 'climatic', '~> 0.2.35'
spec.add_dependency 'dir_glob_ignore', '~> 0.3'
spec.add_dependency 'universe_compiler', '~> 0.5.5'
spec.add_dependency 'pry'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment