Sha256: 3b3b22a7ead52637809833c5078d84cd06bb55e0c611206d4de84c6212ab6f4a

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
class String
  def camelize(uppercase_first_letter = true)
    string = self
    if uppercase_first_letter
      string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
    else
      string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
    end
    string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
  end
end

module KCommercial
  module Resources
    class SwiftGenerator
      # The super module default is MKResources
      # @return [String]
      attr_reader :super_module

      # The key defines for the generator
      # @return [Array<String>]
      attr_reader :keys

      # Create a new SwiftGenerator
      # @param [Array<String>] keys the defines for keys
      def initialize(keys, options = {})
        @keys = keys
        @keys ||= []
        @super_module = options.delete(:super) { :MKResources}
      end

      def extension_contents
        <<EOF
//
//  SharedResources.swift
//  MKSharedResources
//
//  Created by Dong Zhao on 2020/12/31.
//
import MKResources
import #{super_module}
extension R {
#{append_prefix(swift_contents,"\t")}
}
EOF
      end

      private

      def class_name
        "Colors"
      end

      def swift_contents
        key_functions = keys.map { |key| "#{key_to_code(key)}" }.join('')
        <<EOF
// swiftlint:disable all
open class #{class_name}: #{super_module}.R.#{class_name}{
#{append_prefix(key_functions, "\t")}
}
// swiftlint:enable all
EOF
      end

      def key_to_code(key)
        raise NotImplementedError
      end

      def key_to_function_name(key)
        key = key.camelize(false)
        if key.length > 0
          key[0] = key[0].downcase
        end
        key
      end

      def append_prefix(contents, prefix)
        contents.lines.map { |l| "#{prefix}#{l}"}.join('')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
KCommercialPipeline-0.2.5.1 lib/KCommercialPipeline/core/resource/swift/swift_generator.rb