-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathGoProjectModelConverterProvider.java
More file actions
323 lines (289 loc) · 12.2 KB
/
GoProjectModelConverterProvider.java
File metadata and controls
323 lines (289 loc) · 12.2 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan
*
* 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 com.goide.project.migration;
import com.goide.GoConstants;
import com.goide.project.GoApplicationLibrariesService;
import com.goide.project.GoProjectLibrariesService;
import com.goide.sdk.GoSdkType;
import com.goide.sdk.GoSdkUtil;
import com.intellij.conversion.*;
import com.intellij.ide.impl.convert.JDomConvertingUtil;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.ProjectRootManagerImpl;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.SystemProperties;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.serialization.JDomSerializationUtil;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
public class GoProjectModelConverterProvider extends ConverterProvider {
public static final String PROJECT_JDK_NAME_ATTR = "project-jdk-name"; // ProjectRootManagerImpl.PROJECT_JDK_NAME_ATTR
public static final String PROJECT_JDK_TYPE_ATTR = "project-jdk-type"; // ProjectRootManagerImpl.PROJECT_JDK_TYPE_ATTR
private static final String PROJECT_ROOT_MANAGER = "ProjectRootManager";
protected GoProjectModelConverterProvider() {
super("go-project-model");
}
@NotNull
@Override
public String getConversionDescription() {
return "Go project model has been changed so project and its modules need to be updated";
}
@NotNull
@Override
public ProjectConverter createConverter(@NotNull ConversionContext context) {
return new ProjectConverter() {
private final Collection<File> additionalCreatedFiles = ContainerUtil.newArrayList();
private final Collection<File> additionalAffectedFiles = ContainerUtil.newArrayList();
@Nullable
@Override
public ConversionProcessor<ProjectSettings> createProjectFileConverter() {
return new ProjectFileConverter();
}
@Nullable
@Override
public ConversionProcessor<ModuleSettings> createModuleFileConverter() {
return new ModuleFileConverter();
}
@Nullable
@Override
public ConversionProcessor<RunManagerSettings> createRunConfigurationsConverter() {
return new RunConfigurationsConverter();
}
@Override
public boolean isConversionNeeded() {
Element component = getProjectRootManager(context);
return component != null && isGoSdkType(component.getAttributeValue(PROJECT_JDK_TYPE_ATTR));
}
@Override
public void preProcessingFinished() throws CannotConvertException {
Element component = getProjectRootManager(context);
if (component != null) {
try {
File miscFile = miscFile(context);
updateSdkType(miscFile, component);
additionalAffectedFiles.add(miscFile);
File oldGoSettings = new File(context.getSettingsBaseDir(), "go_settings.xml");
if (oldGoSettings.exists()) {
Element oldGoSettingsRoot = rootElement(oldGoSettings);
if (isAttachProjectDirToLibraries(oldGoSettingsRoot)) {
File librariesConfigFile = new File(context.getSettingsBaseDir(), GoConstants.GO_LIBRARIES_CONFIG_FILE);
if (librariesConfigFile.exists()) {
//noinspection ResultOfMethodCallIgnored
librariesConfigFile.delete();
additionalAffectedFiles.add(librariesConfigFile);
}
else {
additionalCreatedFiles.add(librariesConfigFile);
}
FileUtil.writeToFile(librariesConfigFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project></project>");
addProjectDirToLibraries(librariesConfigFile, rootElement(librariesConfigFile));
}
}
//noinspection ResultOfMethodCallIgnored
oldGoSettings.delete();
}
catch (IOException e) {
throw new CannotConvertException("Cannot convert go project", e);
}
}
convertSdks();
}
@NotNull
@Override
public Collection<File> getAdditionalAffectedFiles() {
return additionalAffectedFiles;
}
@NotNull
@Override
public Collection<File> getCreatedFiles() {
return additionalCreatedFiles;
}
};
}
@NotNull
private static Element rootElement(@NotNull File file) throws CannotConvertException {
return JDomConvertingUtil.loadDocument(file).getRootElement();
}
private static class ProjectFileConverter extends ConversionProcessor<ProjectSettings> {
@Override
public boolean isConversionNeeded(@NotNull ProjectSettings settings) {
Element projectRootManager = getProjectRootManager(settings.getRootElement());
return projectRootManager != null && isGoSdkType(projectRootManager.getAttributeValue(PROJECT_JDK_TYPE_ATTR));
}
@Override
public void process(@NotNull ProjectSettings settings) throws CannotConvertException {
Element projectRootManager = getProjectRootManager(settings.getRootElement());
if (projectRootManager != null) {
updateSdkType(settings.getFile(), projectRootManager);
}
if (isAttachProjectDirToLibraries(settings.getRootElement())) {
addProjectDirToLibraries(settings.getFile(), settings.getRootElement());
}
convertSdks();
}
}
private static class ModuleFileConverter extends ConversionProcessor<ModuleSettings> {
@Override
public boolean isConversionNeeded(@NotNull ModuleSettings settings) {
if ("GO_APP_ENGINE_MODULE".equals(settings.getModuleType())) return true;
for (Element element : settings.getOrderEntries()) {
if (isGoSdkType(element.getAttributeValue("jdkType"))) {
return true;
}
}
return false;
}
@Override
public void process(@NotNull ModuleSettings settings) throws CannotConvertException {
settings.setModuleType(GoConstants.MODULE_TYPE_ID);
for (Element element : settings.getOrderEntries()) {
if (isGoSdkType(element.getAttributeValue("jdkType"))) {
element.setAttribute("jdkType", GoConstants.SDK_TYPE_ID);
}
}
convertSdks();
}
}
private static Element getProjectRootManager(@NotNull ConversionContext context) {
File miscFile = miscFile(context);
try {
if (miscFile.exists()) {
return getProjectRootManager(rootElement(miscFile));
}
}
catch (CannotConvertException e) {
return null;
}
return null;
}
@Nullable
private static Element getProjectRootManager(@Nullable Element rootElement) {
return rootElement != null ? JDomSerializationUtil.findComponent(rootElement, PROJECT_ROOT_MANAGER) : null;
}
private static void updateSdkType(@NotNull File file, @NotNull Element projectRootManager) throws CannotConvertException {
projectRootManager.setAttribute(PROJECT_JDK_TYPE_ATTR, GoConstants.SDK_TYPE_ID);
saveFile(file, projectRootManager, "Cannot save sdk type changing");
}
@NotNull
private static File miscFile(@NotNull ConversionContext context) {
return new File(context.getSettingsBaseDir(), "misc.xml");
}
private static void addProjectDirToLibraries(@NotNull File file, @NotNull Element rootElement) throws CannotConvertException {
GoProjectLibrariesService librariesService = new GoProjectLibrariesService();
librariesService.setLibraryRootUrls("file://$PROJECT_DIR$");
Element componentElement = JDomSerializationUtil.findOrCreateComponentElement(rootElement, GoConstants.GO_LIBRARIES_SERVICE_NAME);
XmlSerializer.serializeInto(librariesService.getState(), componentElement);
saveFile(file, rootElement, "Cannot save libraries settings");
}
private static boolean isAttachProjectDirToLibraries(Element rootElement) {
Element goProjectSettings = JDomSerializationUtil.findComponent(rootElement, "GoProjectSettings");
if (goProjectSettings != null) {
for (Element option : goProjectSettings.getChildren("option")) {
if ("prependGoPath".equals(option.getAttributeValue("name"))) {
goProjectSettings.detach();
return "true".equalsIgnoreCase(option.getAttributeValue("value"));
}
}
goProjectSettings.detach();
}
return false;
}
private static boolean isGoSdkType(String sdkTypeName) {
return "Google Go SDK".equals(sdkTypeName) || "Google Go App Engine SDK".equals(sdkTypeName);
}
private static void saveFile(@NotNull File file, @NotNull Element rootElement, String errorMessage) throws CannotConvertException {
try {
JDOMUtil.writeDocument(rootElement.getDocument(), file, SystemProperties.getLineSeparator());
}
catch (IOException e) {
throw new CannotConvertException(errorMessage, e);
}
}
private static void convertSdks() {
ProjectJdkTable sdkTable = ProjectJdkTable.getInstance();
Collection<String> globalGoPathUrls = ContainerUtil.newLinkedHashSet();
Collection<Sdk> sdksToDelete = ContainerUtil.newArrayList();
Collection<Sdk> sdksToAdd = ContainerUtil.newArrayList();
GoSdkType sdkType = GoSdkType.getInstance();
for (Sdk sdk : sdkTable.getAllJdks()) {
String sdkTypeName = sdk.getSdkType().getName();
if (isGoSdkType(sdkTypeName)) {
sdksToDelete.add(sdk);
String sdkHome = sdkType.adjustSelectedSdkHome(sdk.getHomePath());
if (sdkType.isValidSdkHome(sdkHome)) {
ProjectJdkImpl newSdk = new ProjectJdkImpl(sdk.getName(), sdkType, sdkHome, sdkType.getVersionString(sdkHome));
sdkType.setupSdkPaths(newSdk);
sdksToAdd.add(newSdk);
for (String classesRoot : sdk.getRootProvider().getUrls(OrderRootType.CLASSES)) {
if (!classesRoot.equals(sdk.getHomePath())) {
globalGoPathUrls.add(classesRoot);
}
}
}
}
}
for (VirtualFile file : GoSdkUtil.getGoPathsRootsFromEnvironment()) {
globalGoPathUrls.remove(file.getUrl());
}
AccessToken l = WriteAction.start();
try {
for (Sdk sdk : sdksToDelete) {
sdkTable.removeJdk(sdk);
}
for (Sdk sdk : sdksToAdd) {
sdkTable.addJdk(sdk);
}
globalGoPathUrls.addAll(GoApplicationLibrariesService.getInstance().getLibraryRootUrls());
GoApplicationLibrariesService.getInstance().setLibraryRootUrls(globalGoPathUrls);
}
finally {
l.finish();
}
}
private static class RunConfigurationsConverter extends ConversionProcessor<RunManagerSettings> {
@Override
public boolean isConversionNeeded(@NotNull RunManagerSettings settings) {
for (Element element : settings.getRunConfigurations()) {
String confType = element.getAttributeValue("type");
if ("GaeLocalAppEngineServer".equals(confType) || "GoTestConfiguration".equals(confType)) {
return true;
}
}
return false;
}
@Override
public void process(@NotNull RunManagerSettings settings) throws CannotConvertException {
for (Element element : settings.getRunConfigurations()) {
String confType = element.getAttributeValue("type");
if ("GaeLocalAppEngineServer".equals(confType) || "GoTestConfiguration".equals(confType)) {
element.detach();
}
}
}
}
}