Skip to content

Commit

Permalink
fix .content.xml file push
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-przybyl-wttech committed Aug 22, 2024
1 parent 5fddff0 commit b67d822
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
33 changes: 32 additions & 1 deletion cmd/aem/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/wttech/aemc/pkg"
"github.com/wttech/aemc/pkg/common/pathx"
"github.com/wttech/aemc/pkg/content"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -183,8 +185,10 @@ func (c *CLI) contentPushCmd() *cobra.Command {
}
clean, _ := cmd.Flags().GetBool("clean")
filterRoots := determineFilterRoots(cmd)
filterFileContent := determineFilterFileContent(cmd)
if err = instance.ContentManager().Push(path, clean, pkg.PackageCreateOpts{
FilterRoots: filterRoots,
FilterRoots: filterRoots,
FilterFileContent: filterFileContent,
}); err != nil {
c.Error(err)
return
Expand Down Expand Up @@ -315,3 +319,30 @@ func determineFilterRoots(cmd *cobra.Command) []string {
}
return nil
}

func determineFilterFileContent(cmd *cobra.Command) string {
file, _ := determineContentFile(cmd)
if file == "" || !strings.HasSuffix(file, content.JCRContentFile) || content.IsContentFile(file) {
return ""
}

dir := filepath.Dir(file)
filterRoot := pkg.DetermineFilterRoot(file)
entries, err := os.ReadDir(dir)
if err != nil {
return ""
}

filterFileContent := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
filterFileContent += "<workspaceFilter version=\"1.0\">\n"
filterFileContent += fmt.Sprintf("<filter root=\"%s\">\n", filterRoot)
for _, entry := range entries {
if entry.Name() != content.JCRContentFile {
jcrPath := pkg.DetermineFilterRoot(filepath.Join(dir, entry.Name()))
filterFileContent += fmt.Sprintf(" <exclude pattern=\"%s(/.*)?\"/>\n", jcrPath)
}
}
filterFileContent += " </filter>\n"
filterFileContent += "</workspaceFilter>\n"
return filterFileContent
}
21 changes: 2 additions & 19 deletions pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,8 @@ func (cm *ContentManager) Push(contentPath string, clean bool, packageOpts Packa
defer func() {
_ = pathx.DeleteIfExists(workDir)
}()
if pathx.IsDir(contentPath) {
_, jcrPath, _ := strings.Cut(contentPath, content.JCRRoot)
if err := filex.CopyDir(contentPath, filepath.Join(workDir, content.JCRRoot, jcrPath)); err != nil {
return err
}
} else if pathx.IsFile(contentPath) {
_, jcrPath, _ := strings.Cut(contentPath, content.JCRRoot)
jcrDir := filepath.Dir(jcrPath)
if err := filex.Copy(contentPath, filepath.Join(workDir, content.JCRRoot, jcrPath), true); err != nil {
return err
}
if strings.HasSuffix(contentPath, content.JCRContentFile) {
contentDir := strings.ReplaceAll(contentPath, content.JCRContentFile, content.JCRContentDirName)
if pathx.Exists(contentDir) {
if err := filex.CopyDir(contentDir, filepath.Join(workDir, content.JCRRoot, jcrDir, content.JCRContentDirName)); err != nil {
return err
}
}
}
if err := copyContentFiles(contentPath, workDir); err != nil {
return err
}
contentManager := cm.instance.manager.aem.contentManager
if err := contentManager.CleanDir(filepath.Join(workDir, content.JCRRoot)); err != nil {
Expand Down
25 changes: 12 additions & 13 deletions pkg/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ func copyPackageDefaultFiles(targetTmpDir string, data map[string]any) error {
}

type PackageCreateOpts struct {
PID string
FilterRoots []string
FilterFile string
ContentPath string
PID string
FilterRoots []string
FilterFile string
FilterFileContent string
ContentPath string
}

func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
Expand Down Expand Up @@ -200,8 +201,13 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
return "", err
}
}
if opts.FilterFileContent != "" {
if err = filex.WriteString(filepath.Join(tmpDir, "META-INF", "vault", FilterXML), opts.FilterFileContent); err != nil {
return "", err
}
}
if opts.ContentPath != "" {
if err = pm.copyContentFiles(opts, tmpDir); err != nil {
if err = copyContentFiles(opts.ContentPath, tmpDir); err != nil {
return "", err
}
}
Expand All @@ -228,19 +234,12 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
return status.Path, nil
}

func (pm *PackageManager) copyContentFiles(opts PackageCreateOpts, tmpDir string) error {
contentPath := opts.ContentPath
func copyContentFiles(contentPath string, tmpDir string) error {
_, jcrPath, _ := strings.Cut(contentPath, content.JCRRoot)
if pathx.IsDir(contentPath) {
if err := filex.CopyDir(contentPath, filepath.Join(tmpDir, content.JCRRoot, jcrPath)); err != nil {
return err
}
} else if strings.HasSuffix(contentPath, content.JCRContentFile) && !content.IsContentFile(contentPath) {
contentDir := filepath.Dir(contentPath)
jcrDir := filepath.Dir(jcrPath)
if err := filex.CopyDir(contentDir, filepath.Join(tmpDir, content.JCRRoot, jcrDir)); err != nil {
return err
}
} else if pathx.IsFile(contentPath) {
jcrDir := filepath.Dir(jcrPath)
if err := filex.Copy(contentPath, filepath.Join(tmpDir, content.JCRRoot, jcrPath), true); err != nil {
Expand Down

0 comments on commit b67d822

Please sign in to comment.