"create project from existing sources" fixed for mixed non-java projects (IDEA-91446)

This commit is contained in:
nik
2014-02-06 11:23:28 +04:00
parent 8abb5a0213
commit 76bfc1ff82
8 changed files with 171 additions and 15 deletions
@@ -67,6 +67,10 @@ public class DetectedRootData {
return roots.toArray(new DetectedProjectRoot[roots.size()]);
}
public boolean isEmpty() {
return myRoots.isEmpty();
}
public boolean isIncluded() {
return myIncluded;
}
@@ -89,7 +93,7 @@ public class DetectedRootData {
mySelectedRoot = root;
}
public void removeRoot(DetectedProjectRoot root) {
myRoots.remove(root);
public Collection<ProjectStructureDetector> removeRoot(DetectedProjectRoot root) {
return myRoots.remove(root);
}
}
@@ -15,16 +15,20 @@
*/
package com.intellij.ide.util.importProject;
import com.intellij.ide.util.projectWizard.importSources.DetectedContentRoot;
import com.intellij.ide.util.projectWizard.importSources.DetectedProjectRoot;
import com.intellij.ide.util.projectWizard.importSources.DetectedSourceRoot;
import com.intellij.ide.util.projectWizard.importSources.ProjectStructureDetector;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
@@ -173,7 +177,7 @@ public class RootDetectionProcessor {
child.removeRoot(projectRoot);
}
}
if (child.getAllRoots().length == 0) {
if (child.isEmpty()) {
rootData.remove(childDirectory);
}
}
@@ -220,9 +224,65 @@ public class RootDetectionProcessor {
}
}
List<DetectedRootData> dataCollection = mergeContentRoots(rootData);
if (myProgressIndicator != null) {
myProgressIndicator.setText2("");
}
return dataCollection;
}
private List<DetectedRootData> mergeContentRoots(Map<File, DetectedRootData> rootData) {
LOG.debug(rootData.size() + " roots found, merging content roots");
boolean hasSourceRoots = false;
Set<ModuleType> typesToReplace = new HashSet<ModuleType>();
Set<ModuleType> moduleTypes = new HashSet<ModuleType>();
for (DetectedRootData data : rootData.values()) {
for (DetectedProjectRoot root : data.getAllRoots()) {
if (root instanceof DetectedContentRoot) {
Collections.addAll(typesToReplace, ((DetectedContentRoot)root).getTypesToReplace());
moduleTypes.add(((DetectedContentRoot)root).getModuleType());
}
else if (root instanceof DetectedSourceRoot) {
LOG.debug("Source root found: " + root.getDirectory() + ", content roots will be ignored");
hasSourceRoots = true;
break;
}
}
}
moduleTypes.removeAll(typesToReplace);
if (hasSourceRoots || moduleTypes.size() <= 1) {
Iterator<DetectedRootData> iterator = rootData.values().iterator();
DetectedContentRoot firstRoot = null;
ProjectStructureDetector firstDetector = null;
while (iterator.hasNext()) {
DetectedRootData data = iterator.next();
for (DetectedProjectRoot root : data.getAllRoots()) {
if (root instanceof DetectedContentRoot) {
LOG.debug("Removed detected " + root.getRootTypeName() + " content root: " + root.getDirectory());
Collection<ProjectStructureDetector> detectors = data.removeRoot(root);
if ((firstRoot == null || firstDetector == null) && moduleTypes.contains(((DetectedContentRoot)root).getModuleType())) {
firstRoot = (DetectedContentRoot)root;
firstDetector = ContainerUtil.getFirstItem(detectors);
}
}
}
if (data.isEmpty()) {
iterator.remove();
}
}
if (!hasSourceRoots && firstRoot != null && firstDetector != null) {
DetectedContentRoot baseRoot = new DetectedContentRoot(myBaseDir, firstRoot.getRootTypeName(), firstRoot.getModuleType());
DetectedRootData data = rootData.get(myBaseDir);
if (data == null) {
rootData.put(myBaseDir, new DetectedRootData(firstDetector, baseRoot));
}
else {
data.addRoot(firstDetector, baseRoot);
}
LOG.debug("Added " + firstRoot.getRootTypeName() + " content root for " + myBaseDir);
}
}
return new ArrayList<DetectedRootData>(rootData.values());
}
}
@@ -29,6 +29,7 @@ import com.intellij.util.concurrency.SwingWorker;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.update.UiNotifyConnector;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.TestOnly;
import javax.swing.*;
import java.awt.*;
@@ -97,6 +98,14 @@ public abstract class AbstractStepWithProgress<Result> extends ModuleWizardStep
return progressPanel;
}
@TestOnly
public void performStep() {
Result result = calculate();
createResultsPanel();
onFinished(result, false);
updateDataModel();
}
private void cancelSearch() {
if (myProgressIndicator != null) {
myProgressIndicator.cancel();
@@ -0,0 +1,53 @@
/*
* 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 com.intellij.ide.util.projectWizard.importSources;
import com.intellij.openapi.module.ModuleType;
import org.jetbrains.annotations.NotNull;
import java.io.File;
/**
* @author nik
*/
public final class DetectedContentRoot extends DetectedProjectRoot {
@NotNull private final String myRootTypeName;
@NotNull private final ModuleType myModuleType;
@NotNull private final ModuleType[] myTypesToReplace;
public DetectedContentRoot(@NotNull File directory, @NotNull String rootTypeName, @NotNull ModuleType moduleType, @NotNull ModuleType... typesToReplace) {
super(directory);
myRootTypeName = rootTypeName;
myModuleType = moduleType;
myTypesToReplace = typesToReplace;
}
@NotNull
@Override
public String getRootTypeName() {
return myRootTypeName;
}
@NotNull
public ModuleType getModuleType() {
return myModuleType;
}
@NotNull
public ModuleType[] getTypesToReplace() {
return myTypesToReplace;
}
}
@@ -44,4 +44,6 @@ public interface ProjectFromSourcesBuilder {
WizardContext getContext();
boolean hasRootsFromOtherDetectors(ProjectStructureDetector thisDetector);
void setupModulesByContentRoots(ProjectDescriptor projectDescriptor, Collection<DetectedProjectRoot> roots);
}
@@ -25,10 +25,7 @@ import com.intellij.ide.util.newProjectWizard.modes.ImportImlMode;
import com.intellij.ide.util.projectWizard.ExistingModuleLoader;
import com.intellij.ide.util.projectWizard.ModuleBuilder;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.ide.util.projectWizard.importSources.DetectedProjectRoot;
import com.intellij.ide.util.projectWizard.importSources.JavaModuleSourceRoot;
import com.intellij.ide.util.projectWizard.importSources.ProjectFromSourcesBuilder;
import com.intellij.ide.util.projectWizard.importSources.ProjectStructureDetector;
import com.intellij.ide.util.projectWizard.importSources.*;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.diagnostic.Logger;
@@ -319,6 +316,19 @@ public class ProjectFromSourcesBuilderImpl extends ProjectImportBuilder implemen
return false;
}
@Override
public void setupModulesByContentRoots(ProjectDescriptor projectDescriptor, Collection<DetectedProjectRoot> roots) {
if (projectDescriptor.getModules().isEmpty()) {
List<ModuleDescriptor> modules = new ArrayList<ModuleDescriptor>();
for (DetectedProjectRoot root : roots) {
if (root instanceof DetectedContentRoot) {
modules.add(new ModuleDescriptor(root.getDirectory(), ((DetectedContentRoot)root).getModuleType(), Collections.<DetectedProjectRoot>emptyList()));
}
}
projectDescriptor.setModules(modules);
}
}
@NotNull
private static Module createModule(ProjectDescriptor projectDescriptor, final ModuleDescriptor descriptor,
final Map<LibraryDescriptor, Library> projectLibs, final ModifiableModuleModel moduleModel)
@@ -17,7 +17,11 @@ package com.intellij.ide.projectWizard;
import com.intellij.ide.util.importProject.DetectedRootData;
import com.intellij.ide.util.importProject.RootDetectionProcessor;
import com.intellij.ide.util.projectWizard.AbstractStepWithProgress;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.ide.util.projectWizard.importSources.DetectedProjectRoot;
import com.intellij.ide.util.projectWizard.importSources.ProjectStructureDetector;
import com.intellij.ide.util.projectWizard.importSources.impl.ProjectFromSourcesBuilderImpl;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
@@ -26,6 +30,8 @@ import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.util.containers.MultiMap;
import com.intellij.util.ui.EmptyIcon;
import org.jetbrains.annotations.NotNull;
import java.io.File;
@@ -65,12 +71,26 @@ public abstract class ImportFromSourcesTestCase extends PlatformTestCase {
myRootDir = dir;
try {
myProject = doCreateProject(getIprFile());
myBuilder.setBaseProjectPath(dir.getAbsolutePath());
List<DetectedRootData> list = RootDetectionProcessor.detectRoots(dir);
myBuilder.setupProjectStructure(RootDetectionProcessor.createRootsMap(list));
MultiMap<ProjectStructureDetector,DetectedProjectRoot> map = RootDetectionProcessor.createRootsMap(list);
myBuilder.setupProjectStructure(map);
for (ProjectStructureDetector detector : map.keySet()) {
List<ModuleWizardStep> steps = detector.createWizardSteps(myBuilder, myBuilder.getProjectDescriptor(detector), EmptyIcon.ICON_16);
for (ModuleWizardStep step : steps) {
if (step instanceof AbstractStepWithProgress<?>) {
performStep((AbstractStepWithProgress<?>)step);
}
}
}
myBuilder.commit(myProject, null, ModulesProvider.EMPTY_MODULES_PROVIDER);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static <Result> void performStep(AbstractStepWithProgress<Result> step) {
step.performStep();
}
}
@@ -19,11 +19,14 @@ import com.intellij.ide.util.importProject.ModuleDescriptor;
import com.intellij.ide.util.importProject.ProjectDescriptor;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.ProjectWizardStepFactory;
import com.intellij.ide.util.projectWizard.importSources.DetectedContentRoot;
import com.intellij.ide.util.projectWizard.importSources.DetectedProjectRoot;
import com.intellij.ide.util.projectWizard.importSources.ProjectFromSourcesBuilder;
import com.intellij.ide.util.projectWizard.importSources.ProjectStructureDetector;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.WebModuleType;
import com.intellij.openapi.util.io.FileUtilRt;
import com.jetbrains.python.PythonModuleTypeBase;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
@@ -50,13 +53,7 @@ public class PyProjectStructureDetector extends ProjectStructureDetector {
for (File child : children) {
if (FileUtilRt.extensionEquals(child.getName(), "py")) {
LOG.info("Found Python file " + child.getPath());
result.add(new DetectedProjectRoot(dir) {
@NotNull
@Override
public String getRootTypeName() {
return "Python";
}
});
result.add(new DetectedContentRoot(dir, "Python", PythonModuleTypeBase.getInstance(), WebModuleType.getInstance()));
return DirectoryProcessingResult.SKIP_CHILDREN;
}
}
@@ -67,6 +64,7 @@ public class PyProjectStructureDetector extends ProjectStructureDetector {
public void setupProjectStructure(@NotNull Collection<DetectedProjectRoot> roots,
@NotNull ProjectDescriptor projectDescriptor,
@NotNull ProjectFromSourcesBuilder builder) {
builder.setupModulesByContentRoots(projectDescriptor, roots);
if (!roots.isEmpty() && !builder.hasRootsFromOtherDetectors(this)) {
List<ModuleDescriptor> modules = projectDescriptor.getModules();
if (modules.isEmpty()) {