Sfoglia il codice sorgente

Read directly from nodejs packeto buildpack

Mohammed Nafees 4 anni fa
parent
commit
4a891097c8

+ 1 - 0
Makefile

@@ -20,4 +20,5 @@ build-cli-dev:
 	go build -tags cli -o $(BINDIR)/porter ./cli
 
 test-runtime:
+	cp ./cmd/test-runtime/buildpacks/nodejs.buildpack.toml $(BINDIR)/
 	go build -tags test-runtime -o $(BINDIR)/test-runtime ./cmd/test-runtime

+ 136 - 0
cmd/test-runtime/buildpacks/nodejs.buildpack.toml

@@ -0,0 +1,136 @@
+api = "0.4"
+
+[buildpack]
+  homepage = "https://github.com/paketo-buildpacks/nodejs"
+  id = "paketo-buildpacks/nodejs"
+  name = "Paketo Node.js Buildpack"
+
+[metadata]
+  include-files = ["buildpack.toml"]
+
+[[order]]
+
+  [[order.group]]
+    id = "paketo-buildpacks/ca-certificates"
+    optional = true
+    version = "2.4.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-engine"
+    version = "0.10.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/yarn"
+    version = "0.4.3"
+
+  [[order.group]]
+    id = "paketo-buildpacks/yarn-install"
+    version = "0.5.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-module-bom"
+    optional = true
+    version = "0.1.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-run-script"
+    optional = true
+    version = "0.2.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/yarn-start"
+    version = "0.4.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/procfile"
+    optional = true
+    version = "4.4.1"
+
+  [[order.group]]
+    id = "paketo-buildpacks/environment-variables"
+    optional = true
+    version = "3.2.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/image-labels"
+    optional = true
+    version = "3.2.3"
+
+[[order]]
+
+  [[order.group]]
+    id = "paketo-buildpacks/ca-certificates"
+    optional = true
+    version = "2.4.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-engine"
+    version = "0.10.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/npm-install"
+    version = "0.5.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-module-bom"
+    optional = true
+    version = "0.1.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-run-script"
+    optional = true
+    version = "0.2.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/npm-start"
+    version = "0.5.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/procfile"
+    optional = true
+    version = "4.4.1"
+
+  [[order.group]]
+    id = "paketo-buildpacks/environment-variables"
+    optional = true
+    version = "3.2.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/image-labels"
+    optional = true
+    version = "3.2.3"
+
+[[order]]
+
+  [[order.group]]
+    id = "paketo-buildpacks/ca-certificates"
+    optional = true
+    version = "2.4.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-engine"
+    version = "0.10.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-module-bom"
+    optional = true
+    version = "0.1.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/node-start"
+    version = "0.5.0"
+
+  [[order.group]]
+    id = "paketo-buildpacks/procfile"
+    optional = true
+    version = "4.4.1"
+
+  [[order.group]]
+    id = "paketo-buildpacks/environment-variables"
+    optional = true
+    version = "3.2.2"
+
+  [[order.group]]
+    id = "paketo-buildpacks/image-labels"
+    optional = true
+    version = "3.2.3"

+ 25 - 5
cmd/test-runtime/runtimes/common.go

@@ -1,24 +1,44 @@
 package runtimes
 
+import (
+	"os"
+	"path/filepath"
+)
+
+type buildpackOrderGroupInfo struct {
+	ID       string
+	Optional bool
+	Version  string
+}
+
 type BuildpackInfo struct {
-	Packs []string
+	Packs []buildpackOrderGroupInfo
 	// FIXME: env vars for https://github.com/paketo-buildpacks/environment-variables
 	//        and for https://github.com/paketo-buildpacks/image-labels
-	Env map[string]string
+	EnvVars map[string]string
+	Data    interface{}
 }
 
 func newBuildpackInfo() *BuildpackInfo {
 	return &BuildpackInfo{
-		Env: make(map[string]string),
+		EnvVars: make(map[string]string),
 	}
 }
 
-func (info *BuildpackInfo) addPack(pack string) {
+func (info *BuildpackInfo) addPack(pack buildpackOrderGroupInfo) {
 	info.Packs = append(info.Packs, pack)
 }
 
 func (info *BuildpackInfo) addEnvVar(id string, val string) {
-	info.Env[id] = val
+	info.EnvVars[id] = val
+}
+
+func getExecPath() string {
+	ex, err := os.Executable()
+	if err != nil {
+		panic(err)
+	}
+	return filepath.Dir(ex)
 }
 
 type Runtime interface {

+ 0 - 178
cmd/test-runtime/runtimes/node.go

@@ -1,178 +0,0 @@
-package runtimes
-
-import (
-	"sync"
-
-	nodemodulebom "github.com/paketo-buildpacks/node-module-bom"
-	npminstall "github.com/paketo-buildpacks/npm-install"
-	"github.com/paketo-buildpacks/packit"
-	yarninstall "github.com/paketo-buildpacks/yarn-install"
-)
-
-type NodeRuntime struct {
-	// An internal representation of https://github.com/paketo-buildpacks/nodejs/blob/main/buildpack.toml
-	packs map[string]*BuildpackInfo
-	wg    sync.WaitGroup
-}
-
-func NewNodeRuntime() *NodeRuntime {
-	packs := make(map[string]*BuildpackInfo)
-
-	// yarn
-	packs["yarn"] = newBuildpackInfo()
-	packs["yarn"].addPack("paketo-buildpacks/ca-certificates")
-	packs["yarn"].addPack("paketo-buildpacks/node-engine")
-	packs["yarn"].addPack("paketo-buildpacks/yarn")
-	packs["yarn"].addPack("paketo-buildpacks/yarn-install")
-	packs["yarn"].addPack("paketo-buildpacks/node-module-bom")
-	packs["yarn"].addPack("paketo-buildpacks/node-run-script")
-	packs["yarn"].addPack("paketo-buildpacks/yarn-start")
-	packs["yarn"].addPack("paketo-buildpacks/procfile")
-	packs["yarn"].addPack("paketo-buildpacks/environment-variables")
-	packs["yarn"].addPack("paketo-buildpacks/image-labels")
-	packs["yarn"].addEnvVar("SSL_CERT_DIR", "")
-	packs["yarn"].addEnvVar("SSL_CERT_FILE", "")
-	packs["yarn"].addEnvVar("BP_NODE_OPTIMIZE_MEMORY", "")
-	packs["yarn"].addEnvVar("BP_NODE_PROJECT_PATH", "")
-	packs["yarn"].addEnvVar("BP_NODE_VERSION", "")
-	packs["yarn"].addEnvVar("BP_NODE_RUN_SCRIPTS", "")
-
-	// npm
-	packs["npm"] = newBuildpackInfo()
-	packs["npm"].addPack("paketo-buildpacks/ca-certificates")
-	packs["npm"].addPack("paketo-buildpacks/node-engine")
-	packs["npm"].addPack("paketo-buildpacks/npm-install")
-	packs["npm"].addPack("paketo-buildpacks/node-module-bom")
-	packs["npm"].addPack("paketo-buildpacks/node-run-script")
-	packs["npm"].addPack("paketo-buildpacks/npm-start")
-	packs["npm"].addPack("paketo-buildpacks/procfile")
-	packs["npm"].addPack("paketo-buildpacks/environment-variables")
-	packs["npm"].addPack("paketo-buildpacks/image-labels")
-	packs["npm"].addEnvVar("SSL_CERT_DIR", "")
-	packs["npm"].addEnvVar("SSL_CERT_FILE", "")
-	packs["npm"].addEnvVar("BP_NODE_OPTIMIZE_MEMORY", "")
-	packs["npm"].addEnvVar("BP_NODE_PROJECT_PATH", "")
-	packs["npm"].addEnvVar("BP_NODE_VERSION", "")
-	packs["npm"].addEnvVar("BP_NODE_RUN_SCRIPTS", "")
-
-	// no package manager
-	packs["standalone"] = newBuildpackInfo()
-	packs["standalone"].addPack("paketo-buildpacks/ca-certificates")
-	packs["standalone"].addPack("paketo-buildpacks/node-engine")
-	packs["standalone"].addPack("paketo-buildpacks/node-module-bom")
-	packs["standalone"].addPack("paketo-buildpacks/node-start")
-	packs["standalone"].addPack("paketo-buildpacks/procfile")
-	packs["standalone"].addPack("paketo-buildpacks/environment-variables")
-	packs["standalone"].addPack("paketo-buildpacks/image-labels")
-	packs["standalone"].addEnvVar("SSL_CERT_DIR", "")
-	packs["standalone"].addEnvVar("SSL_CERT_FILE", "")
-	packs["standalone"].addEnvVar("BP_NODE_OPTIMIZE_MEMORY", "")
-	packs["standalone"].addEnvVar("BP_NODE_PROJECT_PATH", "")
-	packs["standalone"].addEnvVar("BP_NODE_VERSION", "")
-	packs["standalone"].addEnvVar("BP_LAUNCHPOINT", "")
-	packs["standalone"].addEnvVar("BP_LIVE_RELOAD_ENABLED", "")
-
-	return &NodeRuntime{
-		packs: packs,
-	}
-}
-
-func (runtime *NodeRuntime) detectYarn(results chan struct {
-	string
-	bool
-}, workingDir string) {
-	yarnProjectPathParser := yarninstall.NewProjectPathParser()
-	yarnVersionParser := yarninstall.NewPackageJSONParser()
-	detect := yarninstall.Detect(yarnProjectPathParser, yarnVersionParser)
-	_, err := detect(packit.DetectContext{
-		WorkingDir: workingDir,
-	})
-	if err == nil {
-		results <- struct {
-			string
-			bool
-		}{"yarn", true}
-	} else {
-		results <- struct {
-			string
-			bool
-		}{"yarn", false}
-	}
-	runtime.wg.Done()
-}
-
-func (runtime *NodeRuntime) detectNPM(results chan struct {
-	string
-	bool
-}, workingDir string) {
-	npmProjectPathParser := npminstall.NewProjectPathParser()
-	npmVersionParser := npminstall.NewPackageJSONParser()
-	detect := npminstall.Detect(npmProjectPathParser, npmVersionParser)
-	_, err := detect(packit.DetectContext{
-		WorkingDir: workingDir,
-	})
-	if err == nil {
-		results <- struct {
-			string
-			bool
-		}{"npm", true}
-	} else {
-		results <- struct {
-			string
-			bool
-		}{"npm", false}
-	}
-	runtime.wg.Done()
-}
-
-func (runtime *NodeRuntime) detectStandalone(results chan struct {
-	string
-	bool
-}, workingDir string) {
-	// FIXME: the detect function seems to be working for non-node projects as well?
-	detect := nodemodulebom.Detect()
-	_, err := detect(packit.DetectContext{
-		WorkingDir: workingDir,
-	})
-	if err == nil {
-		results <- struct {
-			string
-			bool
-		}{"standalone", true}
-	} else {
-		results <- struct {
-			string
-			bool
-		}{"standalone", false}
-	}
-	runtime.wg.Done()
-}
-
-func (runtime *NodeRuntime) Detect(workingDir string) *BuildpackInfo {
-	results := make(chan struct {
-		string
-		bool
-	}, 3)
-
-	runtime.wg.Add(3)
-	go runtime.detectYarn(results, workingDir)
-	go runtime.detectNPM(results, workingDir)
-	go runtime.detectStandalone(results, workingDir)
-	runtime.wg.Wait()
-	close(results)
-
-	detected := make(map[string]bool)
-	for result := range results {
-		detected[result.string] = result.bool
-	}
-
-	if detected["yarn"] {
-		return runtime.packs["yarn"]
-	} else if detected["npm"] {
-		return runtime.packs["npm"]
-	} else if detected["standalone"] {
-		return runtime.packs["standalone"]
-	}
-
-	return nil
-}

+ 201 - 0
cmd/test-runtime/runtimes/nodejs.go

@@ -0,0 +1,201 @@
+package runtimes
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+	"sync"
+
+	nodemodulebom "github.com/paketo-buildpacks/node-module-bom"
+	npminstall "github.com/paketo-buildpacks/npm-install"
+	"github.com/paketo-buildpacks/packit"
+	yarninstall "github.com/paketo-buildpacks/yarn-install"
+	"github.com/pelletier/go-toml"
+)
+
+const (
+	yarn       = "yarn"
+	npm        = "npm"
+	standalone = "standalone"
+
+	tomlFile = "nodejs.buildpack.toml"
+)
+
+type NodeRuntime struct {
+	// An internal representation of https://github.com/paketo-buildpacks/nodejs/blob/main/buildpack.toml
+	packs map[string]*BuildpackInfo
+	wg    sync.WaitGroup
+}
+
+func NewNodeRuntime() *NodeRuntime {
+	packs := make(map[string]*BuildpackInfo)
+
+	buildpackToml, err := toml.LoadFile(filepath.Join(getExecPath(), "nodejs.buildpack.toml"))
+	if err != nil {
+		fmt.Printf("Error while reading %s: %v\n", tomlFile, err)
+		os.Exit(1)
+	}
+	order := buildpackToml.Get("order").([]*toml.Tree)
+
+	// yarn
+	packs[yarn] = newBuildpackInfo()
+	yarnGroup := order[0].GetArray("group").([]*toml.Tree)
+	for i := 0; i < len(yarnGroup); i++ {
+		packs[yarn].addPack(
+			buildpackOrderGroupInfo{
+				ID:       yarnGroup[i].Get("id").(string),
+				Optional: yarnGroup[i].GetDefault("optional", false).(bool),
+				Version:  yarnGroup[i].Get("version").(string),
+			},
+		)
+	}
+	packs[yarn].addEnvVar("SSL_CERT_DIR", "")
+	packs[yarn].addEnvVar("SSL_CERT_FILE", "")
+	packs[yarn].addEnvVar("BP_NODE_OPTIMIZE_MEMORY", "")
+	packs[yarn].addEnvVar("BP_NODE_PROJECT_PATH", "")
+	packs[yarn].addEnvVar("BP_NODE_VERSION", "")
+	packs[yarn].addEnvVar("BP_NODE_RUN_SCRIPTS", "")
+
+	// npm
+	packs[npm] = newBuildpackInfo()
+	npmGroup := order[1].GetArray("group").([]*toml.Tree)
+	for i := 0; i < len(npmGroup); i++ {
+		packs[npm].addPack(
+			buildpackOrderGroupInfo{
+				ID:       npmGroup[i].Get("id").(string),
+				Optional: npmGroup[i].GetDefault("optional", false).(bool),
+				Version:  npmGroup[i].Get("version").(string),
+			},
+		)
+	}
+	packs[npm].addEnvVar("SSL_CERT_DIR", "")
+	packs[npm].addEnvVar("SSL_CERT_FILE", "")
+	packs[npm].addEnvVar("BP_NODE_OPTIMIZE_MEMORY", "")
+	packs[npm].addEnvVar("BP_NODE_PROJECT_PATH", "")
+	packs[npm].addEnvVar("BP_NODE_VERSION", "")
+	packs[npm].addEnvVar("BP_NODE_RUN_SCRIPTS", "")
+
+	// no package manager
+	packs[standalone] = newBuildpackInfo()
+	standaloneGroup := order[2].GetArray("group").([]*toml.Tree)
+	for i := 0; i < len(standaloneGroup); i++ {
+		packs[standalone].addPack(
+			buildpackOrderGroupInfo{
+				ID:       standaloneGroup[i].Get("id").(string),
+				Optional: standaloneGroup[i].GetDefault("optional", false).(bool),
+				Version:  standaloneGroup[i].Get("version").(string),
+			},
+		)
+	}
+	packs[standalone].addEnvVar("SSL_CERT_DIR", "")
+	packs[standalone].addEnvVar("SSL_CERT_FILE", "")
+	packs[standalone].addEnvVar("BP_NODE_OPTIMIZE_MEMORY", "")
+	packs[standalone].addEnvVar("BP_NODE_PROJECT_PATH", "")
+	packs[standalone].addEnvVar("BP_NODE_VERSION", "")
+	packs[standalone].addEnvVar("BP_LAUNCHPOINT", "")
+	packs[standalone].addEnvVar("BP_LIVE_RELOAD_ENABLED", "")
+
+	return &NodeRuntime{
+		packs: packs,
+	}
+}
+
+func (runtime *NodeRuntime) detectYarn(results chan struct {
+	string
+	bool
+}, workingDir string) {
+	yarnProjectPathParser := yarninstall.NewProjectPathParser()
+	yarnVersionParser := yarninstall.NewPackageJSONParser()
+	detect := yarninstall.Detect(yarnProjectPathParser, yarnVersionParser)
+	_, err := detect(packit.DetectContext{
+		WorkingDir: workingDir,
+	})
+	if err == nil {
+		results <- struct {
+			string
+			bool
+		}{yarn, true}
+	} else {
+		results <- struct {
+			string
+			bool
+		}{yarn, false}
+	}
+	runtime.wg.Done()
+}
+
+func (runtime *NodeRuntime) detectNPM(results chan struct {
+	string
+	bool
+}, workingDir string) {
+	npmProjectPathParser := npminstall.NewProjectPathParser()
+	npmVersionParser := npminstall.NewPackageJSONParser()
+	detect := npminstall.Detect(npmProjectPathParser, npmVersionParser)
+	_, err := detect(packit.DetectContext{
+		WorkingDir: workingDir,
+	})
+	if err == nil {
+		results <- struct {
+			string
+			bool
+		}{npm, true}
+	} else {
+		results <- struct {
+			string
+			bool
+		}{npm, false}
+	}
+	runtime.wg.Done()
+}
+
+func (runtime *NodeRuntime) detectStandalone(results chan struct {
+	string
+	bool
+}, workingDir string) {
+	// FIXME: the detect function seems to be working for non-node projects as well?
+	detect := nodemodulebom.Detect()
+	_, err := detect(packit.DetectContext{
+		WorkingDir: workingDir,
+	})
+	if err == nil {
+		results <- struct {
+			string
+			bool
+		}{standalone, true}
+	} else {
+		results <- struct {
+			string
+			bool
+		}{standalone, false}
+	}
+	runtime.wg.Done()
+}
+
+func (runtime *NodeRuntime) Detect(workingDir string) *BuildpackInfo {
+	results := make(chan struct {
+		string
+		bool
+	}, 3)
+
+	runtime.wg.Add(3)
+	go runtime.detectYarn(results, workingDir)
+	go runtime.detectNPM(results, workingDir)
+	go runtime.detectStandalone(results, workingDir)
+	runtime.wg.Wait()
+	close(results)
+
+	detected := make(map[string]bool)
+	for result := range results {
+		detected[result.string] = result.bool
+	}
+
+	if detected[yarn] {
+		return runtime.packs[yarn]
+	} else if detected[npm] {
+		return runtime.packs[npm]
+	} else if detected[standalone] {
+		return runtime.packs[standalone]
+	}
+
+	return nil
+}

+ 1 - 0
go.mod

@@ -50,6 +50,7 @@ require (
 	github.com/paketo-buildpacks/rails-assets v0.3.0
 	github.com/paketo-buildpacks/rake v0.1.0
 	github.com/paketo-buildpacks/yarn-install v0.5.0
+	github.com/pelletier/go-toml v1.9.4 // indirect
 	github.com/pkg/errors v0.9.1
 	github.com/rogpeppe/go-internal v1.5.2 // indirect
 	github.com/rs/zerolog v1.20.0