-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bugs/1591208
- Loading branch information
Showing
3 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright (C) 2016 Canonical Ltd | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""This plugin is useful for building parts that use gradle. | ||
The gradle build system is commonly used to build Java projects. | ||
The plugin requires a pom.xml in the root of the source tree. | ||
This plugin uses the common plugin keywords as well as those for "sources". | ||
For more information check the 'plugins' topic for the former and the | ||
'sources' topic for the latter. | ||
Additionally, this plugin uses the following plugin-specific keywords: | ||
- gradle-options: | ||
(list of strings) | ||
flags to pass to the build using the gradle semantics for parameters. | ||
""" | ||
|
||
import glob | ||
import logging | ||
import os | ||
|
||
import snapcraft | ||
import snapcraft.common | ||
import snapcraft.plugins.jdk | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GradlePlugin(snapcraft.plugins.jdk.JdkPlugin): | ||
|
||
@classmethod | ||
def schema(cls): | ||
schema = super().schema() | ||
schema['properties']['gradle-options'] = { | ||
'type': 'array', | ||
'minitems': 1, | ||
'uniqueItems': True, | ||
'items': { | ||
'type': 'string', | ||
}, | ||
'default': [], | ||
} | ||
|
||
# Inform Snapcraft of the properties associated with building. If these | ||
# change in the YAML Snapcraft will consider the build step dirty. | ||
schema['build-properties'].append('gradle-options') | ||
|
||
return schema | ||
|
||
def __init__(self, name, options, project): | ||
super().__init__(name, options, project) | ||
|
||
def build(self): | ||
super().build() | ||
|
||
gradle_cmd = ['./gradlew', 'jar'] | ||
|
||
self.run(gradle_cmd + self.options.gradle_options) | ||
|
||
src = os.path.join(self.builddir, 'build', 'libs') | ||
jarfiles = glob.glob(os.path.join(src, '*.jar')) | ||
warfiles = glob.glob(os.path.join(src, '*.war')) | ||
|
||
if len(jarfiles) > 0: | ||
basedir = 'jar' | ||
elif len(warfiles) > 0: | ||
basedir = 'war' | ||
jarfiles = warfiles | ||
else: | ||
raise RuntimeError("could not find any" | ||
"built jar files for part") | ||
|
||
targetdir = os.path.join(self.installdir, basedir) | ||
os.makedirs(targetdir, exist_ok=True) | ||
for f in jarfiles: | ||
base = os.path.basename(f) | ||
os.link(f, os.path.join(targetdir, base)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright (C) 2016 Canonical Ltd | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
from unittest import mock | ||
|
||
import snapcraft | ||
from snapcraft import tests | ||
from snapcraft.plugins import gradle | ||
|
||
|
||
class GradlePluginTestCase(tests.TestCase): | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
class Options: | ||
gradle_options = [] | ||
self.options = Options() | ||
|
||
self.project_options = snapcraft.ProjectOptions() | ||
|
||
patcher = mock.patch('snapcraft.repo.Ubuntu') | ||
self.ubuntu_mock = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
def test_schema(self): | ||
schema = gradle.GradlePlugin.schema() | ||
|
||
properties = schema['properties'] | ||
self.assertTrue('gradle-options' in properties, | ||
'Expected "gradle-options" to be included in ' | ||
'properties') | ||
|
||
gradle_options = properties['gradle-options'] | ||
|
||
self.assertTrue( | ||
'type' in gradle_options, | ||
'Expected "type" to be included in "gradle-options"') | ||
self.assertEqual(gradle_options['type'], 'array', | ||
'Expected "gradle-options" "type" to be "array", but ' | ||
'it was "{}"'.format(gradle_options['type'])) | ||
|
||
self.assertTrue( | ||
'minitems' in gradle_options, | ||
'Expected "minitems" to be included in "gradle-options"') | ||
self.assertEqual(gradle_options['minitems'], 1, | ||
'Expected "gradle-options" "minitems" to be 1, but ' | ||
'it was "{}"'.format(gradle_options['minitems'])) | ||
|
||
self.assertTrue( | ||
'uniqueItems' in gradle_options, | ||
'Expected "uniqueItems" to be included in "gradle-options"') | ||
self.assertTrue( | ||
gradle_options['uniqueItems'], | ||
'Expected "gradle-options" "uniqueItems" to be "True"') | ||
|
||
@mock.patch.object(gradle.GradlePlugin, 'run') | ||
def test_build(self, run_mock): | ||
plugin = gradle.GradlePlugin('test-part', self.options, | ||
self.project_options) | ||
|
||
def side(l): | ||
os.makedirs(os.path.join(plugin.builddir, | ||
'build', 'libs')) | ||
open(os.path.join(plugin.builddir, | ||
'build', 'libs', 'dummy.jar'), 'w').close() | ||
|
||
run_mock.side_effect = side | ||
os.makedirs(plugin.sourcedir) | ||
|
||
plugin.build() | ||
|
||
run_mock.assert_has_calls([ | ||
mock.call(['./gradlew', 'jar']), | ||
]) | ||
|
||
@mock.patch.object(gradle.GradlePlugin, 'run') | ||
def test_build_war(self, run_mock): | ||
plugin = gradle.GradlePlugin('test-part', self.options, | ||
self.project_options) | ||
|
||
def side(l): | ||
os.makedirs(os.path.join(plugin.builddir, | ||
'build', 'libs')) | ||
open(os.path.join(plugin.builddir, | ||
'build', 'libs', 'dummy.war'), 'w').close() | ||
|
||
run_mock.side_effect = side | ||
os.makedirs(plugin.sourcedir) | ||
|
||
plugin.build() | ||
|
||
run_mock.assert_has_calls([ | ||
mock.call(['./gradlew', 'jar']), | ||
]) | ||
|
||
@mock.patch.object(gradle.GradlePlugin, 'run') | ||
def test_build_fail(self, run_mock): | ||
plugin = gradle.GradlePlugin('test-part', self.options, | ||
self.project_options) | ||
|
||
os.makedirs(plugin.sourcedir) | ||
with self.assertRaises(RuntimeError): | ||
plugin.build() | ||
|
||
run_mock.assert_has_calls([ | ||
mock.call(['./gradlew', 'jar']), | ||
]) |