blob: 69007e85953bde67ba5e687514f21a4b8432871e [file] [log] [blame]
Pierre De Rop3a00a212015-03-01 09:27:46 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*
21 * Master Gradle initialization script
22 *
23 * Depends on bnd_* values from gradle.properties.
24 */
25
26import aQute.bnd.build.Workspace
27
28/* Add bnd as a script dependency */
29buildscript {
30 dependencies {
31 def bndURI = rootDir.toURI().resolve(bnd_jar)
32 if (bndURI.scheme != 'file') {
33 /* If not a local file, copy to a local file in cnf/cache */
34 def cnfCache = mkdir("${rootDir}/${bnd_cnf}/cache")
35 def bndJarFile = new File(cnfCache, 'biz.aQute.bnd.gradle.jar')
36 if (!bndJarFile.exists()) {
37 println "Downloading ${bndURI} to ${bndJarFile} ..."
38 bndURI.toURL().withInputStream { is ->
39 bndJarFile.withOutputStream { os ->
40 def bos = new BufferedOutputStream( os )
41 bos << is
42 }
43 }
44 }
45 bndURI = bndJarFile.toURI()
46 }
47 classpath files(bndURI)
48
49 /* After the rootProject is created, pass URI to projects */
50 gradle.rootProject { rootProject ->
51 rootProject.ext.bndURI = bndURI
52 }
53 }
54}
55
56/* Initialize the bnd workspace */
57def workspace = Workspace.getWorkspace(rootDir, bnd_cnf)
58if (workspace == null) {
59 throw new GradleException("Unable to load workspace ${rootDir}/${bnd_cnf}")
60}
61
62/* Add cnf project to the graph */
63include bnd_cnf
64
65/* Start with the declared build project name */
66def defaultProjectName = bnd_build
67
68/* If in a subproject, use the subproject name */
69for (def currentDir = startParameter.currentDir; currentDir != rootDir; currentDir = currentDir.parentFile) {
70 defaultProjectName = currentDir.name
71}
72
73/* Build a set of project names we need to include from the specified tasks */
74def projectNames = startParameter.taskNames.collect { taskName ->
75 def elements = taskName.split(':')
76 switch (elements.length) {
77 case 1:
78 return defaultProjectName
79 case 2:
80 return elements[0].empty ? bnd_build : elements[0]
81 default:
82 return elements[0].empty ? elements[1] : elements[0]
83 }
84}.toSet()
85
86/* Include the default project name if in a subproject or no tasks specified */
87if ((startParameter.currentDir != rootDir) || projectNames.empty) {
88 projectNames += defaultProjectName
89}
90
91/* If bnd_build used but declared empty, add all non-private folders of rootDir */
92if (projectNames.remove('')) {
93 rootDir.eachDir {
94 def projectName = it.name
95 if (!projectName.startsWith('.')) {
96 projectNames += projectName
97 }
98 }
99}
100
101/* Add each project and its dependencies to the graph */
102projectNames.each { projectName ->
103 // Don't build the org.apache.felix.dependencymanager.benchmark, which requires java8 (build the benchmark bundle only makes sense within eclipse
104 if (! projectName.equals("org.apache.felix.dependencymanager.benchmark")) {
105 include projectName
106 def project = getBndProject(workspace, projectName)
107 project?.dependson.each {
108 include it.name
109 }
110 }
111}
112
113/* Get the bnd project for the specified project name */
114def getBndProject(Workspace workspace, String projectName) {
115 def project = workspace.getProject(projectName)
116 if (project == null) {
117 return null
118 }
119 project.prepare()
120 if (project.isValid()) {
121 return project
122 }
123
124 project.getInfo(workspace, "${rootDir} :")
125 def errorCount = 0
126 project.warnings.each {
127 println "Warning: ${it}"
128 }
129 project.errors.each {
130 println "Error : ${it}"
131 errorCount++
132 }
133 if (!project.isOk()) {
134 def str = 'even though no errors were reported'
135 if (errorCount == 1) {
136 str = 'one error was reported'
137 } else if (errorCount > 1) {
138 str = "${errorCount} errors were reported"
139 }
140 throw new GradleException("Project ${rootDir}/${projectName} is invalid, ${str}")
141 }
142 throw new GradleException("Project ${rootDir}/${projectName} is not a valid bnd project")
143}
144
145/* After the rootProject is created, set up some properties. */
146gradle.rootProject { rootProject ->
147 rootProject.ext.bndWorkspace = workspace
148 rootProject.ext.cnf = rootProject.project(bnd_cnf)
149}