Class: Prick::Build::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/prick/builder/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, step: false) ⇒ Builder

Returns a new instance of Builder.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/prick/builder/builder.rb', line 63

def initialize(conn, path, clean = true, single: false, touched: false, load_pool: true, step: false)
  File.exist?(path) or raise Error, "Can't find #{path}"
  single || File.directory?(path) or raise Error, "Can't find directory #{path}"

  @conn = conn
  @path = path
  @reflections_file = Prick.state.reflections_file
  @clean = clean
  @single = single
  @step = step
  @root = Parser.parse(conn, path, single: single)
  @pool = nil # Initialized by #load_pool
  @batches = nil # Initialized by #create_batches
  self.load_pool if load_pool
end

Instance Attribute Details

#cleanObject

True if database is initially clean - ie. all tables are empty



40
41
42
# File 'lib/prick/builder/builder.rb', line 40

def clean
  @clean
end

#connObject (readonly)

PgConn object



28
29
30
# File 'lib/prick/builder/builder.rb', line 28

def conn
  @conn
end

#dirObject (readonly)

Schema directory



31
32
33
# File 'lib/prick/builder/builder.rb', line 31

def dir
  @dir
end

#pathObject (readonly)

Path to file or directory



34
35
36
# File 'lib/prick/builder/builder.rb', line 34

def path
  @path
end

#poolObject (readonly)

Pool of nodes. Initialized by #load_pool



52
53
54
# File 'lib/prick/builder/builder.rb', line 52

def pool
  @pool
end

#reflections_fileObject (readonly)

Reflections YAML file



37
38
39
# File 'lib/prick/builder/builder.rb', line 37

def reflections_file
  @reflections_file
end

#rootObject (readonly)

Root build node



43
44
45
# File 'lib/prick/builder/builder.rb', line 43

def root
  @root
end

#singleObject (readonly)

True if this is a single-file build



46
47
48
# File 'lib/prick/builder/builder.rb', line 46

def single
  @single
end

#stepObject (readonly)

True if SQL entries should be single-stepped. Default false



49
50
51
# File 'lib/prick/builder/builder.rb', line 49

def step
  @step
end

Instance Method Details

#batchesObject



61
# File 'lib/prick/builder/builder.rb', line 61

def batches() @batches || create_batches end

#create_batchesObject

Group sources into batches



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/prick/builder/builder.rb', line 85

def create_batches
  @batches = []
  kind = nil
  batch = nil

  # TODO: Refactor
  for node in [init_nodes, decl_nodes, fox_seed_nodes, sql_seed_nodes, term_nodes.reverse].flatten
    # Create new batch if required. Order of when-clauses is important
    case node.kind
      when :module
        if batch&.kind != :module
          @batches << batch if batch
          batch = ModuleBatch.new(self)
        end
      when :exe # Exe sources always create a new batch
        @batches << batch if batch
        batch = SqlBatch.new(self)
      when batch&.kind # Same kind as current batch. After this when-clause
                       # we know that the node kind has changed
        if node.kind == :sql && step
          @batches << batch if batch
          batch = SqlBatch.new(self)
        elsif node.kind == :psql && step
          @batches << batch if batch
          batch = SqlBatch.new(self)
        end
      when :sql || node.kind == :inline
        if batch&.kind != :exe || step
          @batches << batch if batch
          batch = SqlBatch.new(self)
        end
      when :psql
        @batches << batch if batch
        batch = PSqlBatch.new(self)
      when :inline
        @batches << batch if batch
        batch = SqlBatch.new(self)
      when :fox
        @batches << batch if batch
        batch = FoxBatch.new(self)
      when :yml # build.yml files
        next
    else
      raise Error, "Unexpected node kind: #{node.kind}"
    end

    # Add node to current batch
    batch.nodes << node
  end

  # Add last batch
  @batches << batch if batch

  @batches
end

#dumpObject



168
169
170
171
# File 'lib/prick/builder/builder.rb', line 168

def dump
  load_pool if pool.nil?
  batches ? batches.each(&:dump) : pool.dump
end

#execute(conn, create_schemas: schemas) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/prick/builder/builder.rb', line 141

def execute(conn, create_schemas: schemas)
  # Load pool
  load_pool if pool.nil?

  # Group batches
  create_batches if batches.nil?

  # Register build in database
  Prick.state.save_build_begin

  # Create schemas
  conn.exec create_schemas.map { |schema| "create schema #{schema}" } if !single

  # Execute batch groups
  t0 = Time.now
#       for batch in batches
#         puts "Executing #{batch.class} batch"
#         batch.execute
#       end
  batches.each(&:execute)
  t1 = Time.now

  # Register build result in database
  dt = t1 - t0
  Prick.state.save_build_end(true, dt)
end

#load_poolObject



79
80
81
82
# File 'lib/prick/builder/builder.rb', line 79

def load_pool
  @pool = NodePool.new
  load_pool_impl(@root)
end