Skip to content

Commit

Permalink
Removed dependence on gocmd (#19)
Browse files Browse the repository at this point in the history
* Removed gocmd dependency and moved its code to build-info-go and jfrog-cli-core.
  • Loading branch information
asafgabai authored Dec 9, 2021
1 parent 8886023 commit c5f4d2e
Show file tree
Hide file tree
Showing 16 changed files with 984 additions and 191 deletions.
127 changes: 92 additions & 35 deletions build/golang.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package build

import (
"errors"
"fmt"
"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/gocmd/cmd"
"github.com/jfrog/gocmd/executers"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/build-info-go/utils"
"path/filepath"
"strings"
"unicode"
)

type GoModule struct {
containingBuild *Build
dependencies []executers.Package
name string
srcPath string
}

func newGoModule(srcPath string, containingBuild *Build) (*GoModule, error) {
log.SetLogger(containingBuild.logger)

var err error
if srcPath == "" {
srcPath, err = cmd.GetProjectRoot()
srcPath, err = utils.GetProjectRoot()
if err != nil {
return nil, err
}
}

// Read module name
name, err := cmd.GetModuleNameByDir(srcPath)
name, err := utils.GetModuleNameByDir(srcPath, containingBuild.logger)
if err != nil {
return nil, err
}
Expand All @@ -35,21 +35,11 @@ func newGoModule(srcPath string, containingBuild *Build) (*GoModule, error) {
}

func (gm *GoModule) CalcDependencies() error {
var err error
err = gm.loadDependencies()
if err != nil {
return err
}
err = gm.createBuildInfoDependencies()
buildInfoDependencies, err := gm.loadDependencies()
if err != nil {
return err
}

var buildInfoDependencies []entities.Dependency
for _, dep := range gm.dependencies {
buildInfoDependencies = append(buildInfoDependencies, dep.Dependencies()...)
}

buildInfoModule := entities.Module{Id: gm.name, Type: entities.Go, Dependencies: buildInfoDependencies}
buildInfo := &entities.BuildInfo{Modules: []entities.Module{buildInfoModule}}

Expand All @@ -65,30 +55,97 @@ func (gm *GoModule) AddArtifacts(artifacts ...entities.Artifact) error {
return gm.containingBuild.SavePartialBuildInfo(partial)
}

// Get the go project dependencies.
func (gm *GoModule) createBuildInfoDependencies() error {
for i, dep := range gm.dependencies {
err := dep.PopulateZip()
func (gm *GoModule) loadDependencies() ([]entities.Dependency, error) {
cachePath, err := utils.GetCachePath()
if err != nil {
return nil, err
}
modulesMap, err := utils.GetDependenciesList(gm.srcPath, gm.containingBuild.logger)
if err != nil || len(modulesMap) == 0 {
return nil, err
}
return gm.getGoDependencies(cachePath, modulesMap)
}

func (gm *GoModule) getGoDependencies(cachePath string, moduleSlice map[string]bool) ([]entities.Dependency, error) {
var buildInfoDependencies []entities.Dependency
for module := range moduleSlice {
moduleInfo := strings.Split(module, "@")
name := goModEncode(moduleInfo[0])
version := goModEncode(moduleInfo[1])
packageId := strings.Join([]string{name, version}, ":")

// We first check if this dependency has a zip in the local Go cache.
// If it does not, nil is returned. This seems to be a bug in Go.
zipPath, err := gm.getPackageZipLocation(cachePath, name, version)
if err != nil {
return nil, err
}
if zipPath == "" {
continue
}
zipDependency, err := populateZip(packageId, zipPath)
if err != nil {
return err
return nil, err
}
buildInfoDependencies = append(buildInfoDependencies, *zipDependency)
}
return buildInfoDependencies, nil
}

// Returns the actual path to the dependency.
// If the path includes capital letters, the Go convention is to use "!" before the letter.
// The letter itself is in lowercase.
func goModEncode(name string) string {
path := ""
for _, letter := range name {
if unicode.IsUpper(letter) {
path += "!" + strings.ToLower(string(letter))
} else {
path += string(letter)
}
gm.dependencies[i] = dep
}
return nil
return path
}

func (gm *GoModule) loadDependencies() error {
cachePath, err := cmd.GetCachePath()
// Returns the path to the package zip file if exists.
func (gm *GoModule) getPackageZipLocation(cachePath, dependencyName, version string) (string, error) {
zipPath, err := gm.getPackagePathIfExists(cachePath, dependencyName, version)
if err != nil {
return err
return "", err
}

if zipPath != "" {
return zipPath, nil
}
modulesMap, err := cmd.GetDependenciesList(gm.srcPath)

return gm.getPackagePathIfExists(filepath.Dir(cachePath), dependencyName, version)
}

// Validates that the package zip file exists and returns its path.
func (gm *GoModule) getPackagePathIfExists(cachePath, dependencyName, version string) (zipPath string, err error) {
zipPath = filepath.Join(cachePath, dependencyName, "@v", version+".zip")
fileExists, err := utils.IsFileExists(zipPath)
if err != nil {
return err
return "", errors.New(fmt.Sprintf("Could not find zip binary for dependency '%s' at %s: %s", dependencyName, zipPath, err))
}
// Zip binary does not exist, so we skip it by returning a nil dependency.
if !fileExists {
gm.containingBuild.logger.Debug("The following file is missing:", zipPath)
return "", nil
}
if modulesMap == nil {
return nil
return zipPath, nil
}

// populateZip adds the zip file as build-info dependency
func populateZip(packageId, zipPath string) (zipDependency *entities.Dependency, err error) {
// Zip file dependency for the build-info
zipDependency = &entities.Dependency{Id: packageId}
checksums, err := utils.GetFileChecksums(zipPath)
if err != nil {
return nil, err
}
gm.dependencies, err = executers.GetDependencies(cachePath, modulesMap)
return err
zipDependency.Type = "zip"
zipDependency.Checksum = &entities.Checksum{Sha1: checksums.Sha1, Md5: checksums.Md5}
return
}
6 changes: 3 additions & 3 deletions build/gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"testing"

"github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/stretchr/testify/assert"
)

func TestDownloadExtractorsFromReleases(t *testing.T) {
tempDirPath, err := fileutils.CreateTempDir()
tempDirPath, err := utils.CreateTempDir()
assert.NoError(t, err)
defer func() {
assert.NoError(t, fileutils.RemoveTempDir(tempDirPath))
assert.NoError(t, utils.RemoveTempDir(tempDirPath))
assert.NoError(t, utils.CleanOldDirs())
}()

// Download JAR
Expand Down
6 changes: 3 additions & 3 deletions build/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"testing"

"github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/stretchr/testify/assert"
)

func TestDownloadDependencies(t *testing.T) {
tempDirPath, err := fileutils.CreateTempDir()
tempDirPath, err := utils.CreateTempDir()
assert.NoError(t, err)
defer func() {
assert.NoError(t, fileutils.RemoveTempDir(tempDirPath))
assert.NoError(t, utils.RemoveTempDir(tempDirPath))
assert.NoError(t, utils.CleanOldDirs())
}()

// Download JAR and create classworlds.conf
Expand Down
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module github.com/jfrog/build-info-go
go 1.15

require (
github.com/jfrog/gocmd v0.6.0
github.com/jfrog/gofrog v1.1.0
github.com/jfrog/jfrog-client-go v1.6.1
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
)
Expand All @@ -15,8 +13,4 @@ exclude (
golang.org/x/text v0.3.5
)

// replace github.com/jfrog/gocmd => github.com/jfrog/gocmd v0.5.6-0.20211125120614-66713cecff51

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.6.4-0.20211130080339-f26b4e80e776

//replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.0.7-0.20211107071406-54da7fb08599
replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.0.7-0.20211128152632-e218c460d703
Loading

0 comments on commit c5f4d2e

Please sign in to comment.