summaryrefslogtreecommitdiff
path: root/dh_elpa
blob: b62050d4a73bfa58331743e18665e839fdb9f697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/perl

=head1 NAME

dh_elpa - install emacs lisp packages into package build directories

=cut

use strict;
use Cwd qw{ getcwd };
use File::Temp qw{tempfile};
use IO::Handle;

use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_elpa> [S<I<debhelper options>>]  [S<I<pkg-file>>]

=head1 DESCRIPTION

B<dh_elpa> is a debhelper program that is responsible for installing
elpa style emacs lisp packages into package build directories.

=head1 FILES

=over 4

=item debian/I<package>.elpa

List of files to be installed into I<package> as an elpa package.

=back

=cut

init(options => {
	"byte-compile!" => \$dh{BYTECOMPILE},
});

=head1 OPTIONS

=over 4

=item B<--byte-compile>, B<--no-byte-compile>

Enable (default) or disable byte compilation of installed emacs lisp
files.  Disabling byte compilation changes the destination directory
to one that is found by the emacs package system.

=back

=cut

sub doit_quietly {
  my ($handle,$tmpfile) = tempfile(UNLINK=>1);
  my $exitcode;

  verbose_print(escape_shell(@_));
  open (CPERR,">&STDERR") or error "$!";
  open (CPOUT,">&STDOUT") or error "$!";
  STDOUT->fdopen($handle,'w');
  STDERR->fdopen($handle,'w');
  my $ret=doit_noerror(@_);
  STDOUT->fdopen(\*CPOUT,'w');
  STDERR->fdopen(\*CPERR,'w');

  if (!$ret){
    $exitcode=$?;
    seek $handle, 0, 0 or error "$!";
    print while (<$handle>);
    my $command=join(" ",@_);
    error("$command returned exit code ".($exitcode >> 8));
  }

}

my $templatedir = "/usr/share/debhelper/dh_elpa/emacsen-common";

sub maybe_install_helper{
  my ($package,$piece, $mode)=@_;
  my $file=pkgfile($package,"emacsen-$piece");

  my $tmp=tmpdir($package);
  my $ecdest="$tmp/usr/lib/emacsen-common/packages";

  # if there is file, leave it for dh_installemacsen
  if ($file eq '') {
    if (! -d "$ecdest/$piece") {
      doit("install","-d","$ecdest/$piece");
    }

    doit("install","-m$mode","$templatedir/$piece",
         "$ecdest/$piece/$package");
  }
}

$dh{BYTECOMPILE} = 1 unless defined($dh{BYTECOMPILE});

my $elpadir;

my $dhelpadir="/usr/share/emacs/site-lisp/elpa";

# TODO: do we really need a seperate elpa-src hierarchy?
if ($dh{BYTECOMPILE}) {
  $elpadir="/usr/share/emacs/site-lisp/elpa-src";
} else {
  $elpadir=$dhelpadir;
}

foreach my $package (@{$dh{DOPACKAGES}}) {

  my $tmp=tmpdir($package);
  my $file=pkgfile($package,"elpa");

  if ($dh{BYTECOMPILE}) {
    maybe_install_helper($package,'compat','0644');
    maybe_install_helper($package,'install','0755');
    maybe_install_helper($package,'remove','0755');

    if (! $dh{NOSCRIPTS}) {
      autoscript($package,"postinst","postinst-emacsen",
                 "s/#PACKAGE#/$package/");
      autoscript($package,"prerm","prerm-emacsen",
                 "s/#PACKAGE#/$package/");
    }
  }

  my $elpapkg=$package;
  # TODO do this more sanely or at least allow an override
  $elpapkg =~ s/^elpa-//;
  verbose_print("Using elpa package name $elpapkg");

  my @files;

  # Call isnative because it sets $dh{VERSION}
  # as a side effect.
  isnative($package);
  if ($file) {
    @files=filearray($file, ".");
  }
  if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL})
      && @ARGV) {
    push @files, @ARGV;
  }

  my $pkg_file;
  if (scalar(@files) == 1) {
    $pkg_file=$files[0];
  } else {
    my $cwd = getcwd();
    my $tempdir = "${cwd}/debian/.debhelper/elpa";
    {
      require File::Path;
      File::Path::rmtree $tempdir ||
	  error "cleaning $tempdir";

      File::Path::make_path $tempdir ||
	  error "creating $tempdir";
    }
    my $version = $dh{VERSION} or error "version not found!";
    $version =~ s/-[^-]+//;  # strip Debian version
    my $pkg_dir = "$elpapkg-$version";

    $pkg_file = "$tempdir/$pkg_dir.tar";
    mkdir "$tempdir/$pkg_dir" or error "$!";

    # copy files into tempdir, flattening hierarchy
    # TODO: do this more correctly
    foreach my $el_file (@files) {
      doit("cp", "-a", $el_file, "$tempdir/$pkg_dir");
    }

    chdir $tempdir or die "$!";
    doit("tar","cf",$pkg_file,$pkg_dir);
    chdir $cwd or die "$!";
  }

  doit_quietly(qw{emacs -batch -Q -l package},
               '--eval',"(add-to-list 'package-directory-list \"$dhelpadir\")",
               '--eval',"(add-to-list 'package-directory-list \"$elpadir\")",
       qw{-f package-initialize -l dh-elpa.el},
       qw{-f dhelpa-batch-install-file}, "$tmp/$elpadir", $pkg_file);
}

=head1 EXAMPLES

Here is an example of using the helper in a dh(1) style debian/rules

=over 4

override_dh_install:
        dh_install
        dh_elpa

=back

=cut