Skip to content

Commit

Permalink
fix: ignore target if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado committed Nov 2, 2023
1 parent 1f5d3ea commit 9f8a18a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/segments/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package segments
import (
"encoding/json"
"encoding/xml"
"errors"
"path/filepath"
"strings"

Expand Down Expand Up @@ -208,13 +207,14 @@ func (n *Project) getNuSpecPackage(_ ProjectItem) *ProjectData {
func (n *Project) getDotnetProject(_ ProjectItem) *ProjectData {
var name string
var content string
var extension string

extensions := []string{".sln", ".slnf", ".csproj", ".fsproj", ".vbproj"}
files := n.env.LsDir(n.env.Pwd())

// get the first match only
for _, file := range files {
extension := filepath.Ext(file.Name())
extension = filepath.Ext(file.Name())
if slices.Contains(extensions, extension) {
name = strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
content = n.env.FileContent(file.Name())
Expand All @@ -226,15 +226,17 @@ func (n *Project) getDotnetProject(_ ProjectItem) *ProjectData {
// so instead of xml.Unmarshal() we use regex:
tag := "(?P<TAG><.*TargetFramework.*>(?P<TFM>.*)</.*TargetFramework.*>)"
values := regex.FindNamedRegexMatch(tag, content)
target := ""
if len(values) == 0 {
n.Error = errors.New("cannot extract TFM from " + name + " project file").Error()
return nil
} else {
target = values["TFM"]
}
target := values["TFM"]

return &ProjectData{
Target: target,
Name: name,
Type: extension,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/segments/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func TestDotnetProject(t *testing.T) {
Case: "invalid or empty contents",
FileName: "Invalid.csproj",
HasFiles: true,
ExpectedEnabled: false,
ExpectedString: "cannot extract TFM from Invalid project file",
ExpectedEnabled: true,
ExpectedString: "Invalid",
},
{
Case: "no files",
Expand Down

0 comments on commit 9f8a18a

Please sign in to comment.