Skip to content

Commit

Permalink
feat: add client-extension template
Browse files Browse the repository at this point in the history
  • Loading branch information
lgdd committed May 27, 2023
1 parent 9db0744 commit c6d30d5
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 1 deletion.
Empty file.
19 changes: 19 additions & 0 deletions lfr/pkg/assets/tpl/ws/maven/client-extensions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>

<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>{{.GroupId}}</groupId>
<artifactId>{{.ArtifactId}}</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>{{.GroupId}}</groupId>
<artifactId>{{.ArtifactId}}-client-extensions</artifactId>
<name>{{.Name}} :: Client Extensions</name>
<packaging>pom</packaging>
</project>
3 changes: 2 additions & 1 deletion lfr/pkg/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (

func init() {
Cmd.AddCommand(createWorkspace)
Cmd.AddCommand(createClientExtension)
Cmd.AddCommand(createMvcPortlet)
Cmd.AddCommand(createSpringPortlet)
Cmd.AddCommand(createApiModule)
Expand All @@ -40,7 +41,7 @@ func init() {
func promptCreateChoices(cmd *cobra.Command, args []string) {
promptTemplate := promptui.Select{
Label: "Choose a template",
Items: []string{"api", "docker", "mvc-portlet", "rest-builder", "service-builder", "spring-mvc-portlet", "workspace"},
Items: []string{"client-extension", "api", "docker", "mvc-portlet", "rest-builder", "service-builder", "spring-mvc-portlet", "workspace"},
}

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

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/iancoleman/strcase"
"github.com/lgdd/liferay-cli/lfr/pkg/config"
"github.com/lgdd/liferay-cli/lfr/pkg/util/fileutil"
"github.com/lgdd/liferay-cli/lfr/pkg/util/printutil"
"github.com/manifoldco/promptui"
cp "github.com/otiai10/copy"
"github.com/spf13/cobra"
)

var (
createClientExtension = &cobra.Command{
Use: "client-extension NAME",
Aliases: []string{"cx"},
Run: generateClientExtension,
Long: `Available Liferay 7.4 U45+/GA45+
Client extensions extend Liferay without using OSGi modules.
Learn more: https://learn.liferay.com/w/dxp/building-applications/client-extensions
Samples available: https://github.com/liferay/liferay-portal/tree/master/workspaces/liferay-sample-workspace/client-extensions
`,
}
)

const (
ClientExtensionSamplePrefix = "liferay-sample-"
)

func generateClientExtension(cmd *cobra.Command, args []string) {
liferayWorkspace, err := fileutil.GetLiferayWorkspacePath()

if err != nil {
panic(err)
}

fetchClientExtensionSamples(config.GetConfigPath())

clientExtensionSamplesPath := filepath.Join(config.GetConfigPath(), "liferay-portal", "workspaces", "liferay-sample-workspace", "client-extensions")
templates := getTemplateNames(clientExtensionSamplesPath)
clientExtensionsWorkspaceDir := filepath.Join(liferayWorkspace, "client-extensions")

var name string
if len(args) >= 1 && len(args[0]) > 0 {
name = args[0]
} else {
promptName := promptui.Prompt{
Label: "Choose a name",
Validate: func(input string) error {
if len(input) == 0 {
return errors.New("the name cannot be empty")
}
return nil
},
}

name, err = promptName.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
os.Exit(1)
}
}

name = strcase.ToKebab(strings.ToLower(name))

promptTemplate := promptui.Select{
Label: "Choose a template",
Items: templates,
}

_, template, err := promptTemplate.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
os.Exit(1)
}

template = filepath.Join(clientExtensionSamplesPath, ClientExtensionSamplePrefix+template)

clientExtensionDir := filepath.Join(clientExtensionsWorkspaceDir, name)

fileutil.CreateDirs(clientExtensionDir)

if err := cp.Copy(template, clientExtensionDir); err != nil {
panic(err)
}

_ = filepath.Walk(clientExtensionDir,
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 getTemplateNames(clientExtensionSamplesPath string) []string {
sampleDirs, err := os.ReadDir(clientExtensionSamplesPath)

if err != nil {
panic(err)
}

var samples []string

for _, sampleDir := range sampleDirs {
if sampleDir.IsDir() && strings.Contains(sampleDir.Name(), ClientExtensionSamplePrefix) {
samples = append(samples, strings.Split(sampleDir.Name(), ClientExtensionSamplePrefix)[1])
}
}

return samples
}

func fetchClientExtensionSamples(destination string) {
liferayPortalPath := filepath.Join(destination, "liferay-portal")

// Clone & checkout if ~/.lfr/liferay-portal does not exist
if _, err := os.Stat(filepath.Join(destination, "liferay-portal")); err != nil {
gitClone := exec.Command("git", "clone", "--depth", "1", "--filter=blob:none", "--no-checkout", "https://github.com/liferay/liferay-portal")
gitClone.Dir = destination

if err := gitClone.Run(); err != nil {
panic(err)
}

gitSparseCheckoutInit := exec.Command("git", "sparse-checkout", "init", "--no-cone")
gitSparseCheckoutInit.Dir = liferayPortalPath

if err := gitSparseCheckoutInit.Run(); err != nil {
panic(err)
}

gitSparseCheckoutSet := exec.Command("git", "sparse-checkout", "set", "workspaces/liferay-sample-workspace/client-extensions")
gitSparseCheckoutSet.Dir = liferayPortalPath

if err := gitSparseCheckoutSet.Run(); err != nil {
panic(err)
}

gitCheckout := exec.Command("git", "checkout")
gitCheckout.Dir = liferayPortalPath

if err := gitCheckout.Run(); err != nil {
panic(err)
}
} else {
// Repo already exists, try to update
gitPull := exec.Command("git", "pull")
gitPull.Dir = liferayPortalPath

if err := gitPull.Run(); err != nil {
panic(err)
}
}
}

0 comments on commit c6d30d5

Please sign in to comment.