Sha256: 2cdd77b3134751105af0e52317116d03005b0f87fe53f2320e608db296bef8eb

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

class Jets::Stack
  class Function
    extend Memoist

    attr_reader :template
    def initialize(template)
      @template = template
    end

    def meth
      attributes = @template.values.first
      handler = attributes[:Properties][:Handler]
      handler.split('.').last
    end

    def lang
      return if internal?

      if source_file
        # Detect language from file extension
        ext = File.extname(source_file).sub(/^\./,'').to_sym
        lang_map[ext]
      else
        puts "WARN: Unable to find a source file for function. Looked at: #{search_expression}".color(:yellow)
      end
    end

    def lang_map
      {
        rb: :ruby,
        py: :python,
        js: :node,
      }
    end

    def source_file
      Dir.glob(search_expression).first
    end
    memoize :source_file

    def search_expression
      base_search_expression.sub('handlers/shared/', "#{Jets.root}/app/shared/")
    end

    def internal_search_expression
      internal = File.expand_path("../internal", File.dirname(__FILE__))
      base_search_expression.sub('handlers/shared/', "#{internal}/app/shared/")
    end

    def base_search_expression
      attributes = @template.values.first
      handler = attributes[:Properties][:Handler]
      handler.split('.')[0..-2].join('.') + '.*' # search_expression
      # Example: handlers/shared/functions/jets/s3_bucket_config.*
    end

    # Internal flag is mainly used to disable WARN messages
    def internal?
      return true if internal_search_expression.include?("jets/base_path")
      return true if internal_search_expression.include?("jets/s3_bucket_config")
      !!Dir.glob(internal_search_expression).first
    end

    # Relative path
    # app/shared/functions/kevin.py => handlers/shared/functions/kevin.py
    def handler_dest
      return unless source_file
      source_file.sub(%r{.*/app/}, "handlers/")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jets-5.0.13 lib/jets/stack/function.rb
jets-5.0.12 lib/jets/stack/function.rb
jets-5.0.11 lib/jets/stack/function.rb
jets-5.0.10 lib/jets/stack/function.rb
jets-5.0.9 lib/jets/stack/function.rb
jets-5.0.8 lib/jets/stack/function.rb