@Nullable private XmlFile createAnnotationsXml( @NotNull VirtualFile root, @NonNls @NotNull String packageName) { final String[] dirs = packageName.split("[\\.]"); for (String dir : dirs) { if (dir.isEmpty()) break; VirtualFile subdir = root.findChild(dir); if (subdir == null) { try { subdir = root.createChildDirectory(null, dir); } catch (IOException e) { LOG.error(e); } } root = subdir; } final PsiDirectory directory = myPsiManager.findDirectory(root); if (directory == null) return null; final PsiFile psiFile = directory.findFile(ANNOTATIONS_XML); if (psiFile instanceof XmlFile) { return (XmlFile) psiFile; } try { final PsiFileFactory factory = PsiFileFactory.getInstance(myPsiManager.getProject()); return (XmlFile) directory.add( factory.createFileFromText(ANNOTATIONS_XML, XmlFileType.INSTANCE, "<root></root>")); } catch (IncorrectOperationException e) { LOG.error(e); } return null; }
@Nullable private static VirtualFile findFileToPatchByComponents( ApplyPatchContext context, final String[] pathNameComponents, final int lastComponentToFind) { VirtualFile patchedDir = context.getBaseDir(); for (int i = context.getSkipTopDirs(); i < lastComponentToFind; i++) { VirtualFile nextChild; if (pathNameComponents[i].equals("..")) { nextChild = patchedDir.getParent(); } else { nextChild = patchedDir.findChild(pathNameComponents[i]); } if (nextChild == null) { if (context.isCreateDirectories()) { try { nextChild = patchedDir.createChildDirectory(null, pathNameComponents[i]); } catch (IOException e) { return null; } } else { context.registerMissingDirectory(patchedDir, pathNameComponents, i); return null; } } patchedDir = nextChild; } return patchedDir; }
private static VirtualFile[] copyFiles( final VirtualFile[] fromFiles, final VirtualFile toDir, VirtualFile rawProjectRoot) throws IOException { final VirtualFile[] toFiles = new VirtualFile[fromFiles.length]; final boolean rootSpecified = rawProjectRoot != null; for (int i = 0, n = fromFiles.length; i < n; i++) { VirtualFile fromFile = fromFiles[i]; VirtualFile toP = toDir; if (rootSpecified) { final List<String> fromParents = new ArrayList<>(4); VirtualFile fromP = fromFile.getParent(); if (!Comparing.equal(fromP, rawProjectRoot)) { do { fromParents.add(fromP.getName()); } while (!Comparing.equal((fromP = fromP.getParent()), rawProjectRoot)); for (int j = fromParents.size() - 1; j >= 0; j--) { toP = toP.createChildDirectory(null, fromParents.get(j)); } } } final VirtualFile toFile = toP.createChildData(null, fromFile.getName()); toFile.setBinaryContent(fromFile.contentsToByteArray()); toFiles[i] = toFile; } return toFiles; }
private VirtualFile findOrCreateChildDirectory(VirtualFile parent, final String name) throws IOException { VirtualFile child = parent.findChild(name); if (child != null) { return child; } return parent.createChildDirectory(this, name); }
private VirtualFile createNlsByPath(@NotNull VirtualFile nlsRoot, @NotNull String[] chunks) throws IOException { VirtualFile currentFolder = nlsRoot; for (int i = 0; i < chunks.length - 1; i++) { String chunk = chunks[i]; VirtualFile nextFolder = currentFolder.findChild(chunk); if (nextFolder == null) { nextFolder = currentFolder.createChildDirectory(this, chunk); } currentFolder = nextFolder; } //noinspection ConstantConditions return currentFolder.createChildData(this, ArrayUtil.getLastElement(chunks)); }
@Override public void configureModule( final Module module, final ModifiableRootModel model, final ContentEntry entry) { try { LibraryUtil.addXtendLibrary(model); Project _project = this.getProject(); VirtualFile _baseDir = _project.getBaseDir(); final VirtualFile srcGenFolder = _baseDir.createChildDirectory(null, "xtend-gen"); entry.addSourceFolder(srcGenFolder, false); String _iD = XtendLanguage.INSTANCE.getID(); LightToolingTest.addFacetToModule(module, _iD); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
public void testDirDeletion() throws Exception { VirtualFile dir = myRoot.createChildDirectory(this, "dir"); VirtualFile subdir = dir.createChildDirectory(this, "subdir"); VirtualFile f = subdir.createChildData(this, "foo.txt"); f.setBinaryContent(new byte[] {123}, -1, 4000); dir.delete(this); revertLastChange(); dir = myRoot.findChild("dir"); subdir = dir.findChild("subdir"); f = subdir.findChild("foo.txt"); assertNotNull(f); assertEquals(123, f.contentsToByteArray()[0]); assertEquals(4000, f.getTimeStamp()); }
public void testCopyDir() throws Exception { File fromDir = createTempDirectory(); File toDir = createTempDirectory(); VirtualFile fromVDir = LocalFileSystem.getInstance() .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/')); VirtualFile toVDir = LocalFileSystem.getInstance() .findFileByPath(toDir.getPath().replace(File.separatorChar, '/')); assertNotNull(fromVDir); assertNotNull(toVDir); final VirtualFile dirToCopy = fromVDir.createChildDirectory(this, "dir"); final VirtualFile file = dirToCopy.createChildData(this, "temp_file"); file.setBinaryContent(new byte[] {0, 1, 2, 3}); final String newName = "dir"; final VirtualFile dirCopy = dirToCopy.copy(this, toVDir, newName); assertEquals(newName, dirCopy.getName()); PlatformTestUtil.assertDirectoriesEqual(toVDir, fromVDir); }
private VirtualFile createFileCopy(VirtualFile tempDir, final VirtualFile file) throws IOException { final String fileName = file.getName(); if (tempDir.findChild(fileName) != null) { int idx = 0; while (true) { //noinspection HardCodedStringLiteral final String dirName = "dir" + idx++; final VirtualFile dir = tempDir.findChild(dirName); if (dir == null) { tempDir = tempDir.createChildDirectory(this, dirName); break; } if (dir.findChild(fileName) == null) { tempDir = dir; break; } } } return VfsUtilCore.copyFile(this, file, tempDir); }
public static VirtualFile[] configureByFiles( @Nullable VirtualFile rawProjectRoot, VirtualFile[] files, @Nullable VirtualFile[] auxiliaryFiles, Module module, @Nullable TripleFunction<ModifiableRootModel, VirtualFile, List<String>, Void> moduleInitializer) throws Exception { return WriteAction.compute( () -> { VirtualFile dummyRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///"); //noinspection ConstantConditions dummyRoot.refresh(false, false); final VirtualFile sourceDir = dummyRoot.createChildDirectory(DesignerTests.class, "s"); assert sourceDir != null; final IndexableFileSet indexableFileSet = new IndexableFileSet() { @Override public boolean isInSet(@NotNull final VirtualFile file) { return file.getFileSystem() == sourceDir.getFileSystem(); } @Override public void iterateIndexableFilesIn( @NotNull final VirtualFile file, @NotNull final ContentIterator iterator) { if (file.isDirectory()) { for (VirtualFile child : file.getChildren()) { iterateIndexableFilesIn(child, iterator); } } else { iterator.processFile(file); } } }; FileBasedIndex.getInstance().registerIndexableSet(indexableFileSet, module.getProject()); Disposer.register( module, new Disposable() { @Override public void dispose() { FileBasedIndex.getInstance().removeIndexableSet(indexableFileSet); ApplicationManager.getApplication() .runWriteAction( () -> { try { sourceDir.delete(null); } catch (IOException e) { throw new RuntimeException(e); } }); } }); final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); VirtualFile[] toFiles = copyFiles(files, sourceDir, rawProjectRoot); if (auxiliaryFiles != null) { copyFiles(auxiliaryFiles, sourceDir, rawProjectRoot); } rootModel.addContentEntry(sourceDir).addSourceFolder(sourceDir, false); final List<String> libs = new ArrayList<>(); if (moduleInitializer != null) { moduleInitializer.fun(rootModel, sourceDir, libs); } rootModel.commit(); for (String path : libs) { VirtualFile virtualFile = path.charAt(0) != '/' ? getFile("lib", path) : getFile(path); FlexTestUtils.addLibrary( module, path, virtualFile.getParent().getPath(), virtualFile.getName(), null, null); } return toFiles; }); }