Skip to content

Commit

Permalink
[CI-12672]: added support for dotnet caching (#115)
Browse files Browse the repository at this point in the history
* [CI-12672]: added support for dotnet caching

* [CI-12672]: fixed PR comments (used defer for f.close, err check for WriteFile)
  • Loading branch information
avijeetsm authored Jul 25, 2024
1 parent c02cf0d commit b36d557
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/plugin/autodetect/auto_detect_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func DetectDirectoriesToCache(skipPrepare bool) ([]string, []string, string, err
tool: "golang",
preparer: newGoPreparer(),
},
{
globToDetect: "*.csproj",
tool: "dotnet",
preparer: newDotnetPreparer(),
},
}

var directoriesToCache []string
Expand Down
118 changes: 118 additions & 0 deletions internal/plugin/autodetect/prepare_dotnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package autodetect

import (
"encoding/xml"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)

type Configuration struct {
XMLName xml.Name `xml:"configuration"`
Config *Config `xml:"config"`
}

type Config struct {
Add []Add `xml:"add"`
}

type Add struct {
Key string `xml:"key,attr"`
Value string `xml:"value,attr"`
}

type dotnetPreparer struct{}

func newDotnetPreparer() *dotnetPreparer {
return &dotnetPreparer{}
}

func (*dotnetPreparer) PrepareRepo(dir string) (string, error) {
pathToCache := filepath.Join(dir, ".nuget", "packages")
configPath := filepath.Join(dir, "nuget.config")
updateConfig := false
foundGlobalPackagesFolder := false

//file not exists
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
f, err := os.Create(configPath)
if err != nil {
return "", err
}
defer f.Close()

configuration := Configuration{}
marshalledConfig, err := xml.MarshalIndent(configuration, "", " ")
if err != nil {
return "", err
}

err = os.WriteFile(configPath, marshalledConfig, 0644)
if err != nil {
return "", fmt.Errorf("failed to write nuget.config: %w", err)
}
}

//file exists
file, err := os.Open(configPath)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

// Read the file contents
data, err := io.ReadAll(file)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}

// Parse the XML
var configuration Configuration
err = xml.Unmarshal(data, &configuration)
if err != nil {
return "", fmt.Errorf("failed to parse XML: %w", err)
}

if configuration.Config == nil {
configuration.Config = &Config{}
updateConfig = true
}

for _, add := range configuration.Config.Add {
if add.Key == "globalPackagesFolder" {
if add.Value != pathToCache && add.Value != filepath.Join(".nuget", "packages") {
return "", fmt.Errorf("unsupported custom globalPackagesFolder value")
}
foundGlobalPackagesFolder = true
}
}

if !foundGlobalPackagesFolder {
// Add the globalPackagesFolder value
configuration.Config.Add = append(configuration.Config.Add, Add{
Key: "globalPackagesFolder",
Value: pathToCache,
})
updateConfig = true
}

if updateConfig {
writeConfigurationToFile(configPath, configuration)
}

return pathToCache, nil
}

func writeConfigurationToFile(filePath string, config Configuration) error {
updatedData, err := xml.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal updated nuget.config: %w", err)
}
err = os.WriteFile(filePath, updatedData, 0644)
if err != nil {
return fmt.Errorf("failed to write updated nuget.config: %w", err)
}
return nil
}

0 comments on commit b36d557

Please sign in to comment.