Skip to content

Commit

Permalink
error for non ruby gems projects
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisojile committed Aug 4, 2021
1 parent f3b3fdc commit c981ed3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 48 deletions.
38 changes: 21 additions & 17 deletions pkg/modules/gem/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ type gem struct {
command *helper.Cmd
}

var errDependenciesNotFound = errors.New(
`* Please install dependencies by running the following command :
1) bundle config set --local path 'vendor/bundle' && bundle install && bundle exec rake install
2) run the spdx-sbom-generator tool command
`)
var errDependenciesNotFound, errInvalidProjectType = errors.New(
`* Please install dependencies by running the following command :
1) bundle config set --local path 'vendor/bundle' && bundle install && bundle exec rake install
2) run the spdx-sbom-generator tool command
`), errors.New(
`* Tool only supports ruby gems projects with valid .gemspec manifest in project root directory`)

// New ...
func New() *gem {
Expand Down Expand Up @@ -55,18 +56,21 @@ func (g *gem) IsValid(path string) bool {

// HasModulesInstalled ...
func (g *gem) HasModulesInstalled(path string) error {
hasRake := hasRakefile(path)
_ = ensurePlatform(path)
hasModule := false
for i := range g.metadata.ModulePath {
if helper.Exists(filepath.Join(path, g.metadata.ModulePath[i])) {
hasModule = true
}
}
if hasRake && hasModule {
return nil
}
return errDependenciesNotFound

if !validateProjectType(path) {
return errInvalidProjectType
}

hasRake, _, hasModule := hasRakefile(path), ensurePlatform(path), false
for i := range g.metadata.ModulePath {
if helper.Exists(filepath.Join(path, g.metadata.ModulePath[i])) {
hasModule = true
}
}
if hasRake && hasModule {
return nil
}
return errDependenciesNotFound
}

// GetVersion ...
Expand Down
69 changes: 38 additions & 31 deletions pkg/modules/gem/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,12 @@ func extractLicense(path string, filename string, isFullPath bool) (string, stri
return copyright, text, licensePath, nil

}

func validateProjectType(path string) bool {
if _, err := detectManifest(path, DETECTION_MODE_SPEC); err != nil {
return false
}
return true
}
// Constructs dependency tree recursively
func buildTree(linesToRead []Line) {

Expand Down Expand Up @@ -879,36 +884,38 @@ func childDepInfo(value string) (string, string, string) {
// Scans the provided path for ecosystem manifest file
func detectManifest(path, mode string) (string, error) {

var manifest string
var err error
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, f := range files {

switch mode {
case DETECTION_MODE_LOCK:
if filepath.Ext(f.Name()) == LOCK_EXTENSION || filepath.Ext(f.Name()) == LEGACY_LOCK_EXTENSION {
manifest = f.Name()
err = errors.New("No file with extension '.lock' was detected in " + path)
}
case DETECTION_MODE_SPEC:
if filepath.Ext(f.Name()) == SPEC_EXTENSION {
manifest = f.Name()
err = errors.New("No file with extension '.gemspec' was detected in " + path)
}
}

if manifest != "" {
break
}

}
if manifest == "" {
return manifest, err
}
return manifest, nil
var manifest string
var err error
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, f := range files {

switch mode {
case DETECTION_MODE_LOCK:
if filepath.Ext(f.Name()) == LOCK_EXTENSION || filepath.Ext(f.Name()) == LEGACY_LOCK_EXTENSION {
manifest = f.Name()
err = errors.New("No file with extension '.lock' was detected in " + path)
}
case DETECTION_MODE_SPEC:
if filepath.Ext(f.Name()) == SPEC_EXTENSION {
manifest = f.Name()
err = errors.New("No file with extension '.gemspec' was detected in " + path)
}
}

if manifest != "" {
err = nil
break
}

}
if manifest == "" {
err = errors.New("gemspec file not found in projects root directory")
return manifest, err
}
return manifest, err
}

// Detect whether current OS is added in the Gemfile.lock PLATFORMS section
Expand Down

0 comments on commit c981ed3

Please sign in to comment.