Skip to content

Commit

Permalink
Java home and project init imprs
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Nov 15, 2024
1 parent 80d0a84 commit 45d81b6
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 84 deletions.
18 changes: 0 additions & 18 deletions cmd/aem/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (c *CLI) instanceCmd() *cobra.Command {
cmd.AddCommand(c.instanceListCmd())
cmd.AddCommand(c.instanceAwaitCmd())
cmd.AddCommand(c.instanceBackupCmd())
cmd.AddCommand(c.instanceValidateCmd())
cmd.AddCommand(c.instanceImportCmd())
return cmd
}
Expand Down Expand Up @@ -334,20 +333,3 @@ func (c *CLI) instanceListCmd() *cobra.Command {
},
}
}

func (c *CLI) instanceValidateCmd() *cobra.Command {
return &cobra.Command{
Use: "validate",
Aliases: []string{"verify"},
Short: "Validate prerequisites for AEM instance(s)",
Run: func(cmd *cobra.Command, args []string) {
if err := c.aem.InstanceManager().LocalOpts.Validate(); err != nil {
c.Error(err)
return
}

c.SetOutput("validated", true)
c.Changed("Validated prerequisites for instance(s)")
},
}
}
37 changes: 23 additions & 14 deletions cmd/aem/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ func (c *CLI) projectScaffoldCmd() *cobra.Command {
return
}

gettingStarted, err := c.aem.Project().GettingStarted()
if err != nil {
c.Error(err)
return
}
c.SetOutput("gettingStarted", gettingStarted)
c.SetOutput("gettingStarted", c.aem.Project().ScaffoldGettingStarted())

if changed {
c.Changed("project files scaffolded")
Expand All @@ -68,31 +63,45 @@ func (c *CLI) projectInitCmd() *cobra.Command {
Short: "Initializes AEMC in the project",
Run: func(cmd *cobra.Command, args []string) {
if !c.aem.Project().IsScaffolded() {
c.Fail(fmt.Sprintf("project need to be set up before running initialization"))
c.Fail(fmt.Sprintf("project need to be scaffolded before running initialization"))
return
}

changed := false

c.SetOutput("gettingStarted", c.aem.Project().InitGettingStartedError())

baseChanged, err := c.aem.BaseOpts().PrepareWithChanged()
changed = changed || baseChanged
if err != nil {
c.Error(err)
return
}
c.SetOutput("baseChanged", baseChanged)

// Download and prepare vendor tools (including JDK and AEM SDK)
vendorPrepared, err := c.aem.VendorManager().PrepareWithChanged()
changed = changed || vendorPrepared
if err != nil {
c.Error(err)
return
}
c.SetOutput("vendorPrepared", vendorPrepared)

gettingStarted, err := c.aem.Project().GettingStarted()
if err != nil {
// Validate AEM instance files and prepared SDK
if err := c.aem.InstanceManager().LocalOpts.Validate(); err != nil {
c.Error(err)
return
}
c.SetOutput("gettingStarted", gettingStarted)

if vendorPrepared {
c.Changed("initialized")
c.SetOutput("gettingStarted", c.aem.Project().InitGettingStartedSuccess())

if changed {
c.Changed("project initialized")
} else {
c.Ok("nothing to initialize")
c.Ok("project already initialized")
}
},
}
cmd.Flags().String(projectKindFlag, project.KindAuto, fmt.Sprintf("Type of AEM to work with (%s)", strings.Join(project.KindStrings(), "|")))
return cmd
}
21 changes: 11 additions & 10 deletions cmd/aem/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

func (c *CLI) vendorCmd() *cobra.Command {
Expand All @@ -23,19 +24,22 @@ func (c *CLI) vendorListCmd() *cobra.Command {
Short: "List vendor tools available",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
errored := false
verbose, _ := cmd.Flags().GetBool("verbose")

javaHome, err := c.aem.VendorManager().JavaManager().FindHomeDir()
if err != nil {
errored = true
log.Warnf("java home not available: %s", err)
javaHome = os.Getenv("JAVA_HOME")
if verbose {
log.Warnf("java home not available: %s", err)
}
}
c.SetOutput("javaHome", javaHome)

javaExecutable, err := c.aem.VendorManager().JavaManager().Executable()
if err != nil {
errored = true
log.Warnf("java executable not available: %s", err)
if verbose {
log.Warnf("java executable not available: %s", err)
}
}
c.SetOutput("javaExecutable", javaExecutable)

Expand All @@ -45,13 +49,10 @@ func (c *CLI) vendorListCmd() *cobra.Command {
oakRunJar := c.aem.VendorManager().OakRun().JarFile()
c.setOutput("oakRunJar", oakRunJar)

if errored {
c.Fail("vendor tool listed with errors")
} else {
c.Ok("vendor tools listed")
}
c.Ok("vendor tools listed")
},
}
cmd.Flags().BoolP("verbose", "v", false, "Log errors")
return cmd
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Opts struct {
config *cfg.Config

LibDir string
TmpDir string
ToolDir string
CacheDir string
Expand All @@ -19,6 +20,7 @@ func NewOpts(config *cfg.Config) *Opts {
return &Opts{
config: config,

LibDir: cv.GetString("base.lib_dir"),
TmpDir: cv.GetString("base.tmp_dir"),
ToolDir: cv.GetString("base.tool_dir"),
CacheDir: cv.GetString("base.cache_dir"),
Expand All @@ -29,9 +31,9 @@ func (o *Opts) Config() *cfg.Config {
return o.config
}

func (o *Opts) Prepare() (bool, error) {
func (o *Opts) PrepareWithChanged() (bool, error) {
changed := false
dirs := []string{o.TmpDir, o.ToolDir, o.CacheDir}
dirs := []string{o.LibDir, o.TmpDir, o.ToolDir, o.CacheDir}
for _, dir := range dirs {
dirEnsured, err := pathx.EnsureWithChanged(dir)
changed = changed || dirEnsured
Expand Down
1 change: 1 addition & 0 deletions pkg/cfg/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (c *Config) setDefaults() {
v.SetDefault("log.timestamp_format", "2006-01-02 15:04:05")
v.SetDefault("log.full_timestamp", true)

v.SetDefault("base.lib_dir", common.LibDir)
v.SetDefault("base.tmp_dir", common.TmpDir)
v.SetDefault("base.tool_dir", common.ToolDir)
v.SetDefault("base.cache_dir", common.CacheDir)
Expand Down
15 changes: 10 additions & 5 deletions pkg/project/app_classic/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
AEM_ENV: '{{.AEM_ENV | default "local"}}'
AEM_INSTANCE_PROCESSING_MODE: auto
AEM_OUTPUT_VALUE: NONE
JAVA_HOME:
sh: sh aemw vendor list --output-value javaHome

dotenv:
- '.env' # VCS-ignored, user-specific
Expand All @@ -18,8 +20,7 @@ tasks:
init:
desc: initialize project
cmds:
- sh aemw init
- sh aemw instance init
- sh aemw project init

status:
desc: check status of AEM instances and dispatcher
Expand Down Expand Up @@ -192,9 +193,13 @@ tasks:
- all/target/*.all-*.zip
vars:
ARGS: -PfedDev {{.AEM_BUILD_ARGS}}
JAVA_HOME:
sh: sh aemw instance init --output-value javaHome
cmd: JAVA_HOME={{.JAVA_HOME}} mvn clean package {{.ARGS}}
cmd: sh mvnw clean package {{.ARGS}}

aem:build:version:
desc: check build tool versions
cmds:
- echo "JAVA_HOME=${JAVA_HOME}"
- sh mvnw -version

aem:deploy:
desc: deploy AEM application
Expand Down
15 changes: 10 additions & 5 deletions pkg/project/app_cloud/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
AEM_ENV: '{{.AEM_ENV | default "local"}}'
AEM_INSTANCE_PROCESSING_MODE: auto
AEM_OUTPUT_VALUE: NONE
JAVA_HOME:
sh: sh aemw vendor list --output-value javaHome

dotenv:
- '.env' # VCS-ignored, user-specific
Expand All @@ -18,8 +20,7 @@ tasks:
init:
desc: initialize project
cmds:
- sh aemw init
- sh aemw instance init
- sh aemw project init

status:
desc: check status of AEM instances and dispatcher
Expand Down Expand Up @@ -187,9 +188,13 @@ tasks:
- all/target/*.all-*.zip
vars:
ARGS: -PfedDev {{.AEM_BUILD_ARGS}}
JAVA_HOME:
sh: sh aemw instance init --output-value javaHome
cmd: JAVA_HOME={{.JAVA_HOME}} mvn clean package {{.ARGS}}
cmd: sh mvnw clean package {{.ARGS}}

aem:build:version:
desc: check build tool versions
cmds:
- echo "JAVA_HOME=${JAVA_HOME}"
- sh mvnw -version

aem:deploy:
desc: deploy AEM application
Expand Down
5 changes: 3 additions & 2 deletions pkg/project/instance/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
AEM_ENV: '{{.AEM_ENV | default "local"}}'
AEM_INSTANCE_PROCESSING_MODE: auto
AEM_OUTPUT_VALUE: NONE
JAVA_HOME:
sh: sh aemw vendor list --output-value javaHome

dotenv:
- '.env' # VCS-ignored, user-specific
Expand All @@ -18,8 +20,7 @@ tasks:
init:
desc: initialize project
cmds:
- sh aemw init
- sh aemw instance init
- sh aemw project init

setup:
desc: start and provision AEM instances
Expand Down
43 changes: 26 additions & 17 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ func (p Project) ScaffoldWithChanged(kind Kind) (bool, error) {
}

func (p Project) Scaffold(kind Kind) error {
if err := p.scaffoldConventionalDirs(); err != nil {
return err
}
if err := p.scaffoldDefaultFiles(kind); err != nil {
return err
}
Expand All @@ -108,17 +105,6 @@ func (p Project) Scaffold(kind Kind) error {
return nil
}

func (p Project) scaffoldConventionalDirs() error {
log.Infof("ensuring conventional project directories")
if err := pathx.Ensure(common.LibDir); err != nil {
return err
}
if err := pathx.Ensure(common.TmpDir); err != nil {
return err
}
return nil
}

func (p Project) scaffoldDefaultFiles(kind Kind) error {
log.Infof("preparing default files for project of kind '%s'", kind)
switch kind {
Expand Down Expand Up @@ -273,14 +259,37 @@ func (p Project) DirsIgnored() []string {
return []string{common.HomeDir, common.DispatcherHomeDir}
}

func (p Project) GettingStarted() (string, error) {
func (p Project) ScaffoldGettingStarted() string {
text := fmt.Sprintf(strings.Join([]string{
"As a next step provide AEM files (JAR or sdk ZIP, license, service packs) to directory '" + common.LibDir + "'.",
"Alternatively, instruct the tool where these files are located by adjusting properties: 'dist_file', 'license_file' in configuration file '" + cfg.FileDefault + "'.",
"",
"Use tasks to manage AEM instances and more:",
"",
"sh taskw --list",
"",
"It is also possible to run individual AEM Compose CLI commands separately.",
"Discover available commands by running:",
"",
"sh aemw --help",
}, "\n"))
return text
}

func (p Project) InitGettingStartedError() string {
text := fmt.Sprintf(strings.Join([]string{
"Be sure to provide AEM files (JAR or sdk ZIP, license, service packs) to directory '" + common.LibDir + "'.",
}, "\n"))
return text
}

func (p Project) InitGettingStartedSuccess() string {
text := fmt.Sprintf(strings.Join([]string{
"AEM Compose project is ready to use.",
"",
fmt.Sprintf("Make sure to exclude the directories from VCS versioning and IDE indexing: %s", strings.Join(p.DirsIgnored(), ", ")),
"",
"Finally, use tasks to manage AEM instances and more:",
"Use tasks to manage AEM instances and more:",
"",
"sh taskw --list",
"",
Expand All @@ -289,5 +298,5 @@ func (p Project) GettingStarted() (string, error) {
"",
"sh aemw --help",
}, "\n"))
return text, nil
return text
}
18 changes: 7 additions & 11 deletions pkg/vendor_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,17 @@ func (vm *VendorManager) InstanceJar() (string, error) {
}

func (vm *VendorManager) PrepareWithChanged() (bool, error) {
// validation phase (quick feedback)
sdk, err := vm.quickstart.IsDistSDK()
if err != nil {
return false, err
}
// preparation phase (slow feedback)
changed := false

baseChanged, err := vm.aem.BaseOpts().Prepare()
changed = changed || baseChanged
javaChanged, err := vm.javaManager.PrepareWithChanged()
changed = changed || javaChanged
if err != nil {
return changed, err
}

javaChanged, err := vm.javaManager.PrepareWithChanged()
changed = changed || javaChanged
sdk, err := vm.quickstart.IsDistSDK()
if err != nil {
return changed, err
return false, err
}
if sdk {
sdkChanged, err := vm.sdk.PrepareWithChanged()
Expand All @@ -59,16 +52,19 @@ func (vm *VendorManager) PrepareWithChanged() (bool, error) {
return changed, err
}
}

oakRunChanged, err := vm.oakRun.PrepareWithChanged()
changed = changed || oakRunChanged
if err != nil {
return changed, err
}

vaultCLIChanged, err := vm.vaultCLI.PrepareWithChanged()
changed = changed || vaultCLIChanged
if err != nil {
return changed, err
}

return changed, nil
}

Expand Down

0 comments on commit 45d81b6

Please sign in to comment.