-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerateCompileCommandsFileTask.java
166 lines (142 loc) · 5.82 KB
/
GenerateCompileCommandsFileTask.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright 2018 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 io.lacasse.vscode.gradle.internal.tasks;
import io.lacasse.vscode.internal.schemas.CompileCommand;
import org.gradle.api.Action;
import org.gradle.api.Transformer;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.apache.commons.io.IOUtils;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.language.cpp.CppBinary;
import org.gradle.language.cpp.tasks.CppCompile;
import org.gradle.nativeplatform.NativeBinarySpec;
import org.gradle.nativeplatform.platform.internal.NativePlatformInternal;
import org.gradle.nativeplatform.toolchain.internal.NativeToolChainInternal;
import org.gradle.nativeplatform.toolchain.internal.ToolType;
import javax.inject.Inject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static org.apache.commons.text.WordUtils.capitalize;
public class GenerateCompileCommandsFileTask extends JsonGeneratorTask<CompileCommandsFile> {
private final RegularFileProperty compileCommandsLocation = getProject().getObjects().fileProperty();
private final RegularFileProperty compiler = getProject().getObjects().fileProperty();
private final ListProperty<CompileCommandsConfiguration> compileCommands = getProject().getObjects().listProperty(CompileCommandsConfiguration.class);
public GenerateCompileCommandsFileTask() {
compileCommands.set(Collections.emptyList());
}
@Override
protected void configure(CompileCommandsFile object) {
for (CompileCommandsConfiguration configuration : compileCommands.get()) {
String command = null;
if (configuration.getOptionsFile().isPresent() && configuration.getOptionsFile().getAsFile().get().exists()) {
String compilerFlags = getCompilerFlags(configuration.getOptionsFile().get().getAsFile());
String compilerPath = getCompiler().get().getAsFile().getAbsolutePath();
command = compilerPath + " " + compilerFlags;
}
for (File sourceFile : configuration.getSources()) {
object.source(getProject().getProjectDir(), command, sourceFile);
}
}
}
private String getCompilerFlags(File optionsFile) {
try (InputStream inStream = new FileInputStream(optionsFile)) {
return IOUtils.readLines(inStream, Charset.defaultCharset()).stream().collect(Collectors.joining(" "));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
protected CompileCommandsFile create() {
return new CompileCommandsFile();
}
@OutputFile
public RegularFileProperty getCompileCommandsFileLocation() {
return compileCommandsLocation;
}
@Override
public File getOutputFile() {
return compileCommandsLocation.getAsFile().get();
}
@Override
public void setOutputFile(File outputFile) {
compileCommandsLocation.set(outputFile);
}
@Override
public File getInputFile() {
return null;
}
public void compileCommands(Action<CompileCommandsConfiguration> action) {
CompileCommandsConfiguration configuration = getProject().getObjects().newInstance(CompileCommandsConfiguration.class);
action.execute(configuration);
compileCommands.add(configuration);
}
@Nested
public ListProperty<CompileCommandsConfiguration> getCompileCommands() {
return compileCommands;
}
@InputFile
public RegularFileProperty getCompiler() {
return compiler;
}
public static String taskName(CppBinary binary) {
return taskName(binary.getName());
}
public static String taskName(NativeBinarySpec binary) {
return taskName(binary.getName());
}
private static String taskName(String name) {
return "generate" + capitalize(name) + "CompileCommands";
}
public static class CompileCommandsConfiguration {
private final ConfigurableFileCollection sources;
private final RegularFileProperty optionsFile;
@Inject
public CompileCommandsConfiguration(ObjectFactory objectFactory, ProjectLayout projectLayout) {
this.sources = projectLayout.configurableFiles();
this.optionsFile = objectFactory.fileProperty();
}
@InputFiles
public ConfigurableFileCollection getSources() {
return sources;
}
@InputFiles
@Optional
public RegularFileProperty getOptionsFile() {
return optionsFile;
}
}
}