All Downloads are FREE. Search and download functionalities are using the official Maven repository.

flair.gradle.extensions.FlairExtension.groovy Maven / Gradle / Ivy

package flair.gradle.extensions

import flair.gradle.plugins.PluginManager
import flair.gradle.variants.Platform
import flair.gradle.variants.Variant
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project

import java.util.regex.Pattern

/**
 * @author SamYStudiO ( [email protected] )
 */
public class FlairExtension extends PlatformContainerExtension implements IExtensionManager
{
	private String moduleName

	private String packageName

	private Boolean autoGenerateVariantDirectories

	public FlairExtension( String name , Project project , Platform platform )
	{
		super( name , project , platform )
	}

	public String getModuleName()
	{
		return moduleName
	}

	public void moduleName( String moduleName )
	{
		this.moduleName = moduleName
	}

	public String getPackageName()
	{
		return packageName
	}

	public void packageName( String packageName )
	{
		this.packageName = packageName
	}

	public Boolean getAutoGenerateVariantDirectories()
	{
		return autoGenerateVariantDirectories
	}

	public void autoGenerateVariantDirectories( Boolean autoGenerateVariantDirectories )
	{
		this.autoGenerateVariantDirectories = autoGenerateVariantDirectories
	}

	@Override
	public IPlatformContainerExtension getPlatformContainer( Platform platform )
	{
		return ( platform && PluginManager.hasPlatformPlugin( project , platform ) ? project.flair[ platform.name.toLowerCase( ) ] : project.flair ) as IPlatformContainerExtension
	}

	@Override
	public Object getProp( String property , Variant variant , boolean returnDefaultIfNull )
	{
		Object value = super.getProp( property , variant , returnDefaultIfNull )

		if( value || !returnDefaultIfNull ) return value else
		{
			switch( property )
			{
				case FlairProperty.MODULE_NAME.name: return "app"
				case FlairProperty.PACKAGE_NAME.name: return ""
				case FlairProperty.AUTO_GENERATE_VARIANT_DIRECTORIES.name: return true

				default: return null
			}
		}
	}

	@Override
	public List getAllActivePlatformProductFlavors()
	{
		NamedDomainObjectContainer iosProductFlavors = PluginManager.hasPlatformPlugin( project , Platform.IOS ) ? getPlatformContainer( Platform.IOS ).getProductFlavors( ) : null
		NamedDomainObjectContainer androidProductFlavors = PluginManager.hasPlatformPlugin( project , Platform.ANDROID ) ? getPlatformContainer( Platform.ANDROID ).getProductFlavors( ) : null
		NamedDomainObjectContainer desktopProductFlavors = PluginManager.hasPlatformPlugin( project , Platform.DESKTOP ) ? getPlatformContainer( Platform.DESKTOP ).getProductFlavors( ) : null
		NamedDomainObjectContainer commonProductFlavors = getProductFlavors( )

		List list = new ArrayList( )

		commonProductFlavors.each {
			list.add( it.name )
		}

		if( iosProductFlavors )
		{
			iosProductFlavors.each {

				if( list.indexOf( it.name ) < 0 )
				{
					list.add( it.name )
				}
			}
		}

		if( androidProductFlavors )
		{
			androidProductFlavors.each {

				if( list.indexOf( it.name ) < 0 )
				{
					list.add( it.name )
				}
			}
		}

		if( desktopProductFlavors )
		{
			desktopProductFlavors.each {

				if( list.indexOf( it.name ) < 0 )
				{
					list.add( it.name )
				}
			}
		}

		return list
	}

	@Override
	public List getAllActivePlatformBuildTypes()
	{
		NamedDomainObjectContainer iosBuildTypes = PluginManager.hasPlatformPlugin( project , Platform.IOS ) ? getPlatformContainer( Platform.IOS ).getBuildTypes( ) : null
		NamedDomainObjectContainer androidBuildTypes = PluginManager.hasPlatformPlugin( project , Platform.ANDROID ) ? getPlatformContainer( Platform.ANDROID ).getBuildTypes( ) : null
		NamedDomainObjectContainer desktopBuildTypes = PluginManager.hasPlatformPlugin( project , Platform.DESKTOP ) ? getPlatformContainer( Platform.DESKTOP ).getBuildTypes( ) : null
		NamedDomainObjectContainer commonBuildTypes = getBuildTypes( )

		List list = new ArrayList( )

		commonBuildTypes.each {
			list.add( it.name )
		}

		if( iosBuildTypes )
		{
			iosBuildTypes.each {

				if( list.indexOf( it.name ) < 0 )
				{
					list.add( it.name )
				}
			}
		}

		if( androidBuildTypes )
		{
			androidBuildTypes.each {

				if( list.indexOf( it.name ) < 0 )
				{
					list.add( it.name )
				}
			}
		}

		if( desktopBuildTypes )
		{
			desktopBuildTypes.each {

				if( list.indexOf( it.name ) < 0 )
				{
					list.add( it.name )
				}
			}
		}

		return list
	}

	@Override
	public List getAllActivePlatformVariants()
	{
		List list = new ArrayList( )

		if( PluginManager.hasPlatformPlugin( project , Platform.IOS ) ) list.addAll( getPlatformVariants( Platform.IOS ) )
		if( PluginManager.hasPlatformPlugin( project , Platform.ANDROID ) ) list.addAll( getPlatformVariants( Platform.ANDROID ) )
		if( PluginManager.hasPlatformPlugin( project , Platform.DESKTOP ) ) list.addAll( getPlatformVariants( Platform.DESKTOP ) )

		return list
	}

	@Override
	public List getPlatformVariants( Platform platform )
	{
		if( !platform ) return getAllActivePlatformVariants( )

		List list = new ArrayList( )

		NamedDomainObjectContainer platformProductFlavors = getPlatformContainer( platform ).getProductFlavors( )
		NamedDomainObjectContainer platformBuildTypes = getPlatformContainer( platform ).getBuildTypes( )

		NamedDomainObjectContainer commonProductFlavors = getProductFlavors( )
		NamedDomainObjectContainer commonBuildTypes = getBuildTypes( )

		List platformDimension = getPlatformContainer( platform ).flavorDimensions
		List commonDimension = flavorDimensions

		List mergedProductFlavors = platformProductFlavors.toList( )

		commonProductFlavors.each { flavor ->

			if( !platformProductFlavors.findByName( flavor.name ) ) mergedProductFlavors.add( flavor )
		}

		List mergedBuildTypes = platformBuildTypes.toList( )

		commonBuildTypes.each { type ->

			if( !platformBuildTypes.findByName( type.name ) ) mergedBuildTypes.add( type )
		}

		List mergedDimension = platformDimension && platformDimension.size( ) ? platformDimension : commonDimension

		// add flavors with no dimension
		if( mergedProductFlavors.size( ) )
		{
			mergedProductFlavors.each { flavor ->

				if( !mergedDimension || !mergedDimension.contains( flavor.dimension ) )
				{
					if( mergedBuildTypes.size( ) )
					{
						mergedBuildTypes.each { type -> list.add( new Variant( project , platform , flavor.name , type.name ) )
						}
					}
					else
					{
						list.add( new Variant( project , platform , flavor.name ) )
					}
				}
			}
		}
		else
		{
			mergedBuildTypes.each { type -> list.add( new Variant( project , platform , ( String ) null , type.name ) )
			}
		}


		// add flavors with dimension
		// TODO rough code! could be done a proper way?
		List> flavorDimensions = new ArrayList<>( )

		mergedDimension.each { dimension ->

			flavorDimensions.add( mergedProductFlavors.findAll( ) { it.dimension == dimension } )
		}

		int totalVariants = 0

		flavorDimensions.each { flavors ->

			totalVariants = totalVariants == 0 ? flavors.size( ) : totalVariants * flavors.size( )
		}

		List[] flavorVariants = new List[totalVariants]

		int flavorsIndex = 0
		int range = 0

		if( totalVariants > 0 )
		{
			flavorDimensions.each { flavors ->

				if( flavorVariants[ 0 ] == null )
				{
					int flavorsSize = flavors.size( )
					int flavorIndex = 0
					range = totalVariants / flavorsSize

					flavors.each { flavor ->

						int start = flavorIndex * range

						for( int i = start; i < start + range; i++ )
						{
							if( !flavorVariants[ i ] ) flavorVariants[ i ] = new ArrayList( )

							flavorVariants[ i ].add( flavor.name )
						}

						flavorIndex++
					}
				}
				else if( flavors.size( ) > 0 )
				{
					range = range / flavors.size( )

					int cpt = 0

					for( int i = 0; i < totalVariants / range; i++ )
					{
						for( int j = 0; j < range; j++ )
						{
							flavorVariants[ cpt ].add( flavors[ i % flavors.size( ) ].name )

							cpt++
						}
					}
				}

				flavorsIndex++
			}

			if( mergedBuildTypes.size( ) )
			{
				mergedBuildTypes.each { type ->

					flavorVariants.each {

						list.add( new Variant( project , platform , it , type.name ) )
					}
				}
			}
			else
			{
				flavorVariants.each {

					list.add( new Variant( project , platform , it ) )
				}
			}
		}

		if( list.size( ) == 0 ) list.add( new Variant( project , platform ) )

		return list
	}

	@Override
	public Object getFlairProperty( FlairProperty property )
	{
		getFlairProperty( ( Variant ) null , property )
	}

	@Override
	public Object getFlairProperty( Platform platform , FlairProperty property )
	{
		getFlairProperty( new Variant( project , platform ) , property )
	}

	@Override
	public Object getFlairProperty( String extensionName , FlairProperty property )
	{
		getFlairProperty( extensionName , ( Variant ) null , property )
	}

	@Override
	public Object getFlairProperty( String extensionName , Platform platform , FlairProperty property )
	{
		getFlairProperty( extensionName , new Variant( project , platform ) , property )
	}

	@Override
	public Object getFlairProperty( Variant variant , FlairProperty property )
	{
		Object value = null
		List list = null

		if( variant && variant.platform && variant.buildType ) value = getPlatformContainer( variant.platform ).getBuildType( variant.buildType ).getProp( property.name , variant )
		if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value

		if( variant && variant.buildType ) value = getBuildType( variant.buildType ).getProp( property.name , variant )
		if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value

		if( variant && variant.platform && variant.productFlavors )
		{
			for( int i = variant.productFlavors.size( ) - 1; i >= 0; i-- )
			{
				value = getPlatformContainer( variant.platform ).getProductFlavor( variant.productFlavors[ i ] ).getProp( property.name , variant )
				if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value
			}
		}

		if( variant && variant.productFlavors )
		{
			for( int i = variant.productFlavors.size( ) - 1; i >= 0; i-- )
			{
				value = getProductFlavor( variant.productFlavors[ i ] ).getProp( property.name , variant )
				if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value
			}
		}

		if( variant && variant.platform ) value = getPlatformContainer( variant.platform ).getProp( property.name , variant )
		if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value

		value = getProp( property.name , variant )
		if( value instanceof List )
		{
			list = mergeLists( value , list )

			if( list.size( ) ) return list
		}
		else if( value != null ) return value

		return getPlatformContainer( variant ? variant.platform : null ).getProp( property.name , variant , true )
	}

	@Override
	public Object getFlairProperty( String extensionName , Variant variant , FlairProperty property )
	{
		Object value = null
		List list = null

		if( variant && variant.platform && variant.buildType ) value = getPlatformContainer( variant.platform ).getBuildType( variant.buildType ).getExtension( extensionName ).getProp( property.name , variant )
		if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value

		if( variant && variant.buildType ) value = getBuildType( variant.buildType ).getExtension( extensionName ).getProp( property.name , variant )
		if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value

		if( variant && variant.platform && variant.productFlavors )
		{
			for( int i = variant.productFlavors.size( ) - 1; i >= 0; i-- )
			{
				value = getPlatformContainer( platform ).getProductFlavor( variant.productFlavors[ i ] ).getExtension( extensionName ).getProp( property.name , variant )
				if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value
			}
		}

		if( variant && variant.productFlavors )
		{
			for( int i = variant.productFlavors.size( ) - 1; i >= 0; i-- )
			{
				value = getProductFlavor( variant.productFlavors[ i ] ).getExtension( extensionName ).getProp( property.name , variant )
				if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value
			}
		}

		if( variant && variant.platform ) value = getPlatformContainer( variant.platform ).getExtension( extensionName ).getProp( property.name , variant )
		if( value instanceof List ) list = mergeLists( value , list ) else if( value != null ) return value

		value = getExtension( extensionName ).getProp( property.name , variant )
		if( value instanceof List )
		{
			list = mergeLists( value , list )

			if( list.size( ) ) return list
		}
		else if( value != null ) return value

		return getPlatformContainer( variant ? variant.platform : null ).getExtension( extensionName ).getProp( property.name , variant , true )
	}

	private List mergeLists( List from , List to )
	{
		if( !to ) return from

		from.each {

			String content = it as String
			Pattern p = Pattern.compile( /.*[a-z]=/ )
			String match = content.find( p )

			if( match )
			{
				if( !to.find { ( it as String ).indexOf( match ) == 0 } ) to.add( content )
			}
			else if( to.indexOf( it ) < 0 ) to.add( it )
		}

		return to
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy