class CapicuaGen::Gaspar::CSHeaderFooterFeature

Característica generadora de caceberas y pies de página en el código fuente generado por otras características

Attributes

exclude_regex[RW]
include_regex[RW]

Public Class Methods

new(values= {}) click to toggle source

Inicializa la característica

Calls superclass method
# File lib/CapicuaGenGaspar/CodeTransformer/CSHeaderFooter/Source/cs_header_footer_feature.rb, line 46
def initialize(values= {})
  super(values)

  # Configuro los tipos si estos no han sido configurados previamente
  self.types= [:code_transformer] if self.types.blank?

  # Configuro los templates
  set_template("header", Template.new(:file => 'header.erb'))
  set_template("footer", Template.new(:file => 'footer.erb'))

  #Configuro los include y exclude
  @include_regex= [/\.cs$/i] unless @include_regex
  @exclude_regex= [/\.Designer\.cs$/i] unless @exclude_regex


end

Public Instance Methods

check_include(file) click to toggle source

Revisa si un archivo debe ser incluido

# File lib/CapicuaGenGaspar/CodeTransformer/CSHeaderFooter/Source/cs_header_footer_feature.rb, line 174
def check_include(file)
  return false if @include_regex.blank?

  @include_regex.each do |include|
    return false unless include.match(file)
  end

  return true if @exclude_regex.blank?

  @exclude_regex.each do |exclude|
    return false if exclude.match(file)
  end

  return true

end
configure_template_targets() click to toggle source

Configura los objetivos de las platillas (despues de establecer el generador)

# File lib/CapicuaGenGaspar/CodeTransformer/CSHeaderFooter/Source/cs_header_footer_feature.rb, line 64
def configure_template_targets

  # Configuro los templates
  @header_template_target= set_template_target('header', TemplateTarget.new())
  @footer_template_target= set_template_target('footer', TemplateTarget.new())

end
generate() click to toggle source

Recorre las plantilla y genera las cabeceras

# File lib/CapicuaGenGaspar/CodeTransformer/CSHeaderFooter/Source/cs_header_footer_feature.rb, line 73
def generate
  message_helper.puts_generating_feature(self)

  message_helper.add_indent

  begin


    resultado= ''

    template_target= get_template_target_by_name('proyect')

    directory_base= self.generation_attributes[:out_dir]
    # Recorro todas las características
    generator.features().each do |feature|
      #Obtengo todos los archivos asociados
      feature.get_relative_out_files(:directory_base => directory_base, :types => :proyect_file).each do |unix_path|
        begin

          # Nombre del archivo
          file     = unix_path.gsub(/\//, '\\')
          file_name= File.basename file

          next unless check_include(file)

          file_content= ''

          Dir.chdir directory_base do
            # Cargo el acrhivo
            file_content= File.read(file)
            file_content.force_encoding(CharDet.detect(file_content)['encoding'])
          end


          # Genero los las licencias
          modified=false

          # Limpio file_content para comparciones

          # Genero cabecera
          header  =generate_template_target(@header_template_target, binding)
          begin
            header_check=header.clone
            header_check.force_encoding(CharDet.detect(file_content)['encoding']) unless header.blank?
            header_check.blank?
            header=header_check
          rescue
          end

          if header.blank? or file_content.gsub(/\s*/, '').include?(header.gsub(/\s*/, ''))
            header =''
          else
            modified=true
          end

          # Genero pie
          footer= generate_template_target(@footer_template_target, binding)
          begin
            footer_check=footer.clone
            footer_check.force_encoding(CharDet.detect(file_content)['encoding']) unless footer.blank?
            footer_check.blank?
            footer_check=footer_check
          rescue
          end

          if footer.blank? or file_content.gsub(/\s*/, '').include?(footer.gsub(/\s*/, ''))
            footer =''
          else
            modified=true
          end

          if modified
            file_out= "#{header}#{file_content}#{footer}"
            Dir.chdir directory_base do
              # Guardo el archivo
              File.write(file, file_out)
              message_helper.puts_created_template("header.erb, footer.erb", file, :override)
            end
          else
            message_helper.puts_created_template("header.erb, footer.erb", file, :ignore)
          end

        rescue => e
          error_message="Error en #{unix_path}"
          message_helper.puts_error_message error_message
          raise e
        end
        
      end

    end

  ensure
    message_helper.remove_indent
    puts

  end

end