Few people know, that there's a possibility to create extensions for mxmlc compiler adding some custom functionality (https://bugs.adobe.com/jira/browse/SDK-18718). In this post I'm going to write a short guide describing creating and applying extension process.
First of all you'll need Flex SDK version 4.1+ and JDK.
Create new java project and set location {FLEX_HOME}\lib as a libraries location directory. Flex provides us several types of extensions:
Each of the extensions runs in a different phase of compilation.
We're to see how it works on IMxmlcExtension example, which runs after flash project is compiled. Create a class implementing IMxmlcExtension interface and implement run method.
First of all you'll need Flex SDK version 4.1+ and JDK.
Create new java project and set location {FLEX_HOME}\lib as a libraries location directory. Flex provides us several types of extensions:
- IApplicationExtension (Defines the API for extensions, which run at the end of a OEM API application compilation)
- ICompcExtension (Defines the API for extensions, which run at the end of a command line library compilation)
- ILibraryExtension (Defines the API for extensions, which run at the end of an OEM API library compilation)
- IMxmlcExtension (Defines the API for extensions, which run at the end of a command line application compilation)
- IPreCompileExtension (Defines the API for extensions, which run before each compilation)
- IPreLinkExtension (Defines the API for extensions, which run before each PreLink run. PreLink's run can be executed multiple times)
Each of the extensions runs in a different phase of compilation.
We're to see how it works on IMxmlcExtension example, which runs after flash project is compiled. Create a class implementing IMxmlcExtension interface and implement run method.
import flex2.compiler.extensions.IMxmlcExtension; public class MyMxmlcExtension implements IMxmlcExtension { @Override public void run(String[] strings) { System.out.println("Hello from mxmlc extension!"); } }
We're going just print a message and see, that extension is running. We need to build jar.
The next step is editing manifest file. We should add the following line to MANIFEST.MF to let flex compiler know, where is our extension extensions-mxmlc: MyMxmlcExtension
Ok, now we can compile the jar including edited manifest file (don't include libraries jars in the output jar).
So, we can create new flex project to test the extension. Put extension jar in the project root. To enable extension we need to pass extension file as the following parameter to mxmlc arguments -extension=MyExtension.jar (relative path basedir is project root, or you can use absolute path). Ok, finally, we're ready to compile flex project and see the result.
It works :)
Ok, let's do another thing. Sometimes it happens, that compiler cannot display output correctly if it's not English (I faced problem of cyrillic symbols many times), because compiler uses operating system language. So, let's force compiler to use english regardless of system language. We're going to create IPrecompileExtension
import flash.localization.LocalizationManager; import flex2.compiler.*; import flex2.compiler.common.Configuration; import flex2.compiler.extensions.IPreCompileExtension; import flex2.compiler.util.ThreadLocalToolkit; import java.util.Collection; import java.util.List; import java.util.Locale; import java.util.Map; public class MyPrecompileExtension implements IPreCompileExtension { @Override public void run(FileSpec fileSpec, SourceList sourceList, Collection<Source> classes, SourcePath sourcePath, ResourceContainer resourceContainer, ResourceBundlePath resourceBundlePath, CompilerSwcContext compilerSwcContext, SymbolTable symbolTable, Configuration configuration, SubCompiler[] subCompilers, PreLink preLink, Map map, List<Source> sources) { LocalizationManager m = ThreadLocalToolkit.getLocalizationManager(); m.setLocale(Locale.ENGLISH); } }
Add extensions-pre-compile: MyPrecompileExtension to MANIFEST.MF and build jar file. Now we'll see English only.
So, you see, that extention is a really nice tool, but it's not ducumented unfortunately, and it's up to us to explore it.
Thanks!