Skip to content

Commit

Permalink
feat: add bundle support
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Sep 24, 2023
1 parent f958a4a commit aeb182a
Show file tree
Hide file tree
Showing 16 changed files with 381 additions and 77 deletions.
8 changes: 5 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"customizations": {
"vscode": {
"extensions": [
"golang.go-nightly",
"github.vscode-github-actions"
]
"golang.go-nightly",
"github.vscode-github-actions",
"redhat.vscode-yaml",
"liuchao.go-struct-tag"
]
}
}
}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
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: 24 additions & 11 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,23 @@ 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}
fmt.Println(sources)

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 +93,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 +139,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
1 change: 1 addition & 0 deletions cmd/project/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ func findClosestShopwareProject() (string, error) {

return "", fmt.Errorf("cannot find Shopware project in current directory")
}

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
44 changes: 44 additions & 0 deletions extension/asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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()

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
}
65 changes: 20 additions & 45 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(ctx, 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,11 @@ func prepareShopwareForAsset(shopwareRoot string, cfgs map[string]ExtensionAsset
return nil
}

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

Check warning on line 244 in extension/asset_platform.go

View workflow job for this annotation

GitHub Actions / Qodana for Go

Unused parameter

Unused parameter `ctx context.Context`
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)
continue
}

list[extName] = createConfigFromPath(extName, extension.GetRootDir())

extCfg, err := readExtensionConfig(extPath)
if err != nil {
logging.FromContext(ctx).Errorf("Skipping extension additional bundles %s as it has a invalid config", extPath)
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))
}
for _, source := range sources {
list[source.Name] = createConfigFromPath(source.Name, source.Path)
}

var basePath string
Expand Down Expand Up @@ -362,8 +337,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

0 comments on commit aeb182a

Please sign in to comment.