7 # DALi traditonally use a static New function to create an object.
8 # E.g. Actor myActor = Actor::New();
10 # However it has been request that for the CSharp DALi API we this coding convention
12 # Actor myActor = new Actor();
14 # Script does the follow:
16 # - greps dali csharp wrapper files for the class constructor (new Actor()) and the static New() e.g. functions ( TextLabel::New(), TextLabel::New( string label) )
18 # regexp for searching swig generated constructors grep -oP -n 'public [A-Z]([A-Z]*[a-z])*\(\)' *
19 # regexp for searching for swig genereated New functions grep -oP -n -i 'static [a-zA-Z]\+ New'
23 "* Copyright (c) #{Time.now.year} Samsung Electronics Co., Ltd.\n"\
25 "* Licensed under the Apache License, Version 2.0 (the \"License\");\n"\
26 "* you may not use this file except in compliance with the License.\n"\
27 "* You may obtain a copy of the License at\n"\
29 "* http://www.apache.org/licenses/LICENSE-2.0\n"\
31 "* Unless required by applicable law or agreed to in writing, software\n"\
32 "* distributed under the License is distributed on an \"AS IS\" BASIS,\n"\
33 "* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"\
34 "* See the License for the specific language governing permissions and\n"\
35 "* limitations under the License.\n"\
38 "// This File has been auto-generated by SWIG and then modified using DALi Ruby Scripts\n"
44 $totalConstructorsGenerated = 0
47 $swigConstructorNumLines = 3
48 $swigNewFuncNumLines = 4
51 # daliClass struct stores information about where the constructor / static New functions are in the file
52 $daliClassStruct = Struct.new("DaliClass", :name, :file, :hasNewFunction, :hasConstructor, :constructorLine, :staticNewLines, :generatedConstructors)
55 $daliClassArray = Array.new
59 pn = Pathname.new(Dir.pwd)
62 $rootPath = fullPath.slice(0..( fullPath.index('/dali-toolkit')))
63 $daliCSharpPath = $rootPath + "dali-toolkit/plugins/dali-swig/automatic/csharp"
64 puts("--------------------------------------------")
65 puts("Modifying constructors for DALi C# files ")
66 puts("E.g. modify TextLabel::New() ==> new TextLabel() ")
72 def getDaliClassItem( className )
74 # puts( "getDaliClassItem "+ className )
75 index = $daliClassArray.index{ |a| a.name == className }
78 # create a new item along with a array for it's properites
79 classItem = $daliClassStruct.new( className );
81 classItem.name = className
82 classItem.hasNewFunction = false
83 classItem.hasConstructor = false
84 classItem.generatedConstructors = Array.new
85 classItem.staticNewLines = Array.new
87 $daliClassArray.push( classItem )
91 classItem = $daliClassArray[ index ]
98 def grepConstructorInfo
100 # grep for strings like public Actor(), have to use double black slash on the parenthesis for some reason
101 result =`grep -oP -n "public [A-Z]([A-Z]*[0-9]*[a-z])*\\(\\)" #{$daliCSharpPath}/*`
103 # result is an array of lines that look like this:
104 # /homepath/dali-toolkit/plugins/dali-swig/csharp/Window.cs:66:public Window()
106 lines = result.split(/\n+/);
109 # Split the line into file name, line number
110 data = line.split(":",3)
115 # Get the class name from the filename
116 className = File.basename(fileName,".cs")
118 # get or create a new DALi class item which stores the new / constructor information
119 classItem = getDaliClassItem( className )
121 classItem.file = fileName
122 classItem.constructorLine = lineNum.to_i
123 classItem.hasConstructor = true
124 # puts classItem.name
132 def grepStaticNewInfo
134 # grep for strings like static Actor::New()
135 result =`grep -oP -n -i "static [a-zA-Z0-9]\+ New" #{$daliCSharpPath}/*`
137 lines = result.split(/\n+/);
141 # Split the line into file name and property macro, split 2 means just create two strings
142 data = line.split(":",3)
146 # # Get the class name from the filename ( e.g. image-actor-impl.cpp => image-actor)
147 className = File.basename(fileName,".cs")
149 # get or create a new DALi class item which stores the property information
150 classItem = getDaliClassItem( className )
152 classItem.file = fileName
153 classItem.hasNewFunction = true
154 classItem.staticNewLines.push( lineNum.to_i )
155 # puts "added line number #{lineNum} for #{classItem.name}"
164 def generateNewConstructors
167 for daliClass in $daliClassArray
169 # if the class doesn't have a New function and a constructor then skip it
170 if ! (daliClass.hasNewFunction && daliClass.hasConstructor)
171 #puts( "Doesn't have a New function #{daliClass.file}" )
175 File.open(daliClass.file, 'r+') do |file|
178 for newEntryLine in daliClass.staticNewLines
179 linesToRead = newEntryLine - currentLine -1
180 # puts("lineToRead = #{linesToRead} #{newEntryLine}")
181 linesToRead.times{ file.gets }
183 currentLine += linesToRead +$swigConstructorNumLines # +3 for 3 lines of the New function that we read in below
185 #puts("line = #{line} _________")
186 parameterString = /\((.*)\)/.match(line) # pulls out the New parameter e.g. (float duration)
191 constructorCall = /\((.*)\)/.match(line) # pulls out the constructor call e.g. ((NDalicPINVOKE.TextLabel_New__SWIG_1(text), true))
193 exceptionLine = file.readline
195 #res = file.line.line.grep(/asd/i)
196 constructorCode = " public #{daliClass.name} #{parameterString} : this #{constructorCall} {\n"\
197 " #{exceptionLine}\n"\
201 daliClass.generatedConstructors.push( constructorCode )
202 #puts constructorCode
208 def InjectConstructors( daliClass, file )
210 for code in daliClass.generatedConstructors
213 $totalConstructorsGenerated+=1
217 def lineShouldBeSkipped(daliClass, line)
219 if line.between?(daliClass.constructorLine-1, daliClass.constructorLine+$swigConstructorNumLines -1)
224 for n in daliClass.staticNewLines
225 if line.between?(n-1, n+$swigNewFuncNumLines )
234 # helper class to color the background
236 def blueBackground; "\e[45m#{self}\e[0m" end
239 def updateCSharpFiles
241 # we now have a populated array of daliClassStructs.
242 # With each class we open it's SWIG generated csharp file
243 # create a new temp file, and copy the SWIG generated code over to the temporary one
244 # except for the existing constructors and New() functions. e.g. Actor() and Actor::New()
245 # We also inject our new constructor functions e.g. Actor()
247 for daliClass in $daliClassArray
248 # puts "writing file #{daliClass.name}..."
250 # check the class has some constructors to write
251 if (daliClass.generatedConstructors.length == 0 )
252 # puts "no constructors for #{daliClass.name}..."
256 if daliClass.name == "Application"
260 $totalDaliClasses+=1 # for stats
262 # create a file to store the modified output
263 tempFileName = "#{daliClass.file}.tmp"
264 tempFile = File.new(tempFileName, 'w')
266 tempFile.puts $fileHeader
269 constructorsWritten = false
271 File.open(daliClass.file, 'r') do |file|
275 if lineShouldBeSkipped( daliClass, currentLine ) # lineShouldBeSkipped ( daliClass, currentLine )
277 if( !constructorsWritten )
278 # inject our newly generated constructors
279 InjectConstructors( daliClass, tempFile );
280 puts("Converting static Class::New(xxx) ---> new Class(xxx) into #{daliClass.name}".blueBackground)
282 constructorsWritten = true # only write our new constructors to the file once
285 end # line should be skipped
288 tempFile.puts( line )
295 # copy the temp file over the top of the exiting file
296 FileUtils.mv tempFileName, daliClass.file
300 puts("Done. Generated #{$totalConstructorsGenerated} Constructors for #{$totalDaliClasses} DALi C# classes".blueBackground)
309 generateNewConstructors