-
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
8 changed files
with
333 additions
and
1 deletion.
There are no files selected for viewing
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,3 @@ | ||
Bundle-Name: {{.CamelCaseName}} | ||
Bundle-SymbolicName: {{.Package}} | ||
Bundle-Version: 1.0.0 |
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,3 @@ | ||
dependencies { | ||
compileOnly group: "com.liferay.portal", name: "release.portal.api" | ||
} |
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,3 @@ | ||
.gradle/ | ||
build/ | ||
target/ |
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,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> |
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 @@ | ||
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"); | ||
} | ||
|
||
} |
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,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) | ||
} |
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,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 | ||
}) | ||
} |