Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 257 lines (211 sloc) 6.027 kb
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
1 #===============================================================================
2 # post-setup.rb - setup documentation
3 #===============================================================================
d9b079a Initial revision
tim authored
4
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
5 EXAMPLES = '.examples'
6 STD_URI = 'http:\/\/www.imagemagick.org'
7 STD_URI_RE = /"#{STD_URI}/
8 DONT_RUN = ['fonts.rb'] # never run these examples
9 ENTITY = Hash['&' => '&amp;', '>' => '&gt;', '<' => '&lt;']
d9b079a Initial revision
tim authored
10
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
11 if defined?(ToplevelInstaller) && self.class == ToplevelInstaller
d9b079a Initial revision
tim authored
12
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
13 IMBASEURI = get_config('imdoc-base-uri')
14 RUBYPROG = get_config('ruby-prog')
15 SRCDIR = srcdir()
16 ALLOW_EXAMPLE_ERRORS = get_config('allow-example-errors') == 'yes'
d9b079a Initial revision
tim authored
17
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
18 else
d9b079a Initial revision
tim authored
19
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
20 IMBASEURI = 'file:///usr/local/share/ImageMagick'
21 RUBYPROG = 'ruby'
22 SRCDIR = '.'
23 ALLOW_EXAMPLE_ERRORS = true
d9b079a Initial revision
tim authored
24
25 end
26
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
27
28 #
29 # A set of example programs in a directory and the output image each example produces.
30 #
31
32 class ExampleSet
33 def initialize(of)
34 @status_quo = get_status_quo()
35 @errs = 0
d9b079a Initial revision
tim authored
36 begin
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
37 File.open(EXAMPLES) do |f|
38 @targets = Marshal.load(f)
39 end
40 rescue
41 @targets = Hash.new
42 @n = 0
43 @of = of
44 @first_time = true
45 else
46 @first_time = false
47 end
48 end
49
50 def persist
51 File.open(EXAMPLES, 'w') do |f|
52 Marshal.dump(@targets, f)
53 end
54 end
55
56 def targets(example)
57 @targets[example] || Array.new
58 end
59
60 def get_status_quo
61 sq = Dir["*"]
62 sq.delete_if { |entry| File.directory?(entry) }
63 end
64
65 def update_targets(example, new)
66 t = targets(example) + new
67 @targets[example] = t.uniq
68 end
69
70 def update_status_quo(example)
71 new_status_quo = get_status_quo()
72 new = new_status_quo - @status_quo
73 update_targets(example, new)
74 @status_quo = new_status_quo
75 end
76
77 def build(example)
78 cmd = "#{RUBYPROG} -I #{SRCDIR}/lib -I #{SRCDIR}/ext/RMagick #{example}"
79 print cmd
80 print " (example #{@n += 1} of #{@of})" if @first_time
81 puts
82 system cmd
83
84 if $? != 0 then
85 puts("post-setup.rb: #{example} example returned error code #{$?}")
86 @errs += 1 unless ALLOW_EXAMPLE_ERRORS
87 if @errs > 4
88 err(<<-END_EXFAIL
89 Too many examples failed. The RMagick documentation cannot be installed
90 successfully. Consult the README.txt file and try again, or send email
d495765 Replace personal email address with rmagick at rubyforge.org
rmagick authored
91 to rmagick@rubyforge.org.
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
92 END_EXFAIL
93 )
94 end
95 end
96
97 update_status_quo(example)
98 end
99
100 def update(example)
101 targets = targets(example)
102 up_to_date = ! targets.empty?
103 targets.each do |target|
104 up_to_date &&= File.exists?(target) && (File.stat(target) >= File.stat(example))
d9b079a Initial revision
tim authored
105 end
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
106 build(example) unless up_to_date
107 end
108
109 end
110
111 #
112 # print message and exit
113 #
114 def err(msg)
115 $stderr.puts "#{$0}: #{msg}"
116 exit
d9b079a Initial revision
tim authored
117 end
118
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
119
120 #
121 # Modify file lines via proc. If no lines changed, discard new version.
122 #
123 def filter(filename, backup=nil, &filter)
124 if ! File.writable? filename then
125 raise ArgumentError, "`#{filename}' is write-protected"
126 end
127
128 backup_name = filename + '.' + (backup || 'old')
129 File.rename(filename, backup_name)
130 changed = false
131 begin
132 File.open(filename, 'w') do |output|
133 File.foreach(backup_name) do |line|
134 old = line
135 line = filter.call(line)
136 output.puts(line)
137 changed ||= line != old
138 end
139 end
140 rescue
141 File.rename(backup_name, filename)
142 raise
143 end
144
145 if !changed
146 newname = filename + '.xxx'
147 File.rename(filename, newname)
148 File.rename(backup_name, filename)
149 File.unlink(newname)
150 elsif !backup
151 # Don't remove old copy if a backup extension was specified
152 File.unlink(backup_name) rescue nil
153 end
d9b079a Initial revision
tim authored
154 end
155
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
156
157 #
158 # Copy an example to the doc directory, wrap in HTML tags
159 #
160 def filetoHTML(file, html)
161 return if File.exists?(html) && File.stat(html) >= File.stat(file)
162
163 File.open(file) do |src|
164 File.open(html, 'w') do |dest|
165 dest.puts <<-END_EXHTMLHEAD
f2b76b2 Use new XHTML when generating example script HTML files
rmagick authored
166 <!DOCTYPE public PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
167 <html xmlns="http://www.w3.org/1999/xhtml">
168 <head>
169 <meta name="generator" content="ex2html.rb" />
170 <meta http-equiv="Content-Type" content=
171 "text/html; charset=us-ascii" />
172 <link rel="stylesheet" type="text/css" href="css/popup.css" />
173
174 <title>RMagick example: #{file}</title>
175 </head>
176
177 <body>
178 <h1>#{file}</h1>
179 <div class="bodybox">
180 <div class="bodyfloat">
181 <pre>
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
182 END_EXHTMLHEAD
183
184 src.each do |line|
185 line.gsub!(/[&><]/) { |s| ENTITY[s] }
186 dest.puts(line)
187 end
188
189 dest.puts <<-END_EXHTMLTAIL
f2b76b2 Use new XHTML when generating example script HTML files
rmagick authored
190 </pre>
191 </div>
192 </div>
193 <div id="close"><a href="javascript:window.close();">Close window</a></div>
194 </body>
195 </html>
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
196 END_EXHTMLTAIL
d9b079a Initial revision
tim authored
197 end
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
198 File.chmod(0644, html)
199 end
d9b079a Initial revision
tim authored
200
201 end
202
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
203 puts "install.rb: entering post-setup phase..."
d9b079a Initial revision
tim authored
204
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
205 #
206 # Don't bother if we're in the sandbox
207 #
a8aea27 Add test for CVS sandbox
tim authored
208 if File.exists? 'CVS/Entries'
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
209 puts "post-setup.rb: in CVS sandbox - stopping..."
a8aea27 Add test for CVS sandbox
tim authored
210 exit
211 end
212
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
213 puts "post-setup.rb: setting up documentation..."
214
215
216 # We're in the source directory. Process the doc in-place. The post-install.rb
00bae67 Minor changes
tim authored
217 # script moves the generated documentation to the ultimate installation directories.
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
218
219
220 cwd = Dir.getwd()
221 Dir.chdir('doc')
222 begin
223
224 # Step 1: replace www.imagemagick.org with local doc uri
225 unless IMBASEURI == STD_URI
226 files = Dir['*.html']
227 files.delete_if { |file| /\.rb\.html\z/.match(file) }
228 files.each do |file|
229 filter(file) { |line| line.gsub(STD_URI_RE, "\"#{IMBASEURI}") }
230 end
231 end
232
233 # Step 2A: edit the shebang line in the examples
234 Dir.chdir('ex')
235 files = Dir['*.rb']
236 files.each do |file|
00bae67 Minor changes
tim authored
237 filter(file) { |line| line.sub(/\A\#!\s*\S*ruby\s/, '#!'+RUBYPROG+' ') }
e9c83e9 Re-write - don't rebuild things that are up to date.
tim authored
238
239 # Step 2B: Make a copy of the example as HTML in the doc directory
240 filetoHTML(file, "../#{file}.html")
241 end
242
243 # Step 3: run the examples
244 examples = Dir['*.rb'].sort
245 examples -= DONT_RUN
246 es = ExampleSet.new(examples.length)
247 begin
248 examples.each { |example| es.update(example) }
249 ensure
250 es.persist
251 end
252
253 ensure
254 Dir.chdir(cwd)
255 end
d9b079a Initial revision
tim authored
256
Something went wrong with that request. Please try again.