File: dsl.rb

package info (click to toggle)
ruby-librarian 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 632 kB
  • sloc: ruby: 6,109; makefile: 11
file content (121 lines) | stat: -rw-r--r-- 3,009 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'librarian/dependency'
require 'librarian/dsl/receiver'
require 'librarian/dsl/target'

module Librarian
  class Dsl

    class Error < Exception
    end

    attr_accessor :environment
    private :environment=

    class << self

      def run(environment, specfile = nil, precache_sources = [], &block)
        new(environment).run(specfile, precache_sources, &block)
      end

    private

      def dependency(name)
        dependency_name = name
        dependency_type = Dependency
        singleton_class = class << self; self end
        singleton_class.instance_eval do
          define_method(:dependency_name) { dependency_name }
          define_method(:dependency_type) { dependency_type }
        end
      end

      define_method(:source_types) { [] }

      def source(options)
        name = options.keys.first
        type = options[name]
        types = source_types
        types << [name, type]
        singleton_class = class << self; self end
        singleton_class.instance_eval do
          define_method(:source_types) { types }
        end
      end

      define_method(:source_shortcuts) { {} }

      def shortcut(name, options)
        instances = source_shortcuts
        instances[name] = options
        singleton_class = class << self; self end
        singleton_class.instance_eval do
          define_method(:source_shortcuts) { instances }
        end
      end

      def delegate_to_class(*names)
        names.each do |name|
          define_method(name) { self.class.send(name) }
        end
      end

    end

    delegate_to_class :dependency_name, :dependency_type, :source_types, :source_shortcuts

    def initialize(environment)
      self.environment = environment
    end

    def default_specfile
      nil
    end

    def post_process_target(target)
      nil
    end

    def receiver(target)
      Receiver.new(target)
    end

    def run(specfile = nil, sources = [], &block)
      specfile, sources = nil, specfile if specfile.kind_of?(Array) && sources.empty?

      Target.new(self).tap do |target|
        target.precache_sources(sources)
        debug_named_source_cache("Pre-Cached Sources", target)

        specfile ||= block if block_given?

        if specfile.kind_of?(Pathname) and !File.exist?(specfile)
          debug { "Specfile #{specfile} not found, using defaults" } unless specfile.nil?
          receiver(target).run(specfile, &default_specfile)
        else
          receiver(target).run(specfile)
        end

        post_process_target(target)

        debug_named_source_cache("Post-Cached Sources", target)
      end.to_spec
    end

    def debug_named_source_cache(name, target)
      source_cache = target.source_cache
      debug { "#{name}:" }
      source_cache.each do |key, value|
        type = key[0]
        attributes = key[1...key.size]
        debug { "  #{key.inspect}" }
      end
    end

  private

    def debug(*args, &block)
      environment.logger.debug(*args, &block)
    end

  end
end