github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/analyzers/ant/ant.go (about) 1 package ant 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/apex/log" 8 "github.com/mitchellh/mapstructure" 9 10 "github.com/fossas/fossa-cli/buildtools/ant" 11 "github.com/fossas/fossa-cli/errors" 12 "github.com/fossas/fossa-cli/files" 13 "github.com/fossas/fossa-cli/graph" 14 "github.com/fossas/fossa-cli/module" 15 "github.com/fossas/fossa-cli/pkg" 16 ) 17 18 // Analyzer implements build context for Ant builds 19 type Analyzer struct { 20 Module module.Module 21 Options Options 22 } 23 24 type Options struct { 25 LibDirectory string `mapstructure:"lib-directory"` 26 } 27 28 // Initialize collects metadata on Java and Ant binaries. 29 func New(m module.Module) (*Analyzer, error) { 30 log.Debugf("Initializing Ant builder...") 31 32 // Decode options. 33 var options Options 34 err := mapstructure.Decode(m.Options, &options) 35 if err != nil { 36 return nil, err 37 } 38 39 return &Analyzer{ 40 Module: m, 41 Options: options, 42 }, nil 43 } 44 45 // Clean is currently not implemented. 46 func (a *Analyzer) Clean() error { 47 return errors.New("Clean is not implemented for Ant") 48 } 49 50 // Build is currently not implemented. 51 func (a *Analyzer) Build() error { 52 return errors.New("Build is not implemented for Ant") 53 } 54 55 // Analyze resolves a lib directory and parses the jars inside. 56 func (a *Analyzer) Analyze() (graph.Deps, error) { 57 log.Debugf("Running Ant analysis: %#v in %s", a.Module, a.Module.Dir) 58 59 libdir := "lib" 60 if a.Options.LibDirectory != "" { 61 libdir = a.Options.LibDirectory 62 } 63 64 log.Debugf("resolving ant libs in: %s", libdir) 65 if ok, err := files.ExistsFolder(a.Module.Dir, libdir); !ok || err != nil { 66 return graph.Deps{}, errors.New("unable to resolve library directory, try specifying it using the `modules.options.libdir` property in `.fossa.yml`") 67 } 68 69 return ant.Graph(libdir) 70 } 71 72 // IsBuilt always returns true for Ant builds. 73 func (a *Analyzer) IsBuilt() (bool, error) { 74 return true, nil 75 } 76 77 // Discover returns any directory that contains a "build.xml" file and a "lib" directory. 78 func Discover(dir string, options map[string]interface{}) ([]module.Module, error) { 79 modules := []module.Module{} 80 81 err := filepath.Walk(dir, func(filename string, info os.FileInfo, err error) error { 82 if err != nil { 83 log.WithError(err).WithField("filename", filename).Debug("failed to access path") 84 return err 85 } 86 87 if !info.IsDir() && (info.Name() == "build.xml") { 88 moduleDir := filepath.Dir(filename) 89 libCheck, err := files.ExistsFolder(moduleDir, "lib") 90 if err != nil { 91 return errors.Wrap(err, "Error discovering ant modules") 92 } 93 94 if libCheck { 95 moduleName := filepath.Base(moduleDir) 96 log.WithFields(log.Fields{ 97 "path": filename, 98 "name": moduleName, 99 }).Debug("constructing Ant module") 100 relPath, err := filepath.Rel(dir, filename) 101 if err != nil { 102 return errors.Wrap(err, "Error discovering ant modules") 103 } 104 105 modules = append(modules, module.Module{ 106 Name: moduleName, 107 Type: pkg.Ant, 108 BuildTarget: filepath.Dir(relPath), 109 Dir: filepath.Dir(relPath), 110 }) 111 } 112 } 113 return nil 114 }) 115 116 if err != nil { 117 return nil, errors.Wrap(err, "Could not find Ant package manifests: %s") 118 } 119 120 return modules, nil 121 }