-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |