Sha256: d28417daeecea6cc3b0cbd77e55caededd8ed252d7edcb7bb48dd61ce6de0c1c

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

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

module Gamefic
  module Sdk
    module Scaffold
      class Binder < OpenStruct
        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 destination.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])
          if File.file?(dst)
            raise "Gamefic generation would overwrite existing file #{rename}"
          else
            map[file] = dst
          end
        end
        map.each_pair { |src, dst| custom_copy src, dst, data }
        system "bundle install --gemfile=#{Shellwords.escape(File.join(destination, 'Gemfile'))}"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
gamefic-sdk-2.3.2 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.3.1 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.3.0 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.2.0 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.1.0 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.0.4 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.0.3 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.0.2 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.0.1 lib/gamefic-sdk/scaffold.rb
gamefic-sdk-2.0.0 lib/gamefic-sdk/scaffold.rb