Skip to content

Commit

Permalink
Merge branch 'master' into bugs/1591208
Browse files Browse the repository at this point in the history
  • Loading branch information
josepht committed Jul 13, 2016
2 parents e88b5b2 + 48cb3ad commit 5690ce5
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 8 deletions.
93 changes: 93 additions & 0 deletions snapcraft/plugins/gradle.py
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))
18 changes: 10 additions & 8 deletions snapcraft/tests/test_commands_list_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class ListPluginsCommandTestCase(tests.TestCase):

# plugin list when wrapper at MAX_CHARACTERS_WRAP
default_plugin_output = (
'ant catkin copy gulp kbuild make nil '
'plainbox-provider python3 scons \n'
'autotools cmake go jdk kernel maven nodejs '
'python2 qmake tar-content\n')
'ant catkin copy gradle jdk kernel maven '
'nodejs python2 qmake tar-content\n'
'autotools cmake go gulp kbuild make nil '
'plainbox-provider python3 scons\n')

def test_list_plugins_non_tty(self):
fake_terminal = fixture_setup.FakeTerminal(isatty=False)
Expand All @@ -47,9 +47,11 @@ def test_list_plugins_small_terminal(self):
self.useFixture(fake_terminal)

expected_output = (
'ant copy kbuild nil python3 \n'
'autotools go kernel nodejs qmake \n'
'catkin gulp make plainbox-provider scons \n'
'cmake jdk maven python2 tar-content\n')
'ant go kernel plainbox-provider tar-content\n'
'autotools gradle make python2 \n'
'catkin gulp maven python3 \n'
'cmake jdk nil qmake \n'
'copy kbuild nodejs scons \n')

main(['list-plugins'])
self.assertEqual(fake_terminal.getvalue(), expected_output)
122 changes: 122 additions & 0 deletions snapcraft/tests/test_plugin_gradle.py
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']),
])

0 comments on commit 5690ce5

Please sign in to comment.