Skip to content

Commit cc821ff

Browse files
refactor
1 parent 99124e4 commit cc821ff

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

pkg/content/content.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ func determineStringSlice(values any, key string) []string {
514514
}
515515

516516
func IsContentFile(path string) bool {
517-
if !strings.HasSuffix(path, JCRContentFile) {
517+
if pathx.IsDir(path) || !strings.HasSuffix(path, JCRContentFile) {
518518
return false
519519
}
520520

@@ -530,3 +530,7 @@ func IsContentFile(path string) bool {
530530
}
531531
return false
532532
}
533+
534+
func DetermineJcrPath(path string) string {
535+
return strings.Split(path, JCRRoot)[1]
536+
}

pkg/content_manager.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66
"github.com/wttech/aemc/pkg/common/pathx"
77
"github.com/wttech/aemc/pkg/common/timex"
88
"github.com/wttech/aemc/pkg/content"
9+
"os"
910
"path/filepath"
1011
"regexp"
1112
"strings"
1213
)
1314

1415
const (
15-
NamespacePattern = "^_([a-z]+)_"
16+
NamespacePattern = "_([a-z]+)_"
1617
)
1718

1819
var (
@@ -36,6 +37,9 @@ func (cm *ContentManager) pkgMgr() *PackageManager {
3637
}
3738

3839
func (cm *ContentManager) tmpDir() string {
40+
if !cm.instance.manager.aem.config.TemplateFileExists() {
41+
return os.TempDir()
42+
}
3943
return cm.instance.manager.aem.baseOpts.TmpDir
4044
}
4145

@@ -75,7 +79,7 @@ func (cm *ContentManager) PullDir(dir string, clean bool, replace bool, packageO
7579
if err := pathx.Ensure(dir); err != nil {
7680
return err
7781
}
78-
before, _, _ := strings.Cut(dir, content.JCRRoot)
82+
mainDir := strings.Split(dir, content.JCRRoot)[0]
7983
contentManager := cm.instance.manager.aem.contentManager
8084
if replace {
8185
if err := contentManager.Prepare(dir); err != nil {
@@ -85,7 +89,7 @@ func (cm *ContentManager) PullDir(dir string, clean bool, replace bool, packageO
8589
if err := contentManager.BeforePullDir(dir); err != nil {
8690
return err
8791
}
88-
if err := filex.CopyDir(filepath.Join(workDir, content.JCRRoot), filepath.Join(before, content.JCRRoot)); err != nil {
92+
if err := filex.CopyDir(filepath.Join(workDir, content.JCRRoot), filepath.Join(mainDir, content.JCRRoot)); err != nil {
8993
return err
9094
}
9195
if err := contentManager.AfterPullDir(dir); err != nil {
@@ -116,12 +120,12 @@ func (cm *ContentManager) PullFile(file string, clean bool, packageOpts PackageC
116120
if err := pathx.Ensure(dir); err != nil {
117121
return err
118122
}
119-
_, after, _ := strings.Cut(dir, content.JCRRoot)
123+
jcrPath := content.DetermineJcrPath(dir)
120124
contentManager := cm.instance.manager.aem.contentManager
121125
if err := contentManager.BeforePullFile(file); err != nil {
122126
return err
123127
}
124-
if err := filex.CopyDir(filepath.Join(workDir, content.JCRRoot, after), dir); err != nil {
128+
if err := filex.CopyDir(filepath.Join(workDir, content.JCRRoot, jcrPath), dir); err != nil {
125129
return err
126130
}
127131
if err := contentManager.AfterPullFile(file); err != nil {
@@ -144,13 +148,6 @@ func (cm *ContentManager) PullFile(file string, clean bool, packageOpts PackageC
144148
return nil
145149
}
146150

147-
func determineCleanFile(file string) string {
148-
if namespacePatternRegex.MatchString(file) && !strings.HasSuffix(file, content.JCRContentFile) {
149-
return filepath.Join(strings.ReplaceAll(file, content.JCRContentFileSuffix, ""), content.JCRContentFile)
150-
}
151-
return file
152-
}
153-
154151
func (cm *ContentManager) Push(packageOpts PackageCreateOpts) error {
155152
remotePath, err := cm.pkgMgr().Create(packageOpts)
156153
if err != nil {
@@ -165,6 +162,13 @@ func (cm *ContentManager) Push(packageOpts PackageCreateOpts) error {
165162
return nil
166163
}
167164

165+
func determineCleanFile(file string) string {
166+
if namespacePatternRegex.MatchString(file) && !strings.HasSuffix(file, content.JCRContentFile) {
167+
return filepath.Join(strings.ReplaceAll(file, content.JCRContentFileSuffix, ""), content.JCRContentFile)
168+
}
169+
return file
170+
}
171+
168172
func (cm *ContentManager) Copy(destInstance *Instance, clean bool, pkgOpts PackageCreateOpts) error {
169173
var pkgFile = pathx.RandomFileName(cm.tmpDir(), "content_copy", ".zip")
170174
defer func() { _ = pathx.DeleteIfExists(pkgFile) }()

pkg/package_manager.go

+22-24
Original file line numberDiff line numberDiff line change
@@ -207,28 +207,28 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
207207
if opts.PushContent {
208208
for _, contentPath := range opts.ContentPaths {
209209
if pathx.IsDir(contentPath) {
210-
_, after, _ := strings.Cut(contentPath, content.JCRRoot)
211-
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
210+
jcrPath := content.DetermineJcrPath(contentPath)
211+
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, jcrPath)); err != nil {
212212
return "", err
213213
}
214-
if err = filex.CopyDir(contentPath, filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
214+
if err = filex.CopyDir(contentPath, filepath.Join(tmpDir, content.JCRRoot, jcrPath)); err != nil {
215215
return "", err
216216
}
217217
} else if pathx.IsFile(contentPath) {
218218
dir := filepath.Dir(contentPath)
219-
_, after, _ := strings.Cut(dir, content.JCRRoot)
220-
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
219+
jcrPath := content.DetermineJcrPath(dir)
220+
if err = pathx.Ensure(filepath.Join(tmpDir, content.JCRRoot, jcrPath)); err != nil {
221221
return "", err
222222
}
223-
_, after, _ = strings.Cut(contentPath, content.JCRRoot)
224-
if err = filex.Copy(contentPath, filepath.Join(tmpDir, content.JCRRoot, after), true); err != nil {
223+
jcrPath = content.DetermineJcrPath(contentPath)
224+
if err = filex.Copy(contentPath, filepath.Join(tmpDir, content.JCRRoot, jcrPath), true); err != nil {
225225
return "", err
226226
}
227227
if strings.HasSuffix(contentPath, content.JCRContentFile) {
228228
dir = strings.ReplaceAll(contentPath, content.JCRContentNode, content.JCRContentDirName)
229-
_, after, _ = strings.Cut(dir, content.JCRRoot)
230229
if pathx.Exists(dir) {
231-
if err = filex.CopyDir(dir, filepath.Join(tmpDir, content.JCRRoot, after)); err != nil {
230+
jcrPath := content.DetermineJcrPath(dir)
231+
if err = filex.CopyDir(dir, filepath.Join(tmpDir, content.JCRRoot, jcrPath)); err != nil {
232232
return "", err
233233
}
234234
}
@@ -262,22 +262,17 @@ func (pm *PackageManager) Create(opts PackageCreateOpts) (string, error) {
262262
func determineFilterRoots(opts PackageCreateOpts) []string {
263263
var filterRoots []string
264264
for _, contentPath := range opts.ContentPaths {
265-
if pathx.IsDir(contentPath) {
266-
filterRoots = append(filterRoots, strings.Split(contentPath, content.JCRRoot)[1])
267-
} else if pathx.IsFile(contentPath) {
268-
contentFile := strings.Split(contentPath, content.JCRRoot)[1]
269-
if content.IsContentFile(contentPath) {
270-
filterRoots = append(filterRoots, strings.ReplaceAll(contentFile, content.JCRContentFile, content.JCRContentNode))
271-
} else if strings.HasSuffix(contentFile, content.JCRContentFile) {
272-
contentFile = namespacePatternRegex.ReplaceAllString(contentFile, "$1:")
273-
filterRoots = append(filterRoots, filepath.Dir(contentFile))
274-
} else if strings.HasSuffix(contentFile, content.JCRContentFileSuffix) {
275-
contentFile = namespacePatternRegex.ReplaceAllString(contentFile, "$1:")
276-
filterRoots = append(filterRoots, strings.ReplaceAll(contentFile, content.JCRContentFileSuffix, ""))
277-
} else {
278-
filterRoots = append(filterRoots, contentFile)
279-
}
265+
filterRoot := content.DetermineJcrPath(contentPath)
266+
if content.IsContentFile(contentPath) {
267+
filterRoot = strings.ReplaceAll(filterRoot, content.JCRContentFile, content.JCRContentNode)
268+
} else if strings.HasSuffix(filterRoot, content.JCRContentFile) {
269+
filterRoot = namespacePatternRegex.ReplaceAllString(filterRoot, "$1:")
270+
filterRoot = filepath.Dir(filterRoot)
271+
} else if strings.HasSuffix(filterRoot, content.JCRContentFileSuffix) {
272+
filterRoot = namespacePatternRegex.ReplaceAllString(filterRoot, "$1:")
273+
filterRoot = strings.ReplaceAll(filterRoot, content.JCRContentFileSuffix, "")
280274
}
275+
filterRoots = append(filterRoots, filterRoot)
281276
}
282277
return filterRoots
283278
}
@@ -301,6 +296,9 @@ func (pm *PackageManager) Copy(remotePath string, destInstance *Instance) error
301296
}
302297

303298
func (pm *PackageManager) tmpDir() string {
299+
if !pm.instance.manager.aem.config.TemplateFileExists() {
300+
return os.TempDir()
301+
}
304302
return pm.instance.manager.aem.baseOpts.TmpDir
305303
}
306304

0 commit comments

Comments
 (0)