File: leiningen.pm

package info (click to toggle)
dh-clojure 0.2.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144 kB
  • sloc: perl: 101; makefile: 72; sh: 13
file content (166 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download
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
package Debian::Debhelper::Buildsystem::leiningen;

=head1 NAME

leiningen -- debhelper build system class for Clojure packages

=head1 DESCRIPTION

See L<dh-clojure-lein(7)> for additional information.

=head1 SEE ALSO

L<dh-clojure-lein(7)>, L<dh(1)>, and L<lein(1)>

=cut

use Cwd;
use File::Path qw(remove_tree);
use strict;
use warnings;
use parent qw(Debian::Debhelper::Buildsystem);
use Debian::Debhelper::Dh_Lib;

sub DESCRIPTION {
    'Leiningen'
}

sub DEFAULT_BUILD_DIRECTORY {
    'target'
}

sub check_auto_buildable {
    my $this = shift;
    return $this->get_sourcepath('project.clj') ? 0 : 1;
}

my $lein = '/usr/bin/lein';
my $reltmp = 'debian/dh-clojure/tmp';
my $abstmp = getcwd . '/debian/dh-clojure/tmp';
my $local_repo = "$abstmp/maven-repo";

sub new {
    my $class = shift;
    my $this  = $class->SUPER::new(@_);

    # key project attributes
    $this->{name} = $this->_get_project('name');
    $this->{version} = $this->_get_project('version');

    # path to jar artifacts
    $this->{artifact} = "target/$this->{name}-$this->{version}.jar";
    $this->{test_artifact} = "target/test/$this->{name}-$this->{version}-test.jar";

    # Absolute because (at least) subprocs might change the parent
    # directory and run lein, e.g. DEB_DH_CLJ_LEIN_CREATE_MAVEN_REPO.
    $ENV{'LEIN_HOME'} = "$abstmp/lein-home";

    # lein may not attempt to access the network
    # (Debian policy, section 4.9)
    $ENV{'LEIN_OFFLINE'} = 'true';

    return $this;
}

sub _get_project {
    my $this = shift;
    my ($property) = @_;

    my $cmd = "DEB_DH_CLJ_LEIN_BUILD=true $lein"
        . " update-in : assoc :local-repo '\"$local_repo\"' --"
        . " update-in :$property println --"
        . ' version';

    my $output = `$cmd`;
    if ($?) {
        print STDERR "$output\n"; # because a stacktrace shows up there
        error_exitcode($cmd)
    }

    my ($value) = split(/\n/, $output);
    return $value;
}

sub _do_lein {
    my $this = shift;
    doit({'update_env' => {'DEB_DH_CLJ_LEIN_BUILD' => 'true'}},
         $lein, 'update-in', ':', 'assoc', ':local-repo', "\"$local_repo\"",
         '--', @_);
}

sub _create_maven_repo {
    # This function also runs at the start of clean() to provide the
    # normal environment for lein clean (including dh-clojure-lein),
    # so it should be careful to "always" work.
    doit('mkdir', '-p', $abstmp);
    # This is currently for dh-clojure internal use only.
    my $override = $ENV{'DEB_DH_CLJ_LEIN_CREATE_MAVEN_REPO'};
    if ($override) {
        doit($override, '/usr/share/maven-repo', "$abstmp/maven-repo");
    } else {
        remove_tree("$abstmp/maven-repo");
        doit('ln', '-sf', '/usr/share/maven-repo', "$abstmp/maven-repo");
    }
}

sub configure {
    _create_maven_repo();
}

sub build {
    my $this = shift;

    $this->_do_lein('pom', "$reltmp/pom.xml"); # only accepts relative paths
    $this->_do_lein('jar');

    doit('test', '-f', $this->{artifact});
}

sub install {
    my $this = shift;
    my $pomsfile = $dh{MAINPACKAGE} . '.poms';

    # write debian/<package>.poms file for maven-repo-helper
    my $pomsline = "$reltmp/pom.xml --artifact=$this->{artifact} --usj-name=$this->{name} --java-lib";
    complex_doit("echo '$pomsline' > debian/$pomsfile");

    # add test artifact to poms file
    if (defined($ENV{DEB_DH_CLJ_INSTALL_TEST_JAR})
        && $ENV{DEB_DH_CLJ_INSTALL_TEST_JAR} eq 'true') {
        $pomsline = "$reltmp/pom.xml --artifact=$this->{test_artifact}"
            . " --usj-name=$this->{name}-test --java-lib --classifier=test";
        complex_doit("echo '$pomsline' >> debian/$pomsfile");
    }

    # install build artifacts to /usr/share/{java,maven-repo}
    doit('mh_install');

    # Since we handle our own .pom files, we don't want
    # mh_resolve_dependencies to touch them, and right now it will
    # (via mh_patchpoms) unless .debianVersion exists.  It's easy to
    # see where that happens in the current source, but it's not
    # documented as far as I've seen, and so we may eventually want to
    # consider pursuing a --no-patch option or something.
    doit('touch', '.debianVersion');

    # generate ${maven:Depends} substvar
    doit('mh_resolve_dependencies', '--non-interactive', '--offline', '--build', "-p$dh{MAINPACKAGE}", '--non-explore');
}

sub test {
    my $this = shift;
    $this->_do_lein('test');
}

sub clean {
    my $this = shift;
    my $pomsfile = $dh{MAINPACKAGE} . '.poms';

    _create_maven_repo(); # provide dh-clojure-lein
    $this->_do_lein('clean');
    doit('rm', '-f', 'debian/' . $pomsfile, '.debianVersion', '.lein-failures');
    remove_tree($abstmp);
    doit('rmdir', '--ignore-fail-on-non-empty', 'debian/dh-clojure');
}

1;