-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
BuildInitPlugin.java
130 lines (109 loc) · 5.1 KB
/
BuildInitPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.buildinit.plugins;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.internal.lambdas.SerializableLambdas;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.jvm.internal.JvmPluginServices;
import org.gradle.api.provider.Provider;
import org.gradle.api.specs.Spec;
import org.gradle.buildinit.InsecureProtocolOption;
import org.gradle.buildinit.tasks.InitBuild;
import org.gradle.internal.file.RelativeFilePathResolver;
import javax.annotation.Nullable;
import java.io.File;
/**
* The build init plugin.
*
* @see <a href="https://docs.gradle.org/current/userguide/build_init_plugin.html">Build Init plugin reference</a>
*/
public abstract class BuildInitPlugin implements Plugin<Project> {
private static final String COMMENTS_PROPERTY = "org.gradle.buildinit.comments";
@Override
public void apply(Project project) {
if (project.getParent() == null) {
project.getTasks().register("init", InitBuild.class, initBuild -> {
initBuild.setGroup("Build Setup");
initBuild.setDescription("Initializes a new Gradle build.");
ProjectInternal projectInternal = (ProjectInternal) project;
RelativeFilePathResolver resolver = projectInternal.getFileResolver();
File buildFile = project.getBuildFile();
FileDetails buildFileDetails = FileDetails.of(buildFile, resolver);
File settingsFile = projectInternal.getGradle().getSettings().getSettingsScript().getResource().getLocation().getFile();
FileDetails settingsFileDetails = FileDetails.of(settingsFile, resolver);
initBuild.onlyIf(
"There is no build script or settings script",
new InitBuildOnlyIfSpec(buildFileDetails, settingsFileDetails, initBuild.getLogger())
);
ProjectInternal.DetachedResolver detachedResolver = projectInternal.newDetachedResolver();
initBuild.getProjectLayoutRegistry().getBuildConverter().configureClasspath(
detachedResolver, project.getObjects(), projectInternal.getServices().get(JvmPluginServices.class));
initBuild.getUseDefaults().convention(false);
initBuild.getInsecureProtocol().convention(InsecureProtocolOption.WARN);
initBuild.getComments().convention(getCommentsProperty(project).orElse(true));
});
}
}
private static Provider<Boolean> getCommentsProperty(Project project) {
return project.getProviders().gradleProperty(COMMENTS_PROPERTY).map(SerializableLambdas.transformer(Boolean::parseBoolean));
}
private static class InitBuildOnlyIfSpec implements Spec<Task> {
private final FileDetails buildFile;
private final FileDetails settingsFile;
private final Logger logger;
private InitBuildOnlyIfSpec(FileDetails buildFile, FileDetails settingsFile, Logger logger) {
this.buildFile = buildFile;
this.settingsFile = settingsFile;
this.logger = logger;
}
@Override
public boolean isSatisfiedBy(Task element) {
String skippedMsg = reasonToSkip(buildFile, settingsFile);
if (skippedMsg != null) {
logger.warn(skippedMsg);
return false;
}
return true;
}
}
private static String reasonToSkip(FileDetails buildFile, FileDetails settingsFile) {
if (buildFile != null && buildFile.file.exists()) {
return "The build file '" + buildFile.pathForDisplay + "' already exists. Skipping build initialization.";
}
if (settingsFile != null && settingsFile.file.exists()) {
return "The settings file '" + settingsFile.pathForDisplay + "' already exists. Skipping build initialization.";
}
return null;
}
private static class FileDetails {
final File file;
final String pathForDisplay;
public FileDetails(File file, String pathForDisplay) {
this.file = file;
this.pathForDisplay = pathForDisplay;
}
@Nullable
public static FileDetails of(@Nullable File file, RelativeFilePathResolver resolver) {
if (file == null) {
return null;
}
return new FileDetails(file, resolver.resolveForDisplay(file));
}
}
}