Debian Codegraph @

gradle-helper-plugin/src/main/java/org/debian/gradle/ProjectNameInitializer.java

raw
1 /**
2 * Copyright 2016 Emmanuel Bourg
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.debian.gradle;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Scanner;
22
23 import org.gradle.BuildAdapter;
24 import org.gradle.api.initialization.Settings;
25 import org.gradle.api.invocation.Gradle;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30 * Build listener initializing the name of the root project if necessary.
31 *
32 * Gradle uses the rootProject.name property in the settings.gradle file,
33 * but if the property isn't defined the name of the directory is used instead.
34 * Thus the name of the artifacts created depends on the name of the base
35 * directory, and this can lead to build failures during the install phase
36 * because the files don't have the expected name.
37 *
38 * To work around this issue this build listener changes the project name
39 * on the fly and uses the name of the source package if the settings.gradle
40 * file is missing, or if it doesn't define the rootProject.name property.
41 */
42 class ProjectNameInitializer extends BuildAdapter {
43
44 private final Logger log = LoggerFactory.getLogger(ProjectNameInitializer.class);
45
46 @Override
47 public void settingsEvaluated(Settings settings) {
48 if ("buildSrc".equals(settings.getRootProject().getName())) {
49 return;
50 }
51
52 File settingsFile = new File(settings.getSettingsDir(), "settings.gradle");
53 if (!settingsFile.exists()) {
54 log.info("\tSettings file not found (" + settingsFile + ")");
55 } else if (!contains(settingsFile, "rootProject.name")) {
56 log.info("\tSettings file found (" + settingsFile + "), but rootProject.name isn't defined");
57 } else {
58 return;
59 }
60
61 String name = System.getProperty("debian.package");
62 log.info("\tRoot project name not defined in settings.gradle, defaulting to '" + name + "' instead of the name of the root directory '" + settings.getRootProject().getName() + "'");
63 settings.getRootProject().setName(name);
64 }
65
66 private boolean contains(File file, String s) {
67 try (Scanner scanner = new Scanner(file, "ISO-8859-1")) {
68 while (scanner.hasNextLine()) {
69 String line = scanner.nextLine();
70 if (line != null && line.trim().startsWith(s)) {
71 return true;
72 }
73 }
74 } catch (IOException e) {
75 log.warn("Couldn't look for " + s + " in " + file, e);
76 }
77
78 return false;
79 }
80
81 }