Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bundle support #227

Merged
merged 5 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
"vscode": {
"extensions": [
"golang.go-nightly",
"github.vscode-github-actions"
"github.vscode-github-actions",
"redhat.vscode-yaml",
"liuchao.go-struct-tag",
"redhat.vscode-yaml"
]
}
}
}
}
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"go.formatTool": "gofumpt",
"go.formatTool": "gofmt",
"go.useLanguageServer": true,
"gopls": {
"formatting.gofumpt": true,
Expand All @@ -15,5 +15,8 @@
".idea": true
},
"go.toolsManagement.autoUpdate": false,
"go.toolsManagement.checkForUpdates": "off"
"go.toolsManagement.checkForUpdates": "off",
"yaml.schemas": {
"https://raw.githubusercontent.com/FriendsOfShopware/shopware-cli/main/extension/shopware-extension-schema.json": "file:///workspaces/shopware-cli/.shopware-extension.yml"
}
}
2 changes: 1 addition & 1 deletion cmd/extension/extension_admin_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var extensionAdminWatchCmd = &cobra.Command{

name, _ := ext.GetName()

options := esbuild.NewAssetCompileOptionsAdmin(name, ext.GetPath(), ext.GetType())
options := esbuild.NewAssetCompileOptionsAdmin(name, ext.GetPath())
options.ProductionMode = false

esbuildContext, esBuildError := esbuild.Context(cmd.Context(), options)
Expand Down
15 changes: 13 additions & 2 deletions cmd/extension/extension_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var extensionAssetBundleCmd = &cobra.Command{
Short: "Builds assets for extensions",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
assetCfg := extension.AssetBuildConfig{EnableESBuildForAdmin: false, EnableESBuildForStorefront: false}
assetCfg := extension.AssetBuildConfig{
EnableESBuildForAdmin: false,
EnableESBuildForStorefront: false,
ShopwareRoot: os.Getenv("SHOPWARE_PROJECT_ROOT"),
}
validatedExtensions := make([]extension.Extension, 0)

for _, arg := range args {
Expand All @@ -40,7 +44,14 @@ var extensionAssetBundleCmd = &cobra.Command{
assetCfg.EnableESBuildForStorefront = extCfg.Build.Zip.Assets.EnableESBuildForStorefront
}

err := extension.BuildAssetsForExtensions(cmd.Context(), os.Getenv("SHOPWARE_PROJECT_ROOT"), validatedExtensions, assetCfg)
constraint, err := validatedExtensions[0].GetShopwareVersionConstraint()
if err != nil {
return fmt.Errorf("cannot get shopware version constraint: %w", err)
}

assetCfg.ShopwareVersion = constraint

err = extension.BuildAssetsForExtensions(cmd.Context(), extension.ConvertExtensionsToSources(cmd.Context(), validatedExtensions), assetCfg)
if err != nil {
return fmt.Errorf("cannot build assets: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/extension/extension_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,20 @@ var extensionZipCmd = &cobra.Command{
return err
}

shopwareConstraint, err := tempExt.GetShopwareVersionConstraint()
if err != nil {
return fmt.Errorf("get shopware version constraint: %w", err)
}

assetBuildConfig := extension.AssetBuildConfig{
EnableESBuildForAdmin: extCfg.Build.Zip.Assets.EnableESBuildForAdmin,
EnableESBuildForStorefront: extCfg.Build.Zip.Assets.EnableESBuildForStorefront,
CleanupNodeModules: true,
ShopwareRoot: os.Getenv("SHOPWARE_PROJECT_ROOT"),
ShopwareVersion: shopwareConstraint,
}

if err := extension.BuildAssetsForExtensions(cmd.Context(), os.Getenv("SHOPWARE_PROJECT_ROOT"), []extension.Extension{tempExt}, assetBuildConfig); err != nil {
if err := extension.BuildAssetsForExtensions(cmd.Context(), extension.ConvertExtensionsToSources(cmd.Context(), []extension.Extension{tempExt}), assetBuildConfig); err != nil {
return fmt.Errorf("building assets: %w", err)
}

Expand Down
35 changes: 23 additions & 12 deletions cmd/project/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var projectCI = &cobra.Command{

composer := exec.CommandContext(cmd.Context(), "composer", "install", "--no-dev", "--no-interaction", "--no-progress", "--optimize-autoloader", "--classmap-authoritative")
composer.Dir = args[0]
composer.Stdin = os.Stdin
composer.Stdout = os.Stdout
composer.Stderr = os.Stderr

Expand All @@ -66,11 +67,21 @@ var projectCI = &cobra.Command{

logging.FromContext(cmd.Context()).Infof("Looking for extensions to build assets in project")

extensions := extension.FindExtensionsFromProject(cmd.Context(), args[0])
sources := extension.FindAssetSourcesOfProject(cmd.Context(), args[0])
constraint, err := extension.GetShopwareProjectConstraint(args[0])
if err != nil {
return err
}

assetCfg := extension.AssetBuildConfig{EnableESBuildForAdmin: false, EnableESBuildForStorefront: false, CleanupNodeModules: true}
assetCfg := extension.AssetBuildConfig{
EnableESBuildForAdmin: false,
EnableESBuildForStorefront: false,
CleanupNodeModules: true,
ShopwareRoot: args[0],
ShopwareVersion: constraint,
}

if err := extension.BuildAssetsForExtensions(cmd.Context(), args[0], extensions, assetCfg); err != nil {
if err := extension.BuildAssetsForExtensions(cmd.Context(), sources, assetCfg); err != nil {
return err
}

Expand All @@ -80,8 +91,8 @@ var projectCI = &cobra.Command{
}

if !shopCfg.Build.KeepExtensionSource {
for _, ext := range extensions {
if err := cleanupAdministrationFiles(cmd.Context(), ext.GetRootDir()); err != nil {
for _, source := range sources {
if err := cleanupAdministrationFiles(cmd.Context(), source.Path); err != nil {
return err
}
}
Expand Down Expand Up @@ -126,20 +137,20 @@ var projectCI = &cobra.Command{
if shopCfg.Build.RemoveExtensionAssets {
logging.FromContext(cmd.Context()).Infof("Deleting assets of extensions")

for _, ext := range extensions {
if _, err := os.Stat(path.Join(ext.GetRootDir(), "Resources", "public", "administration", "css")); err == nil {
if err := os.WriteFile(path.Join(ext.GetRootDir(), "Resources", ".administration-css"), []byte{}, os.ModePerm); err != nil {
for _, source := range sources {
if _, err := os.Stat(path.Join(source.Path, "Resources", "public", "administration", "css")); err == nil {
if err := os.WriteFile(path.Join(source.Path, "Resources", ".administration-css"), []byte{}, os.ModePerm); err != nil {
return err
}
}

if _, err := os.Stat(path.Join(ext.GetRootDir(), "Resources", "public", "administration", "js")); err == nil {
if err := os.WriteFile(path.Join(ext.GetRootDir(), "Resources", ".administration-js"), []byte{}, os.ModePerm); err != nil {
if _, err := os.Stat(path.Join(source.Path, "Resources", "public", "administration", "js")); err == nil {
if err := os.WriteFile(path.Join(source.Path, "Resources", ".administration-js"), []byte{}, os.ModePerm); err != nil {
return err
}
}

if err := os.RemoveAll(path.Join(ext.GetRootDir(), "Resources", "public")); err != nil {
if err := os.RemoveAll(path.Join(source.Path, "Resources", "public")); err != nil {
return err
}
}
Expand Down Expand Up @@ -292,7 +303,7 @@ func cleanupAdministrationFiles(ctx context.Context, folder string) error {

logging.FromContext(ctx).Infof("Migrating generated snippet file for %s", folder)

snippetFolder := path.Join(adminFolder, "src", "snippet")
snippetFolder := path.Join(adminFolder, "src", "app", "snippet")
if err := os.MkdirAll(snippetFolder, os.ModePerm); err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions cmd/project/project_admin_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ var projectAdminBuildCmd = &cobra.Command{

logging.FromContext(cmd.Context()).Infof("Looking for extensions to build assets in project")

extensions := extension.FindExtensionsFromProject(cmd.Context(), projectRoot)
sources := extension.FindAssetSourcesOfProject(cmd.Context(), args[0])
constraint, err := extension.GetShopwareProjectConstraint(args[0])
if err != nil {
return err
}

assetCfg := extension.AssetBuildConfig{DisableStorefrontBuild: true}
assetCfg := extension.AssetBuildConfig{
DisableStorefrontBuild: true,
ShopwareRoot: projectRoot,
ShopwareVersion: constraint,
}

if err := extension.BuildAssetsForExtensions(cmd.Context(), projectRoot, extensions, assetCfg); err != nil {
if err := extension.BuildAssetsForExtensions(cmd.Context(), sources, assetCfg); err != nil {
return err
}

Expand Down
14 changes: 11 additions & 3 deletions cmd/project/project_storefront_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ var projectStorefrontBuildCmd = &cobra.Command{

logging.FromContext(cmd.Context()).Infof("Looking for extensions to build assets in project")

extensions := extension.FindExtensionsFromProject(cmd.Context(), projectRoot)
sources := extension.FindAssetSourcesOfProject(cmd.Context(), args[0])
constraint, err := extension.GetShopwareProjectConstraint(args[0])
if err != nil {
return err
}

assetCfg := extension.AssetBuildConfig{DisableAdminBuild: true}
assetCfg := extension.AssetBuildConfig{
DisableAdminBuild: true,
ShopwareRoot: projectRoot,
ShopwareVersion: constraint,
}

if err := extension.BuildAssetsForExtensions(cmd.Context(), projectRoot, extensions, assetCfg); err != nil {
if err := extension.BuildAssetsForExtensions(cmd.Context(), sources, assetCfg); err != nil {
return err
}

Expand Down
46 changes: 46 additions & 0 deletions extension/asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package extension

import (
"context"
"path"
"path/filepath"

"github.com/FriendsOfShopware/shopware-cli/internal/asset"
"github.com/FriendsOfShopware/shopware-cli/logging"
)

func ConvertExtensionsToSources(ctx context.Context, extensions []Extension) []asset.Source {
sources := make([]asset.Source, 0)

for _, ext := range extensions {
name, err := ext.GetName()
if err != nil {
logging.FromContext(ctx).Errorf("Skipping extension %s as it has a invalid name", ext.GetPath())
continue
}

sources = append(sources, asset.Source{
Name: name,
Path: ext.GetRootDir(),
})

extConfig := ext.GetExtensionConfig()

if extConfig != nil {
for _, bundle := range extConfig.Build.ExtraBundles {
bundleName := bundle.Name

if bundleName == "" {
bundleName = filepath.Base(bundle.Path)
}

sources = append(sources, asset.Source{
Name: bundleName,
Path: path.Join(ext.GetRootDir(), bundle.Path),
})
}
}
}

return sources
}
61 changes: 23 additions & 38 deletions extension/asset_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/FriendsOfShopware/shopware-cli/internal/asset"
"github.com/FriendsOfShopware/shopware-cli/internal/esbuild"
"github.com/FriendsOfShopware/shopware-cli/logging"
"github.com/FriendsOfShopware/shopware-cli/version"
Expand All @@ -31,10 +32,12 @@ type AssetBuildConfig struct {
CleanupNodeModules bool
DisableAdminBuild bool
DisableStorefrontBuild bool
ShopwareRoot string
ShopwareVersion *version.Constraints
}

func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensions []Extension, assetConfig AssetBuildConfig) error { // nolint:gocyclo
cfgs := buildAssetConfigFromExtensions(ctx, extensions, shopwareRoot)
func BuildAssetsForExtensions(ctx context.Context, sources []asset.Source, assetConfig AssetBuildConfig) error { // nolint:gocyclo
cfgs := buildAssetConfigFromExtensions(sources, assetConfig.ShopwareRoot)

if len(cfgs) == 1 {
return nil
Expand All @@ -47,9 +50,10 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio

buildWithoutShopwareSource := assetConfig.EnableESBuildForStorefront && assetConfig.EnableESBuildForAdmin

shopwareRoot := assetConfig.ShopwareRoot
var err error
if shopwareRoot == "" && !buildWithoutShopwareSource {
shopwareRoot, err = setupShopwareInTemp(ctx, extensions[0])
shopwareRoot, err = setupShopwareInTemp(ctx, assetConfig.ShopwareVersion)

if err != nil {
return err
Expand Down Expand Up @@ -104,13 +108,13 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio

if !assetConfig.DisableAdminBuild && cfgs.RequiresAdminBuild() {
if assetConfig.EnableESBuildForAdmin {
for _, extension := range extensions {
name, _ := extension.GetName()
if !cfgs.Has(name) {
for _, source := range sources {
if !cfgs.Has(source.Name) {
continue
}

options := esbuild.NewAssetCompileOptionsAdmin(name, extension.GetPath(), extension.GetType())
// @todo: fix me later
options := esbuild.NewAssetCompileOptionsAdmin(source.Name, source.Path)

if _, err := esbuild.CompileExtensionAsset(ctx, options); err != nil {
return err
Expand All @@ -137,13 +141,13 @@ func BuildAssetsForExtensions(ctx context.Context, shopwareRoot string, extensio

if !assetConfig.DisableStorefrontBuild && cfgs.RequiresStorefrontBuild() {
if assetConfig.EnableESBuildForStorefront {
for _, extension := range extensions {
name, _ := extension.GetName()
if !cfgs.Has(name) {
for _, source := range sources {
if !cfgs.Has(source.Name) {
continue
}

options := esbuild.NewAssetCompileOptionsStorefront(name, extension.GetPath(), extension.GetType())
// @todo: fix me later
options := esbuild.NewAssetCompileOptionsStorefront(source.Name, source.Path)
if _, err := esbuild.CompileExtensionAsset(ctx, options); err != nil {
return err
}
Expand Down Expand Up @@ -237,40 +241,21 @@ func prepareShopwareForAsset(shopwareRoot string, cfgs map[string]ExtensionAsset
return nil
}

func buildAssetConfigFromExtensions(ctx context.Context, extensions []Extension, shopwareRoot string) ExtensionAssetConfig {
func buildAssetConfigFromExtensions(sources []asset.Source, shopwareRoot string) ExtensionAssetConfig {
list := make(ExtensionAssetConfig)

for _, extension := range extensions {
extName, err := extension.GetName()
if err != nil {
logging.FromContext(ctx).Errorf("Skipping extension %s as it has a invalid name", extension.GetPath())
continue
}

extPath := extension.GetPath()

if _, err := os.Stat(path.Join(extension.GetRootDir(), "Resources")); os.IsNotExist(err) {
logging.FromContext(ctx).Infof("Skipping building of assets for extension %s as it doesnt contain assets", extName)
for _, source := range sources {
if source.Name == "" {
continue
}

list[extName] = createConfigFromPath(extName, extension.GetRootDir())
resourcesDir := path.Join(source.Path, "Resources", "app")

extCfg, err := readExtensionConfig(extPath)
if err != nil {
logging.FromContext(ctx).Errorf("Skipping extension additional bundles %s as it has a invalid config", extPath)
if _, err := os.Stat(resourcesDir); os.IsNotExist(err) {
continue
}

for _, bundle := range extCfg.Build.ExtraBundles {
bundleName := bundle.Name

if bundleName == "" {
bundleName = filepath.Base(bundle.Path)
}

list[bundleName] = createConfigFromPath(bundleName, path.Join(extension.GetRootDir(), bundle.Path))
}
list[source.Name] = createConfigFromPath(source.Name, source.Path)
}

var basePath string
Expand Down Expand Up @@ -362,8 +347,8 @@ func createConfigFromPath(entryPointName string, extensionRoot string) Extension
return cfg
}

func setupShopwareInTemp(ctx context.Context, ext Extension) (string, error) {
minVersion, err := lookupForMinMatchingVersion(ctx, ext)
func setupShopwareInTemp(ctx context.Context, shopwareVersionConstraint *version.Constraints) (string, error) {
minVersion, err := lookupForMinMatchingVersion(ctx, shopwareVersionConstraint)
if err != nil {
return "", err
}
Expand Down
Loading
Loading