main.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "path/filepath"
  6. bundleinstall "github.com/paketo-buildpacks/bundle-install"
  7. condaenvupdate "github.com/paketo-buildpacks/conda-env-update"
  8. gomodvendor "github.com/paketo-buildpacks/go-mod-vendor"
  9. npminstall "github.com/paketo-buildpacks/npm-install"
  10. "github.com/paketo-buildpacks/packit"
  11. pipenvinstall "github.com/paketo-buildpacks/pipenv-install"
  12. pythonstart "github.com/paketo-buildpacks/python-start"
  13. "github.com/paketo-buildpacks/rackup"
  14. railsassets "github.com/paketo-buildpacks/rails-assets"
  15. "github.com/paketo-buildpacks/rake"
  16. yarninstall "github.com/paketo-buildpacks/yarn-install"
  17. )
  18. const GoModLocation = "go.mod"
  19. var workingDir string
  20. func detectGo() {
  21. /* First check if it is a go.mod project */
  22. goModParser := gomodvendor.NewGoModParser()
  23. detect := gomodvendor.Detect(goModParser)
  24. _, err := detect(packit.DetectContext{
  25. WorkingDir: workingDir,
  26. })
  27. if err == nil {
  28. log.Println("go.mod project detected")
  29. return
  30. }
  31. /* Next, check if it is a Gopkg.toml */
  32. _, err = os.Stat(filepath.Join(workingDir, "Gopkg.toml"))
  33. if err == nil {
  34. log.Println("Gopkg.toml project detected")
  35. return
  36. }
  37. /* Finally, check if it is a Go vendor */
  38. _, err = os.Stat(filepath.Join(workingDir, "vendor"))
  39. if err == nil {
  40. log.Println("Go vendor project detected")
  41. return
  42. }
  43. // FIXME: what about Go projects that do not use any of the
  44. // above but contain a Makefile to call 'go build'
  45. /* Not a Go project */
  46. log.Println("Not a Go project")
  47. }
  48. func detectPython() {
  49. /* Check for Pipfile project */
  50. pipfileParser := pipenvinstall.NewPipfileParser()
  51. pipfileLockParser := pipenvinstall.NewPipfileLockParser()
  52. detect := pipenvinstall.Detect(pipfileParser, pipfileLockParser)
  53. _, err := detect(packit.DetectContext{
  54. WorkingDir: workingDir,
  55. })
  56. if err == nil {
  57. log.Println("Python Pipfile project detected")
  58. return
  59. }
  60. /* Check for pip project */
  61. _, err = os.Stat(filepath.Join(workingDir, "requirements.txt"))
  62. if err == nil {
  63. log.Println("Python pip project detected")
  64. return
  65. }
  66. /* Check for conda project */
  67. detect = condaenvupdate.Detect()
  68. _, err = detect(packit.DetectContext{
  69. WorkingDir: workingDir,
  70. })
  71. if err == nil {
  72. log.Println("Python conda project detected")
  73. return
  74. }
  75. /* Check for all other possibilities of a Python project */
  76. detect = pythonstart.Detect()
  77. _, err = detect(packit.DetectContext{
  78. WorkingDir: workingDir,
  79. })
  80. if err == nil {
  81. log.Println("Python project detected")
  82. return
  83. }
  84. /* Not a Python project */
  85. log.Println("Not a Python project")
  86. }
  87. func detectNode() {
  88. /* Check for yarn project */
  89. yarnProjectPathParser := yarninstall.NewProjectPathParser()
  90. yarnVersionParser := yarninstall.NewPackageJSONParser()
  91. detect := yarninstall.Detect(yarnProjectPathParser, yarnVersionParser)
  92. _, err := detect(packit.DetectContext{
  93. WorkingDir: workingDir,
  94. })
  95. if err == nil {
  96. log.Println("Node yarn project detected")
  97. return
  98. }
  99. /* Check for npm project */
  100. npmProjectPathParser := npminstall.NewProjectPathParser()
  101. npmVersionParser := npminstall.NewPackageJSONParser()
  102. detect = npminstall.Detect(npmProjectPathParser, npmVersionParser)
  103. _, err = detect(packit.DetectContext{
  104. WorkingDir: workingDir,
  105. })
  106. if err == nil {
  107. log.Println("Node npm project detected")
  108. return
  109. }
  110. /* Not a Node project */
  111. log.Println("Not a Node project")
  112. }
  113. func detectRuby() {
  114. /* Check for Gemfile project */
  115. gemfileParser := bundleinstall.NewGemfileParser()
  116. detect := bundleinstall.Detect(gemfileParser)
  117. _, err := detect(packit.DetectContext{
  118. WorkingDir: workingDir,
  119. })
  120. if err == nil {
  121. log.Println("Ruby Gemfile project detected")
  122. return
  123. }
  124. /* Check for rackup project */
  125. gemParser := rackup.NewGemfileLockParser()
  126. detect = rackup.Detect(gemParser)
  127. _, err = detect(packit.DetectContext{
  128. WorkingDir: workingDir,
  129. })
  130. if err == nil {
  131. log.Println("Ruby rackup project detected")
  132. return
  133. }
  134. /* Check for Rails app with assets */
  135. railsGemfileParser := railsassets.NewGemfileParser()
  136. detect = railsassets.Detect(railsGemfileParser)
  137. _, err = detect(packit.DetectContext{
  138. WorkingDir: workingDir,
  139. })
  140. if err == nil {
  141. log.Println("Ruby rails project detected")
  142. return
  143. }
  144. /* Check for rakefile project */
  145. rakeGemfileParser := rake.NewGemfileParser()
  146. detect = rake.Detect(rakeGemfileParser)
  147. _, err = detect(packit.DetectContext{
  148. WorkingDir: workingDir,
  149. })
  150. if err == nil {
  151. log.Println("Ruby rakefile project detected")
  152. return
  153. }
  154. /* Not a Ruby project */
  155. log.Println("Not a Ruby project")
  156. }
  157. func main() {
  158. if len(os.Args) < 2 {
  159. log.Fatalln("Usage: ./test-runtime <project directory>")
  160. }
  161. workingDir = os.Args[1]
  162. detectGo()
  163. detectPython()
  164. detectNode()
  165. detectRuby()
  166. }