public String getErrorText(String inputString) { if (FileTypeManager.getInstance().isFileIgnored(inputString)) { return "Trying to create a " + (myIsDirectory ? "directory" : "package") + " with ignored name, result will not be visible"; } if (!myIsDirectory && inputString.length() > 0 && !PsiDirectoryFactory.getInstance(myProject).isValidPackageName(inputString)) { return "Not a valid package name"; } return null; }
@Nullable protected static PsiDirectory resolveDirectory(@NotNull PsiDirectory defaultTargetDirectory) { final Project project = defaultTargetDirectory.getProject(); final Boolean showDirsChooser = defaultTargetDirectory.getCopyableUserData(CopyPasteDelegator.SHOW_CHOOSER_KEY); if (showDirsChooser != null && showDirsChooser.booleanValue()) { final PsiDirectoryContainer directoryContainer = PsiDirectoryFactory.getInstance(project).getDirectoryContainer(defaultTargetDirectory); if (directoryContainer == null) { return defaultTargetDirectory; } return MoveFilesOrDirectoriesUtil.resolveToDirectory(project, directoryContainer); } return defaultTargetDirectory; }
@NotNull public static PsiElement createBundleFile( @NotNull PhpClass bundleClass, @NotNull String template, @NotNull String className, Map<String, String> vars) throws Exception { VirtualFile directory = bundleClass.getContainingFile().getContainingDirectory().getVirtualFile(); if (fileExists(directory, new String[] {className})) { throw new Exception("File already exists"); } String COMPILER_TEMPLATE = "/resources/fileTemplates/" + template + ".php"; String fileTemplateContent = getFileTemplateContent(COMPILER_TEMPLATE); if (fileTemplateContent == null) { throw new Exception("Template content error"); } String[] split = className.split("\\\\"); String ns = bundleClass.getNamespaceName(); String join = StringUtils.join(Arrays.copyOf(split, split.length - 1), "/"); vars.put("ns", (ns.startsWith("\\") ? ns.substring(1) : ns) + join.replace("/", "\\")); vars.put("class", split[split.length - 1]); for (Map.Entry<String, String> entry : vars.entrySet()) { fileTemplateContent = fileTemplateContent.replace("{{ " + entry.getKey() + " }}", entry.getValue()); } VirtualFile compilerDirectory = getAndCreateDirectory(directory, join); if (compilerDirectory == null) { throw new Exception("Directory creation failed"); } Project project = bundleClass.getProject(); PsiFile fileFromText = PsiFileFactory.getInstance(project) .createFileFromText( split[split.length - 1] + ".php", PhpFileType.INSTANCE, fileTemplateContent); CodeStyleManager.getInstance(project).reformat(fileFromText); return PsiDirectoryFactory.getInstance(project) .createDirectory(compilerDirectory) .add(fileFromText); }
@Override public void findRelatedStepDefsRoots( @NotNull final Module module, @NotNull final PsiFile featureFile, @NotNull final List<PsiDirectory> newStepDefinitionsRoots, @NotNull final Set<String> processedStepDirectories) { final ContentEntry[] contentEntries = ModuleRootManager.getInstance(module).getContentEntries(); for (final ContentEntry contentEntry : contentEntries) { final SourceFolder[] sourceFolders = contentEntry.getSourceFolders(); for (SourceFolder sf : sourceFolders) { // ToDo: check if inside test folder VirtualFile sfDirectory = sf.getFile(); if (sfDirectory != null && sfDirectory.isDirectory()) { PsiDirectory sourceRoot = PsiDirectoryFactory.getInstance(module.getProject()).createDirectory(sfDirectory); if (!processedStepDirectories.contains(sourceRoot.getVirtualFile().getPath())) { newStepDefinitionsRoots.add(sourceRoot); } } } } }
@NotNull public static PsiElement createCompilerPass( @NotNull PhpClass bundleClass, @NotNull String className) throws Exception { VirtualFile directory = bundleClass.getContainingFile().getContainingDirectory().getVirtualFile(); if (fileExists(directory, className)) { throw new Exception("File already exists"); } PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(bundleClass); if (scopeForUseOperator == null) { throw new Exception("No 'use' scope found"); } VirtualFile compilerDirectory = getAndCreateCompilerDirectory(directory); if (compilerDirectory == null) { throw new Exception("Directory creation failed"); } Project project = bundleClass.getProject(); if (bundleClass.findOwnMethodByName("build") == null) { insertUseIfNecessary( scopeForUseOperator, "\\Symfony\\Component\\DependencyInjection\\ContainerBuilder"); Method method = PhpPsiElementFactory.createMethod( project, "" + "public function build(ContainerBuilder $container)\n" + " {\n" + " parent::build($container);\n" + " }"); PhpCodeEditUtil.insertClassMember(bundleClass, method); } Method buildMethod = bundleClass.findOwnMethodByName("build"); if (buildMethod == null) { throw new Exception("No 'build' method found"); } String relativePath = VfsUtil.getRelativePath(compilerDirectory, directory); if (relativePath == null) { throw new Exception("path error"); } MethodReference methodReference = PhpPsiElementFactory.createMethodReference( project, "$container->addCompilerPass(new " + className + "());"); String ns = bundleClass.getNamespaceName() + relativePath.replace("/", "\\"); String nsClass = ns + "\\" + className; insertUseIfNecessary(scopeForUseOperator, nsClass); GroupStatement groupStatement = PhpPsiUtil.getChildByCondition(buildMethod, GroupStatement.INSTANCEOF); if (groupStatement != null) { PsiElement semicolon = methodReference.getNextSibling(); groupStatement.addRangeBefore(methodReference, semicolon, groupStatement.getLastChild()); } String COMPILER_TEMPLATE = "/resources/fileTemplates/compiler_pass.php"; String fileTemplateContent = getFileTemplateContent(COMPILER_TEMPLATE); if (fileTemplateContent == null) { throw new Exception("Template content error"); } String replace = fileTemplateContent .replace("{{ ns }}", ns.startsWith("\\") ? ns.substring(1) : ns) .replace("{{ class }}", className); PsiFile fileFromText = PsiFileFactory.getInstance(project) .createFileFromText(className + ".php", PhpFileType.INSTANCE, replace); CodeStyleManager.getInstance(project).reformat(fileFromText); return PsiDirectoryFactory.getInstance(project) .createDirectory(compilerDirectory) .add(fileFromText); }