-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerate_phpdoc.rb
executable file
·295 lines (250 loc) · 7.91 KB
/
generate_phpdoc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env ruby
require 'vips'
# This file generates the phpdoc comments for the magic methods and properties.
# It's in Ruby, since we use the whole of gobject-introspection, not just the
# small bit exposed by php-vips-ext.
# Regenerate docs with something like:
#
# cd src
# ../examples/generate_phpdoc.rb
# gobject-introspection 3.0.7 crashes a lot if it GCs while doing
# something
GC.disable
Vips::init
# these have hand-written methods, don't autodoc them
$no_generate = %w(
bandjoin
bandrank
ifthenelse
add
subtract
multiply
divide
remainder
extract_area
)
# Find all the vips enums
$enums = []
Vips.constants.each do |name|
const = Vips.const_get name
if const.respond_to? :superclass and
const.superclass == GLib::Enum
$enums << name.to_s
end
end
# map Ruby type names to PHP type names
$to_php_map = {
"Vips::Image" => "Image",
"Array<Integer>" => "array",
"Array<Double>" => "array",
"Array<Image>" => "array",
"Integer" => "integer",
"gint" => "integer",
"guint64" => "integer",
"Double" => "float",
"gdouble" => "float",
"Float" => "float",
"String" => "string",
"Boolean" => "bool",
"gboolean" => "bool",
"Vips::Blob" => "string",
"gchararray" => "string",
"gpointer" => "string",
}
class Vips::Argument
def to_php
return $to_php_map[type] if $to_php_map.include?(type)
return type if $enums.include?(type)
# is there a "Vips" at the front of the type name? remove it and try the
# enums again
trim = type.to_s.tap{|s| s.slice!("Vips")}
return trim if $enums.include?(trim)
# is there a "::" at the front of the type name? remove it and try the
# enums again
trim.slice! "::"
return trim if $enums.include?(trim)
# no mapping found
puts "no mapping found for #{type}"
return ""
end
end
def generate_operation(file, op)
flags = op.flags
return if (flags & :deprecated) != 0
nickname = Vips::nickname_find op.gtype
return if $no_generate.include? nickname
all_args = op.get_args.select {|arg| not arg.isset}
# separate args into various categories
required_input = all_args.select do |arg|
(arg.flags & :input) != 0 and
(arg.flags & :required) != 0
end
optional_input = all_args.select do |arg|
(arg.flags & :input) != 0 and
(arg.flags & :required) == 0
end
required_output = all_args.select do |arg|
(arg.flags & :output) != 0 and
(arg.flags & :required) != 0
end
# required input args with :modify are copied and appended to
# output
modified_required_input = required_input.select do |arg|
(arg.flags & :modify) != 0
end
required_output += modified_required_input
optional_output = all_args.select do |arg|
(arg.flags & :output) != 0 and
(arg.flags & :required) == 0
end
# optional input args with :modify are copied and appended to
# output
modified_optional_input = optional_input.select do |arg|
(arg.flags & :modify) != 0
end
optional_output += modified_optional_input
# find the first input image, if any ... we will be a method of this
# instance
member_x = required_input.find do |x|
x.gtype.type_is_a? GLib::Type["VipsImage"]
end
if member_x != nil
required_input.delete member_x
end
file << " * @method "
file << "static " if not member_x
if required_output.length == 0
file << "void "
elsif required_output.length == 1
file << "#{required_output[0].to_php} "
else
file << "array("
file << required_output.map(&:to_php).join(", ")
file << ") "
end
file << "#{nickname}("
required_input.each do |arg|
file << "#{arg.to_php} $#{arg.name}, "
end
file << "array $options = []) "
file << "#{op.description.capitalize}.\n"
end
def generate_class(file, gtype)
begin
# can fail for abstract types
# can't find a way to get to #abstract? from a gtype
op = Vips::Operation.new gtype.name
rescue
op = nil
end
generate_operation(file, op) if op
gtype.children.each {|x| generate_class file, x}
end
preamble = <<EOF
<?php
/**
* This file was generated automatically. Do not edit!
*
* PHP version 7
*
* LICENSE:
*
* Copyright (c) 2016 John Cupitt
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @category Images
* @package Jcupitt\\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @version GIT:ad44dfdd31056a41cbf217244ce62e7a702e0282
* @link https://github.com/jcupitt/php-vips
*/
EOF
class_header = <<EOF
* @category Images
* @package Jcupitt\\Vips
* @author John Cupitt <jcupitt@gmail.com>
* @copyright 2016 John Cupitt
* @license https://opensource.org/licenses/MIT MIT
* @version Release:0.1.2
* @link https://github.com/jcupitt/php-vips
EOF
# The image class is the most complex
puts "Generating ImageAutodoc.php ..."
File.open("ImageAutodoc.php", "w") do |file|
file << preamble
file << "\n"
file << "namespace Jcupitt\\Vips;\n"
file << "\n"
$enums.each do |name|
file << "use Jcupitt\\Vips\\Enum\\#{name};\n"
end
file << "\n"
file << "/**\n"
file << " * Autodocs for the Image class.\n"
file << class_header
file << " *\n"
generate_class file, GLib::Type["VipsOperation"]
# extract_area is in there twice, once as "crop" ... do them by hand
file << <<EOF
* @method Image extract_area(integer $left, integer $top, integer $width, integer $height, array $options = []) Extract an area from an image.
* @method Image crop(integer $left, integer $top, integer $width, integer $height, array $options = []) Extract an area from an image.
*
EOF
# all magic properties
Vips::Image.properties.each do |name|
php_name = name.tr "-", "_"
p = Vips::Image.property name
file << " * @property #{$to_php_map[p.value_type.name]} "
file << "$#{php_name} #{p.blurb}\n"
end
file << <<EOF
*/
abstract class ImageAutodoc
{
}
?>
EOF
end
# generate all the enums
$enums.each do |name|
const = Vips.const_get name
puts "Generating #{name}.php ..."
File.open("#{name}.php", "w") do |file|
file << preamble
file << "\n"
file << "namespace Jcupitt\\Vips\\Enum;\n"
file << "\n"
file << "/**\n"
file << " * The #{name} enum.\n"
file << class_header
file << " */\n"
file << "abstract class #{name} {\n"
const.values.each do |value|
php_name = value.nick.tr "-", "_"
next if value.nick == "last"
file << " const #{php_name.upcase} = '#{php_name}';\n"
end
file << "}\n"
file << "?>\n"
end
end