/
AbstractExternalSystemLocalSettings.java
186 lines (159 loc) · 7.69 KB
/
AbstractExternalSystemLocalSettings.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.externalSystem.settings;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.externalSystem.ExternalSystemManager;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
import com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo;
import com.intellij.openapi.externalSystem.model.project.ExternalProjectBuildClasspathPojo;
import com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.util.SmartList;
import com.intellij.util.containers.CollectionFactory;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import static com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil.*;
/**
* Holds local project-level external system-related settings (should be kept at the '*.iws' or 'workspace.xml').
* <p/>
* For example, we don't want to store recent tasks list at common external system settings, hence, that data
* is kept at user-local settings.
* <p/>
* <b>Note:</b> non-abstract subclasses of this class are expected to be marked by {@link State} annotation configured
* to be stored under a distinct name at a {@link StoragePathMacros#CACHE_FILE}.
*/
public abstract class AbstractExternalSystemLocalSettings<S extends AbstractExternalSystemLocalSettings.State> {
protected S state;
@NotNull private final ProjectSystemId myExternalSystemId;
@NotNull private final Project myProject;
protected AbstractExternalSystemLocalSettings(@NotNull ProjectSystemId externalSystemId, @NotNull Project project, @NotNull S state) {
myExternalSystemId = externalSystemId;
myProject = project;
this.state = state;
}
protected AbstractExternalSystemLocalSettings(@NotNull ProjectSystemId externalSystemId, @NotNull Project project) {
//noinspection unchecked
this(externalSystemId, project, (S)new State());
}
/**
* Asks current settings to drop all information related to external projects which root configs are located at the given paths.
*
* @param linkedProjectPathsToForget target root external project paths
*/
public void forgetExternalProjects(@NotNull Set<String> linkedProjectPathsToForget) {
Map<ExternalProjectPojo, Collection<ExternalProjectPojo>> projects = state.availableProjects;
for (Iterator<Map.Entry<ExternalProjectPojo, Collection<ExternalProjectPojo>>> it = projects.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<ExternalProjectPojo, Collection<ExternalProjectPojo>> entry = it.next();
if (linkedProjectPathsToForget.contains(entry.getKey().getPath())) {
it.remove();
}
}
if (!ContainerUtil.isEmpty(state.recentTasks)) {
for (Iterator<ExternalTaskExecutionInfo> it = state.recentTasks.iterator(); it.hasNext(); ) {
ExternalTaskExecutionInfo taskInfo = it.next();
String path = taskInfo.getSettings().getExternalProjectPath();
if (linkedProjectPathsToForget.contains(path) ||
linkedProjectPathsToForget.contains(getRootProjectPath(path, myExternalSystemId, myProject))) {
it.remove();
}
}
}
for (Iterator<Map.Entry<String, ExternalProjectBuildClasspathPojo>> it = state.projectBuildClasspath.entrySet().iterator();
it.hasNext(); ) {
Map.Entry<String, ExternalProjectBuildClasspathPojo> entry = it.next();
if (linkedProjectPathsToForget.contains(entry.getKey())
|| linkedProjectPathsToForget.contains(getRootProjectPath(entry.getKey(), myExternalSystemId, myProject))) {
it.remove();
}
}
for (Iterator<Map.Entry<String, SyncType>> it = state.projectSyncType.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, SyncType> entry = it.next();
if (linkedProjectPathsToForget.contains(entry.getKey())
|| linkedProjectPathsToForget.contains(getRootProjectPath(entry.getKey(), myExternalSystemId, myProject))) {
it.remove();
}
}
Map<String, Long> modificationStamps = state.modificationStamps;
for (String path : linkedProjectPathsToForget) {
modificationStamps.remove(path);
}
}
@NotNull
public Map<ExternalProjectPojo, Collection<ExternalProjectPojo>> getAvailableProjects() {
return state.availableProjects;
}
public void setAvailableProjects(@NotNull Map<ExternalProjectPojo, Collection<ExternalProjectPojo>> projects) {
state.availableProjects = projects;
}
@NotNull
public List<ExternalTaskExecutionInfo> getRecentTasks() {
return ContainerUtil.notNullize(state.recentTasks);
}
@NotNull
public Map<String, Long> getExternalConfigModificationStamps() {
return state.modificationStamps;
}
@NotNull
public Map<String, ExternalProjectBuildClasspathPojo> getProjectBuildClasspath() {
return state.projectBuildClasspath;
}
@NotNull
public Map<String, SyncType> getProjectSyncType() {
return state.projectSyncType;
}
@Nullable
public S getState() {
return state;
}
public void loadState(@NotNull State state) {
//noinspection unchecked
this.state = (S)state;
pruneOutdatedEntries();
}
private void pruneOutdatedEntries() {
ExternalSystemManager<?, ?, ?, ?, ?> manager = getManager(myExternalSystemId);
assert manager != null;
Set<String> pathsToForget = new HashSet<>();
for (ExternalProjectPojo pojo : state.availableProjects.keySet()) {
pathsToForget.add(pojo.getPath());
}
for (ExternalTaskExecutionInfo taskInfo : ContainerUtil.notNullize(state.recentTasks)) {
pathsToForget.add(taskInfo.getSettings().getExternalProjectPath());
}
AbstractExternalSystemSettings<?, ?, ?> settings = manager.getSettingsProvider().fun(myProject);
for (ExternalProjectSettings projectSettings : settings.getLinkedProjectsSettings()) {
pathsToForget.remove(projectSettings.getExternalProjectPath());
}
for (Module module : ModuleManager.getInstance(myProject).getModules()) {
if (!isExternalSystemAwareModule(myExternalSystemId, module)) continue;
pathsToForget.remove(getExternalProjectPath(module));
}
if (!pathsToForget.isEmpty()) {
forgetExternalProjects(pathsToForget);
}
}
public void setProjectBuildClasspath(Map<String, ExternalProjectBuildClasspathPojo> value) {
state.projectBuildClasspath = value;
}
@Deprecated(forRemoval = true)
public void fillState(@NotNull State otherState) {
otherState.recentTasks.clear();
otherState.availableProjects = state.availableProjects;
otherState.modificationStamps = state.modificationStamps;
otherState.projectBuildClasspath = state.projectBuildClasspath;
otherState.projectSyncType = state.projectSyncType;
}
public static class State {
public final List<ExternalTaskExecutionInfo> recentTasks = new SmartList<>();
public Map<ExternalProjectPojo, Collection<ExternalProjectPojo>> availableProjects = CollectionFactory.createSmallMemoryFootprintMap();
public Map<String/* linked project path */, Long/* last config modification stamp */> modificationStamps = CollectionFactory.createSmallMemoryFootprintMap();
public Map<String/* linked project path */, ExternalProjectBuildClasspathPojo> projectBuildClasspath = CollectionFactory.createSmallMemoryFootprintMap();
public Map<String/* linked project path */, SyncType> projectSyncType = CollectionFactory.createSmallMemoryFootprintMap();
}
public enum SyncType {
PREVIEW, IMPORT, RE_IMPORT
}
}