Dewifile 6.28 KB
# -*- mode: cperl -*-
use Cwd;

my ($MY_EMACS, $is_win);

if (defined $ENV{OSTYPE} && $ENV{OSTYPE} =~ m/cygwin/i
    && defined $ENV{HOMEDRIVE} && defined $ENV{HOMEPATH})
{
    $is_win = 1;
} else {
    $is_win = 0;
}

#set_opt 'debug',   'true';
#set_opt 'dryrun',  'true';
set_opt 'verbose', 'true' if (defined $ENV{DEWI_VERBOSE});

DIR: foreach my $dir (split /:/, $ENV{PATH}) {
    if (-x $dir . '/emacs') {
        $MY_EMACS = $dir . '/emacs';
        last DIR;
    }
}

$MY_EMACS = $ENV{EMACS} if (defined $ENV{EMACS} && $ENV{EMACS} ne q{});

# I need emacs on Win7, too. `dewi' will run in `cygwin'.
my ($method, @emacs_dirs);
if (defined $ENV{OSTYPE} && $ENV{OSTYPE} =~ m/cygwin/i) {
    # Let's not burden windoze with symlinks.
    $method = 'copy';
} else {
    $method = 'symlink';
}

push @emacs_dirs, $ENV{HOME} . '/.emacs.d';

# Well, the windows7 support makes this Dewifile a little messy. I hate having
# to change things in two places all the time, to let's add some Perl to cure
# that situation. Now, for simple globs I just have to add glob->destination
# mappings to `%ft_regular_globs'. And for recursive registration, I only have
# to add base->destination mappings to `%ft_recursive_globs'. The rest should
# then fall in place.

my %ft_regular_globs = (
    'dict/*-mode' => '/.emacs.d/dict',
    'themes/*.el' => '/.emacs.d/themes',
    'vendor/*.el' => '/.emacs.d/vendor' );

my %ft_recursive_globs = (
    'private'  => '/.emacs.d',
    'snippets' => '/.emacs.d' );

my %vendor_packages = (
    'use-package' => { globs => [ 'use-package.el',
                                  'bind-key.el' ] },

    'evil' => { globs => [ '*.el',
                           'lib/undo-tree.el' ],
                excludes => [ 'evil-tests.el' ] },

    'highlight-parentheses'
        => { globs => [ 'highlight-parentheses.el' ] },

    'ido-hacks' => { globs => [ 'ido-hacks.el' ] },

    'paredit' => { globs => [ 'paredit.el' ] },

    'winring' => { globs => [ '*.el' ] },

    'rscope' => { globs => [ 'rscope.el',
                             'rscope-nav.el' ] },

    'markdown-mode' => { globs => [ 'markdown-mode.el' ] },

    'textile-mode' => { globs => [ 'textile-mode.el' ] },

    'ox-textile' => { globs => [ 'ox-textile.el' ] },

    'htmlize' => { globs => [ 'htmlize.el' ] },

    'wide-n' => { globs => [ 'wide-n.el' ] } );

sub exclude {
    my ($excludes) = @_;
    my %excl = map { $_ => 1 } @{ $excludes };
    return sub {grep { not exists $excl{$_} } @_ };
}

sub with_prefix {
    my ($prefix, $data) = @_;
    my @ret = map { qq{$prefix/$_} } @{ $data };
    return \@ret;
}

sub ft_vendor_register {
    my ($dest, $name, $data) = @_;
    register { glob => with_prefix(qq{vendor/$name}, $data->{globs}),
               post_glob => exclude(with_prefix(qq{vendor/$name},
                                                $data->{excludes})),
               method => $method,
               destination => qq{$dest/vendor/$name} };
}

sub ft_register_vendors {
    my ($dest) = @_;
    foreach my $vendor (sort keys %vendor_packages) {
        ft_vendor_register($dest, $vendor, $vendor_packages{$vendor});
    }
}

sub ft_emacs_register {
    my ($glob, $dest) = @_;

    register { glob        => $glob,
               method      => $method,
               destination => $dest };
}

sub ft_emacs_rereg {
    # Like `ft_emacs_register()', but recursive.
    my ($base, $dest) = @_;

    register { glob        => \&recursivefiles,
               globarg     => { basedir => $base,
                                regex   => '.' },
               post_glob   => sub {
                   return remove_hashes( remove_tilde(@_) ) },
               method      => $method,
               destination => \&recursive_dirname,
               destarg     => $dest };
}

sub ft_register_all {
    my ($dest) = @_;
    my $emacs_d = $dest . q{/.emacs.d};

    ft_emacs_register('init.el', $emacs_d);
    ft_emacs_register('gnus.el', $emacs_d);
    deploy_directory($emacs_d . "/server");

    foreach my $glob (keys %ft_regular_globs) {
        ft_emacs_register($glob, $dest . $ft_regular_globs{$glob});
    }
    foreach my $base (keys %ft_recursive_globs) {
        ft_emacs_rereg($base, $dest . $ft_recursive_globs{$base});
    }
    ft_register_vendors $emacs_d;
}

ft_register_all q{~};

# Installing in ~/ will make cygwin emacs work. Win32-Emacs will need
# its setup elsewhere. On win7, that's 'C:\Users\MyName\AppData\Roaming".
# We can get that path using $HOMEDRIVE and $HOMEPATH, so:
if ($is_win) {
    my $dest = $ENV{HOMEDRIVE} . $ENV{HOMEPATH};
    $is_win = 1;
    open DAT, "cygpath -t unix \"$dest\" |"
        or early_exit "Couldn't run `cygpath': $!";
    $dest = <DAT>;
    chomp $dest;
    close DAT;
    if (defined $ENV{MSWINTYPE} && $ENV{MSWINTYPE} eq q{WINXP}) {
        $dest = $dest . '/Application Data';
    } else {
        # Assume Windows 7 and later.
        $dest = $dest . '/AppData/Roaming';
    }

    ft_register_all $dest;
    push @emacs_dirs, $dest;
}

sub emacs_byte_compile {
    my (@cmd, $old_dir);

    $old_dir = getcwd();
    push @cmd, $MY_EMACS, q{-batch}, q{-q}, q{-no-site-file}, q{-no-init-file};
    push @cmd, q{-l}, qq{$old_dir/compile.el};
    chdir($emacs_dirs[0]) or early_exit "Couldn't chdir($emacs_dirs[0]): $!";
    system(@cmd);
    chdir $old_dir or early_exit "Couldn't chdir($old_dir): $!";
}

if (-f q{compile.el}) {
    add_hook { type => 'perl',
               event => 'post-deploy',
               code => \&emacs_byte_compile };
} else {
    add_hook { type => 'perl',
               event => 'post-deploy',
               code => sub {
                   print "\n  Byte-compilation disabled, run:\n";
                   print "    % make gen-compile-el\n";
                   print "  ...to enable.\n\n" } };
}

unless ($method eq q{copy}) {
    foreach my $d (qw{ ~/.emacs.d/vendor
                       ~/.emacs.d/private })
    {
        nuke_dead_links_register $d, q{*.el};
    }
    foreach my $d (grep { -d } glob($ENV{HOME} . "/.emacs.d/vendor/*")) {
        nuke_dead_links_register $d, q{*.el};
    }
    nuke_dead_links_register q{~/.emacs.d/snippets}, q{*};
    foreach my $d (grep { -d } glob($ENV{HOME} . "/.emacs.d/snippets/*")) {
        nuke_dead_links_register $d, q{*};
    }
    add_hook { type  => 'perl',
               event => 'pre-deploy',
               code  => \&nuke_dead_links };
}

end