Class: Pith::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/pith/project.rb,
lib/pith/plugins/publication/project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_dir, output_dir = nil, attributes = {}) ⇒ Project

Returns a new instance of Project.



13
14
15
16
17
18
19
20
21
22
# File 'lib/pith/project.rb', line 13

def initialize(input_dir, output_dir = nil, attributes = {})
  @input_dir = Pathname(input_dir)
  @output_dir = output_dir ? Pathname(output_dir) : (@input_dir + "_out")
  @input_map = {}
  @output_map = {}
  attributes.each do |k,v|
    send("#{k}=", v)
  end
  FileUtils.rm_rf(output_dir.to_s)
end

Instance Attribute Details

#input_dirObject (readonly)

Returns the value of attribute input_dir.



24
25
26
# File 'lib/pith/project.rb', line 24

def input_dir
  @input_dir
end

#loggerObject



101
102
103
# File 'lib/pith/project.rb', line 101

def logger
  @logger ||= Logger.new(nil)
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



25
26
27
# File 'lib/pith/project.rb', line 25

def output_dir
  @output_dir
end

Instance Method Details

#buildObject

Public: build the project, generating output files.



67
68
69
70
71
72
# File 'lib/pith/project.rb', line 67

def build
  sync
  output_dir.mkpath
  outputs.each(&:build)
  output_dir.touch
end

#configObject



109
110
111
# File 'lib/pith/project.rb', line 109

def config
  config_provider.config
end

#config_providerObject



105
106
107
# File 'lib/pith/project.rb', line 105

def config_provider
  @config_provider ||= Pith::ConfigProvider.new(self)
end

#has_errors?Boolean

Public: check for errors.

Returns true if any errors were encountered during the last build.

Returns:

  • (Boolean)


93
94
95
# File 'lib/pith/project.rb', line 93

def has_errors?
  outputs.any?(&:error)
end

#input(path) ⇒ Object

Public: find an input.

path - an path relative to input_dir

Returns the first input whose path matches. Returns nil if no match is found.



50
51
52
# File 'lib/pith/project.rb', line 50

def input(path)
  @input_map[Pathname(path)]
end

#inputsObject

Public: get inputs

Returns Pith::Input objects representing the files in the input_dir.



31
32
33
# File 'lib/pith/project.rb', line 31

def inputs
  @input_map.values
end

#last_built_atObject



97
98
99
# File 'lib/pith/project.rb', line 97

def last_built_at
  output_dir.mtime
end

#output(path) ⇒ Object

Public: find an output.

path - an path relative to output_dir

Returns the first output whose path matches. Returns nil if no match is found.



61
62
63
# File 'lib/pith/project.rb', line 61

def output(path)
  @output_map[Pathname(path)]
end

#outputsObject

Public: get outputs

Returns Pith::Output objects representing the files in the output_dir.



39
40
41
# File 'lib/pith/project.rb', line 39

def outputs
  inputs.map(&:output).compact
end

#published_inputsObject

Return all the published inputs, in order of publication.



10
11
12
# File 'lib/pith/plugins/publication/project.rb', line 10

def published_inputs
  inputs.select { |i| i.published? }.sort_by { |i| i.published_at }
end

#syncObject

Public: re-sync with the file-system.



76
77
78
79
# File 'lib/pith/project.rb', line 76

def sync
  config_provider.sync
  sync_input_files
end

#sync_every(period) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/pith/project.rb', line 81

def sync_every(period)
  @next_sync ||= 0
  now = Time.now.to_i
  if now >= @next_sync
    sync
    @next_sync = now + period
  end
end