From 8e5592080860fc5963f2ef35317ed479b3b9f3a4 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 21 Aug 2024 21:31:46 +0200 Subject: [PATCH] Support for Xcode project folders --- pkg/filter/filter.go | 8 +++---- .../entity_modifier_internal_test.go | 24 +++++++++++++++++++ pkg/heartbeat/entity_modify.go | 12 ++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/pkg/filter/filter.go b/pkg/filter/filter.go index ab274459..cf6eb8a1 100644 --- a/pkg/filter/filter.go +++ b/pkg/filter/filter.go @@ -63,12 +63,12 @@ func WithLengthValidator() heartbeat.HandleOption { func Filter(h heartbeat.Heartbeat, config Config) error { // filter by pattern if err := filterByPattern(h.Entity, config.Include, config.Exclude); err != nil { - return fmt.Errorf(fmt.Sprintf("filter by pattern: %s", err)) + return fmt.Errorf("filter by pattern: %s", err) } err := filterFileEntity(h, config) if err != nil { - return fmt.Errorf(fmt.Sprintf("filter file: %s", err)) + return fmt.Errorf("filter file: %s", err) } return nil @@ -92,7 +92,7 @@ func filterByPattern(entity string, include, exclude []regex.Regex) error { // filter by exclude pattern for _, pattern := range exclude { if pattern.MatchString(entity) { - return fmt.Errorf(fmt.Sprintf("skipping because matches exclude pattern %q", pattern.String())) + return fmt.Errorf("skipping because matches exclude pattern %q", pattern.String()) } } @@ -123,7 +123,7 @@ func filterFileEntity(h heartbeat.Heartbeat, config Config) error { // skip files that don't exist on disk if _, err := os.Stat(entity); os.IsNotExist(err) { - return fmt.Errorf(fmt.Sprintf("skipping because of non-existing file %q", entity)) + return fmt.Errorf("skipping because of non-existing file %q", entity) } // when including only with project file, skip files when the project doesn't have a .wakatime-project file diff --git a/pkg/heartbeat/entity_modifier_internal_test.go b/pkg/heartbeat/entity_modifier_internal_test.go index 6b31371b..a528d652 100644 --- a/pkg/heartbeat/entity_modifier_internal_test.go +++ b/pkg/heartbeat/entity_modifier_internal_test.go @@ -41,6 +41,30 @@ func TestIsXCodePlayground(t *testing.T) { } } +func TestIsXCodeProject(t *testing.T) { + tests := map[string]struct { + Dir string + Expected bool + }{ + "project directory": { + Dir: setupTestXCodePlayground(t, "wakatime.xcodeproj"), + Expected: true, + }, + "not project": { + Dir: setupTestXCodePlayground(t, "wakatime"), + Expected: false, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + ret := isXCodeProject(test.Dir) + + assert.Equal(t, test.Expected, ret) + }) + } +} + func setupTestXCodePlayground(t *testing.T, dir string) string { tmpDir := t.TempDir() diff --git a/pkg/heartbeat/entity_modify.go b/pkg/heartbeat/entity_modify.go index 7977995f..f25a7d53 100644 --- a/pkg/heartbeat/entity_modify.go +++ b/pkg/heartbeat/entity_modify.go @@ -19,6 +19,10 @@ func WithEntityModifier() HandleOption { if h.EntityType == FileType && isXCodePlayground(h.Entity) { hh[n].Entity = filepath.Join(h.Entity, "Contents.swift") } + // Support XCode projects + if h.EntityType == FileType && isXCodeProject(h.Entity) { + hh[n].Entity = filepath.Join(h.Entity, "project.pbxproj") + } } return next(hh) @@ -35,3 +39,11 @@ func isXCodePlayground(fp string) bool { return isDir(fp) } + +func isXCodeProject(fp string) bool { + if !(strings.HasSuffix(fp, ".xcodeproj")) { + return false + } + + return isDir(fp) +}