/
project.rb
86 lines (74 loc) · 1.92 KB
/
project.rb
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
# Class: Project (ProScribe)
# A project.
#
# ## Internal usage
#
# p = Project.new(config_file)
#
module ProScribe
class Project
# Attribute: config (ProScribe::Project)
# Returns a hash of the project's configuration, pulled from Scribefile.
#
attr_reader :config
def initialize(config_file)
@root = File.dirname(config_file)
@config = Hashie::Mash.new(YAML::load_file(config_file))
# Defaults
@config.manual ||= '.'
@config.output ||= 'doc'
@config.files ||= Array.new
end
# Attribute: root (ProScribe::Project)
# Returns the root path.
#
# ## Usage
# project.root
# project.root(*args)
#
def root(*a)
File.join(@root, *a)
end
# Attribute: manual_path (ProScribe::Project)
# Returns the absolute path to the projects's manual.
#
def manual_path
root(@config.manual)
end
# Method: make (ProScribe::Project)
# Creates the temp dir from the project's manual and inline comments.
#
def make
dir
# Copy the files over
copy_files ProScribe.root('data/default/'), dir
copy_files manual_path, dir
# Extract block comments
config.files.each do |group|
ex = ProScribe::Extractor.new Dir[root(group.source)]
ex.write! File.join(dir, group.target)
end
end
# Attribute: dir (ProScribe::Project)
# Returns the path to the temporary Proton project.
#
def dir(*a)
@dir ||= begin
dir = File.join(Dir.tmpdir, File.basename(root))
FileUtils.rm_rf dir
FileUtils.mkdir_p dir
dir
end
File.join(@dir, *a)
end
private
def copy_files(from, to)
Dir["#{from}/**/*"].each do |f|
next unless File.file?(f)
target = File.join(to, f.gsub(from, ''))
FileUtils.mkdir_p File.dirname(target)
FileUtils.cp f, target
end
end
end
end