From 45d81b6633bcc3bf0981b25aae054fb00ea3c7a8 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Fri, 15 Nov 2024 16:00:34 +0100 Subject: [PATCH] Java home and project init imprs --- cmd/aem/instance.go | 18 ------------ cmd/aem/project.go | 37 +++++++++++++++--------- cmd/aem/vendor.go | 21 +++++++------- pkg/base/base.go | 6 ++-- pkg/cfg/defaults.go | 1 + pkg/project/app_classic/Taskfile.yml | 15 ++++++---- pkg/project/app_cloud/Taskfile.yml | 15 ++++++---- pkg/project/instance/Taskfile.yml | 5 ++-- pkg/project/project.go | 43 +++++++++++++++++----------- pkg/vendor_manager.go | 18 +++++------- 10 files changed, 95 insertions(+), 84 deletions(-) diff --git a/cmd/aem/instance.go b/cmd/aem/instance.go index 439e519a..5c1cf37b 100644 --- a/cmd/aem/instance.go +++ b/cmd/aem/instance.go @@ -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 } @@ -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)") - }, - } -} diff --git a/cmd/aem/project.go b/cmd/aem/project.go index 789d606e..2fdf384e 100644 --- a/cmd/aem/project.go +++ b/cmd/aem/project.go @@ -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") @@ -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 } diff --git a/cmd/aem/vendor.go b/cmd/aem/vendor.go index 2f0a4c0b..3af8d21a 100644 --- a/cmd/aem/vendor.go +++ b/cmd/aem/vendor.go @@ -3,6 +3,7 @@ package main import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "os" ) func (c *CLI) vendorCmd() *cobra.Command { @@ -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) @@ -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 } diff --git a/pkg/base/base.go b/pkg/base/base.go index d95bed85..af05afee 100644 --- a/pkg/base/base.go +++ b/pkg/base/base.go @@ -8,6 +8,7 @@ import ( type Opts struct { config *cfg.Config + LibDir string TmpDir string ToolDir string CacheDir string @@ -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"), @@ -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 diff --git a/pkg/cfg/defaults.go b/pkg/cfg/defaults.go index 66e4ed0a..f78b94f8 100644 --- a/pkg/cfg/defaults.go +++ b/pkg/cfg/defaults.go @@ -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) diff --git a/pkg/project/app_classic/Taskfile.yml b/pkg/project/app_classic/Taskfile.yml index 94343a98..6aef4c89 100644 --- a/pkg/project/app_classic/Taskfile.yml +++ b/pkg/project/app_classic/Taskfile.yml @@ -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 @@ -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 @@ -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 diff --git a/pkg/project/app_cloud/Taskfile.yml b/pkg/project/app_cloud/Taskfile.yml index 870f241e..ec9bffe8 100644 --- a/pkg/project/app_cloud/Taskfile.yml +++ b/pkg/project/app_cloud/Taskfile.yml @@ -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 @@ -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 @@ -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 diff --git a/pkg/project/instance/Taskfile.yml b/pkg/project/instance/Taskfile.yml index 71f03ae3..49a604a2 100644 --- a/pkg/project/instance/Taskfile.yml +++ b/pkg/project/instance/Taskfile.yml @@ -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 @@ -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 diff --git a/pkg/project/project.go b/pkg/project/project.go index 7230aa5a..2f2acb9e 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -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 } @@ -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 { @@ -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", "", @@ -289,5 +298,5 @@ func (p Project) GettingStarted() (string, error) { "", "sh aemw --help", }, "\n")) - return text, nil + return text } diff --git a/pkg/vendor_manager.go b/pkg/vendor_manager.go index 6e3e761d..726b4d9e 100644 --- a/pkg/vendor_manager.go +++ b/pkg/vendor_manager.go @@ -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() @@ -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 }