new framework detection: detection in wizard finished

This commit is contained in:
nik
2011-08-08 13:18:53 +04:00
parent 657424f3d4
commit b944902dc8
16 changed files with 302 additions and 135 deletions
@@ -0,0 +1,57 @@
/*
* Copyright 2000-2011 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.importProject;
import com.intellij.facet.Facet;
import com.intellij.facet.FacetConfiguration;
import com.intellij.framework.detection.FacetBasedFrameworkDetector;
import com.intellij.framework.detection.impl.FacetBasedDetectedFrameworkDescription;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ModifiableModelsProvider;
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
/**
* @author nik
*/
public class FacetBasedDetectedFrameworkDescriptionInWizard<F extends Facet, C extends FacetConfiguration> extends FacetBasedDetectedFrameworkDescription<F, C> {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.util.importProject.FacetBasedDetectedFrameworkDescriptionInWizard");
private final ModuleDescriptor myModuleDescriptor;
public FacetBasedDetectedFrameworkDescriptionInWizard(@NotNull ModuleDescriptor moduleDescriptor,
FacetBasedFrameworkDetector<F, C> detector,
@NotNull C configuration,
Set<VirtualFile> files) {
super(detector, configuration, files);
myModuleDescriptor = moduleDescriptor;
}
@Override
protected String getModuleName() {
return myModuleDescriptor.getName();
}
@Override
public void configureFramework(ModifiableModelsProvider modifiableModelsProvider, ModulesProvider modulesProvider) {
Module module = modulesProvider.getModule(getModuleName());
LOG.assertTrue(module != null, getModuleName());
doConfigure(modifiableModelsProvider, module);
}
}
@@ -17,38 +17,72 @@ package com.intellij.ide.util.importProject;
import com.intellij.facet.Facet;
import com.intellij.facet.FacetConfiguration;
import com.intellij.facet.FacetType;
import com.intellij.framework.detection.DetectedFrameworkDescription;
import com.intellij.framework.detection.FacetBasedFrameworkDetector;
import com.intellij.framework.detection.impl.FrameworkDetectionContextBase;
import com.intellij.ide.util.projectWizard.SourcePathsBuilder;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.io.File;
import java.util.*;
/**
* @author nik
*/
public class FrameworkDetectionInWizardContext extends FrameworkDetectionContextBase {
private final SourcePathsBuilder myBuilder;
public FrameworkDetectionInWizardContext(SourcePathsBuilder builder) {
myBuilder = builder;
public abstract class FrameworkDetectionInWizardContext extends FrameworkDetectionContextBase {
protected FrameworkDetectionInWizardContext() {
}
@NotNull
@Override
public <F extends Facet, C extends FacetConfiguration> List<? extends DetectedFrameworkDescription> createDetectedFacetDescriptions(@NotNull FacetType<F, C> facetType,
@NotNull Collection<VirtualFile> files,
@NotNull FacetConfigurationCreator<C> creator) {
return Collections.emptyList();
public <F extends Facet, C extends FacetConfiguration> List<? extends DetectedFrameworkDescription> createDetectedFacetDescriptions(@NotNull FacetBasedFrameworkDetector<F, C> detector,
@NotNull Collection<VirtualFile> files) {
final List<ModuleDescriptor> descriptors = getModuleDescriptors();
MultiMap<ModuleDescriptor, VirtualFile> filesByModule = new MultiMap<ModuleDescriptor, VirtualFile>();
for (VirtualFile file : files) {
final File ioFile = VfsUtil.virtualToIoFile(file);
ModuleDescriptor descriptor = findDescriptorByFile(descriptors, ioFile);
if (descriptor != null) {
filesByModule.putValue(descriptor, file);
}
}
final List<DetectedFrameworkDescription> result = new ArrayList<DetectedFrameworkDescription>();
for (ModuleDescriptor module : filesByModule.keySet()) {
final List<Pair<C,Collection<VirtualFile>>> pairs = detector.createConfigurations(filesByModule.get(module), Collections.<C>emptyList());
for (Pair<C, Collection<VirtualFile>> pair : pairs) {
result.add(new FacetBasedDetectedFrameworkDescriptionInWizard<F, C>(module, detector, pair.getFirst(),
new HashSet<VirtualFile>(pair.getSecond())));
}
}
return result;
}
protected abstract List<ModuleDescriptor> getModuleDescriptors();
@Nullable
private static ModuleDescriptor findDescriptorByFile(List<ModuleDescriptor> descriptors, File file) {
for (ModuleDescriptor descriptor : descriptors) {
for (File root : descriptor.getContentRoots()) {
if (FileUtil.isAncestor(root, file, false)) {
return descriptor;
}
}
}
return null;
}
public VirtualFile getBaseDir() {
final String path = myBuilder.getContentEntryPath();
final String path = getContentPath();
return path != null ? LocalFileSystem.getInstance().refreshAndFindFileByPath(path) : null;
}
@Nullable
protected abstract String getContentPath();
}
@@ -16,31 +16,23 @@
package com.intellij.ide.util.importProject;
import com.intellij.facet.FacetType;
import com.intellij.facet.FacetTypeRegistry;
import com.intellij.facet.autodetecting.FacetDetector;
import com.intellij.facet.autodetecting.UnderlyingFacetSelector;
import com.intellij.facet.impl.autodetecting.FacetDetectorForWizardRegistry;
import com.intellij.facet.impl.autodetecting.FacetDetectorRegistryEx;
import com.intellij.facet.impl.autodetecting.FileContentPattern;
import com.intellij.framework.detection.DetectedFrameworkDescription;
import com.intellij.framework.detection.DetectionExcludesConfiguration;
import com.intellij.framework.detection.FrameworkDetectionContext;
import com.intellij.framework.detection.FrameworkDetector;
import com.intellij.framework.detection.impl.FrameworkDetectionProcessor;
import com.intellij.framework.detection.impl.ui.DetectedFrameworksComponent;
import com.intellij.ide.util.newProjectWizard.ProjectFromSourcesBuilder;
import com.intellij.ide.util.projectWizard.AbstractStepWithProgress;
import com.intellij.ide.util.projectWizard.SourcePathsBuilder;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.PlatformModifiableModelsProvider;
import com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Ref;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -63,10 +55,20 @@ public abstract class FrameworkDetectionStep extends AbstractStepWithProgress<Li
private JLabel myFrameworksDetectedLabel;
private final FrameworkDetectionContext myContext;
public FrameworkDetectionStep(final Icon icon, SourcePathsBuilder builder) {
public FrameworkDetectionStep(final Icon icon, final SourcePathsBuilder builder) {
super(ProjectBundle.message("message.text.stop.searching.for.frameworks", ApplicationNamesInfo.getInstance().getProductName()));
myIcon = icon;
myContext = new FrameworkDetectionInWizardContext(builder);
myContext = new FrameworkDetectionInWizardContext() {
@Override
protected List<ModuleDescriptor> getModuleDescriptors() {
return FrameworkDetectionStep.this.getModuleDescriptors();
}
@Override
protected String getContentPath() {
return builder.getContentEntryPath();
}
};
myDetectedFrameworksComponent = new DetectedFrameworksComponent(myContext);
}
@@ -97,13 +99,11 @@ public abstract class FrameworkDetectionStep extends AbstractStepWithProgress<Li
roots.addAll(moduleDescriptor.getContentRoots());
}
List<DetectedFrameworkDescription> result = new ArrayList<DetectedFrameworkDescription>();
FrameworkDetectionProcessor processor = new FrameworkDetectionProcessor(progressIndicator, myContext);
processor.processRoots(roots);
return result;
return processor.processRoots(roots);
}
protected abstract List<ModuleDescriptor> getModuleDescriptors();
public abstract List<ModuleDescriptor> getModuleDescriptors();
private List<File> getRoots() {
List<File> roots = new ArrayList<File>();
@@ -127,26 +127,8 @@ public abstract class FrameworkDetectionStep extends AbstractStepWithProgress<Li
return myIcon;
}
public static boolean isEnabled(@NotNull ModuleType moduleType) {
for (FacetType<?,?> facetType : FacetTypeRegistry.getInstance().getFacetTypes()) {
if (facetType.isSuitableModuleType(moduleType)) {
final Ref<Boolean> hasDetector = Ref.create(false);
//noinspection unchecked
facetType.registerDetectors(new FacetDetectorRegistryEx(new FacetDetectorForWizardRegistry() {
public void register(FileType fileType,
@NotNull FileContentPattern fileContentPattern,
FacetDetector facetDetector,
UnderlyingFacetSelector underlyingFacetSelector) {
hasDetector.set(true);
}
}, null));
if (hasDetector.get()) {
return true;
}
}
}
return false;
public static boolean isEnabled() {
return FrameworkDetector.EP_NAME.getExtensions().length > 0;
}
@NonNls
@@ -157,7 +139,7 @@ public abstract class FrameworkDetectionStep extends AbstractStepWithProgress<Li
public void updateProject(@NotNull Project project) {
final PlatformModifiableModelsProvider modelsProvider = new PlatformModifiableModelsProvider();
for (DetectedFrameworkDescription description : myDetectedFrameworksComponent.getSelectedFrameworks()) {
description.configureFramework(modelsProvider);
description.configureFramework(modelsProvider, new DefaultModulesProvider(project));
}
myDetectedFrameworksComponent.processUncheckedNodes(DetectionExcludesConfiguration.getInstance(project));
}
@@ -32,7 +32,6 @@ import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.module.StdModuleTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -70,9 +69,9 @@ public class CreateFromSourcesMode extends WizardMode {
sequence.addCommonStep(new ModulesDetectionStep(projectBuilder, moduleInsight, icon, "reference.dialogs.new.project.fromCode.page2"));
sequence.addCommonStep(factory.createProjectJdkStep(context));
if (FrameworkDetectionStep.isEnabled(StdModuleTypes.JAVA)) {
if (FrameworkDetectionStep.isEnabled()) {
FrameworkDetectionStep frameworkDetectionStep = new FrameworkDetectionStep(icon, projectBuilder) {
protected List<ModuleDescriptor> getModuleDescriptors() {
public List<ModuleDescriptor> getModuleDescriptors() {
return projectBuilder.getModules();
}
};