Sha256: b2e0675f5d7f69f0772e2d5b66a7c248dffd1ee1f274a154c448d3847f9bdbc9

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'ptools'
require 'fileutils'

module Klipp

  class Project

    def initialize(template)
      @template = template
    end

    def create
      # Check template directory exists
      # Copy all files while adjusting paths
      source_template_dir = File.join(Klipp::Configuration.templates_dir, @template.name)
      target_template_dir = File.join(Dir.pwd, @template.name)

      raise "Target directory already exists. Klipp will not overwrite your project: #{target_template_dir}" if File.exists? target_template_dir

      @source_files = Dir.glob(File.join(source_template_dir, '**', '*'))
      @source_files.each do |source_file|
        transfer_file source_file, target_file(source_template_dir, source_file, target_template_dir)
      end
    end

    def target_file(source_template_dir, source_file, target_template_dir)
      stripped_path = source_file.gsub(source_template_dir, '')
      customizable_path = @template.replace_tokens(stripped_path)
      File.join(target_template_dir, customizable_path)
    end

    def transfer_file(source_file, target_file)
      FileUtils.mkdir_p File.dirname(target_file)

      if File.directory? source_file
        FileUtils.mkdir_p target_file
      elsif File.binary? source_file
        FileUtils.cp(source_file, target_file)
      else
        IO.write target_file, @template.replace_tokens(File.read(source_file))
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
klipp-0.0.1 lib/klipp/project.rb