Skip to content

Commit

Permalink
feat: add command template
Browse files Browse the repository at this point in the history
  • Loading branch information
lgdd committed May 29, 2023
1 parent b10d2b5 commit abf3674
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lfr/pkg/assets/tpl/cmd/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bundle-Name: {{.CamelCaseName}}
Bundle-SymbolicName: {{.Package}}
Bundle-Version: 1.0.0
3 changes: 3 additions & 0 deletions lfr/pkg/assets/tpl/cmd/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
compileOnly group: "com.liferay.portal", name: "release.portal.api"
}
3 changes: 3 additions & 0 deletions lfr/pkg/assets/tpl/cmd/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gradle/
build/
target/
90 changes: 90 additions & 0 deletions lfr/pkg/assets/tpl/cmd/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>{{.WorkspacePackage}}</groupId>
<artifactId>{{.WorkspaceName}}-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>{{.Package}}</groupId>
<artifactId>{{.Name}}</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>{{.WorkspaceCamelCaseName}} :: Modules :: {{.CamelCaseName}}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>release.portal.api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>5.2.0</version>
<executions>
<execution>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.ant.bnd</artifactId>
<version>3.2.6</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.css.builder</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<baseDir>src/main/resources/META-INF/resources</baseDir>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
<excludes>
<exclude>**/META-INF/resources/**/.sass-cache/</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions lfr/pkg/assets/tpl/cmd/src/main/java/Cmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package {{.Package}};

import org.osgi.service.component.annotations.Component;

@Component(
immediate = true,
property = {
"osgi.command.scope=lfr",
"osgi.command.function=hello"
},
service = {{.CamelCaseName}}.class
)
public class {{.CamelCaseName}} {

public void hello(){
System.out.println("hello world");
}

}
3 changes: 2 additions & 1 deletion lfr/pkg/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
Cmd.AddCommand(createMvcPortlet)
Cmd.AddCommand(createSpringPortlet)
Cmd.AddCommand(createApiModule)
Cmd.AddCommand(createCmdModule)
Cmd.AddCommand(createServiceBuilder)
Cmd.AddCommand(createRestBuilder)
Cmd.AddCommand(createDocker)
Expand All @@ -42,7 +43,7 @@ func init() {
func promptCreateChoices(cmd *cobra.Command, args []string) {
promptTemplate := promptui.Select{
Label: "Choose a template",
Items: []string{"client-extension", "api", "docker", "mvc-portlet", "rest-builder", "service-builder", "spring-mvc-portlet", "workspace"},
Items: []string{"client-extension", "api", "command", "docker", "mvc-portlet", "rest-builder", "service-builder", "spring-mvc-portlet", "workspace"},
}

_, template, err := promptTemplate.Run()
Expand Down
20 changes: 20 additions & 0 deletions lfr/pkg/cmd/create/create_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package create

import (
"github.com/lgdd/liferay-cli/lfr/pkg/generate/gogocmd"
"github.com/spf13/cobra"
)

var (
createCmdModule = &cobra.Command{
Use: "command NAME",
Aliases: []string{"cmd"},
Args: cobra.ExactArgs(1),
Run: generateCmdModule,
}
)

func generateCmdModule(cmd *cobra.Command, args []string) {
name := args[0]
gogocmd.Generate(name)
}
193 changes: 193 additions & 0 deletions lfr/pkg/generate/gogocmd/gogocmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package gogocmd

import (
"encoding/xml"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/iancoleman/strcase"
"github.com/lgdd/liferay-cli/lfr/pkg/project"
"github.com/lgdd/liferay-cli/lfr/pkg/util/fileutil"
"github.com/lgdd/liferay-cli/lfr/pkg/util/printutil"
)

// CmdData contains the data to be injected into the template files
type CmdData struct {
Package string
Name string
CamelCaseName string
WorkspaceName string
WorkspaceCamelCaseName string
WorkspacePackage string
}

// Generates the structure for an API module
func Generate(name string) {
sep := string(os.PathSeparator)
liferayWorkspace, err := fileutil.GetLiferayWorkspacePath()

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

portletPackage := project.PackageName
workspacePackage, _ := project.GetGroupId()

if portletPackage == "org.acme" && workspacePackage != "org.acme" {
portletPackage = strings.Join([]string{workspacePackage, strcase.ToDelimited(name, '.')}, ".")
}

name = strcase.ToKebab(name)
destPortletParentPath := filepath.Join(liferayWorkspace, "modules")
destPortletPath := filepath.Join(destPortletParentPath, name)
packagePath := strings.ReplaceAll(portletPackage, ".", string(os.PathSeparator))
packagePath = filepath.Join(destPortletPath, "src", "main", "java", packagePath)
camelCaseName := strcase.ToCamel(name)
workspaceSplit := strings.Split(liferayWorkspace, sep)
workspaceName := workspaceSplit[len(workspaceSplit)-1]

err = fileutil.CreateDirsFromAssets("tpl/cmd", destPortletPath)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

err = fileutil.CreateFilesFromAssets("tpl/cmd", destPortletPath)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

err = os.Rename(filepath.Join(destPortletPath, "gitignore"), filepath.Join(destPortletPath, ".gitignore"))

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

fileutil.CreateDirs(packagePath)
fileutil.CreateDirs(filepath.Join(destPortletPath, "src", "main", "resources", "META-INF", "resources"))
fileutil.CreateFiles([]string{filepath.Join(destPortletPath, "src", "main", "resources", ".gitkeep")})

updateJavaFiles(camelCaseName, destPortletPath, packagePath)

if fileutil.IsGradleWorkspace(liferayWorkspace) {
pomPath := filepath.Join(destPortletPath, "pom.xml")
err = os.Remove(pomPath)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}
}

if fileutil.IsMavenWorkspace(liferayWorkspace) {
buildGradlePath := filepath.Join(destPortletPath, "build.gradle")
err = os.Remove(buildGradlePath)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

pomParentPath := filepath.Join(destPortletPath, "../pom.xml")
pomParent, err := os.Open(pomParentPath)
if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}
defer pomParent.Close()

byteValue, _ := ioutil.ReadAll(pomParent)

var pom project.Pom
err = xml.Unmarshal(byteValue, &pom)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

modules := append(pom.Modules.Module, name)
pom.Modules.Module = modules
pom.Xsi = "http://www.w3.org/2001/XMLSchema-instance"
pom.SchemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"

finalPomBytes, _ := xml.MarshalIndent(pom, "", " ")

err = ioutil.WriteFile(pomParentPath, []byte(project.XMLHeader+string(finalPomBytes)), 0644)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

printutil.Warning("update ")
fmt.Printf("%s\n", pomParentPath)
}

data := &CmdData{
Package: portletPackage,
Name: name,
CamelCaseName: camelCaseName,
WorkspaceName: workspaceName,
WorkspaceCamelCaseName: strcase.ToCamel(workspaceName),
WorkspacePackage: workspacePackage,
}

err = updateMvcPortletWithData(destPortletPath, data)

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}

_ = filepath.Walk(destPortletPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
printutil.Success("created ")
fmt.Printf("%s\n", path)
}
return nil
})

}

func updateJavaFiles(camelCaseName, modulePath, packagePath string) {
defaultSrcPath := filepath.Join(modulePath, "src", "main", "java")
err := os.Rename(filepath.Join(defaultSrcPath, "Cmd.java"), filepath.Join(packagePath, camelCaseName+".java"))

if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
}
}

func updateMvcPortletWithData(destPortletPath string, data *CmdData) error {
return filepath.Walk(destPortletPath, func(path string, info fs.FileInfo, err error) error {

if err != nil {
return err
}

if !info.IsDir() {
err = fileutil.UpdateWithData(path, data)
}

if err != nil {
return err
}

return nil
})
}

0 comments on commit abf3674

Please sign in to comment.