Sha256: 849b46fa295c437c7c68ff7795af97736fac71b588fcb5cd665e522a89e6b571

Contents?: true

Size: 1.71 KB

Versions: 136

Compression:

Stored size: 1.71 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?
      !!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

136 entries across 136 versions & 3 rubygems

Version Path
jets-4.0.12 lib/jets/stack/function.rb
jets-4.0.11 lib/jets/stack/function.rb
jets-4.0.10 lib/jets/stack/function.rb
jets-4.0.9 lib/jets/stack/function.rb
jets-4.0.8 lib/jets/stack/function.rb
jets-4.0.7 lib/jets/stack/function.rb
jets-4.0.6 lib/jets/stack/function.rb
jets-4.0.5 lib/jets/stack/function.rb
jets-4.0.4 lib/jets/stack/function.rb
jets-4.0.3 lib/jets/stack/function.rb
jets-4.0.2 lib/jets/stack/function.rb
jets-4.0.1 lib/jets/stack/function.rb
jets-4.0.0 lib/jets/stack/function.rb
jets-3.2.2 lib/jets/stack/function.rb
jets-3.2.1 lib/jets/stack/function.rb
jets.benforeva-3.0.17.pre.mount.pre.fix lib/jets/stack/function.rb
jets-3.2.0 lib/jets/stack/function.rb
jets-3.1.5 lib/jets/stack/function.rb
jets-3.1.4 lib/jets/stack/function.rb
jets-3.1.3 lib/jets/stack/function.rb