Sha256: 771dcec2682b5972982e59c4a94d75af9fb1ad3b7791276f21f3273fadd329f6

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'fileutils'
require 'erb'
require 'ostruct'
require 'pathname'
require 'securerandom'
require 'shellwords'

module Gamefic
  module Sdk
    module Scaffold
      class Binder < OpenStruct
        def camelcase string
          string.split(/[_\s\-]+/).collect(&:capitalize).join
        end

        def snake_case string
          string.split(/[_\s\-]+/).collect(&:downcase).join('_')
        end

        def get_binding
          binding
        end
      end

      module_function

      def render(file, data)
        template = File.read(file)
        erb = ERB.new(template)
        erb.result(data.get_binding)
      end

      def custom_copy origin, destination, data
        FileUtils.mkdir_p File.dirname(destination)
        if origin.end_with?('.gf.erb')
          File.write destination[0..-8], render(origin, data)
        else
          FileUtils.cp_r origin, destination
        end
      end

      def build name, destination
        dir = File.join(SCAFFOLDS_PATH, name)
        raise LoadError, "Scaffold `#{name}` does not exist" unless File.directory?(dir)

        path = Pathname.new('.').join(destination).realdirpath
        data = Binder.new(name: File.basename(path))
        files = Dir.glob(File.join(dir, '**', '*'), File::FNM_DOTMATCH).select { |entry| File.file?(entry) }
        map = {}
        files.each do |file|
          rename = File.join(File.dirname(file), File.basename(file)).gsub('__name__', data.name)
          dst = File.join(destination, rename[dir.length..-1])
          raise "Gamefic generation would overwrite existing file #{rename}" if File.file?(dst)

          map[file] = dst
        end
        map.each_pair { |src, dst| custom_copy src, dst, data }
        system "cd #{Shellwords.escape(destination)} && bundle install"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gamefic-sdk-3.1.0 lib/gamefic-sdk/scaffold.rb