Skip to content

Commit

Permalink
feat(ci): build admin assets only needed
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Oct 26, 2023
1 parent 91c8703 commit f0d1efc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist/
/*.zip
/FroshTools
/result
/project
dump.sql*

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

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

if err := extension.BuildAssetsForExtensions(cmd.Context(), sources, assetCfg); err != nil {
Expand Down
41 changes: 28 additions & 13 deletions extension/asset_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ const (
)

type AssetBuildConfig struct {
EnableESBuildForAdmin bool
EnableESBuildForStorefront bool
CleanupNodeModules bool
DisableAdminBuild bool
DisableStorefrontBuild bool
ShopwareRoot string
ShopwareVersion *version.Constraints
Browserslist string
EnableESBuildForAdmin bool
EnableESBuildForStorefront bool
CleanupNodeModules bool
DisableAdminBuild bool
DisableStorefrontBuild bool
ShopwareRoot string
ShopwareVersion *version.Constraints
Browserslist string
SkipExtensionsWithBuildFiles bool
}

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

if len(cfgs) == 1 {
return nil
Expand Down Expand Up @@ -276,7 +277,7 @@ func prepareShopwareForAsset(shopwareRoot string, cfgs map[string]ExtensionAsset
return nil
}

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

for _, source := range sources {
Expand All @@ -290,15 +291,29 @@ func buildAssetConfigFromExtensions(sources []asset.Source, shopwareRoot string)
continue
}

list[source.Name] = createConfigFromPath(source.Name, source.Path)
sourceConfig := createConfigFromPath(source.Name, source.Path)

if assetCfg.SkipExtensionsWithBuildFiles {
expectedAdminCompiledFile := path.Join(source.Path, "Resources", "public", "administration", "js", esbuild.ToKebabCase(source.Name)+".js")

if _, err := os.Stat(expectedAdminCompiledFile); err == nil {
// clear out the entrypoint, so the admin does not build it
sourceConfig.Administration.EntryFilePath = nil
sourceConfig.Administration.Webpack = nil

logging.FromContext(ctx).Infof("Skipping building administration assets for \"%s\" as compiled files are present", source.Name)
}
}

list[source.Name] = sourceConfig
}

var basePath string
if shopwareRoot == "" {
if assetCfg.ShopwareRoot == "" {
basePath = "src/Storefront/"
} else {
basePath = strings.TrimLeft(
strings.Replace(PlatformPath(shopwareRoot, "Storefront", ""), shopwareRoot, "", 1),
strings.Replace(PlatformPath(assetCfg.ShopwareRoot, "Storefront", ""), assetCfg.ShopwareRoot, "", 1),
"/",
) + "/"
}
Expand Down
10 changes: 5 additions & 5 deletions extension/asset_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGenerateConfigWithAdminAndStorefrontFiles(t *testing.T) {
assert.NoError(t, os.MkdirAll(path.Join(dir, "Resources", "app", "storefront", "src"), os.ModePerm))
assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "storefront", "src", "main.js"), []byte("test"), os.ModePerm))

config := buildAssetConfigFromExtensions([]asset.Source{{Name: "FroshTools", Path: dir}}, "")
config := buildAssetConfigFromExtensions(getTestContext(), []asset.Source{{Name: "FroshTools", Path: dir}}, AssetBuildConfig{})

assert.True(t, config.Has("FroshTools"))
assert.True(t, config.RequiresAdminBuild())
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestGenerateConfigWithTypeScript(t *testing.T) {
assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "storefront", "src", "main.ts"), []byte("test"), os.ModePerm))
assert.NoError(t, os.WriteFile(path.Join(dir, "Resources", "app", "storefront", "build", "webpack.config.js"), []byte("test"), os.ModePerm))

config := buildAssetConfigFromExtensions([]asset.Source{{Name: "FroshTools", Path: dir}}, "")
config := buildAssetConfigFromExtensions(getTestContext(), []asset.Source{{Name: "FroshTools", Path: dir}}, AssetBuildConfig{})

assert.True(t, config.Has("FroshTools"))
assert.True(t, config.RequiresAdminBuild())
Expand All @@ -71,7 +71,7 @@ func TestGenerateConfigWithTypeScript(t *testing.T) {
}

func TestGenerateConfigAddsStorefrontAlwaysAsEntrypoint(t *testing.T) {
config := buildAssetConfigFromExtensions([]asset.Source{}, "")
config := buildAssetConfigFromExtensions(getTestContext(), []asset.Source{}, AssetBuildConfig{})

assert.False(t, config.RequiresStorefrontBuild())
assert.False(t, config.RequiresAdminBuild())
Expand All @@ -80,15 +80,15 @@ func TestGenerateConfigAddsStorefrontAlwaysAsEntrypoint(t *testing.T) {
func TestGenerateConfigDoesNotAddExtensionWithoutConfig(t *testing.T) {
dir := t.TempDir()

config := buildAssetConfigFromExtensions([]asset.Source{{Name: "FroshApp", Path: dir}}, "")
config := buildAssetConfigFromExtensions(getTestContext(), []asset.Source{{Name: "FroshApp", Path: dir}}, AssetBuildConfig{})

assert.False(t, config.Has("FroshApp"))
}

func TestGenerateConfigDoesNotAddExtensionWithoutName(t *testing.T) {
dir := t.TempDir()

config := buildAssetConfigFromExtensions([]asset.Source{{Name: "", Path: dir}}, "")
config := buildAssetConfigFromExtensions(getTestContext(), []asset.Source{{Name: "", Path: dir}}, AssetBuildConfig{})

assert.Len(t, config, 1)
}

0 comments on commit f0d1efc

Please sign in to comment.