External system: adding more source types to ExternalSystemSourceType (GENERATED, RESOURCE, TEST_RESOURCE)

This commit is contained in:
Vladislav.Soroka
2013-11-05 19:24:30 +04:00
parent 1030d67d31
commit 7b0e4f4fd6
2 changed files with 34 additions and 21 deletions
@@ -15,6 +15,9 @@ public class ExternalSystemSourceType implements Serializable {
@NotNull public static final ExternalSystemSourceType SOURCE = new ExternalSystemSourceType("SOURCE");
@NotNull public static final ExternalSystemSourceType TEST = new ExternalSystemSourceType("TEST");
@NotNull public static final ExternalSystemSourceType EXCLUDED = new ExternalSystemSourceType("EXCLUDED");
@NotNull public static final ExternalSystemSourceType GENERATED = new ExternalSystemSourceType("GENERATED");
@NotNull public static final ExternalSystemSourceType RESOURCE = new ExternalSystemSourceType("RESOURCE");
@NotNull public static final ExternalSystemSourceType TEST_RESOURCE = new ExternalSystemSourceType("TEST_RESOURCE");
private static final long serialVersionUID = 1L;
@@ -14,12 +14,17 @@ import com.intellij.openapi.externalSystem.util.Order;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.impl.SourceFolderImpl;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.containers.ContainerUtilRt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.java.JavaResourceRootType;
import org.jetbrains.jps.model.java.JavaSourceRootProperties;
import org.jetbrains.jps.model.java.JavaSourceRootType;
import org.jetbrains.jps.model.module.JpsModuleSourceRootType;
import java.util.Collection;
import java.util.List;
@@ -93,11 +98,23 @@ public class ContentRootDataService implements ProjectDataService<ContentRootDat
LOG.info(String.format("Importing content root '%s' for module '%s'", contentRoot.getRootPath(), module.getName()));
final Set<String> retainedPaths = ContainerUtilRt.newHashSet();
for (String path : contentRoot.getPaths(ExternalSystemSourceType.SOURCE)) {
createSourceRootIfAbsent(contentEntry, path, module.getName());
createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.SOURCE, false);
retainedPaths.add(ExternalSystemApiUtil.toCanonicalPath(path));
}
for (String path : contentRoot.getPaths(ExternalSystemSourceType.TEST)) {
createTestRootIfAbsent(contentEntry, path, module.getName());
createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.TEST_SOURCE, false);
retainedPaths.add(ExternalSystemApiUtil.toCanonicalPath(path));
}
for (String path : contentRoot.getPaths(ExternalSystemSourceType.RESOURCE)) {
createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaResourceRootType.RESOURCE, false);
retainedPaths.add(ExternalSystemApiUtil.toCanonicalPath(path));
}
for (String path : contentRoot.getPaths(ExternalSystemSourceType.TEST_RESOURCE)) {
createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaResourceRootType.TEST_RESOURCE, false);
retainedPaths.add(ExternalSystemApiUtil.toCanonicalPath(path));
}
for (String path : contentRoot.getPaths(ExternalSystemSourceType.GENERATED)) {
createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.SOURCE, true);
retainedPaths.add(ExternalSystemApiUtil.toCanonicalPath(path));
}
for (String path : contentRoot.getPaths(ExternalSystemSourceType.EXCLUDED)) {
@@ -151,8 +168,10 @@ public class ContentRootDataService implements ProjectDataService<ContentRootDat
return model.addContentEntry(toVfsUrl(path));
}
private static void createSourceRootIfAbsent(@NotNull ContentEntry entry, @NotNull String path, @NotNull String moduleName) {
List<SourceFolder> folders = entry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES);
private static void createSourceRootIfAbsent(
@NotNull ContentEntry entry, @NotNull String path, @NotNull String moduleName,
@NotNull JpsModuleSourceRootType sourceRootType, boolean generated) {
List<SourceFolder> folders = entry.getSourceFolders(sourceRootType);
for (SourceFolder folder : folders) {
VirtualFile file = folder.getFile();
if (file == null) {
@@ -163,7 +182,13 @@ public class ContentRootDataService implements ProjectDataService<ContentRootDat
}
}
LOG.info(String.format("Importing source root '%s' for content root '%s' of module '%s'", path, entry.getUrl(), moduleName));
entry.addSourceFolder(toVfsUrl(path), false);
SourceFolder sourceFolder = entry.addSourceFolder(toVfsUrl(path), sourceRootType);
if (generated && sourceFolder instanceof SourceFolderImpl) {
JpsElement properties = ((SourceFolderImpl)sourceFolder).getJpsElement().getProperties();
if(properties instanceof JavaSourceRootProperties) {
JavaSourceRootProperties.class.cast(properties).setForGeneratedSources(true);
}
}
}
private static void createExcludedRootIfAbsent(@NotNull ContentEntry entry, @NotNull String path, @NotNull String moduleName) {
@@ -176,21 +201,6 @@ public class ContentRootDataService implements ProjectDataService<ContentRootDat
entry.addExcludeFolder(toVfsUrl(path));
}
private static void createTestRootIfAbsent(@NotNull ContentEntry entry, @NotNull String path, @NotNull String moduleName) {
List<SourceFolder> folders = entry.getSourceFolders(JavaModuleSourceRootTypes.TESTS);
for (SourceFolder folder : folders) {
VirtualFile file = folder.getFile();
if (file == null) {
continue;
}
if (ExternalSystemApiUtil.getLocalFileSystemPath(file).equals(path)) {
return;
}
}
LOG.info(String.format("Importing test root '%s' for content root '%s' of module '%s'", path, entry.getUrl(), moduleName));
entry.addSourceFolder(toVfsUrl(path), true);
}
@Override
public void removeData(@NotNull Collection<? extends ContentEntry> toRemove, @NotNull Project project, boolean synchronous) {
}