Search in sources :

Example 61 with LocalFileSystem

use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.

the class ProjectFromSourcesBuilderImpl method setupRootModel.

private static void setupRootModel(ProjectDescriptor projectDescriptor, final ModuleDescriptor descriptor, final ModifiableRootModel rootModel, final Map<LibraryDescriptor, Library> projectLibs) {
    final CompilerModuleExtension compilerModuleExtension = rootModel.getModuleExtension(CompilerModuleExtension.class);
    compilerModuleExtension.setExcludeOutput(true);
    rootModel.inheritSdk();
    final Set<File> contentRoots = descriptor.getContentRoots();
    for (File contentRoot : contentRoots) {
        final LocalFileSystem lfs = LocalFileSystem.getInstance();
        VirtualFile moduleContentRoot = lfs.refreshAndFindFileByPath(FileUtil.toSystemIndependentName(contentRoot.getPath()));
        if (moduleContentRoot != null) {
            final ContentEntry contentEntry = rootModel.addContentEntry(moduleContentRoot);
            final Collection<DetectedSourceRoot> sourceRoots = descriptor.getSourceRoots(contentRoot);
            for (DetectedSourceRoot srcRoot : sourceRoots) {
                final String srcpath = FileUtil.toSystemIndependentName(srcRoot.getDirectory().getPath());
                final VirtualFile sourceRoot = lfs.refreshAndFindFileByPath(srcpath);
                if (sourceRoot != null) {
                    contentEntry.addSourceFolder(sourceRoot, shouldBeTestRoot(srcRoot.getDirectory()), getPackagePrefix(srcRoot));
                }
            }
        }
    }
    compilerModuleExtension.inheritCompilerOutputPath(true);
    final LibraryTable moduleLibraryTable = rootModel.getModuleLibraryTable();
    for (LibraryDescriptor libDescriptor : ModuleInsight.getLibraryDependencies(descriptor, projectDescriptor.getLibraries())) {
        final Library projectLib = projectLibs.get(libDescriptor);
        if (projectLib != null) {
            rootModel.addLibraryEntry(projectLib);
        } else {
            // add as module library
            final Collection<File> jars = libDescriptor.getJars();
            for (File file : jars) {
                Library library = moduleLibraryTable.createLibrary();
                Library.ModifiableModel modifiableModel = library.getModifiableModel();
                modifiableModel.addRoot(VfsUtil.getUrlForLibraryRoot(file), OrderRootType.CLASSES);
                modifiableModel.commit();
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Library(com.intellij.openapi.roots.libraries.Library) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) LibraryDescriptor(com.intellij.ide.util.importProject.LibraryDescriptor)

Example 62 with LocalFileSystem

use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.

the class ExternalSystemVcsRegistrar method handle.

@SuppressWarnings("unchecked")
public static void handle(@NotNull final Project project) {
    for (final ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
        final AbstractExternalSystemSettings settings = manager.getSettingsProvider().fun(project);
        settings.subscribe(new ExternalSystemSettingsListenerAdapter() {

            @Override
            public void onProjectsLinked(@NotNull final Collection linked) {
                List<VcsDirectoryMapping> newMappings = ContainerUtilRt.newArrayList();
                final LocalFileSystem fileSystem = LocalFileSystem.getInstance();
                ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
                for (Object o : linked) {
                    final ExternalProjectSettings settings = (ExternalProjectSettings) o;
                    VirtualFile dir = fileSystem.refreshAndFindFileByPath(settings.getExternalProjectPath());
                    if (dir == null) {
                        continue;
                    }
                    if (!dir.isDirectory()) {
                        dir = dir.getParent();
                    }
                    newMappings.addAll(VcsUtil.findRoots(dir, project));
                }
                // There is a possible case that no VCS mappings are configured for the current project. There is a single
                // mapping like <Project> - <No VCS> then. We want to replace it if only one mapping to the project root dir
                // has been detected then.
                List<VcsDirectoryMapping> oldMappings = vcsManager.getDirectoryMappings();
                if (oldMappings.size() == 1 && newMappings.size() == 1 && StringUtil.isEmpty(oldMappings.get(0).getVcs())) {
                    VcsDirectoryMapping newMapping = newMappings.iterator().next();
                    String detectedDirPath = newMapping.getDirectory();
                    VirtualFile detectedDir = fileSystem.findFileByPath(detectedDirPath);
                    if (detectedDir != null && detectedDir.equals(project.getBaseDir())) {
                        newMappings.clear();
                        newMappings.add(new VcsDirectoryMapping("", newMapping.getVcs()));
                        vcsManager.setDirectoryMappings(newMappings);
                        return;
                    }
                }
                newMappings.addAll(oldMappings);
                vcsManager.setDirectoryMappings(newMappings);
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ExternalSystemSettingsListenerAdapter(com.intellij.openapi.externalSystem.settings.ExternalSystemSettingsListenerAdapter) VcsDirectoryMapping(com.intellij.openapi.vcs.VcsDirectoryMapping) ProjectLevelVcsManager(com.intellij.openapi.vcs.ProjectLevelVcsManager) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings) AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Collection(java.util.Collection) List(java.util.List)

Example 63 with LocalFileSystem

use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.

the class ManagingContentRootsTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            LocalFileSystem fs = LocalFileSystem.getInstance();
            dir = fs.refreshAndFindFileByIoFile(createTempDirectory());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) IOException(java.io.IOException)

Example 64 with LocalFileSystem

use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.

the class NewFilesProcessor method addToVcsIfNeeded.

/**
   * @param localFileNames names of local files to add to VCS
   */
private static void addToVcsIfNeeded(@NotNull final Module module, @NotNull final String... localFileNames) {
    final LocalFileSystem fs = LocalFileSystem.getInstance();
    fs.refresh(false);
    final Project project = module.getProject();
    Arrays.stream(localFileNames).map(o -> fs.findFileByPath(o)).filter(o -> o != null).forEach(file -> {
        final AbstractVcs<?> vcs = VcsUtil.getVcsFor(project, file);
        if (vcs == null) {
            return;
        }
        final CheckinEnvironment environment = vcs.getCheckinEnvironment();
        if (environment != null) {
            environment.scheduleUnversionedFilesForAddition(Collections.singletonList(file));
        }
    });
}
Also used : VcsUtil(com.intellij.vcsUtil.VcsUtil) Arrays(java.util.Arrays) ModuleExtKt(com.jetbrains.extensions.ModuleExtKt) StringUtil(com.intellij.openapi.util.text.StringUtil) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Sdk(com.intellij.openapi.projectRoots.Sdk) CheckinEnvironment(com.intellij.openapi.vcs.checkin.CheckinEnvironment) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Project(com.intellij.openapi.project.Project) CheckinEnvironment(com.intellij.openapi.vcs.checkin.CheckinEnvironment) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem)

Example 65 with LocalFileSystem

use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.

the class PyExtractSuperclassHelper method createDirectories.

/**
   * Create all intermediate dirs with inits from one of roots up to target dir.
   *
   * @param project
   * @param target  a full path to target dir
   * @return deepest child directory, or null if target is not in roots or process fails at some point.
   */
@Nullable
private static PsiDirectory createDirectories(Project project, String target) throws IOException {
    String the_rest = null;
    VirtualFile the_root = null;
    PsiDirectory ret = null;
    // NOTE: we don't canonicalize target; must be ok in reasonable cases, and is far easier in unit test mode
    target = FileUtil.toSystemIndependentName(target);
    for (VirtualFile file : ProjectRootManager.getInstance(project).getContentRoots()) {
        final String root_path = file.getPath();
        if (target.startsWith(root_path)) {
            the_rest = target.substring(root_path.length());
            the_root = file;
            break;
        }
    }
    if (the_root == null) {
        throw new IOException("Can't find '" + target + "' among roots");
    }
    if (the_rest != null) {
        final LocalFileSystem lfs = LocalFileSystem.getInstance();
        final PsiManager psi_mgr = PsiManager.getInstance(project);
        String[] dirs = the_rest.split("/");
        int i = 0;
        if ("".equals(dirs[0]))
            i = 1;
        while (i < dirs.length) {
            VirtualFile subdir = the_root.findChild(dirs[i]);
            if (subdir != null) {
                if (!subdir.isDirectory()) {
                    throw new IOException("Expected dir, but got non-dir: " + subdir.getPath());
                }
            } else {
                subdir = the_root.createChildDirectory(lfs, dirs[i]);
            }
            VirtualFile init_vfile = subdir.findChild(PyNames.INIT_DOT_PY);
            if (init_vfile == null)
                init_vfile = subdir.createChildData(lfs, PyNames.INIT_DOT_PY);
            /*
        // here we could add an __all__ clause to the __init__.py.
        // * there's no point to do so; we import the class directly;
        // * we can't do this consistently since __init__.py may already exist and be nontrivial.
        if (i == dirs.length - 1) {
          PsiFile init_file = psi_mgr.findFile(init_vfile);
          LOG.assertTrue(init_file != null);
          final PyElementGenerator gen = PyElementGenerator.getInstance(project);
          final PyStatement statement = gen.createFromText(LanguageLevel.getDefault(), PyStatement.class, PyNames.ALL + " = [\"" + lastName + "\"]");
          init_file.add(statement);
        }
        */
            the_root = subdir;
            i += 1;
        }
        ret = psi_mgr.findDirectory(the_root);
    }
    return ret;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)70 VirtualFile (com.intellij.openapi.vfs.VirtualFile)62 File (java.io.File)32 Nullable (org.jetbrains.annotations.Nullable)10 NotNull (org.jetbrains.annotations.NotNull)8 IOException (java.io.IOException)7 Project (com.intellij.openapi.project.Project)5 Change (com.intellij.openapi.vcs.changes.Change)5 Test (org.junit.Test)5 Module (com.intellij.openapi.module.Module)4 Library (com.intellij.openapi.roots.libraries.Library)4 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)4 PsiFile (com.intellij.psi.PsiFile)4 Application (com.intellij.openapi.application.Application)3 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)3 Notification (com.intellij.notification.Notification)2 ModuleManager (com.intellij.openapi.module.ModuleManager)2 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 FileAnnotation (com.intellij.openapi.vcs.annotate.FileAnnotation)2