Initial commit.
This commit is contained in:
88
projecter/exported.go
Normal file
88
projecter/exported.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package projecter
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
// local
|
||||
"go.dev.pztrn.name/glp/configuration"
|
||||
"go.dev.pztrn.name/glp/outputters"
|
||||
"go.dev.pztrn.name/glp/structs"
|
||||
)
|
||||
|
||||
var (
|
||||
packages []string
|
||||
|
||||
outputFormat string
|
||||
outputFile string
|
||||
|
||||
projects map[string]*Project
|
||||
projectsMutex sync.RWMutex
|
||||
)
|
||||
|
||||
// Initialize initializes package.
|
||||
func Initialize(pkgs string, outFormat string, outFile string) {
|
||||
log.Println("Initializing projects handler...")
|
||||
|
||||
packages = strings.Split(pkgs, ",")
|
||||
projects = make(map[string]*Project)
|
||||
|
||||
outputFormat = outFormat
|
||||
outputFile = outFile
|
||||
|
||||
log.Println("Packages list that was passed:", packages)
|
||||
}
|
||||
|
||||
// GetProject returns project by it's path.
|
||||
func GetProject(path string) *Project {
|
||||
projectsMutex.RLock()
|
||||
defer projectsMutex.RUnlock()
|
||||
|
||||
prj, found := projects[path]
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
|
||||
return prj
|
||||
}
|
||||
|
||||
// Parse starts projects parsing.
|
||||
func Parse() {
|
||||
// Create project for every passed package.
|
||||
// This is done in main goroutine and therefore no mutex is used.
|
||||
for _, pkgPath := range packages {
|
||||
prj := NewProject(pkgPath)
|
||||
projects[pkgPath] = prj
|
||||
}
|
||||
|
||||
if configuration.Cfg.Log.Debug {
|
||||
log.Printf("Projects generated: %+v\n", projects)
|
||||
}
|
||||
|
||||
// We should start asynchronous projects parsing.
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, prj := range projects {
|
||||
wg.Add(1)
|
||||
go func(prj *Project) {
|
||||
prj.process()
|
||||
wg.Done()
|
||||
}(prj)
|
||||
}
|
||||
|
||||
// Wait until all projects will be parsed.
|
||||
wg.Wait()
|
||||
|
||||
// Collect dependencies list from all parsed projects.
|
||||
var deps []*structs.Dependency
|
||||
|
||||
for _, prj := range projects {
|
||||
deps = append(deps, prj.GetDeps()...)
|
||||
}
|
||||
|
||||
outputters.Write(outputFormat, outputFile, deps)
|
||||
|
||||
log.Println("Parsing done")
|
||||
}
|
80
projecter/project.go
Normal file
80
projecter/project.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package projecter
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"go.dev.pztrn.name/glp/parsers"
|
||||
"go.dev.pztrn.name/glp/structs"
|
||||
)
|
||||
|
||||
// Project represents single project (or package) that was passed via
|
||||
// -pkgs parameter.
|
||||
type Project struct {
|
||||
packagePath string
|
||||
parserName string
|
||||
flavor string
|
||||
|
||||
deps []*structs.Dependency
|
||||
}
|
||||
|
||||
// NewProject creates new project and returns it.
|
||||
func NewProject(packagePath string) *Project {
|
||||
p := &Project{}
|
||||
p.initialize(packagePath)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// GetDeps returns list of dependencies for project.
|
||||
func (p *Project) GetDeps() []*structs.Dependency {
|
||||
return p.deps
|
||||
}
|
||||
|
||||
// Initializes project.
|
||||
func (p *Project) initialize(packagePath string) {
|
||||
p.packagePath = packagePath
|
||||
|
||||
// Prepare package path to be used.
|
||||
// First - replace "~" with actual home directory.
|
||||
if strings.Contains(p.packagePath, "~") {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to get user's home directory:", err.Error())
|
||||
}
|
||||
|
||||
p.packagePath = strings.Replace(p.packagePath, "~", homeDir, -1)
|
||||
}
|
||||
|
||||
// Get absolute path.
|
||||
var err error
|
||||
p.packagePath, err = filepath.Abs(p.packagePath)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to get absolute path for package '"+p.packagePath+":", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Starts project parsing.
|
||||
func (p *Project) process() {
|
||||
// We should determine project type.
|
||||
p.parserName, p.flavor = parsers.Detect(p.packagePath)
|
||||
|
||||
if p.parserName == "unknown" {
|
||||
log.Println("Project", p.packagePath, "cannot be parsed with glp")
|
||||
return
|
||||
}
|
||||
|
||||
// Lets try to get dependencies, their versions and URLs.
|
||||
deps, err := parsers.GetDependencies(p.parserName, p.flavor, p.packagePath)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to get dependencies:", err.Error())
|
||||
}
|
||||
|
||||
p.deps = deps
|
||||
|
||||
// ToDo: get licensing information.
|
||||
}
|
Reference in New Issue
Block a user