flair.gradle.tasks.GenerateResourcesClass.groovy Maven / Gradle / Ivy
package flair.gradle.tasks
import flair.gradle.extensions.FlairProperty
import flair.gradle.extensions.IExtensionManager
import org.gradle.api.file.FileTree
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
/**
* @author SamYStudiO ( [email protected] )
*/
public class GenerateResourcesClass extends AbstractTask
{
public static String template
@InputFiles
def Set inputFiles
@OutputFile
def File outputFile
public GenerateResourcesClass()
{
group = TaskGroup.GENERATED.name
description = ""
}
@TaskAction
public void generate()
{
IExtensionManager extensionManager = project.flair as IExtensionManager
String moduleName = extensionManager.getFlairProperty( FlairProperty.MODULE_NAME )
String packageName = extensionManager.getFlairProperty( FlairProperty.PACKAGE_NAME )
if( !moduleName || !packageName || !template ) return
File classFile = project.file( "${ moduleName }/src/main/generated/R.as" )
if( !classFile.exists( ) ) return
String classFileContent = template
FileTree resources = project.fileTree( "${ moduleName }/src" )
String bools = ""
String colors = ""
String dimens = ""
String drawables = ""
String integers = ""
String objects = ""
String raws = ""
String sounds = ""
String strings = ""
String xmls = ""
ArrayList boolList = new ArrayList( )
ArrayList colorList = new ArrayList( )
ArrayList dimenList = new ArrayList( )
ArrayList drawableList = new ArrayList( )
ArrayList integerList = new ArrayList( )
ArrayList objectList = new ArrayList( )
ArrayList rawList = new ArrayList( )
ArrayList soundList = new ArrayList( )
ArrayList stringList = new ArrayList( )
ArrayList xmlList = new ArrayList( )
Node node
resources.each { file ->
if( file.parentFile.parentFile.name == "res" || file.parentFile.parentFile.parentFile.name == "res" )
{
File parent = file.parentFile
while( parent.parentFile.name != "res" )
{
parent = parent.parentFile
}
String parentName = parent.name
String filename = file.name.split( "\\." )[ 0 ]
String ext = file.name.split( "\\." )[ 1 ]
if( parentName.indexOf( "xml" ) == 0 && !xmlList.contains( filename ) )
{
xmls += "\tpublic const ${ filename } : XML = getXml( \"${ filename }\" );" + System.lineSeparator( )
xmlList.add( filename )
}
else if( parentName.indexOf( "raw" ) == 0 )
{
if( ( ext == "mpeg" || ext == "mp3" ) && !soundList.contains( filename ) )
{
sounds += "\tpublic const ${ filename } : Sound = getSound( \"${ filename }\" );" + System.lineSeparator( )
soundList.add( filename )
}
else if( ext == "json" && !objectList.contains( filename ) )
{
objects += "\tpublic const ${ filename } : Object = getObject( \"${ filename }\" );" + System.lineSeparator( )
objectList.add( filename )
}
else if( !rawList.contains( filename ) )
{
raws += "\tpublic const ${ filename } : ByteArray = getByteArray( \"${ filename }\" );" + System.lineSeparator( )
rawList.add( filename )
}
}
else if( parentName.indexOf( "values" ) == 0 )
{
node = new XmlParser( ).parse( file )
node.string.each { string ->
if( !stringList.contains( [email protected]( ) ) )
{
strings += "\tpublic const ${ string.@name } : String = getString( \"${ string.@name }\" );" + System.lineSeparator( )
stringList.add( [email protected]( ) )
}
}
node.color.each { color ->
if( !colorList.contains( [email protected]( ) ) )
{
colors += "\tpublic const ${ color.@name } : uint = getColor( \"${ color.@name }\" );" + System.lineSeparator( )
colorList.add( [email protected]( ) )
}
}
node.bool.each { bool ->
if( !boolList.contains( [email protected]( ) ) )
{
bools += "\tpublic const ${ bool.@name } : Boolean = getBoolean( \"${ bool.@name }\" );" + System.lineSeparator( )
boolList.add( [email protected]( ) )
}
}
node.dimen.each { dimen ->
if( !dimenList.contains( [email protected]( ) ) )
{
dimens += "\tpublic const ${ dimen.@name } : Number = getDimen( \"${ dimen.@name }\" );" + System.lineSeparator( )
dimenList.add( [email protected]( ) )
}
}
node.integer.each { integer ->
if( !integerList.contains( [email protected]( ) ) )
{
integers += "\tpublic const ${ integer.@name } : int = getInteger( \"${ integer.@name }\" );" + System.lineSeparator( )
integerList.add( [email protected]( ) )
}
}
}
else if( parentName.indexOf( "drawable" ) == 0 )
{
boolean isAtlas = ( ext != "xml" ) && new File( file.parentFile.path + File.separator + filename + ".xml" ).exists( )
if( ext == "xml" )
{
node = new XmlParser( ).parse( file )
node.SubTexture.each { texture ->
if( !drawableList.contains( [email protected]( ) ) )
{
drawables += "\tpublic const ${ texture.@name } : Texture = getDrawable( \"${ texture.@name }\" );" + System.lineSeparator( )
drawableList.add( [email protected]( ) )
}
}
}
else if( !isAtlas && !drawableList.contains( filename ) )
{
drawables += "\tpublic const ${ filename } : Texture = getDrawable( \"${ filename }\" );" + System.lineSeparator( )
drawableList.add( filename )
}
}
}
}
classFileContent = classFileContent.replaceAll( /}[^{}]*new R\( new Singleton\(\) \);/ , "}" + System.lineSeparator( ) + System.lineSeparator( ) + "import flair.resources.*;" + System.lineSeparator( ) + "import flash.media.Sound;" + System.lineSeparator( ) + "import flash.utils.ByteArray;" + System.lineSeparator( ) + "import starling.textures.Texture;" + System.lineSeparator( ) + System.lineSeparator( ) + "new R( new Singleton() );" )
classFileContent = classFileContent.replaceAll( /class RBool\s*\{[^}]*}/ , "class RBool" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + bools + "}" )
classFileContent = classFileContent.replaceAll( /class RColor\s*\{[^}]*}/ , "class RColor" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + colors + "}" )
classFileContent = classFileContent.replaceAll( /class RDimen\s*\{[^}]*}/ , "class RDimen" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + dimens + "}" )
classFileContent = classFileContent.replaceAll( /class RDrawable\s*\{[^}]*}/ , "class RDrawable" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + drawables + "}" )
classFileContent = classFileContent.replaceAll( /class RInteger\s*\{[^}]*}/ , "class RInteger" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + integers + "}" )
classFileContent = classFileContent.replaceAll( /class RObject\s*\{[^}]*}/ , "class RObject" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + objects + "}" )
classFileContent = classFileContent.replaceAll( /class RRaw\s*\{[^}]*}/ , "class RRaw" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + raws + "}" )
classFileContent = classFileContent.replaceAll( /class RSound\s*\{[^}]*}/ , "class RSound" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + sounds + "}" )
classFileContent = classFileContent.replaceAll( /class RString\s*\{[^}]*}/ , "class RString" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + strings + "}" )
classFileContent = classFileContent.replaceAll( /class RXml\s*\{[^}]*}/ , "class RXml" + System.lineSeparator( ) + "{" + System.lineSeparator( ) + xmls + "}" )
classFile.write( classFileContent )
}
public void findInputAndOutputFiles()
{
IExtensionManager extensionManager = project.flair as IExtensionManager
String moduleName = extensionManager.getFlairProperty( FlairProperty.MODULE_NAME )
inputFiles = new ArrayList( )
List list = new ArrayList( )
extensionManager.allActivePlatformVariants.each {
it.directories.each { directory ->
if( list.indexOf( directory ) < 0 ) list.add( directory )
}
}
list.each {
inputFiles.add( project.file( "${ moduleName }/src/${ it }/res" ) )
}
outputFile = project.file( "${ moduleName }/src/main/generated/R.as" )
}
}