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.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pith/project.rb', line 15

def initialize(input_dir, output_dir = nil, attributes = {})
  @input_dir = Pathname(input_dir)
  @output_dir = output_dir ? Pathname(output_dir) : (@input_dir + "_out")
  @logger = Logger.new(nil)
  attributes.each do |k,v|
    send("#{k}=", v)
  end
  @input_map = {}
  @output_map = {}
  @mtimes ||= {}
  @config_provider ||= Pith::ConfigProvider.new(self)
  @mutex = Mutex.new
end

Instance Attribute Details

#config_providerObject (readonly)

Returns the value of attribute config_provider.



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

def config_provider
  @config_provider
end

#input_dirObject (readonly)

Returns the value of attribute input_dir.



29
30
31
# File 'lib/pith/project.rb', line 29

def input_dir
  @input_dir
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



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

def output_dir
  @output_dir
end

Instance Method Details

#buildObject

Public: build the project, generating output files.



72
73
74
75
76
77
# File 'lib/pith/project.rb', line 72

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

#configObject



111
112
113
# File 'lib/pith/project.rb', line 111

def config
  config_provider.config
end

#has_errors?Boolean

Public: check for errors.

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

Returns:

  • (Boolean)


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

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.



55
56
57
# File 'lib/pith/project.rb', line 55

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

#inputsObject

Public: get inputs

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



36
37
38
# File 'lib/pith/project.rb', line 36

def inputs
  @input_map.values
end

#last_built_atObject



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

def last_built_at
  output_dir.mtime
end

#listen_for_changesObject

Public: start a Thread to automatically sync when inputs change.



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

def listen_for_changes
  Listen.to(input_dir.to_s) do
    sync
  end.start
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.



66
67
68
# File 'lib/pith/project.rb', line 66

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

#outputsObject

Public: get outputs

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



44
45
46
# File 'lib/pith/project.rb', line 44

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.



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

def sync
  @mutex.synchronize do
    config_provider.sync
    sync_input_files
    cleanup_output_files
  end
end