Skip to content

Commit

Permalink
Better spec root detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hasty committed Sep 3, 2024
1 parent 550a31f commit bfc67c6
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions matter/spec/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,43 @@ func NewPath(path string) (Path, error) {
} else {
p.Absolute = path
}
dir := filepath.Dir(p.Absolute)
parts := strings.Split(dir, string(filepath.Separator))

rootIndex := -1
for i := len(parts) - 1; i >= 0; i-- {
part := parts[i]
if strings.EqualFold(part, "connectedhomeip-spec") {
rootIndex = i
break
}
}
if rootIndex == -1 {
for i := len(parts) - 1; i >= 0; i-- {
alchemyYmlPath := filepath.Join(strings.Join(append(parts[i+1:], ".github", "workflows"), string(filepath.Separator)), "alchemy.yml")
alchemyYmlPathExists, err := files.Exists(alchemyYmlPath)
if err == nil && alchemyYmlPathExists {
rootIndex = i
break
}
}
}
if rootIndex >= 0 {
p.Relative = filepath.Join(strings.Join(parts[rootIndex+1:], string(filepath.Separator)), filepath.Base(path))
} else {
specPath := DeriveSpecPath(p.Absolute)
if specPath == "" {
return p, fmt.Errorf("unable to determine spec root for path %s", p.Absolute)
}

return p, nil
var err error
p.Relative, err = filepath.Rel(specPath, p.Absolute)
return p, err
}

func DeriveSpecPath(path string) string {
slog.Info("deriving spec path", slog.String("path", path))
if !filepath.IsAbs(path) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return ""
}
}
slog.Info("deriving spec path", slog.String("abs path", path))
dir := filepath.Dir(path)
for {
alchemyConfigExists, err := files.Exists(filepath.Join(dir, ".github/workflows/alchemy.yml"))
if err != nil {
slog.Warn("error checking for alchemy config file", slog.Any("error", err))
break
}
if alchemyConfigExists {
return dir
}
lastSeparator := strings.LastIndex(dir, string(filepath.Separator))
if lastSeparator == -1 {
break
}
dir = dir[:lastSeparator]
}

// Fallback if we can't find the alchemy.yml file
parts := strings.Split(dir, string(filepath.Separator))
for i, part := range parts {
slog.Info("spec path part", slog.String("part", part))
if strings.EqualFold(part, "connectedhomeip-spec") {
return strings.Join(parts[0:i+1], string(filepath.Separator))
}
Expand All @@ -100,7 +93,6 @@ func DeriveSpecPathFromPaths(paths []string) string {
for _, path := range paths {
specPath := DeriveSpecPath(path)
if specPath != "" {
slog.Info("derived spec path", slog.String("path", path))
return specPath
}
}
Expand Down

0 comments on commit bfc67c6

Please sign in to comment.