-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdf6fd7
commit 3f78c07
Showing
20 changed files
with
1,537 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.