Class: Codestrap::Strap::Standard

Inherits:
Abstract
  • Object
show all
Defined in:
lib/codestrap/strap/standard.rb

Overview

Standard project renderer

Instance Attribute Summary

Attributes inherited from Abstract

#dst, #ignore, #objects, #overwrite, #src

Instance Method Summary collapse

Methods inherited from Abstract

#abstract, abstract_methods, #overwrite?, #post, #to_disk, #working_dir

Constructor Details

#initialize(*args) ⇒ Standard

Returns a new instance of Standard.



17
18
19
# File 'lib/codestrap/strap/standard.rb', line 17

def initialize(*args)
  self.working_dir
end

Instance Method Details

#executeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/codestrap/strap/standard.rb', line 72

def execute
  @compiled.each do |obj|
    case obj[:type]
      when :dir
        FileUtils.mkpath obj[:dst]
      when :regular
        FileUtils.install obj[:src], obj[:dst]
      when :erb
        renderer         = Codestrap::Stub::Factory.new('Standard').construct
        renderer.objects = self.objects
        renderer.src     = obj[:src]
        renderer.dst     = obj[:dst]
        renderer.pre
        renderer.execute
        renderer.post
        renderer.to_disk
      else
        raise Exception, "Unknown type: #{obj[:type].to_s}"
    end
  end
end

#preObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/codestrap/strap/standard.rb', line 21

def pre
  @compiled ||= []

  # Take ignored array from Codestrap.config
  ignore_hash = {}
  Array(self.ignore).each do |ignore|
    ignore_hash[ignore] = true
  end
  Find.find(self.src) do |src|
    # Ignore files
    if ignore_hash.has_key? File.basename(src)
      Find.prune
    end

    proj_path = String(src.gsub(/^#{Regexp.escape(self.src)}/, ''))

    # Destination path substitutions
    proj_path =~ /:(?:stub|strap):(.+?):/
    object      = $1
    replacement = proj_path.gsub(/:(?:stub|strap):.+?:/, "<%= #{object} %>")
    if proj_path != replacement
      # Regex replacement
      ns        = Codestrap::Namespace.new(self.objects.objects)
      erb       = ERB.new(replacement)
      proj_path = erb.result(ns.get_binding)
    end

    dst = File.join(self.working_dir, proj_path)

    # Directories
    if File.directory? src
      @compiled.push({file: proj_path, src: src, dst: dst, type: :dir})
      next
    end

    # Files
    file = Codestrap::Patch::File.new src
    if file.stat.file?
      # Read modeline
      if not File.binary?(src) and file.has_mode_line?
        # Templates
        @compiled.push({file: proj_path, src: src, dst: dst, type: file.template})
      else
        # Files
        @compiled.push({file: proj_path, src: src, dst: dst, type: :regular})
      end
      next
    end
  end
end