Skip to content

Commit c2f2263

Browse files
committed
Implement R library detection
#214
1 parent 8230a53 commit c2f2263

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ test:
44
@go test ./...
55

66
build: test
7-
export CGO_ENABLED=0 export GOOS=linux && go build -a -tags netgo -ldflags '-w -X main.version=v1.1.0' -o repo_info_extractor_linux
8-
export CGO_ENABLED=0 export GOOS=darwin && go build -a -tags netgo -ldflags '-w -X main.version=v1.1.0' -o repo_info_extractor_osx
9-
export CGO_ENABLED=0 export GOOS=windows && go build -a -tags netgo -ldflags '-w -X main.version=v1.1.0' -o repo_info_extractor_windows.exe
10-
export GOOS=$GOOS_OLD
7+
export CGO_ENABLED=0 export GOOS=linux && export GOARCH=amd64 && go build -a -tags netgo -ldflags '-w -X main.version=v2.3.2' -o repo_info_extractor_linux
8+
export CGO_ENABLED=0 export GOOS=darwin && export GOARCH=amd64 && go build -a -tags netgo -ldflags '-w -X main.version=v2.3.2' -o repo_info_extractor_osx
9+
export CGO_ENABLED=0 export GOOS=windows && export GOARCH=amd64 && go build -a -tags netgo -ldflags '-w -X main.version=v2.3.2' -o repo_info_extractor_windows.exe

librarydetection/languages/R.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package languages
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/codersrank-org/repo_info_extractor/v2/librarydetection"
7+
)
8+
9+
// NewRubyScriptAnalyzer constructor
10+
func NewRAnalyzer() librarydetection.Analyzer {
11+
return &rAnalyzer{}
12+
}
13+
14+
type rAnalyzer struct{}
15+
16+
func (a *rAnalyzer) ExtractLibraries(contents string) ([]string, error) {
17+
// According to https://community.rstudio.com/t/analysis-package-naming-or-can-package-name-differ-from-rproj-name/9421
18+
// Package name can contain letters, numbers and coma
19+
20+
// for this format library(shiny)
21+
requireRegex, err := regexp.Compile(`(?m)^\s*(?:require|library|package)\s*\(\s*["]*([\w\d,]+)"*\s*\)`)
22+
if err != nil {
23+
return nil, err
24+
}
25+
// for this format BiocManager123::repositories()
26+
requireRegex2, err := regexp.Compile(`(?m)^\s*([\w\d,]+)::`)
27+
if err != nil {
28+
return nil, err
29+
}
30+
// for this format library(package=quantmod)
31+
requireRegex3, err := regexp.Compile(`(?m)^\s*(?:require|library|package)\s*\(\s*package\s*=\s*([\w\d,]+)\s*\)`)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
return executeRegexes(contents, []*regexp.Regexp{requireRegex, requireRegex2, requireRegex3}), nil
37+
}

librarydetection/languages/R_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package languages_test
22

33
import (
4+
"fmt"
45
"io/ioutil"
56

67
. "github.com/onsi/ginkgo"
@@ -24,14 +25,17 @@ var _ = Describe("RLibraryDetection", func() {
2425
"DT",
2526
"WGCNA",
2627
"impute",
27-
"BiocManager",
28+
"BiocManager123",
2829
}
2930

3031
analyzer := languages.NewRAnalyzer()
3132

3233
Describe("Extract R Libraries", func() {
3334
It("Should be able to extract libraries", func() {
3435
libs, err := analyzer.ExtractLibraries(string(fixture))
36+
for _, l := range libs {
37+
fmt.Println(l)
38+
}
3539
if err != nil {
3640
panic(err)
3741
}

librarydetection/languages/fixtures/r.fixture

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ require(ggplot)
22
library(package=quantmod)
33
require("tibble")
44
package ( tidyr)
5-
library("bslib")
5+
library ( "bslib" )
66
require(package=DT)
77
library( WGCNA)
88
library (impute)
99
library(shiny)
10-
BiocManager::repositories()
10+
# library(example) This is comment
11+
str_replace("library(example)", "example", "something")
12+
BiocManager123::repositories()
13+
str_replace("Something::repositories()", "Something", "something")

0 commit comments

Comments
 (0)