FigEx is a utility tool to export styles and icons from Figma using the Figma REST API.
Features:
- Support for modes and variables in Figma
- Code generation using Jinja2 templating syntax for any code language
- Export of icons as SVG, PDF, PNG, WEBP or Android XML vectors
- Simple configuration with many options
config.json
is a simple configuration, telling FigEx what to put where:
{
"type": "values",
"templatePath": "../samples/AndroidValues.xml.figex",
"destinationPath": "~/Downloads/AndroidValuesModeB.xml",
"defaultMode": "modeB",
"templateVariables": {
"templateVarDemo": "\uD83D\uDE80\uD83D\uDE80\uD83D\uDE80"
}
},
{
"type": "icons",
"format": "androidxml",
"filter": "{% if fullName|startsWith('icon-', true) %} true {% else %} false {% endif %}",
"fileNames" : "{{ fullName|replaceSpecialChars('_')|lowercase }}",
"destinationPath": "~/Downloads/icons",
"clearDestination": true
}
You can defined template files with Jinja-tokens to generate source code files in any language:
<?xml version="1.0" encoding="utf-8"?>
<resources>
{{ }}
{%- for color in colors -%}
<color name="{{ color.name.snake }}">#{{ color.argb }}</color>
{% endfor %}
</resources>
- Make sure Java is installed on your machine, run
java --version
to confirm - Download a
figex.zip
from the release list - Extract the zip file and place it in your system
- Add the extracted directory to your system's
$PATH
(macOS) - Create a config file for your project, see the
sample/config.json
- Create a Figma personal access token
- Run
export FIGMA_TOKEN="your token"
- Run
figex -c "path to your config"
- Make sure Java is installed on your machine, run
java --version
to confirm - Download a
figex.zip
from the release list - Extract the zip file and place it in your system
- Create a config file for your project, see the
sample/config.json
- Create a Figma personal access token
- Run
set FIGMA_TOKEN="your token"
- Run
java -jar "path to figex jar" -c "path to your config"
You can also use FigEx as a gradle dependency, e.g. for your buildSrc
in an Android Studio project.
- Add JitPack to your
buildSrc
'ssettings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io" }
}
}
- Add FigEx to your
buildSrc
'sbuild.gradle
implementation("com.github.iodigital-com:figex:Tag|)
Important: FigEx makes use of com.android.tools:sdk-common
as a build dependency, this means there can be a conflict with your Android Gradle Plugin version. If you experience sync issues after adding FigEx, try the following. FigEx will not be able to covert SVG to Android Vector graphics. AGP 8.4.0-8.5.0 is currently tested and working. Consider using FigEx as a standalone tool as an alternative (see above).
implementation("com.github.iodigital-com:figex:Tag") {
exclude("com.android.tools")
}
- Add a Gradle task in the
buildSrc
abstract class ExportFigmaTask : DefaultTask() {
@get:InputFile
var configFile: File = File(IoBuildConfig.rootDir, "config/figex/figex.json")
@get: Input
var token: String = ""
@TaskAction
fun action() = runBlocking {
try {
export(
configFile = configFile,
figmaToken = requireNotNull(
System.getenv("FIGMA_TOKEN") ?: token.takeIf { it.isNotBlank() }
) { "Missing Figma token" },
)
} catch (e: Exception) {
e.printStackTrace()
throw e
}
}
}
- Register the task in your main
build.gradle
tasks.register<ExportFigmaTask>("updateFigmaResources").configure {
token = ...
configFile = File(...) // Optional, defaults to config/figex/figex.json
}
- Update your Figma resources via gradle:
gradle updateFigmaResources
See the example config in the samples
directory.
figmaFileKey
: The key for the figma file. You can obtain it from any Figma URL, the section in>>
and<<
is the key:figma.com/file/>>dqsg8P1c2ayjNJPyPYmv4X<<
modeAliases
: Aliases for the modes. FigEx sees modes only as their IDs, e.g.8124:0
and8124:1
. You can defined aliases here for your convenience. There is no way to look up the name for a mode from the free Figma API, so you need to figure out what is what.exports
: Defines the exports to be done. There are two kinds of exports:"type": "values"
is used to export any values like colors, dimensions or text stylestemplatePath
: The path to the Jinja2 template. Seesamples/AndroidValues.xml.figex
for an example and see below for more detailsdestinationPath
: The path to where the generated file should be writtendefaultMode
: The default mode to be used for the values. If thedefaultMode
is e.g.test
thencolor.test.argb
is the same ascolor.argb
templateVariables
: A map of extra variables for the template. If you definetest
here you can later use{{ test }}
in your template file
"type": "icons"
is used to export icons and illustrationsformat
: One ofsvg
,pdf
,png
,webp
orandroidxml
filter
: A template that should readtrue
to include a component in the exportfileNames
: A template defining the file name of the exported component. A/
will cause a directory to be createddestinationPath
: The directory to which the files should be writtenclearDestination
: Iftrue
, all files in the destination directory will be deleted before exporting the iconsrasterScales
: A list of scale objects defining the sizes for raster graphics exports (png
andwebp
)scale
: A float defining the scale,1
being original sizenameSuffix
: A string that is appended to the name generated fromfileNames
, comes before the file suffixnamePrefix
: A string tha tis prepended to the name generated fromfileNames
. A/
will cause a directory to be created.
useAndroidRasterScales
: A shorthand to createmdpi
,hdpi
,xhdpi
,xxhdpi
,xxxhdpi
exports of raster graphics. Ignored ifrasterScales
is defined
The templating engine uses Jinja syntax. You can use loops, if statements and more. FigEx's templating is build with jinjava which is also the base of HubSpot's HubL templating system. This means the syntax for if-statements and loops also applies to FigEx, same goes for the filters available. Of course, HubSpot specific variables and functions are not available.
This templating is used in the filter
and fileNames
configurations.
figma
: AFigma
objectdate
: The current datefull_name
: The full name of the component. If part of a component set, comprised of the name of the component set and the component name. The component name otherwise.name
: The name of the componentkey
: The key of the componentid
: The id of the componentset_name
: The name of the set of which this component is a part of, empty if not part of a setset_key
: The ket of the set of which this component is a part of, empty if not part of a setset_id
: The id of the set of which this component is a part of, empty if not part of a setscale
: A scale object representing the current scale for the export
This templating is used in the file at the templatePath
configuration.
colors
: A list ofColor
objectsdimens
: A list ofDimension
objectstext_styles
: A list ofTextStyle
objectsfigma
: AFigma
objectdate
: The current date
name
: A name objecta
,r
,g
,b
: The alpha, red, green and blue value 0..1 (for the default mode)a255
,r255
,g255
,b255
: The alpha, red, green and blue value 0..255 (for the default mode)argb
: The argb hex string (for the default mode)modeA
: A nested color object formodeA
, contains the same values as above. The name depends on your modes and themodeAliases
in the configmodeB
: A nested color object formodeB
, contains the same values as above. The name depends on your modes and themodeAliases
in the config
name
: A name objectvalue
: The value (for the default mode)modeA
: A nested color object formodeA
, contains the same values as above. The name depends on your modes and themodeAliases
in the configmodeB
: A nested color object formodeB
, contains the same values as above. The name depends on your modes and themodeAliases
in the config
name
: A name objectfont_family
: As defined in Figma (for the default mode)font_post_script_name
: As defined in Figma (for the default mode)font_size
: As defined in Figma (for the default mode)font_weight
: As defined in Figma (for the default mode)letter_spacing
: As defined in Figma (for the default mode)line_height_percent
: As defined in Figma (for the default mode)line_height_percent_font_size
: As defined in Figma (for the default mode)line_height_px
: As defined in Figma (for the default mode)line_height_unit
: As defined in Figma (for the default mode)text_align_horizontal
: As defined in Figma (for the default mode)text_align_vertical
: As defined in Figma (for the default mode)text_auto_resize
: As defined in Figma (for the default mode)modeA
: A nested color object formodeA
, contains the same values as above. The name depends on your modes and themodeAliases
in the configmodeB
: A nested color object formodeB
, contains the same values as above. The name depends on your modes and themodeAliases
in the config
Hint: You can also use Jinja filters to modify the name, e.g. {{ color.name|lowercase|replace("some", "other") }}
original
: The name as defined in Figmasnake
: The name in snake casekebab
: The name in kebab casepascal
: The name in pascal case
file
: The Figma file namelast_modified
: Last modified date of the Figma fileversion
: The version of the Figma file
scale
: The scale as floating point number as configuredname_prefix
: The prefix of the filename as configuredname_suffix
: The suffix of the filename as configured
- Clone the Git
./gradlew clean build
will build the project and create files infigma-exported/build/distributions