Skip to content

Commit

Permalink
feat: add cargo detection for rust
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Feb 5, 2024
1 parent bdf6fd7 commit 3f78c07
Show file tree
Hide file tree
Showing 20 changed files with 1,537 additions and 36 deletions.
6 changes: 6 additions & 0 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"strings"
"time"

"github.com/cidverse/repoanalyzer/analyzer/cargo"
"github.com/cidverse/repoanalyzer/analyzer/composer"
"github.com/cidverse/repoanalyzer/analyzer/container"
"github.com/cidverse/repoanalyzer/analyzer/dotnet"
"github.com/cidverse/repoanalyzer/analyzer/gomod"
"github.com/cidverse/repoanalyzer/analyzer/gradle"
"github.com/cidverse/repoanalyzer/analyzer/helm"
Expand Down Expand Up @@ -67,5 +70,8 @@ func initAnalyzers() {
node.Analyzer{},
python.Analyzer{},
mkdocs.Analyzer{},
dotnet.Analyzer{},
composer.Analyzer{},
cargo.Analyzer{},
)
}
56 changes: 56 additions & 0 deletions analyzer/cargo/cargo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cargo

import (
"path/filepath"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/gosimple/slug"
"github.com/rs/zerolog/log"
)

type Analyzer struct{}

func (a Analyzer) GetName() string {
return "cargo"
}

func (a Analyzer) Analyze(ctx analyzerapi.AnalyzerContext) []*analyzerapi.ProjectModule {
var result []*analyzerapi.ProjectModule

for _, file := range ctx.FilesByExtension["toml"] {
filename := filepath.Base(file)
if filename == "Cargo.toml" {
// parse Cargo.toml
cargoFile, err := parseCargoFile(file)
if err != nil {
log.Debug().Err(err).Msg("failed to parse Cargo.toml")
}

// module
module := analyzerapi.ProjectModule{
RootDirectory: ctx.ProjectDir,
Directory: filepath.Dir(file),
Name: filepath.Base(filepath.Dir(file)),
Slug: slug.Make(filepath.Base(filepath.Dir(file))),
Discovery: []analyzerapi.ProjectModuleDiscovery{{File: file}},
BuildSystem: analyzerapi.BuildSystemCargo,
BuildSystemSyntax: analyzerapi.BuildSystemSyntaxDefault,
Language: analyzerapi.GetSingleLanguageMap(analyzerapi.LanguageRust, getRustVersionFromCargoFile(cargoFile)),
Dependencies: nil,
Submodules: nil,
Files: ctx.Files,
FilesByExtension: ctx.FilesByExtension,
}
analyzerapi.AddModuleToResult(&result, &module)
}
}

return result
}

func getRustVersionFromCargoFile(cargoFile Config) string {
if cargoFile.Package.RustVersion != "" {
return cargoFile.Package.RustVersion + ".0"
}
return "0.0.0"
}
27 changes: 27 additions & 0 deletions analyzer/cargo/cargo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cargo

import (
"testing"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/cidverse/repoanalyzer/util"
"github.com/stretchr/testify/assert"
)

func TestAnalyzer_AnalyzeCargo(t *testing.T) {
ctx := analyzerapi.GetAnalyzerContext(util.GetTestDataDir(t, "cargo"))

analyzer := Analyzer{}
result := analyzer.Analyze(ctx)

// module
assert.Len(t, result, 1)
assert.Equal(t, "cargo", result[0].Name)
assert.Equal(t, "cargo", string(result[0].BuildSystem))
assert.Equal(t, "default", string(result[0].BuildSystemSyntax))

// print result
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
36 changes: 36 additions & 0 deletions analyzer/cargo/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cargo

import (
"fmt"
"os"

"github.com/pelletier/go-toml/v2"
)

type Config struct {
Package Package `toml:"package"`
}

type Package struct {
Name string `toml:"name"`
RustVersion string `toml:"rust-version"`
}

func parseCargoFile(file string) (Config, error) {
content, err := os.ReadFile(file)
if err != nil {
return Config{}, fmt.Errorf("failed to open Cargo.toml: %w", err)
}

return parseCargoFileFromByteArray(content)
}

func parseCargoFileFromByteArray(content []byte) (Config, error) {
var cfg Config
err := toml.Unmarshal(content, &cfg)
if err != nil {
return Config{}, fmt.Errorf("failed to parse Cargo.toml: %w", err)
}

return cfg, nil
}
47 changes: 47 additions & 0 deletions analyzer/cargo/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cargo

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseCargoFile(t *testing.T) {
testCases := []struct {
name string
input string
expected Config
}{
{
name: "Valid Cargo file",
input: `
[package]
name = "my-project"
version = "0.1.0"
authors = ["Firstname Lastname <[email protected]>"]
edition = "2021"
license = "MIT"
description = """
Multiline
Description
"""
rust-version = "1.56"
`,
expected: Config{
Package: Package{
Name: "my-project",
RustVersion: "1.56",
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cfg, err := parseCargoFileFromByteArray([]byte(tc.input))

assert.Nil(t, err)
assert.Equal(t, tc.expected, cfg)
})
}
}
8 changes: 4 additions & 4 deletions analyzer/composer/composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package composer
import (
"testing"

"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/cidverse/repoanalyzer/util"
"github.com/stretchr/testify/assert"
)

Expand All @@ -24,5 +22,7 @@ func TestAnalyzer_AnalyzeComposer(t *testing.T) {
assert.Equal(t, "8.0.0", result[0].Language[analyzerapi.LanguagePHP])

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
8 changes: 4 additions & 4 deletions analyzer/dotnet/dotnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package dotnet
import (
"testing"

"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/cidverse/repoanalyzer/util"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,5 +21,7 @@ func TestAnalyzer_AnalyzeVisualStudioSolution(t *testing.T) {
assert.Equal(t, "default", string(result[0].BuildSystemSyntax))

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
6 changes: 3 additions & 3 deletions analyzer/gomod/gomod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"path/filepath"
"testing"

"github.com/rs/zerolog/log"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -34,5 +32,7 @@ func TestGoModAnalyzer_Analyze(t *testing.T) {
assert.Len(t, result[0].Submodules, 0)

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
12 changes: 7 additions & 5 deletions analyzer/gradle/gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"os"
"testing"

"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/cidverse/repoanalyzer/util"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -35,7 +33,9 @@ func TestGradleAnalyzer_AnalyzeGroovy(t *testing.T) {
assert.Equal(t, string(analyzerapi.GradleGroovyDSL), string(result[0].Submodules[0].BuildSystemSyntax))

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}

func TestGradleAnalyzer_AnalyzeKotlin(t *testing.T) {
Expand All @@ -59,5 +59,7 @@ func TestGradleAnalyzer_AnalyzeKotlin(t *testing.T) {
assert.Equal(t, string(analyzerapi.GradleKotlinDSL), string(result[0].Submodules[0].BuildSystemSyntax))

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
8 changes: 5 additions & 3 deletions analyzer/helm/helm_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package helm

import (
"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"
"testing"

"github.com/cidverse/repoanalyzer/util"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/stretchr/testify/assert"
)
Expand All @@ -22,5 +22,7 @@ func TestAnalyzer_AnalyzeHugo(t *testing.T) {
assert.Nil(t, result[0].Language)

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
8 changes: 5 additions & 3 deletions analyzer/hugo/hugo_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package hugo

import (
"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"
"testing"

"github.com/cidverse/repoanalyzer/util"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/stretchr/testify/assert"
)
Expand All @@ -20,5 +20,7 @@ func TestAnalyzer_AnalyzeHugo(t *testing.T) {
assert.Equal(t, "hugo", result[0].Name)

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
5 changes: 3 additions & 2 deletions analyzer/maven/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,5 +26,7 @@ func TestAnalyzer_AnalyzeMaven(t *testing.T) {
assert.Equal(t, "4.12", result[0].Dependencies[0].Version)

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
11 changes: 7 additions & 4 deletions analyzer/mkdocs/mkdocs_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package mkdocs

import (
"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"
"testing"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/cidverse/repoanalyzer/util"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,7 +21,9 @@ func TestAnalyzer_AnalyzeMkdocs(t *testing.T) {
assert.Equal(t, "default", string(result[0].BuildSystemSyntax))

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}

func TestAnalyzer_AnalyzeTechdocs(t *testing.T) {
Expand All @@ -38,5 +39,7 @@ func TestAnalyzer_AnalyzeTechdocs(t *testing.T) {
assert.Equal(t, "mkdocs-techdocs", string(result[0].BuildSystemSyntax))

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
8 changes: 5 additions & 3 deletions analyzer/node/node_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package node

import (
"github.com/cidverse/repoanalyzer/util"
"github.com/rs/zerolog/log"
"testing"

"github.com/cidverse/repoanalyzer/util"

"github.com/cidverse/repoanalyzer/analyzerapi"
)

Expand All @@ -14,5 +14,7 @@ func TestAnalyzer_AnalyzeReact(t *testing.T) {
result := analyzer.Analyze(ctx)

// print result
log.Info().Interface("result", result).Msg("output")
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
Loading

0 comments on commit 3f78c07

Please sign in to comment.