Skip to content

Instantly share code, notes, and snippets.

@mash
Created March 10, 2010 05:49
Show Gist options
  • Save mash/327568 to your computer and use it in GitHub Desktop.
Save mash/327568 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use Path::Class;
my $output = '';
my %namespaces;
my $root = $ARGV[0];
die "usage: $0 path/to/as3/root > singlefile.as" unless -d $root;
chdir $root;
find( { wanted => \&wanted, no_chdir => 1 }, '.' );
$output = remove_imports( $output, [ keys %namespaces ] );
print $output;
use Data::Dumper;
warn Dumper( \%namespaces );
sub wanted {
my $file = $_;
extract_namespace_and_save( $file );
return unless -f $file && -r $file && ($file =~ m!\.as$!);
my $content = file( $file )->slurp;
$content = modify_actionscript( $content );
$output .= $content ."\n";
}
# ./jp/maaash/contextfreeart -> jp.maaash.contextfreeart.* if directory
# ./jp/maaash/ContextFreeArt -> jp.maaash.ContextFreeArt if file
sub extract_namespace_and_save {
my $file = shift;
return if $file eq '.';
if ( -d $file ) { $file .= '.*'; }
$file =~ s!^./!!;
$file =~ s!\.as$!!;
$file =~ tr!/!.!;
$namespaces{ $file } = 1;
}
sub modify_actionscript {
my $content = shift;
# packageを除去
$content =~ s!
(
^package
\s+
[0-9a-z.]*
\s?
{
)
!//$1!imsx;
# 一番最後の } を削除
$content =~ s|
}
([^}]*$)
|//}\n$1|isx;
# public class -> class, public interface -> interface
$content =~ s!
(
public
\s+
(
(class|interface)
\s+
.*$
)
)
!//$1\n$2!imx;
return $content;
}
sub remove_imports {
my ($output, $namespaces) = @_;
for my $namespace (@$namespaces) {
# 自分で定義したネームスペースのimportはコメントアウト
$output =~ s!
(
import
\s+
$namespace
\s*
;?
)
!//$1!gmsx;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment