Skip to content

Commit

Permalink
Merge pull request #2 from shyim/add-real-file-templates
Browse files Browse the repository at this point in the history
feat: Improve file template handling, add Changelog action
  • Loading branch information
shyim authored Sep 21, 2021
2 parents 906290d + b4a76c0 commit 7632e09
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 74 deletions.
9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,13 @@ tasks {
// https://plugins.jetbrains.com/docs/intellij/deployment.html#specifying-a-release-channel
channels.set(listOf(properties("pluginVersion").split('-').getOrElse(1) { "default" }.split('.').first()))
}

processResources {
exclude("fileTemplates/j2ee/**")
from(fileTree("src/main/resources/fileTemplates/j2ee").files) {
eachFile {
relativePath = RelativePath(true, "fileTemplates", "j2ee", this.name)
}
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ platformDownloadSources = true

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
platformPlugins = com.jetbrains.php:212.4746.100,JavaScriptLanguage
platformPlugins=com.jetbrains.php:212.4746.100,JavaScriptLanguage,Git4Idea

# Java language level used to compile sources and to generate the files for - Java 11 is required since 2020.3
javaVersion = 11
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/de/shyim/shopware6/ui/NewChangelogDialog.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.shyim.shopware6.ui.NewChangelogDialog">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1ddb8" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Title:"/>
</properties>
</component>
<component id="8686f" class="javax.swing.JTextField" binding="titleField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ecfbe" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Ticket:"/>
</properties>
</component>
<component id="13b7b" class="javax.swing.JTextField" binding="ticketField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="8ad3d" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Flag:"/>
</properties>
</component>
<component id="e4a1d" class="javax.swing.JTextField" binding="flagField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<vspacer id="bf0a7">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
10 changes: 10 additions & 0 deletions src/main/java/de/shyim/shopware6/ui/NewChangelogDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.shyim.shopware6.ui;

import javax.swing.*;

public class NewChangelogDialog {
public JTextField titleField;
public JTextField ticketField;
public JTextField flagField;
public JPanel panel;
}
53 changes: 53 additions & 0 deletions src/main/java/de/shyim/shopware6/ui/NewComponentDialog.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.shyim.shopware6.ui.NewComponentDialog">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5d067" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Name"/>
</properties>
</component>
<component id="4880f" class="javax.swing.JTextField" binding="componentName">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="97ced" class="javax.swing.JCheckBox" binding="createSCSSFile">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<selected value="true"/>
<text value="Create SCSS file?"/>
</properties>
</component>
<vspacer id="8f1d9">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="42946" class="javax.swing.JCheckBox" binding="createTwigFileCheckBox" default-binding="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Create Twig file?"/>
</properties>
</component>
</children>
</grid>
</form>
10 changes: 10 additions & 0 deletions src/main/java/de/shyim/shopware6/ui/NewComponentDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.shyim.shopware6.ui;

import javax.swing.*;

public class NewComponentDialog {
public JTextField componentName;
public JCheckBox createSCSSFile;
public JCheckBox createTwigFileCheckBox;
public JPanel panel;
}
20 changes: 9 additions & 11 deletions src/main/kotlin/de/shyim/shopware6/action/generator/ActionUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.ui.Messages
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.codeStyle.CodeStyleManager
import java.io.IOException


class ActionUtil {
Expand All @@ -25,7 +23,13 @@ class ActionUtil {
return directories[0] ?: return null
}

fun buildFile(event: AnActionEvent, project: Project, templatePath: String, fileName: String?, fileType: LanguageFileType) {
fun buildFile(
event: AnActionEvent,
project: Project,
content: String,
fileName: String?,
fileType: LanguageFileType
) {
val dataContext = event.dataContext
val view = LangDataKeys.IDE_VIEW.getData(dataContext) ?: return
val initialBaseDir = this.getViewDirectory(dataContext) ?: return
Expand All @@ -34,13 +38,7 @@ class ActionUtil {
Messages.showInfoMessage("File exists", "Error")
return
}
val content: String
content = try {
StreamUtil.readText(ActionUtil::class.java.getResourceAsStream(templatePath), "UTF-8")
} catch (e: IOException) {
e.printStackTrace()
return
}

val factory = PsiFileFactory.getInstance(project)
val file = factory.createFileFromText(fileName, fileType, content)
ApplicationManager.getApplication().runWriteAction {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.shyim.shopware6.action.generator

import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.fileTypes.PlainTextFileType
import com.intellij.openapi.project.DumbAwareAction
import de.shyim.shopware6.action.generator.ui.NewChangelogDialogWrapper
import de.shyim.shopware6.templates.ShopwareTemplates
import git4idea.branch.GitBranchUtil
import org.apache.commons.lang.StringUtils
import java.text.SimpleDateFormat
import java.util.*

class NewChangelogAction: DumbAwareAction("Create a Changelog", "Create a new Changelog file", AllIcons.FileTypes.Text) {
override fun actionPerformed(e: AnActionEvent) {
if (e.project == null) {
return
}

val currentBranch = GitBranchUtil.getCurrentRepository(e.project!!)?.currentBranch
var defaultTitle = ""
var defaultTicket = ""

if (currentBranch != null && currentBranch.name.startsWith("next-")) {
val branchParts = currentBranch.name.split("/")

if (branchParts.count() == 2) {
defaultTicket = branchParts[0].uppercase()
defaultTitle = StringUtils.capitalize(branchParts[1])
}
}

val dialog = NewChangelogDialogWrapper(defaultTitle, defaultTicket)
val dialogResult = dialog.showAndGetConfig() ?: return

val date = Date()
val modifiedDate: String = SimpleDateFormat("yyyy-MM-dd").format(date)

val fileName = modifiedDate + "-" + dialogResult.title.lowercase() + ".md"

ActionUtil.buildFile(
e,
e.project!!,
ShopwareTemplates.applyChangelogTemplate(e.project!!, dialogResult),
fileName,
PlainTextFileType.INSTANCE
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package de.shyim.shopware6.action.generator

import com.intellij.icons.AllIcons
import com.intellij.ide.highlighter.HtmlFileType
import com.intellij.ide.util.DirectoryChooser
import com.intellij.lang.javascript.JavaScriptFileType;
import com.intellij.lang.javascript.JavaScriptFileType
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.io.StreamUtil
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.codeStyle.CodeStyleManager
import de.shyim.shopware6.action.generator.ui.NewComponentDialogWrapper
import java.io.IOException
import de.shyim.shopware6.templates.ShopwareTemplates

class NewComponentAction: DumbAwareAction("Create a component", "Create a new Vue component", AllIcons.FileTypes.JavaScript) {
override fun actionPerformed(e: AnActionEvent) {
Expand All @@ -22,41 +21,32 @@ class NewComponentAction: DumbAwareAction("Create a component", "Create a new Vu
}

val dialog = NewComponentDialogWrapper()
val componentName = dialog.showAndGetName()

if (componentName == "") {
return
}
val config = dialog.showAndGetName() ?: return

// Create folder

val folder = ActionUtil.getViewDirectory(e.dataContext) ?: return

if (folder.findSubdirectory(componentName) != null) {
if (folder.findSubdirectory(config.name) != null) {
Messages.showInfoMessage("Component already exists", "Error")
return
}

var componentFolder: PsiDirectory? = null
ApplicationManager.getApplication().runWriteAction {
componentFolder = folder.createSubdirectory(componentName)
componentFolder = folder.createSubdirectory(config.name)
}

if (componentFolder == null) {
return
}

// Create index.js

var content: String
content = try {
StreamUtil.readText(ActionUtil::class.java.getResourceAsStream("/fileTemplates/vue/component/index.js"), "UTF-8")
} catch (e: IOException) {
e.printStackTrace()
return
}

content = content.replace("#COMPONENT-NAME#", componentName)
val content = ShopwareTemplates.applyShopwareAdminVueComponent(
e.project!!,
ShopwareTemplates.SHOPWARE_ADMIN_VUE_COMPONENT,
config
)

val factory = PsiFileFactory.getInstance(e.project)
val file = factory.createFileFromText("index.js", JavaScriptFileType.INSTANCE, content)
Expand All @@ -66,22 +56,49 @@ class NewComponentAction: DumbAwareAction("Create a component", "Create a new Vu
componentFolder!!.add(file)
}

// Create html file
if (config.generateTwig) {
// Create html file

val htmlFile = factory.createFileFromText(
"${config.name}.html.twig",
HtmlFileType.INSTANCE,
ShopwareTemplates.applyShopwareAdminVueComponent(
e.project!!,
ShopwareTemplates.SHOPWARE_ADMIN_VUE_COMPONENT_TWIG,
config
)
)

ApplicationManager.getApplication().runWriteAction {
CodeStyleManager.getInstance(e.project!!).reformat(htmlFile)
componentFolder!!.add(htmlFile)
}
}

val htmlFile = factory.createFileFromText("$componentName.html.twig", HtmlFileType.INSTANCE, "")

ApplicationManager.getApplication().runWriteAction {
CodeStyleManager.getInstance(e.project!!).reformat(htmlFile)
componentFolder!!.add(htmlFile)
}
if (config.generateCss) {
// Create css file

// Create css file
val cssFile = factory.createFileFromText(
"${config.name}.scss",
HtmlFileType.INSTANCE,
ShopwareTemplates.applyShopwareAdminVueComponent(
e.project!!,
ShopwareTemplates.SHOPWARE_ADMIN_VUE_COMPONENT_SCSS,
config
)
)

val cssFile = factory.createFileFromText("$componentName.scss", HtmlFileType.INSTANCE, "")
ApplicationManager.getApplication().runWriteAction {
CodeStyleManager.getInstance(e.project!!).reformat(cssFile)
componentFolder!!.add(cssFile)
}
}

ApplicationManager.getApplication().runWriteAction {
CodeStyleManager.getInstance(e.project!!).reformat(cssFile)
componentFolder!!.add(cssFile)
val view = LangDataKeys.IDE_VIEW.getData(e.dataContext) ?: return
val psiFile = componentFolder!!.findFile("index.js")
if (psiFile != null) {
view.selectElement(psiFile)
}
}
}
Loading

0 comments on commit 7632e09

Please sign in to comment.