Skip to content

HTTPS clone URL

Subversion checkout URL

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