Fix C# build break
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / property-wrapper.rb
1 #!/usr/bin/env ruby
2 require 'pathname'
3 require 'scanf'
4
5 # Script does the following:
6 # - greps dali-core for DALI_PROPERTY macro which holds all the information about a property ( type, read only etc)
7 # - uses the filename of the macro to detect the class the properties belong to. E.g. actor-impl.cpp  = Actor
8 # - Scans each property macro and builds a list of DALi classes with an array of properties
9 # - Generates the csharp get/set  code for each property
10 # - Pastes the property get / set code into the DALi csharp files
11
12 # Given a DALi C++ property type this table stores the
13 # information needed to produce a csharp getter / setter
14 $typeTable =  [
15         ["BOOLEAN",     "bool",             "ref",  "bool temp = false;"],
16         ["FLOAT",       "float",            "ref",  "float temp = 0.0f;"],
17         ["INTEGER",     "int",              "ref",  "int temp = 0;"],
18         ["VECTOR2",     "Vector2",          "",     "Vector2 temp = new Vector2(0.0f,0.0f);"],
19         ["VECTOR3",     "Vector3",          "",     "Vector3 temp = new Vector3(0.0f,0.0f,0.0f);"],
20         ["VECTOR4",     "Vector4",          "",     "Vector4 temp = new Vector4(0.0f,0.0f,0.0f,0.0f);"],
21         ["MATRIX3",     "Matrix3",          "",     "Matrix3 temp = new Matrix3();"],
22         ["MATRIX",      "Matrix",           "",     "Matrix temp = new Matrix();"  ],
23         ["RECTANGLE",   "RectInteger",      "",     "RectInteger temp = new RectInteger(0,0,0,0);"],
24         ["ROTATION",    "Quaternion",       "",    "Quaternion temp = new Quaternion();"],
25         ["STRING",      "string",           "out",  "string temp;"],
26         ["ARRAY",       "Dali.Property.Array",   "",     "Dali.Property.Array temp = new Dali.Property.Array();"],
27         ["MAP",         "Dali.Property.Map",     "",     "Dali.Property.Map temp = new Dali.Property.Map();"],
28     ]
29
30 # Some csharp classes are renamed ( e.g. C++ Control is called View in C#)
31 $renameMap = [
32               ["Control", "View"]
33              ]
34
35 $daliSwigPath = String.new;
36
37 def getCSharpName( cppClassName )
38
39     entry = $renameMap.select{ |a| a.first == cppClassName }
40     if( entry.empty?)
41       return cppClassName
42     end
43     return entry[0][1]
44 end
45
46 # use the table above to get information for a specific type
47 def getCSharpType( type )
48
49     entry = $typeTable.select{ |a| a.first == type }
50     if( entry.empty? )
51       return nil
52     end
53     return entry[0]
54 end
55
56
57 # Property struct stores the information about a property after parsing the C++ DALI_PROPERTY macro
58 $propertyStruct = Struct.new("Property", :name, :type, :writable, :animatable,:constrainInput, :enum, :shortenum, :develAPI, :csharpGetter, :csharpSetter, :childProperty,)
59
60 # daliClass struct stores a class name and an array of properties
61 $daliClassStruct = Struct.new("DaliClass", :name, :properties )
62
63 # class array stores all the dali classes ( actor, image etc)
64 $daliClassArray = Array.new
65
66 # list of files not generated by swig that we have tried to inject properties into
67 $filesNotWrapped= Array.new
68
69 # stats
70 $totalProperties = 0
71 $totalDaliClasses = 0
72
73 # global paths
74 $rootPath = ""
75 $daliCorePath = ""
76 $daliSwigPath = ""
77
78 # Extracts data  DALI__PROPERTY( "points", ARRAY,true,false,false,Dali::Path::Property::POINTS )
79 def extractPropertyInfo( propertyMacro )
80
81     # want to extract the property name, type + read only status
82     # split the DALI_PROPERTY macro definition by comma and quotes, and delete any empty segments
83     data = propertyMacro.split(/[\s,"]/).reject { |s| s.empty? }
84
85     propertyName = data[1]
86
87     # e.g. turn viewMatrix into ViewMatrix
88     propertyName[0] = propertyName[0].capitalize
89
90     # extract the property enum name Dali::Path::Property::POINTS -> POINTS
91     shortenum =  data[6].split(":").last
92
93     develAPI = false;
94     # Check if the property uses devel API
95     # Currently we ignore devel properties for now
96     if data[6].include? "Devel"
97       develAPI = true;
98       puts("Ignoring DEVEL API property: " + shortenum);
99     end
100
101
102
103     # store the :name, :type, :writable, :animatable, :constrainInput, :enum
104     property = $propertyStruct.new;
105
106     property.name = propertyName
107     property.type = data[2]
108     property.writable = (data[3]=="true")
109     property.animatable = (data[4] == "true")
110     property.constrainInput = (data[5]=="true")
111     property.enum = shortenum
112     property.develAPI = develAPI;
113     return property;
114 end
115
116 # Extracts data from Toolkit property definition
117 def extractToolkitPropertyInfo( propertyMacro )
118
119     # Extract the property name, type
120     property = $propertyStruct.new;
121
122     # Split the macro definition by comma and quotes, close bracket and delete any empty segments
123     data = propertyMacro.split(/[\s,")]/).reject { |s| s.empty? }
124
125     if(data[1] == "PropertyRegistration")
126
127       # Properties defined in Control using PropertyRegistration
128       # const PropertyRegistration Control::Impl::PROPERTY_1(typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty);
129
130       # Creates an array of strings that looks like this:
131       # const 0
132       # PropertyRegistration 1
133       # Control::Impl::PROPERTY_1 2
134       # typeRegistration 3
135       # styleName 4
136       # Toolkit::Control::Property::STYLE_NAME 5
137       # Property::STRING 6
138       # &Control::Impl::SetProperty 7
139       # &Control::Impl::GetProperty 8
140       #
141
142       property.name = data[4]
143
144       propertyType = data[6].rpartition("::")
145       property.type = propertyType[2]
146
147       propertyEnum = data[5].rpartition("::")
148       property.enum = propertyEnum[2]
149
150     else
151
152       # Properties defined in macro DALI_PROPERTY_REGISTRATION or DALI_ANIMATABLE_PROPERTY_REGISTRATION or DALI_CHILD_PROPERTY_REGISTRATION
153       # or DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT:
154       # DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE)
155       # DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA)
156
157       # Creates an array of strings that looks like this:
158       # DALI_PROPERTY_REGISTRATION( 0
159       # Toolkit 1
160       # PageTurnView 2
161       # pageSize 3
162       # VECTOR2 4
163       # PAGE_SIZE 5
164       #
165
166       property.name = data[3]
167
168       #puts property.name
169       if property.name == "image"
170         property.name = "imageMap"
171       end
172
173       if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(" )
174         # TODO: Need to work out the property type from the value
175         property.type = "VECTOR4"
176       else
177         property.type = data[4]
178       end
179
180       property.enum = data[data.length-1]
181
182     end
183
184     # e.g. turn styleName into StyleName
185     property.name[0] = property.name[0].capitalize
186
187     property.writable = true
188     property.animatable = false
189     property.constrainInput = false
190     property.childProperty = false;
191
192     # check to see if it's a child property
193     if( data[0] == "DALI_CHILD_PROPERTY_REGISTRATION(" )
194       #puts(" #{property.name} is child property ")
195       property.childProperty = true;
196     end
197     if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION(" )
198       #puts("  #{property.name} is animatable")
199       property.animatable = true;
200     end
201
202     return property;
203 end
204
205 def writePropertiesToCSharpFile( daliClass )
206
207    # open the CSharp file autogenerated by SWIG
208   swigFiles =  $daliSwigPath + "/automatic/csharp/"
209
210   # some C++ classes are renamed for C#
211   className = getCSharpName( daliClass.name )
212
213   fileName =(swigFiles + className ) + ".cs"
214
215
216   # it's possible some classes in dali-core aren't being wrapped by swig, so if the swig generated file
217   # doesn't exist just return
218   if( ! File.exist?(fileName) )
219     $filesNotWrapped.push("#{daliClass.name}.cs ")
220     return
221   end
222
223   File.open(fileName, 'r+') do |file|
224
225   last_line  =0
226   file.each { last_line = file.pos unless file.eof? }
227
228   # we seek to the end of the file... minus 3 characters which lets us overwrite the 2 closing brackets
229   # so we can insert the getter/setter stuff into the file.
230   file.seek( last_line-3, IO::SEEK_SET)
231
232   for property in daliClass.properties
233
234     if( (!property.childProperty) && (!property.develAPI))
235       file.write( property.csharpGetter );
236       file.write( property.csharpSetter );
237     end
238
239   end
240
241   file.write("\n}\n\n}");  # re-insert the closing brackets we over-wrote
242   end
243
244   puts("Injected #{daliClass.properties.length} C# Properties from #{daliClass.name} into #{className}.cs".blueBackground)
245
246 end
247
248 def writeChildPropertiesToCSharpFile( daliClass )
249
250   # open the CSharp file autogenerated by SWIG
251   swigFiles =  $daliSwigPath + "/automatic/csharp/"
252
253   # Add all the child properties to Control
254   fileName = (swigFiles+"View") + ".cs"
255
256   if( ! File.exist?(fileName) )
257     return
258   end
259
260   File.open(fileName, 'r+') do |file|
261
262   last_line  =0
263   file.each { last_line = file.pos unless file.eof? }
264
265   # we seek to the end of the file... minus 3 characters which lets us overwrite the 2 closing brackets
266   # so we can insert the getter/setter stuff into the file.
267   file.seek( last_line-3, IO::SEEK_SET)
268
269   $childPropertyCount = 0
270
271   for property in daliClass.properties
272
273     if (property.childProperty)
274       file.write( property.csharpGetter );
275       file.write( property.csharpSetter );
276       $childPropertyCount += 1
277     end
278
279   end
280
281   file.write("\n}\n\n}");  # re-insert the closing brackets we over-wrote
282   end
283
284   puts("Injected #{$childPropertyCount} C# Child Properties into #{"View"}.cs".blueBackground)
285
286 end
287
288 # Write the CSharp data to the generated .cs file
289 def writeCSharpData
290
291     for daliClass in  $daliClassArray
292
293         #puts ( daliClass.name )
294
295
296         hasChildProperties = false
297
298         for property in daliClass.properties
299             propertyInfo = getCSharpType( property.type )
300
301             if( propertyInfo.length() < 2 )
302                 # some types aren't supported yet like Rotation
303                 next
304             end
305
306             $totalProperties+=1  #  keep track of total
307
308             propertyType = propertyInfo[1]    # e.g. bool or int
309             propertyArg =  propertyInfo[2]  # e.g. ref or out
310             tempDeclaration = propertyInfo[3] # e.g. bool temp;
311
312             csharpClassName = getCSharpName(daliClass.name);
313
314
315             propertyName = "#{csharpClassName}.Property.#{property.enum}"
316
317             if property.childProperty
318               propertyName = "#{csharpClassName}.ChildProperty.#{property.enum}"
319               hasChildProperties = true
320             end
321
322             property.csharpGetter ="  public #{propertyType} #{property.name} \n"\
323                      "  { \n"\
324                      "    get \n" \
325                      "    {\n"\
326                      "      #{tempDeclaration}\n"\
327                      "      GetProperty( #{propertyName}).Get( #{propertyArg} temp );\n"\
328                      "      return temp;\n"\
329                      "    }\n"
330
331             if property.writable
332                   #text.SetProperty(TextLabel.Property.HORIZONTAL_ALIGNMENT, new Property.Value("CENTER"));
333                   property.csharpSetter = "    set \n" \
334                          "    { \n"\
335                          "      SetProperty( #{propertyName}, new Dali.Property.Value( value ) );\n" \
336                          "    }\n"\
337                          "  }\n"
338             else
339                    property.csharpSetter = "}"  # close the opening property declaration
340             end
341         end
342         # write normal properties to the class's own csharp file
343         writePropertiesToCSharpFile( daliClass )
344         # write child properties to View.cs ( on Control has child properties)
345         if (hasChildProperties)
346           writeChildPropertiesToCSharpFile( daliClass )
347         end
348     end
349
350 end
351
352 def getDaliClassItem( className )
353
354    # puts( "getDaliClassItem  "+ className )
355     index = $daliClassArray.index{ |a| a.name == className }
356
357     if( index == nil)
358         # create a new item along with a array for it's properites
359         classItem = $daliClassStruct.new( className, Array.new )
360         $daliClassArray.push( classItem )
361         $totalDaliClasses+=1  # for stats
362     else
363         # puts("class found " + className )
364         classItem = $daliClassArray[ index ]
365     end
366
367     return classItem;
368
369 end
370
371
372
373 def init
374
375     pn = Pathname.new(Dir.pwd)
376     fullPath = pn.to_s
377
378     $rootPath = fullPath.slice(0..( fullPath.index('/dali-toolkit')))
379     $daliCorePath = $rootPath + "dali-core/dali"   # source code path
380     $daliSwigPath = $rootPath + "dali-toolkit/plugins/dali-swig"
381     $daliToolkitPath = $rootPath + "dali-toolkit/dali-toolkit"  # source code path
382
383     puts("--------------------------------------------")
384     puts("Injecting DALi properties into SWIG generated C# files ")
385     puts("")
386
387
388 end
389
390 def writeDaliCoreProperties
391
392     puts("Scanning for DALI_PROPERTY macro in dali-core");
393     puts("Scanning folder: #{$daliCorePath}\n\n");
394
395     # Executed a recursive grep over dali-core for the DALI_PROPERTY macro
396     result =`grep --include \\*.cpp  -r "DALI_PROPERTY( \" #{$daliCorePath}`
397
398
399     # We now have a list of lines that look like this:
400     # dali/internal/event/animation/path-impl.cpp:DALI__PROPERTY( "points", ARRAY,true,false,false,Dali::Path::Property::POINTS )
401
402     lines = result.split(/\n+/);
403     for line in lines
404
405
406       # Split the line into file name and property macro, splt 2 means just create two strings as we don't want to split
407       # property Dali::Path::Property::POINTS string as well
408
409       data  = line.split(":",2)
410       fileName = data[0];
411       macro = data[1];
412
413       # Get the class name from the filename  ( e.g. image-actor-impl.cpp => image-actor)
414       className = File.basename(fileName,"-impl.cpp").capitalize
415
416       # convert it from image-actor to ImageActor
417       className = className.split(/ |\_|\-/).map(&:capitalize).join
418
419       # Get the property information ( name, type, read/writeable)
420       propertyInfo = extractPropertyInfo( macro );
421
422       # get or create a new DALi class item which stores the property information
423       classItem  = getDaliClassItem( className )
424
425       classItem.properties.push( propertyInfo )
426
427     end
428
429     writeCSharpData()
430 end
431
432 def writeDaliToolkitProperties
433
434
435     puts("\nScanning for PROPERTY_REGISTRATION macros in dali-toolkit");
436     puts("Scanning folder: #{$daliToolkitPath}\n\n");
437
438     $daliClassArray.clear;
439
440     # Executed a recursive grep over dali-toolkit for following macros
441     # DALI_PROPERTY_REGISTRATION
442     # DALI_ANIMATABLE_PROPERTY_REGISTRATION
443     # DALI_CHILD_PROPERTY_REGISTRATION
444     result =`grep --include \\*.cpp  -w 'Control::Impl::SetProperty\\|DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(\\|DALI_CHILD_PROPERTY_REGISTRATION(\\|DALI_ANIMATABLE_PROPERTY_REGISTRATION(\\|DALI_PROPERTY_REGISTRATION' -r #{$daliToolkitPath}`
445
446     if( result == "" )
447       puts("Error parsing #{$daliToolkitPath} no properties found")
448       return
449     end
450     # create an array to store each DALi class and it's assoc
451     classArray = Array.new
452
453     # We now have a list of lines that look like this:
454     # text-controls/text-label-impl.cpp:DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
455     lines = result.split(/\n+/);
456     for line in lines
457
458
459       # Split the line into file name and property macro, split 2 means just create two strings
460       data  = line.split(":",2)
461       fileName = data[0];
462       macro = data[1];
463
464       # Get the class name from the filename  ( e.g. image-actor-impl.cpp => image-actor)
465       className = File.basename(fileName,"-impl.cpp").capitalize
466
467       # convert it from image-actor to ImageActor
468       className = className.split(/ |\_|\-/).map(&:capitalize).join
469
470       #puts(className);
471       #puts(fileName);
472
473         # Get the property information ( name, type, read/writeable)
474       propertyInfo = extractToolkitPropertyInfo( macro );
475
476       # get or create a new DALi class item which stores the property information
477       classItem  = getDaliClassItem( className )
478
479       classItem.properties.push( propertyInfo )
480
481     end
482
483     writeCSharpData()
484
485 end
486
487 # helper class to color the background
488 class String
489 def blueBackground;   "\e[44m#{self}\e[0m" end
490 end
491
492 def writeStats
493
494   puts("\nFiles that have not been wrapped file by SWIG ( not included in dali.i file):")
495   for i in $filesNotWrapped
496     puts(i)
497   end
498
499   puts("Done. Injected #{$totalProperties} properties into #{$totalDaliClasses} DALi C# classes".blueBackground)
500
501 end
502
503 init()
504
505 writeDaliCoreProperties()
506
507 writeDaliToolkitProperties()
508
509 writeStats()
510