# A debhelper build system class for handling Gradle based projects.
#
# Copyright: 2015, Emmanuel Bourg
# License: Apache-2.0

package Debian::Debhelper::Buildsystem::gradle;

use strict;
use base 'Debian::Debhelper::Buildsystem';
use Debian::Debhelper::Dh_Lib qw(%dh doit);

sub DESCRIPTION {
	"Gradle (build.gradle)"
}

sub check_auto_buildable {
	my $this=shift;
	return (-e $this->get_sourcepath("build.gradle")) ? 1 : 0;
}

sub new {
	my $class=shift;
	my $this=$class->SUPER::new(@_);
	
	@{$this->{gradle_cmd}} = ("gradle",
		"--info",
		"--console", "plain",
		"--offline",
		"--stacktrace",
		"--no-daemon",
		"--refresh-dependencies",
		"--gradle-user-home", ".gradle",
	#	"--init-script", "/usr/share/gradle-debian-helper/init.gradle",
		);
	
	# Copy the init script under .gradle/init.d to work around a bug with the --init-script parameter (GRADLE-3197)
	$this->doit_in_builddir("mkdir", "-p", ".gradle/init.d");
	$this->doit_in_builddir("cp", "/usr/share/gradle-debian-helper/init.gradle", ".gradle/init.d/");
	
	return $this;
}

sub build {
	my $this=shift;

	if (!@_) {
		push(@_, "jar");
	}
	
	$this->doit_in_builddir(@{$this->{gradle_cmd}}, @_);
}

sub clean {
	my $this=shift;

	doit(@{$this->{gradle_cmd}}, "clean");
	doit("rm", "-Rf", "$this->{cwd}/.gradle");
}

1
