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

Empty lines #255

Merged
merged 7 commits into from
May 7, 2024
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
5 changes: 0 additions & 5 deletions pkg/common/stringsx/stringsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/gobwas/glob"
"github.com/iancoleman/strcase"
"github.com/samber/lo"
"regexp"
"strconv"
"strings"
)
Expand All @@ -32,10 +31,6 @@ func MatchSome(value string, patterns []string) bool {
return lo.SomeBy(patterns, func(p string) bool { return Match(value, p) })
}

func MatchGroups(value string, pattern string) []string {
return regexp.MustCompile(pattern).FindStringSubmatch(value)
}

func Between(str string, start string, end string) (result string) {
s := strings.Index(str, start)
if s == -1 {
Expand Down
38 changes: 28 additions & 10 deletions pkg/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)

Expand All @@ -31,6 +32,16 @@ const (
JCRContentDirName = "_jcr_content"
)

var (
propPatternRegex *regexp.Regexp
namespacePatternRegex *regexp.Regexp
)

func init() {
propPatternRegex = regexp.MustCompile(PropPattern)
namespacePatternRegex = regexp.MustCompile(NamespacePattern)
}

type Manager struct {
baseOpts *base.Opts

Expand Down Expand Up @@ -189,7 +200,7 @@ func (c Manager) cleanNamespaces(lines []string) []string {
if strings.HasPrefix(line, JCRRootPrefix) {
var rootResult []string
for _, part := range strings.Split(line, " ") {
groups := stringsx.MatchGroups(part, NamespacePattern)
groups := namespacePatternRegex.FindStringSubmatch(part)
if groups == nil {
rootResult = append(rootResult, part)
} else {
Expand All @@ -210,8 +221,10 @@ func (c Manager) cleanNamespaces(lines []string) []string {
}

func (c Manager) lineProcess(path string, line string) (bool, string) {
groups := stringsx.MatchGroups(line, PropPattern)
if groups == nil {
groups := propPatternRegex.FindStringSubmatch(line)
if strings.TrimSpace(line) == "" {
return true, ""
} else if groups == nil {
return false, line
} else if groups[1] == JCRMixinTypesProp {
return c.normalizeMixins(path, line, groups[2], groups[3])
Expand Down Expand Up @@ -318,8 +331,11 @@ func deleteEmptyDirs(root string) error {

func (c Manager) doParentsBackup(root string) error {
return eachParentFiles(root, func(parent string) error {
if err := createBackupIndicator(parent); err != nil {
return err
}
return eachFilesInDir(parent, func(path string) error {
if !strings.HasSuffix(path, ParentsBackupSuffix) {
if !strings.HasSuffix(path, ParentsBackupSuffix) && !strings.HasSuffix(path, ParentsBackupDirIndicator) {
log.Infof("doing backup of parent file '%s'", path)
if err := c.backupFile(path); err != nil {
return err
Expand All @@ -332,8 +348,11 @@ func (c Manager) doParentsBackup(root string) error {

func (c Manager) doSiblingsBackup(file string) error {
dir := filepath.Dir(file)
if err := createBackupIndicator(dir); err != nil {
return err
}
return eachFilesInDir(dir, func(path string) error {
if path != file && !strings.HasSuffix(path, ParentsBackupSuffix) {
if path != file && !strings.HasSuffix(path, ParentsBackupSuffix) && !strings.HasSuffix(path, ParentsBackupDirIndicator) {
log.Infof("doing backup of file '%s'", path)
if err := c.backupFile(path); err != nil {
return err
Expand Down Expand Up @@ -441,14 +460,13 @@ func writeLines(path string, lines []string) error {
return err
}

func (c Manager) backupFile(path string) error {
dir := filepath.Dir(path)
func createBackupIndicator(dir string) error {
indicator, err := os.Create(filepath.Join(dir, ParentsBackupDirIndicator))
if err != nil {
return err
}
defer func() { _ = indicator.Close() }()
return err
}

func (c Manager) backupFile(path string) error {
source, err := os.Open(path)
if err != nil {
return err
Expand Down
20 changes: 15 additions & 5 deletions pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ import (
)

const (
NamespacePattern = "_([a-z]+)_"
NamespacePattern = "^_([a-z]+)_"
)

var (
namespacePatternRegex *regexp.Regexp
)

func init() {
namespacePatternRegex = regexp.MustCompile(NamespacePattern)
}

type ContentManager struct {
instance *Instance
}
Expand Down Expand Up @@ -125,17 +133,19 @@ func (cm *ContentManager) PullFile(file string, clean bool, packageOpts PackageC
return err
}
if strings.HasSuffix(file, content.JCRContentFile) {
if err := contentManager.CleanDir(filepath.Join(dir, content.JCRContentDirName)); err != nil {
return err
root := filepath.Join(dir, content.JCRContentDirName)
if pathx.Exists(root) {
if err := contentManager.CleanDir(root); err != nil {
return err
}
}
}
}
return nil
}

func determineCleanFile(file string) string {
re := regexp.MustCompile(NamespacePattern)
if re.MatchString(file) && !strings.HasSuffix(file, content.JCRContentFile) {
if namespacePatternRegex.MatchString(file) && !strings.HasSuffix(file, content.JCRContentFile) {
return filepath.Join(strings.ReplaceAll(file, content.JCRContentFileSuffix, ""), content.JCRContentFile)
}
return file
Expand Down
7 changes: 2 additions & 5 deletions pkg/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
Expand Down Expand Up @@ -238,12 +237,10 @@ func determineFilterRoot(opts PackageCreateOpts) string {
if content.IsContentFile(opts.ContentFile) {
return strings.ReplaceAll(contentFile, content.JCRContentFile, content.JCRContentNode)
} else if strings.HasSuffix(contentFile, content.JCRContentFile) {
re := regexp.MustCompile(NamespacePattern)
contentFile = re.ReplaceAllString(contentFile, "$1:")
contentFile = namespacePatternRegex.ReplaceAllString(contentFile, "$1:")
return filepath.Dir(contentFile)
} else if strings.HasSuffix(contentFile, content.JCRContentFileSuffix) {
re := regexp.MustCompile(NamespacePattern)
contentFile = re.ReplaceAllString(contentFile, "$1:")
contentFile = namespacePatternRegex.ReplaceAllString(contentFile, "$1:")
return strings.ReplaceAll(contentFile, content.JCRContentFileSuffix, "")
}
return contentFile
Expand Down
Loading