fixpackages makes ActionScript refactoring a little less painful

October 05, 2010 at 01:05 PM | Uncategorized | View Comments

Damnit, Adobe, you do have a way of making simple tasks especially painful. I've written a little Python script — fixpackages — which makes one common task — refactoring ActionScript packages — a little bit less painful.

It walks through a tree of ActionScript source updating the (stupid) package statement so it matches the file's path, then fixing any imports which have obviously been broken.

For example:

$ head -n3 src/new/Foo.as src/main.as
==> src/new/Foo.as <==
package old { // <--- package doesn't match file path
class Foo.as {

==> src/main.as <==
package {
class main {
    import old.Foo; // <--- import statement is broken
$ fixpackages src/
Changing package old --> new in src/new/Foo.as
Fixed 1 imports in src/main.as
$ head -n3 src/new/Foo.as src/main.as
==> src/new/Foo.as <==
package new { // <--- package matches source path
class Foo.as {

==> src/main.as <==
package {
class main {
    import new.Foo; // <--- all is right in the world
$

The code can be downloaded or forked over at GitHub: fixpackages — http://gist.github.com/473832

And, of course, the standard warning: fixpackages will almost undoubtedly trash your source tree, post annoying messages on Twitter and steal your significant other. Don't run it without having your code under source control, clicking "deny" and taking h{im,er} out for a nice dinner.