Skip to content

Commit

Permalink
feat: add support to choose Liferay edition
Browse files Browse the repository at this point in the history
New flag: --edition or -e that takes either "dxp" (i.e. enterprise edition) or "portal" (i.e. portal edition). Also the code has been reworked to use https://github.com/lgdd/liferay-product-info instead of the official JSON and its weird format.

Refs: #6
  • Loading branch information
lgdd committed Feb 4, 2024
1 parent c9c6cb4 commit ab58caa
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 869 deletions.
5 changes: 4 additions & 1 deletion lfr/pkg/cmd/create/create_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ var (
Version string
// Build is Maven or Gradle
Build string
// Edition is DXP (Enterprise) or Portal (Community)
Edition string
// Init holds the option to initialize the Liferay bundle
Init bool
)

func init() {
createWorkspace.Flags().StringVarP(&Version, "version", "v", "7.4", "Liferay major version (7.x)")
createWorkspace.Flags().StringVarP(&Build, "build", "b", "gradle", "build tool (gradle or maven)")
createWorkspace.Flags().StringVarP(&Edition, "edition", "e", "portal", "Liferay edition (dxp or portal)")
createWorkspace.Flags().BoolVarP(&Init, "init", "i", false, "executes Liferay bundle initialization (i.e. download & unzip in the workspace)")
}

Expand All @@ -39,7 +42,7 @@ func generateWorkspace(cmd *cobra.Command, args []string) {
os.Exit(1)
}
name := args[0]
err := workspace.Generate(name, Build, Version)
err := workspace.Generate(name, Build, Version, Edition)
if err != nil {
printutil.Danger(fmt.Sprintf("%s\n", err.Error()))
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions lfr/pkg/generate/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
)

// Generate the structure of a Liferay workspace
func Generate(base, build, version string) error {
metadata, err := project.NewMetadata(base, version)
func Generate(base, build, version, edition string) error {
metadata, err := project.NewMetadata(base, version, edition)
if err != nil {
return err
}
Expand Down
280 changes: 174 additions & 106 deletions lfr/pkg/project/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@ type Metadata struct {
Name string
}

type GitHubAsset struct {
BrowserDownloadURL string `json:"browser_download_url"`
type ProductInfo struct {
AppServerTomcatVersion string `json:"appServerTomcatVersion"`
BundleURL string `json:"bundleUrl"`
BundleChecksumMD5 string `json:"bundleChecksumMD5"`
BundleChecksumMD5URL string `json:"bundleChecksumMD5Url"`
LiferayDockerImage string `json:"liferayDockerImage"`
LiferayProductVersion string `json:"liferayProductVersion"`
Promoted string `json:"promoted"`
ReleaseDate string `json:"releaseDate"`
TargetPlatformVersion string `json:"targetPlatformVersion"`
Name string `json:"name"`
}

type GitHubRelease struct {
TagName string `json:"tag_name"`
Assets []GitHubAsset `json:"assets"`
}

// Build options
// Build & Edition options
const (
Gradle = "gradle"
Maven = "maven"
DXP = "dxp"
Portal = "portal"
)

// Package name to use for the project, default is org.acme
Expand Down Expand Up @@ -107,108 +113,170 @@ func GetGroupId() (string, error) {
}

// Returns metadata for a given project and the chosen Liferay version
func NewMetadata(base, version string) (*Metadata, error) {
switch version {
case "7.4":

bar := progressbar.NewOptions(-1,
progressbar.OptionSetDescription("Fetching latest info from GitHub"),
progressbar.OptionSpinnerType(11))

resp, err := http.Get("https://api.github.com/repos/liferay/liferay-portal/releases/latest")

release := &GitHubRelease{
TagName: "7.4.3.78-ga78",
Assets: []GitHubAsset{
{
BrowserDownloadURL: "https://github.com/liferay/liferay-portal/releases/download/7.4.3.78-ga78/liferay-ce-portal-tomcat-7.4.3.78-ga78-20230524080442157.tar.gz",
},
},
}
downloadURL := release.Assets[0].BrowserDownloadURL
func NewMetadata(base, version, edition string) (*Metadata, error) {
var productInfoURLBuilder strings.Builder
productInfoURLBuilder.WriteString("https://raw.githubusercontent.com/lgdd/liferay-product-info/main/")
productInfoVersion := strings.ReplaceAll(version, ".", "")

if err != nil {
bar.Clear()
printutil.Warning("Can not fetch info from GitHub\n")
printutil.Warning("Start offline mode process\n\n")
} else {
body, _ := io.ReadAll(resp.Body)
io.Copy(io.MultiWriter(bar), resp.Body)

defer resp.Body.Close()

if err := json.Unmarshal(body, &release); err != nil {
bar.Clear()
fmt.Println("Can not unmarshal GitHub release response")
fmt.Println(body)
fmt.Println(err.Error())
fmt.Println("Start offline mode process")
} else {

for _, asset := range release.Assets {
if strings.Contains(asset.BrowserDownloadURL, "tomcat") && strings.Contains(asset.BrowserDownloadURL, "tar.gz") {
downloadURL = asset.BrowserDownloadURL
break
}
}
}
if edition != DXP && edition != Portal {
return nil, errors.New("unknown edition (it should be 'dxp' or 'portal')")
}

}
if version != "7.4" && version != "7.3" && version != "7.2" && version != "7.1" && version != "7.0" {
return nil, fmt.Errorf("invalid or unsupported Liferay version")
}

productInfoURLBuilder.WriteString(edition)
productInfoURLBuilder.WriteString("_")
productInfoURLBuilder.WriteString(productInfoVersion)
productInfoURLBuilder.WriteString("_product_info.json")

bar := progressbar.NewOptions(-1,
progressbar.OptionSetDescription("Fetching latest info from https://github.com/lgdd/liferay-product-info"),
progressbar.OptionSpinnerType(11))

resp, err := http.Get(productInfoURLBuilder.String())

if err != nil {
bar.Clear()
printutil.Danger(err.Error())
os.Exit(1)
}

var productInfoList []ProductInfo
body, _ := io.ReadAll(resp.Body)

defer resp.Body.Close()

if err := json.Unmarshal(body, &productInfoList); err != nil {
bar.Clear()
fmt.Println("Can not unmarshal response")
fmt.Println(body)
fmt.Println(err.Error())
fmt.Println("Get offline data")
return getOfflineMetadata(base, version, edition)
}

latestProductInfo := productInfoList[len(productInfoList)-1]
bar.Clear()
return &Metadata{
Product: latestProductInfo.Name,
BundleUrl: latestProductInfo.BundleURL,
TargetPlatform: latestProductInfo.TargetPlatformVersion,
DockerImage: latestProductInfo.LiferayDockerImage,
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
}

gaUpdateSplit := strings.Split(release.TagName, "-")
gaUpdate := gaUpdateSplit[len(gaUpdateSplit)-1]

return &Metadata{
Product: strings.Join([]string{"portal", version, gaUpdate}, "-"),
BundleUrl: downloadURL,
TargetPlatform: strings.Split(release.TagName, "-")[0],
DockerImage: strings.Join([]string{"liferay/portal", release.TagName}, ":"),
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.3":
return &Metadata{
Product: "portal-7.3-ga8",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.3.7-ga8/liferay-ce-portal-tomcat-7.3.7-ga8-20210610183559721.tar.gz",
TargetPlatform: "7.3.7",
DockerImage: "liferay/portal:7.3.7-ga8",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.2":
return &Metadata{
Product: "portal-7.2-ga2",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.2.1-ga2/liferay-ce-portal-tomcat-7.2.1-ga2-20191111141448326.tar.gz",
TargetPlatform: "7.2.1-1",
DockerImage: "liferay/portal:7.2.1-ga2",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.1":
return &Metadata{
Product: "portal-7.1-ga4",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.1.3-ga4/liferay-ce-portal-tomcat-7.1.3-ga4-20190508171117552.tar.gz",
TargetPlatform: "7.1.3-1",
DockerImage: "liferay/portal:7.1.3-ga4",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.0":
return &Metadata{
Product: "portal-7.0-ga7",
BundleUrl: "https://releases-cdn.liferay.com/portal/7.0.6-ga7/liferay-ce-portal-tomcat-7.0-ga7-20180507111753223.zip",
TargetPlatform: "7.0.6-2",
DockerImage: "liferay/portal:7.0.6-ga7",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
func getOfflineMetadata(base, version, edition string) (*Metadata, error) {
if edition == DXP {
switch version {
case "7.4":
return &Metadata{
Product: "7.4.13.u102",
BundleUrl: "https://releases-cdn.liferay.com/dxp/7.4.13-u102/liferay-dxp-tomcat-7.4.13.u102-20231109153600206.tar.gz",
TargetPlatform: "7.4.13.u102",
DockerImage: "liferay/dxp:7.4.13-u102",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.3":
return &Metadata{
Product: "dxp-7.3-u35",
BundleUrl: "https://releases-cdn.liferay.com/dxp/7.3.10-u35/liferay-dxp-tomcat-7.3.10.u35-20231114110531823.tar.gz",
TargetPlatform: "7.3.10.u35",
DockerImage: "liferay/dxp:7.3.10-u35",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.2":
return &Metadata{
Product: "dxp-7.2-sp8",
BundleUrl: "https://api.liferay.com/downloads/portal/7.2.10.8/liferay-dxp-tomcat-7.2.10.8-sp8-slim-20220912234451782.tar.gz",
TargetPlatform: "7.2.10.8",
DockerImage: "liferay/dxp:7.2.10-sp8",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.1":
return &Metadata{
Product: "dxp-7.1-sp8",
BundleUrl: "https://releases-cdn.liferay.com/dxp/7.1.10.8/liferay-dxp-tomcat-7.1.10.8-sp8-slim-20220926154152962.tar.gz",
TargetPlatform: "7.1.10.8",
DockerImage: "liferay/dxp:7.1.10-sp8",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.0":
return &Metadata{
Product: "dxp-7.0-sp17",
BundleUrl: "https://releases-cdn.liferay.com/dxp/7.0.10.17/liferay-dxp-digital-enterprise-tomcat-7.0.10.17-sp17-slim-20211014075354439.tar.gz",
TargetPlatform: "7.0.10.17",
DockerImage: "liferay/dxp:7.0.10-sp17",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
}
} else {
switch version {
case "7.4":
return &Metadata{
Product: "portal-7.4-ga102",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.4.3.102-ga102/liferay-ce-portal-tomcat-7.4.3.102-ga102-20231109165213885.tar.gz",
TargetPlatform: "7.4.3.102",
DockerImage: "liferay/portal:7.4.3.102-ga102",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.3":
return &Metadata{
Product: "portal-7.3-ga8",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.3.7-ga8/liferay-ce-portal-tomcat-7.3.7-ga8-20210610183559721.tar.gz",
TargetPlatform: "7.3.7",
DockerImage: "liferay/portal:7.3.7-ga8",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.2":
return &Metadata{
Product: "portal-7.2-ga2",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.2.1-ga2/liferay-ce-portal-tomcat-7.2.1-ga2-20191111141448326.tar.gz",
TargetPlatform: "7.2.1-1",
DockerImage: "liferay/portal:7.2.1-ga2",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.1":
return &Metadata{
Product: "portal-7.1-ga4",
BundleUrl: "https://github.com/liferay/liferay-portal/releases/download/7.1.3-ga4/liferay-ce-portal-tomcat-7.1.3-ga4-20190508171117552.tar.gz",
TargetPlatform: "7.1.3-1",
DockerImage: "liferay/portal:7.1.3-ga4",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
case "7.0":
return &Metadata{
Product: "portal-7.0-ga7",
BundleUrl: "https://releases-cdn.liferay.com/portal/7.0.6-ga7/liferay-ce-portal-tomcat-7.0-ga7-20180507111753223.zip",
TargetPlatform: "7.0.6-2",
DockerImage: "liferay/portal:7.0.6-ga7",
GroupId: strcase.ToDelimited(PackageName, '.'),
ArtifactId: strcase.ToKebab(strings.ToLower(base)),
Name: strcase.ToCamel(strings.ToLower(base)),
}, nil
}
}
return nil, fmt.Errorf("invalid Liferay version")
return nil, fmt.Errorf("invalid or unsupported Liferay version")
}
Loading

0 comments on commit ab58caa

Please sign in to comment.