// Copyright 2000-2019 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.platform;

import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
final class PlatformProjectConfigurator implements DirectoryProjectConfigurator {
  private static final Logger LOG = Logger.getInstance(PlatformProjectConfigurator.class);

  @Override
  public void configureProject(@NotNull Project project, @NotNull VirtualFile baseDir, @NotNull Ref<Module> moduleRef) {
    final ModuleManager moduleManager = ModuleManager.getInstance(project);
    final Module[] modules = moduleManager.getModules();
    if (modules.length != 0) {
      LOG.info("PlatformProjectConfigurator is not applicable because modules are already configured (module count: " + modules.length + ")");
      return;
    }

    ApplicationManager.getApplication().runWriteAction(() -> {
      String moduleName = baseDir.getName().replace(":", "");     // correct module name when opening root of drive as project (RUBY-5181)
      String imlName = baseDir.getPath() + "/.idea/" + moduleName + ModuleFileType.DOT_DEFAULT_EXTENSION;
      ModuleTypeManager instance = ModuleTypeManager.getInstance();
      String id = instance == null ? "unknown" : instance.getDefaultModuleType().getId();
      final Module module = moduleManager.newModule(imlName, id);
      ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
      ModifiableRootModel rootModel = rootManager.getModifiableModel();
      if (rootModel.getContentRoots().length == 0) {
        rootModel.addContentEntry(baseDir);
      }
      rootModel.inheritSdk();
      rootModel.commit();
      moduleRef.set(module);
    });
  }
}

// Copyright 2000-2019 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.platform;

import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
final class PlatformProjectConfigurator implements DirectoryProjectConfigurator {
  private static final Logger LOG = Logger.getInstance(PlatformProjectConfigurator.class);

  @Override
  public void configureProject(@NotNull Project project, @NotNull VirtualFile baseDir, @NotNull Ref<Module> moduleRef) {
    final ModuleManager moduleManager = ModuleManager.getInstance(project);
    final Module[] modules = moduleManager.getModules();
    if (modules.length != 0) {
      LOG.info("PlatformProjectConfigurator is not applicable because modules are already configured (module count: " + modules.length + ")");
      return;
    }

    // correct module name when opening root of drive as project (RUBY-5181)
    String moduleName = baseDir.getName().replace(":", "");
    String imlName = baseDir.getPath() + "/.idea/" + moduleName + ModuleFileType.DOT_DEFAULT_EXTENSION;
    ModuleTypeManager moduleTypeManager = ModuleTypeManager.getInstance();

    ApplicationManager.getApplication().runWriteAction(() -> {
      Module module = moduleManager.newModule(imlName, moduleTypeManager == null ? "unknown" : moduleTypeManager.getDefaultModuleType().getId());
      ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
      try {
        VirtualFile[] contentRoots = model.getContentRoots();
        if (contentRoots.length == 0) {
          model.addContentEntry(baseDir);
        }
        else {
          LOG.info("PlatformProjectConfigurator did not add content entry because content roots are already configured " +
                   "(content root count: " + contentRoots.length + ")");
        }
        model.inheritSdk();
        model.commit();
      }
      finally {
        if (!model.isDisposed()) {
          model.dispose();
        }
      }
      moduleRef.set(module);
    });
  }
}

add LOG.info if content root is not added