summaryrefslogtreecommitdiff
path: root/lib/sitegen.rb
blob: c68737580b5583debb901a5e5a97ddfd8534359c (plain)
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
# coding: utf-8
require 'date'
require 'set'

module Sitegen
	def self.init
		@mk = {}
		@want = Set[]
	end
	def self.add(page)
		@deps = nil
		page.local_depends.keys.each do |filename|
			@mk[filename] = page unless filename.empty?
		end
	end
	def self.pages
		@mk.values.to_set
	end
	def self.want(filename)
		@deps = nil
		@want.add(filename)
	end
	def self.dependencies
		if @deps.nil?
			libfiles = Dir::entries('lib').select{|s|s!='..'}.map{|s|"lib/#{s}"}.to_set
			ret = {}
			ret[:all] = @want
			@want.each do |filename|
				ret[filename] = libfiles.clone.merge(@mk[filename].local_depends[filename])
			end
			@deps = ret
		end
		@deps
	end
	def self.Makefile()
		str = ''
		dependencies.each do |target, deps|
			str += "#{target.to_s}: #{deps.sort.join(' ')}\n"
		end
		return str
	end

	def self.make(target)
		newest = DateTime::new(0)
		(dependencies[target] || []).each do |dep|
			ts = dep.make(dep)
			newest = ts if ts > newest
		end
		unless target.is_a?(String)
			return DateTime::now
		end
		if File::exist?(target)
			ctime = File::ctime(target)
			if ctime > newest
				return ctime
			end
		end
		generate(target)
		return File::ctime(target)
	end

	def self.generate(target)
		# TODO
	end
end