if(!fileName.endsWith("." + extension)) {
fileName = fileName.concat("." + extension);
}
DataContext dataContext = event.getDataContext();
IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (view == null) {
return;
}
final PsiDirectory initialBaseDir = view.getOrChooseDirectory();
if (initialBaseDir == null) {
return;
}
if(initialBaseDir.findFile(fileName) != null) {
Messages.showInfoMessage("File exists", "Error");
return;
}
String content;
try {
content = inputStreamToString(ServiceActionUtil.class.getResourceAsStream(templatePath));
} catch (IOException e) {
e.printStackTrace();
return;
}
final PsiFileFactory factory = PsiFileFactory.getInstance(project);
String bundleName = "Acme\\DemoBundle";
SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(project);
SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(initialBaseDir);
if(symfonyBundle != null) {
bundleName = StringUtils.strip(symfonyBundle.getNamespaceName(), "\\");
}
String underscoreBundle = bundleName.replace("\\", ".").toLowerCase();
if(underscoreBundle.endsWith("bundle")) {
underscoreBundle = underscoreBundle.substring(0, underscoreBundle.length() - 6);
}
content = content.replace("{{ BundleName }}", bundleName).replace("{{ BundleNameUnderscore }}", underscoreBundle);
final PsiFile file = factory.createFileFromText(fileName, fileType, content);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
CodeStyleManager.getInstance(project).reformat(file);
initialBaseDir.add(file);
}
});
PsiFile psiFile = initialBaseDir.findFile(fileName);
if(psiFile != null) {
view.selectElement(psiFile);
}
}