-
Notifications
You must be signed in to change notification settings - Fork 241
/
dotnet.go
90 lines (71 loc) · 2.13 KB
/
dotnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package scanner
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func configureDotnet(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
if !checksPass(sourceDir, dirContains("*.csproj", "Microsoft.NET.Sdk.Web")) {
return nil, nil
}
csprojName, csprojPath, err := findCSProjFile(sourceDir)
if err != nil {
return nil, nil
}
dotnetSdkVersion, err := extractDotnetTargetFramework(csprojPath)
if err != nil {
return nil, nil
}
// we don't support .NET Framework or .NET version below 6.0
isDotnetFramework := !strings.Contains(dotnetSdkVersion, ".")
if isDotnetFramework || dotnetSdkVersion < "6.0" {
if isDotnetFramework {
fmt.Println("The .NET Framework is not supported.")
} else {
fmt.Println("The .NET version found is", dotnetSdkVersion)
}
fmt.Println("We only supports projects with .NET version 6.0 or above.")
return nil, nil
}
s := &SourceInfo{
Family: ".NET",
Port: 8080,
}
vars := make(map[string]interface{})
vars["dotnetAppName"] = csprojName
vars["dotnetSdkVersion"] = dotnetSdkVersion
s.Files = templatesExecute("templates/dotnet", vars)
return s, nil
}
func findCSProjFile(dir string) (string, string, error) {
var csprojName, csprojPath string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(path) == ".csproj" {
csprojName = strings.TrimSuffix(info.Name(), ".csproj")
csprojPath = path
return filepath.SkipDir // Stop walking the directory
}
return nil
})
return csprojName, csprojPath, err
}
func extractDotnetTargetFramework(filePath string) (string, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to read file: %v", err)
}
pattern := `<TargetFramework>(.*?)<\/TargetFramework>`
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(string(content))
if len(match) > 1 {
sdkVersion := strings.TrimPrefix(match[1], "net")
sdkVersion = strings.TrimPrefix(sdkVersion, "coreapp") // Handle .NET Core
return sdkVersion, nil
}
return "", fmt.Errorf("failed to extract .NET version")
}