Parcourir la source

Add extensive comments to api_nodejs.go

Mohammed Nafees il y a 4 ans
Parent
commit
185873d742

+ 1 - 0
go.mod

@@ -40,6 +40,7 @@ require (
 	github.com/moby/term v0.0.0-20201216013528-df9cb8a40635
 	github.com/opencontainers/image-spec v1.0.1
 	github.com/paketo-buildpacks/conda-env-update v0.2.2
+	github.com/paketo-buildpacks/dep-ensure v0.1.1
 	github.com/paketo-buildpacks/go-mod-vendor v0.3.1
 	github.com/paketo-buildpacks/node-run-script v0.2.0
 	github.com/paketo-buildpacks/node-start v0.5.0

+ 2 - 0
go.sum

@@ -1054,6 +1054,8 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
 github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
 github.com/paketo-buildpacks/conda-env-update v0.2.2 h1:0bT6Se/r/06Gc3sBeg+OJ/9BVIp8D/rEzeJNtvxOBSQ=
 github.com/paketo-buildpacks/conda-env-update v0.2.2/go.mod h1:OKSG9rf9ClDLcoHA3JJgbRECShCpNJKrmkYgIiYAu9k=
+github.com/paketo-buildpacks/dep-ensure v0.1.1 h1:/vPTihH/2Z7paIHzwrWkMeuOGtXxyv8zrhT64YMFBn0=
+github.com/paketo-buildpacks/dep-ensure v0.1.1/go.mod h1:r0bjiaauIgEdXKU+FDgvseqTAjWXYuOGXi2omjRJkYc=
 github.com/paketo-buildpacks/go-mod-vendor v0.3.1 h1:4ltB7mmMg2dGRNDLxwPMLoY98rePUhny/6L0QUn8aYU=
 github.com/paketo-buildpacks/go-mod-vendor v0.3.1/go.mod h1:ycd4yAggQShyoQg+bXZyaxazx/Nms98f1SH9NkG/I3k=
 github.com/paketo-buildpacks/node-run-script v0.2.0 h1:IAmnpavIuBM49RaKDIK/OdRbo12TfdUCzJ88CvYe4Kk=

+ 27 - 0
internal/integrations/buildpacks/api_go.go

@@ -0,0 +1,27 @@
+package buildpacks
+
+import (
+	"sync"
+
+	"github.com/google/go-github/github"
+)
+
+type apiGoRuntime struct {
+	ghClient *github.Client
+	wg       sync.WaitGroup
+}
+
+func NewAPIGoRuntime(client *github.Client) *apiGoRuntime {
+	return &apiGoRuntime{
+		ghClient: client,
+	}
+}
+
+func (runtime *apiGoRuntime) Detect(
+	directoryContent []*github.RepositoryContent,
+	owner string, name string,
+	repoContentOptions github.RepositoryContentGetOptions,
+) map[string]interface{} {
+
+	return nil
+}

+ 25 - 6
internal/integrations/buildpacks/api_nodejs.go

@@ -3,6 +3,7 @@ package buildpacks
 import (
 	"context"
 	"encoding/json"
+	"fmt"
 	"strings"
 	"sync"
 
@@ -113,9 +114,13 @@ func (runtime *apiNodeRuntime) Detect(
 		bool
 	}, 3)
 
+	fmt.Printf("Starting detection for a NodeJS runtime for %s/%s\n", owner, name)
 	runtime.wg.Add(3)
+	fmt.Println("Checking for yarn")
 	go runtime.detectYarn(results, directoryContent)
+	fmt.Println("Checking for NPM")
 	go runtime.detectNPM(results, directoryContent)
+	fmt.Println("Checking for NodeJS standalone")
 	go runtime.detectStandalone(results, directoryContent)
 	runtime.wg.Wait()
 	close(results)
@@ -132,6 +137,7 @@ func (runtime *apiNodeRuntime) Detect(
 	if atLeastOne {
 		if detected[yarn] || detected[npm] {
 			// it is safe to assume that the project contains a package.json
+			fmt.Println("package.json file detected")
 			fileContent, _, _, err := runtime.ghClient.Repositories.GetContents(
 				context.Background(),
 				owner,
@@ -140,7 +146,7 @@ func (runtime *apiNodeRuntime) Detect(
 				&repoContentOptions,
 			)
 			if err != nil {
-				// FIXME: log somewhere
+				fmt.Printf("Error fetching contents of package.json: %v\n", err)
 				return nil
 			}
 			var packageJSON struct {
@@ -149,22 +155,35 @@ func (runtime *apiNodeRuntime) Detect(
 					Node string `json:"node"`
 				} `json:"engines"`
 			}
-			err = json.NewDecoder(strings.NewReader(*fileContent.Content)).Decode(&packageJSON)
+
+			data, err := fileContent.GetContent()
 			if err != nil {
-				// FIXME: log somewhere
+				fmt.Printf("Error calling GetContent() on package.json: %v\n", err)
+				return nil
+			}
+			err = json.NewDecoder(strings.NewReader(data)).Decode(&packageJSON)
+			if err != nil {
+				fmt.Printf("Error decoding package.json contents to struct: %v\n", err)
 				return nil
 			}
 
 			if detected[yarn] {
-				return map[string]interface{}{"runtime": yarn, "scripts": packageJSON.Scripts, "node_engine": packageJSON.Engines.Node}
+				fmt.Printf("NodeJS yarn runtime detected for %s/%s\n", owner, name)
+				return map[string]interface{}{
+					"runtime": yarn, "scripts": packageJSON.Scripts, "node_engine": packageJSON.Engines.Node,
+				}
 			} else {
-				return map[string]interface{}{"runtime": npm, "scripts": packageJSON.Scripts, "node_engine": packageJSON.Engines.Node}
-
+				fmt.Printf("NodeJS npm runtime detected for %s/%s\n", owner, name)
+				return map[string]interface{}{
+					"runtime": npm, "scripts": packageJSON.Scripts, "node_engine": packageJSON.Engines.Node,
+				}
 			}
 		}
 
+		fmt.Printf("NodeJS standalone runtime detected for %s/%s\n", owner, name)
 		return map[string]interface{}{"runtime": "node-standalone"}
 	}
 
+	fmt.Printf("No NodeJS runtime detected for %s/%s\n", owner, name)
 	return nil
 }

+ 16 - 8
internal/integrations/buildpacks/cli_go.go

@@ -3,6 +3,7 @@ package buildpacks
 import (
 	"sync"
 
+	depensure "github.com/paketo-buildpacks/dep-ensure"
 	gomodvendor "github.com/paketo-buildpacks/go-mod-vendor"
 	"github.com/paketo-buildpacks/packit"
 )
@@ -12,11 +13,6 @@ type cliGoRuntime struct {
 	wg    sync.WaitGroup
 }
 
-const (
-	mod = "mod"
-	dep = "dep"
-)
-
 func NewCLIGoRuntime() *cliGoRuntime {
 	packs := make(map[string]*BuildpackInfo)
 
@@ -40,7 +36,6 @@ func (runtime *cliGoRuntime) detectMod(results chan struct {
 }, workingDir string) {
 	goModParser := gomodvendor.NewGoModParser()
 	detect := gomodvendor.Detect(goModParser)
-
 	_, err := detect(packit.DetectContext{
 		WorkingDir: workingDir,
 	})
@@ -62,7 +57,21 @@ func (runtime *cliGoRuntime) detectDep(results chan struct {
 	string
 	bool
 }, workingDir string) {
-
+	detect := depensure.Detect()
+	_, err := detect(packit.DetectContext{
+		WorkingDir: workingDir,
+	})
+	if err == nil {
+		results <- struct {
+			string
+			bool
+		}{dep, true}
+	} else {
+		results <- struct {
+			string
+			bool
+		}{dep, false}
+	}
 	runtime.wg.Done()
 }
 
@@ -70,7 +79,6 @@ func (runtime *cliGoRuntime) detectStandalone(results chan struct {
 	string
 	bool
 }, workingDir string) {
-
 	runtime.wg.Done()
 }
 

+ 2 - 0
internal/integrations/buildpacks/common.go

@@ -10,6 +10,8 @@ import (
 const (
 	yarn       = "yarn"
 	npm        = "npm"
+	mod        = "mod"
+	dep        = "dep"
 	standalone = "standalone"
 )