Просмотр исходного кода

Preliminary Go runtime detectors

Mohammed Nafees 4 лет назад
Родитель
Сommit
e210143838

+ 2 - 0
cmd/test-runtime/runtimes/common.go

@@ -5,6 +5,8 @@ import (
 	"path/filepath"
 )
 
+const standalone = "standalone"
+
 type buildpackOrderGroupInfo struct {
 	ID       string
 	Optional bool

+ 90 - 0
cmd/test-runtime/runtimes/go.go

@@ -0,0 +1,90 @@
+package runtimes
+
+import (
+	"sync"
+
+	gomodvendor "github.com/paketo-buildpacks/go-mod-vendor"
+	"github.com/paketo-buildpacks/packit"
+)
+
+type goRuntime struct {
+	packs map[string]*BuildpackInfo
+	wg    sync.WaitGroup
+}
+
+const (
+	mod = "mod"
+	dep = "dep"
+)
+
+func NewGoRuntime() *goRuntime {
+	packs := make(map[string]*BuildpackInfo)
+
+	// mod
+	packs[mod] = newBuildpackInfo()
+
+	// dep
+	packs[dep] = newBuildpackInfo()
+
+	// go build
+	packs[standalone] = newBuildpackInfo()
+
+	return &goRuntime{
+		packs: packs,
+	}
+}
+
+func (runtime *goRuntime) detectMod(results chan struct {
+	string
+	bool
+}, workingDir string) {
+	goModParser := gomodvendor.NewGoModParser()
+	detect := gomodvendor.Detect(goModParser)
+
+	_, err := detect(packit.DetectContext{
+		WorkingDir: workingDir,
+	})
+	if err == nil {
+		results <- struct {
+			string
+			bool
+		}{mod, true}
+	} else {
+		results <- struct {
+			string
+			bool
+		}{mod, false}
+	}
+	runtime.wg.Done()
+}
+
+func (runtime *goRuntime) detectDep(results chan struct {
+	string
+	bool
+}, workingDir string) {
+
+	runtime.wg.Done()
+}
+
+func (runtime *goRuntime) detectStandalone(results chan struct {
+	string
+	bool
+}, workingDir string) {
+
+	runtime.wg.Done()
+}
+
+func (runtime *goRuntime) Detect(workingDir string) (BuildpackInfo, map[string]interface{}) {
+	results := make(chan struct {
+		string
+		bool
+	}, 3)
+
+	runtime.wg.Add(3)
+	go runtime.detectMod(results, workingDir)
+	go runtime.detectDep(results, workingDir)
+	go runtime.detectStandalone(results, workingDir)
+	runtime.wg.Wait()
+
+	return BuildpackInfo{}, nil
+}

+ 12 - 14
cmd/test-runtime/runtimes/nodejs.go

@@ -15,25 +15,23 @@ import (
 )
 
 const (
-	yarn       = "yarn"
-	npm        = "npm"
-	standalone = "standalone"
+	yarn = "yarn"
+	npm  = "npm"
 
-	tomlFile = "nodejs.buildpack.toml"
+	nodejsTomlFile = "nodejs.buildpack.toml"
 )
 
-type NodeRuntime struct {
-	// An internal representation of https://github.com/paketo-buildpacks/nodejs/blob/main/buildpack.toml
+type nodeRuntime struct {
 	packs map[string]*BuildpackInfo
 	wg    sync.WaitGroup
 }
 
-func NewNodeRuntime() *NodeRuntime {
+func NewNodeRuntime() *nodeRuntime {
 	packs := make(map[string]*BuildpackInfo)
 
-	buildpackToml, err := toml.LoadFile(filepath.Join(getExecPath(), "nodejs.buildpack.toml"))
+	buildpackToml, err := toml.LoadFile(filepath.Join(getExecPath(), nodejsTomlFile))
 	if err != nil {
-		fmt.Printf("Error while reading %s: %v\n", tomlFile, err)
+		fmt.Printf("Error while reading %s: %v\n", nodejsTomlFile, err)
 		os.Exit(1)
 	}
 	order := buildpackToml.Get("order").([]*toml.Tree)
@@ -96,12 +94,12 @@ func NewNodeRuntime() *NodeRuntime {
 	packs[standalone].addEnvVar("BP_LAUNCHPOINT", "")
 	packs[standalone].addEnvVar("BP_LIVE_RELOAD_ENABLED", "")
 
-	return &NodeRuntime{
+	return &nodeRuntime{
 		packs: packs,
 	}
 }
 
-func (runtime *NodeRuntime) detectYarn(results chan struct {
+func (runtime *nodeRuntime) detectYarn(results chan struct {
 	string
 	bool
 }, workingDir string) {
@@ -125,7 +123,7 @@ func (runtime *NodeRuntime) detectYarn(results chan struct {
 	runtime.wg.Done()
 }
 
-func (runtime *NodeRuntime) detectNPM(results chan struct {
+func (runtime *nodeRuntime) detectNPM(results chan struct {
 	string
 	bool
 }, workingDir string) {
@@ -149,7 +147,7 @@ func (runtime *NodeRuntime) detectNPM(results chan struct {
 	runtime.wg.Done()
 }
 
-func (runtime *NodeRuntime) detectStandalone(results chan struct {
+func (runtime *nodeRuntime) detectStandalone(results chan struct {
 	string
 	bool
 }, workingDir string) {
@@ -172,7 +170,7 @@ func (runtime *NodeRuntime) detectStandalone(results chan struct {
 	runtime.wg.Done()
 }
 
-func (runtime *NodeRuntime) Detect(workingDir string) (BuildpackInfo, map[string]interface{}) {
+func (runtime *nodeRuntime) Detect(workingDir string) (BuildpackInfo, map[string]interface{}) {
 	results := make(chan struct {
 		string
 		bool