Home  >  

Bashing together Method References

Author photo
January 20, 2010 | | Comments (5)
AddThis Social Bookmark Button

The ActionScript compiler generates SWFs (Flash applications) by compiling source code for classes that you write and linking in classes from SWCs (libraries) that are referenced by your code. Classes that are not referenced are not included in the SWF

I needed a SWF that contained all of the classes from a SWC, so I wrote a bash script to generate an ActionScript class that contained references to each class. The bash script reads the library's asdoc directory, and emits a reference for each class that it encounters. Classes beginning with the letter "I" are ignored, because the library that I was working with used the letter "I" to denote interfaces.

The following script accepts two parameters: the path to the asdoc directory and the path to the output file:

#!/bin/bash

function listPages {
    for f in $(find . -name \*.html); do
       if [ "${f:2:3}" == "com" ]; then echo "$f"; fi
    done
}

function writeIncludes {
    for f in $PAGES; do
        PACKAGE="$(dirname $f|sed -e "s^./^^"|tr -d '.'|tr '/' '.')"
        CLASS="$(basename $f|sed -e "s^.html^^")"
        if [ "$CLASS" != "package-detail" -a "$CLASS" != "class-list" ]; then
           echo "    import $PACKAGE.$CLASS;"
        fi
    done
}

# Does not process any classes that have names starting with "I" (interfaces)
function writeRefs {
    for f in $PAGES; do
        CLASS="$(basename $f|sed -e "s^.html^^")"
        if [ "$CLASS" != "package-detail" -a "$CLASS" != "class-list" -a ${CLASS:0:1} != "I" ]; then
           CLASS_LC="$(echo "$CLASS"|tr '[:upper:]' '[:lower:]')"
           echo "      private var $CLASS_LC:$CLASS = new $CLASS();"
        fi
    done
}

function doIt {
    PAGES="$(listPages)"
    echo "package {"
    writeIncludes
    echo ""
    echo "   public class Includer {"
    writeRefs
    echo "   }"
    echo "}"
}

WD=$(pwd)
if [ "$1" ]; then cd "$1"; fi
doIt > "$WD/$2"

Here is a second bash script to drive the first:

#!/bin/bash
DIR=/path/to/asdoc
./classRefs "$DIR" "Includer/src/Includer.as"

Any constructors that require parameters will need to be corrected so the emitted class will compile. It does not matter what values you provide; null works fine for any Object, including String parameters.

Buy the book and learn more FB tips.

_______________________________

Mike Slinn
Independent Flex / Java contractor and author
http://slinnbooks.com
http://mslinn.com

Adobe Flex Certified Expert

Read more from Mike Slinn. Mike Slinn's Atom feed mslinn on Twitter

Comments

5 Comments

Marvin Froeder said:

If you do use mxmlc to build your swf (no idea if that is your case) you can do that using -include-libraries

Adobe Flex Compiler (mxmlc)
Version 3.2.0 build 3958
Copyright (c) 2004-2007 Adobe Systems, Inc. All rights reserved.

-compiler.include-libraries [library] [...]
alias -include-libraries
a list of libraries (SWCs) to completely include in the SWF
(repeatable)


VELO

Erik said:

The SWC file is just a zip. Open it up and you find your swf inside, with a bonus XML describing all classes. Saves you alot of trouble. You could then runtime load the swf into you application if you'd wish.


Greetz Erik

villa bali said:

Just wondering how far away we are from getting the FlexUnit4CIRunner going? I am looking for a good solution for integrating with Hudson, and right now the best documented solution is Peter Martin's FlexAntTask.jar.
However, unless I integrate his JUnitTestRunner with FlexUnit4, I'm pretty much stuck working with FlexUnit1. Is there a tutorial or more information out there regarding the use of the CI Runner in FlexUnit4?
villa bali

David Wilhelm said:

Thanks for sharing... I've used bash to parse asdoc in order to generate a ctags file of the core Flash API (the source code of which is not available AFAIK). I use that for autocompletion in Vim. I think your script may help me improve it a bit :)

Alan Shaw said:

Actually there's no need to instantiate a variable of a class to get the class linked into your swf. Just mentioning the class name as a statement, within your Includer class but outside of any member function, suffices.

Leave a comment


Type the characters you see in the picture above.


Tag Cloud

Technical Speakers

Who is the best technical speaker you have seen?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.