The first one uses the compc task to compile all classes of a given source directory to a swc library:
<macrodef name="compile-lib">
<attribute name="source-dir" default="${source.dir}"/>
<attribute name="target-dir" default="${build.dir}"/>
<attribute name="swc-name"/>
<element name="args" optional="true"/>
<sequential>
<fileset id="sources" dir="@{source-dir}">
<include name="**/*.as"/>
<include name="**/*.mxml"/>
</fileset>
<pathconvert property="classes" pathsep=" " refid="sources">
<compositemapper>
<chainedmapper>
<globmapper from="@{source-dir}/*" to="*" handledirsep="true" />
<mapper type="package" from="*.as" to="*" />
</chainedmapper>
<chainedmapper>
<globmapper from="@{source-dir}/*" to="*" handledirsep="true" />
<mapper type="package" from="*.mxml" to="*" />
</chainedmapper>
</compositemapper>
</pathconvert>
<echo message="Compiling library @{swc-name} including classes: ${classes}"/>
<compc
output="@{target-dir}/@{swc-name}.swc"
include-classes="${classes}"
link-report="@{target-dir}/@{swc-name}-link-report.xml"
keep-generated-actionscript="true"
optimize="true"
incremental="true">
<source-path path-element="@{source-dir}"/>
<external-library-path file="${libs.dir}/*.swc" append="false"/>
<external-library-path file="${FLEX_HOME}/frameworks/libs/*.swc" append="false"/>
<external-library-path file="${flex.playerglobal}" append="false"/>
<args/>
</compc>
</sequential>
</macrodef>
In a first step the macro puts all *.as and *.mxml files in the source-dir into a fileset and then converts them to a comma separated list containing all qualified class names using pathconvert (for more on this see here and here and for an alternative approach see here).
After that it compiles the swc library using the compc task. Again you can pass additional compiler arguments using the args element (see part 1).
The second macro takes a swc library and creates an optimized runtime shared library out of it:
<macrodef name="create-rsl">
<attribute name="source-dir" default="${build.dir}"/>
<attribute name="target-dir" default="${build.dir}"/>
<attribute name="swc-name"/>
<sequential>
<unzip src="@{source-dir}/@{swc-name}.swc" dest="@{target-dir}" overwrite="true">
<patternset>
<include name="library.swf"/>
</patternset>
</unzip>
<move file="@{target-dir}/library.swf" tofile="@{target-dir}/@{swc-name}-library.swf"/>
<exec executable="${flex.optimizer}" failonerror="true">
<arg value="-input=@{target-dir}/@{swc-name}-library.swf"/>
<arg value="-output=@{target-dir}/@{swc-name}.swf"/>
</exec>
<delete file="@{target-dir}/@{swc-name}-library.swf"/>
</sequential>
</macrodef>
To do so, it first uses the unzip task to extract the library.swf, renames it using the move and then calls the flex optimizer using the exec task. The optimizer strips the swf file of all debugging code and metadata. With those two macros you can create runtime shared libraries with just two calls:
<target name="build-mylibrary">
<compile-lib swc-name="MyLibrary" source-dir="${basedir}/src-mylibrary"/>
<create-rsl swc-name="MyLibrary"/>
</target>
You can have macros for all kinds of things, be it running the flex unit test, building asdoc, running pmd or compiling locales. And once written you can move them from project to project.
More information on macros for Flex/Actionscript can be found here.
No comments:
Post a Comment