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
|
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* 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.jetbrains.jps.gradle.model.impl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.containers.ConcurrentFactoryMap;
import com.intellij.util.containers.FactoryMap;
import com.intellij.util.xmlb.XmlSerializer;
import gnu.trove.THashMap;
import org.jdom.Document;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.builders.storage.BuildDataPaths;
import org.jetbrains.jps.gradle.model.JpsGradleExtensionService;
import org.jetbrains.jps.gradle.model.JpsGradleModuleExtension;
import org.jetbrains.jps.incremental.resources.ResourcesBuilder;
import org.jetbrains.jps.incremental.resources.StandardResourceBuilderEnabler;
import org.jetbrains.jps.model.JpsElementChildRole;
import org.jetbrains.jps.model.JpsElementFactory;
import org.jetbrains.jps.model.JpsSimpleElement;
import org.jetbrains.jps.model.ex.JpsElementChildRoleBase;
import org.jetbrains.jps.model.module.JpsDependencyElement;
import org.jetbrains.jps.model.module.JpsModule;
import java.io.File;
import java.util.Map;
/**
* @author Vladislav.Soroka
* @since 7/10/2014
*/
public class JpsGradleExtensionServiceImpl extends JpsGradleExtensionService {
private static final Logger LOG = Logger.getInstance(JpsGradleExtensionServiceImpl.class);
private static final JpsElementChildRole<JpsSimpleElement<Boolean>> PRODUCTION_ON_TEST_ROLE = JpsElementChildRoleBase.create("production on test");
private final Map<File, GradleProjectConfiguration> myLoadedConfigs =
new THashMap<File, GradleProjectConfiguration>(FileUtil.FILE_HASHING_STRATEGY);
private final FactoryMap<File, Boolean> myConfigFileExists = new ConcurrentFactoryMap<File, Boolean>() {
@Nullable
@Override
protected Boolean create(File key) {
return key.exists();
}
};
public JpsGradleExtensionServiceImpl() {
ResourcesBuilder.registerEnabler(new StandardResourceBuilderEnabler() {
@Override
public boolean isResourceProcessingEnabled(JpsModule module) {
// enable standard resource processing only if this is not a gradle module
// for gradle modules use gradle-aware resource builder
return getExtension(module) == null;
}
});
}
@Nullable
@Override
public JpsGradleModuleExtension getExtension(@NotNull JpsModule module) {
return module.getContainer().getChild(JpsGradleModuleExtensionImpl.ROLE);
}
@NotNull
@Override
public JpsGradleModuleExtension getOrCreateExtension(@NotNull JpsModule module) {
JpsGradleModuleExtension extension = module.getContainer().getChild(JpsGradleModuleExtensionImpl.ROLE);
if (extension == null) {
extension = new JpsGradleModuleExtensionImpl();
module.getContainer().setChild(JpsGradleModuleExtensionImpl.ROLE, extension);
}
return extension;
}
@Override
public void setProductionOnTestDependency(@NotNull JpsDependencyElement dependency, boolean value) {
if (value) {
dependency.getContainer().setChild(PRODUCTION_ON_TEST_ROLE, JpsElementFactory.getInstance().createSimpleElement(true));
}
else {
dependency.getContainer().removeChild(PRODUCTION_ON_TEST_ROLE);
}
}
@Override
public boolean isProductionOnTestDependency(@NotNull JpsDependencyElement dependency) {
JpsSimpleElement<Boolean> child = dependency.getContainer().getChild(PRODUCTION_ON_TEST_ROLE);
return child != null && child.getData();
}
@Override
public boolean hasGradleProjectConfiguration(@NotNull BuildDataPaths paths) {
return myConfigFileExists.get(new File(paths.getDataStorageRoot(), GradleProjectConfiguration.CONFIGURATION_FILE_RELATIVE_PATH));
}
@NotNull
@Override
public GradleProjectConfiguration getGradleProjectConfiguration(BuildDataPaths paths) {
final File dataStorageRoot = paths.getDataStorageRoot();
return getGradleProjectConfiguration(dataStorageRoot);
}
@NotNull
public GradleProjectConfiguration getGradleProjectConfiguration(@NotNull File dataStorageRoot) {
final File configFile = new File(dataStorageRoot, GradleProjectConfiguration.CONFIGURATION_FILE_RELATIVE_PATH);
GradleProjectConfiguration config;
synchronized (myLoadedConfigs) {
config = myLoadedConfigs.get(configFile);
if (config == null) {
config = new GradleProjectConfiguration();
try {
final Document document = JDOMUtil.loadDocument(configFile);
XmlSerializer.deserializeInto(config, document.getRootElement());
}
catch (Exception e) {
LOG.info(e);
}
myLoadedConfigs.put(configFile, config);
}
}
return config;
}
}
|