From 2c107b74ce96bbc1f3c8f51709be6f6c97228272 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Tue, 12 Nov 2024 10:53:44 +0100 Subject: [PATCH] feat: use resolved apps/plugins from php, fixes #427 --- cmd/project/platform.go | 6 +++- cmd/project/project_admin_build.go | 10 +++++- cmd/project/project_storefront_build.go | 10 +++++- extension/project.go | 47 +++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/cmd/project/platform.go b/cmd/project/platform.go index 7b9ae5d..de9b717 100644 --- a/cmd/project/platform.go +++ b/cmd/project/platform.go @@ -51,7 +51,11 @@ func findClosestShopwareProject() (string, error) { } func filterAndWritePluginJson(cmd *cobra.Command, projectRoot string, shopCfg *shop.Config) error { - sources := extension.FindAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg) + sources, err := extension.DumpAndLoadAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg) + + if err != nil { + return err + } cfgs := extension.BuildAssetConfigFromExtensions(cmd.Context(), sources, extension.AssetBuildConfig{}) diff --git a/cmd/project/project_admin_build.go b/cmd/project/project_admin_build.go index 2c40b9d..8cfabcd 100644 --- a/cmd/project/project_admin_build.go +++ b/cmd/project/project_admin_build.go @@ -35,7 +35,15 @@ var projectAdminBuildCmd = &cobra.Command{ logging.FromContext(cmd.Context()).Infof("Looking for extensions to build assets in project") - sources := extension.FindAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg) + if err := runTransparentCommand(commandWithRoot(phpexec.ConsoleCommand(cmd.Context(), "feature:dump"), projectRoot)); err != nil { + return err + } + + sources, err := extension.DumpAndLoadAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg) + + if err != nil { + return err + } forceInstall, _ := cmd.PersistentFlags().GetBool("force-install-dependencies") diff --git a/cmd/project/project_storefront_build.go b/cmd/project/project_storefront_build.go index c90bb2c..b3bad0d 100644 --- a/cmd/project/project_storefront_build.go +++ b/cmd/project/project_storefront_build.go @@ -35,7 +35,15 @@ var projectStorefrontBuildCmd = &cobra.Command{ logging.FromContext(cmd.Context()).Infof("Looking for extensions to build assets in project") - sources := extension.FindAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg) + if err := runTransparentCommand(commandWithRoot(phpexec.ConsoleCommand(cmd.Context(), "feature:dump"), projectRoot)); err != nil { + return err + } + + sources, err := extension.DumpAndLoadAssetSourcesOfProject(cmd.Context(), projectRoot, shopCfg) + + if err != nil { + return err + } forceInstall, _ := cmd.PersistentFlags().GetBool("force-install-dependencies") diff --git a/extension/project.go b/extension/project.go index 44e8673..0b4e044 100644 --- a/extension/project.go +++ b/extension/project.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/FriendsOfShopware/shopware-cli/internal/phpexec" "os" "path" "path/filepath" @@ -98,6 +99,7 @@ func getProjectConstraintFromKernel(project string) (*version.Constraints, error return &v, nil } +// FindAssetSourcesOfProject This finds all assets without invoking any PHP function and thinks all plugins / apps are active. Optional for CI usage. func FindAssetSourcesOfProject(ctx context.Context, project string, shopCfg *shop.Config) []asset.Source { extensions := FindExtensionsFromProject(ctx, project) sources := ConvertExtensionsToSources(ctx, extensions) @@ -152,6 +154,51 @@ func FindAssetSourcesOfProject(ctx context.Context, project string, shopCfg *sho return sources } +func DumpAndLoadAssetSourcesOfProject(ctx context.Context, project string, shopCfg *shop.Config) ([]asset.Source, error) { + dumpExec := phpexec.ConsoleCommand(ctx, "bundle:dump") + dumpExec.Dir = project + dumpExec.Stdin = os.Stdin + dumpExec.Stdout = os.Stdout + dumpExec.Stderr = os.Stderr + + if err := dumpExec.Run(); err != nil { + return nil, fmt.Errorf("could not bundle features: %w", err) + } + + var pluginsJson map[string]ExtensionAssetConfigEntry + + pluginJsonBytes, err := os.ReadFile(path.Join(project, "var", "plugins.json")) + + if err != nil { + return nil, fmt.Errorf("could not read plugins.json: %w", err) + } + + if err := json.Unmarshal(pluginJsonBytes, &pluginsJson); err != nil { + return nil, fmt.Errorf("could not parse plugins.json: %w", err) + } + + var sources []asset.Source + + for name, entry := range pluginsJson { + if entry.Administration.EntryFilePath != nil || entry.Storefront.EntryFilePath != nil { + source := asset.Source{ + Name: name, + Path: entry.BasePath, + } + + if extensionCfg, err := readExtensionConfig(path.Join(project, entry.BasePath)); err == nil { + source.AdminEsbuildCompatible = extensionCfg.Build.Zip.Assets.EnableESBuildForAdmin + source.StorefrontEsbuildCompatible = extensionCfg.Build.Zip.Assets.EnableESBuildForStorefront + source.NpmStrict = extensionCfg.Build.Zip.Assets.NpmStrict + } + + sources = append(sources, source) + } + } + + return sources, nil +} + func FindExtensionsFromProject(ctx context.Context, project string) []Extension { extensions := make(map[string]Extension)