ruby.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package buildpacks
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "regexp"
  7. "strings"
  8. "sync"
  9. "github.com/google/go-github/v41/github"
  10. "github.com/xanzy/go-gitlab"
  11. )
  12. type rubyRuntime struct {
  13. wg sync.WaitGroup
  14. }
  15. func NewRubyRuntime() Runtime {
  16. return &rubyRuntime{}
  17. }
  18. func (runtime *rubyRuntime) detectPuma(gemfileContent string, results chan struct {
  19. string
  20. bool
  21. }) {
  22. pumaFound := false
  23. quotes := `["']`
  24. pumaRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %spuma%s`, quotes, quotes))
  25. scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
  26. for scanner.Scan() {
  27. line := []byte(scanner.Text())
  28. if pumaRe.Match(line) {
  29. pumaFound = true
  30. break
  31. }
  32. }
  33. if pumaFound {
  34. results <- struct {
  35. string
  36. bool
  37. }{puma, true}
  38. }
  39. runtime.wg.Done()
  40. }
  41. func (runtime *rubyRuntime) detectThin(gemfileContent string, results chan struct {
  42. string
  43. bool
  44. }) {
  45. thinFound := false
  46. quotes := `["']`
  47. thinRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %sthin%s`, quotes, quotes))
  48. scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
  49. for scanner.Scan() {
  50. line := []byte(scanner.Text())
  51. if thinRe.Match(line) {
  52. thinFound = true
  53. break
  54. }
  55. }
  56. if thinFound {
  57. results <- struct {
  58. string
  59. bool
  60. }{thin, true}
  61. }
  62. runtime.wg.Done()
  63. }
  64. func (runtime *rubyRuntime) detectUnicorn(gemfileContent string, results chan struct {
  65. string
  66. bool
  67. }) {
  68. unicornFound := false
  69. quotes := `["']`
  70. unicornRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %sunicorn%s`, quotes, quotes))
  71. scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
  72. for scanner.Scan() {
  73. line := []byte(scanner.Text())
  74. if unicornRe.Match(line) {
  75. unicornFound = true
  76. break
  77. }
  78. }
  79. if unicornFound {
  80. results <- struct {
  81. string
  82. bool
  83. }{unicorn, true}
  84. }
  85. runtime.wg.Done()
  86. }
  87. func (runtime *rubyRuntime) detectPassenger(gemfileContent string, results chan struct {
  88. string
  89. bool
  90. }) {
  91. passengerFound := false
  92. quotes := `["']`
  93. passengerRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %spassenger%s`, quotes, quotes))
  94. scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
  95. for scanner.Scan() {
  96. line := []byte(scanner.Text())
  97. if passengerRe.Match(line) {
  98. passengerFound = true
  99. break
  100. }
  101. }
  102. if passengerFound {
  103. results <- struct {
  104. string
  105. bool
  106. }{passenger, true}
  107. }
  108. runtime.wg.Done()
  109. }
  110. func (runtime *rubyRuntime) detectRackupGithub(
  111. client *github.Client, owner, name string,
  112. repoContentOptions github.RepositoryContentGetOptions, results chan struct {
  113. string
  114. bool
  115. },
  116. ) {
  117. fileContent, _, _, err := client.Repositories.GetContents(context.Background(),
  118. owner, name, "Gemfile.lock", &repoContentOptions)
  119. if err != nil {
  120. runtime.wg.Done()
  121. return
  122. }
  123. gemfileLockContent, err := fileContent.GetContent()
  124. if err != nil {
  125. runtime.wg.Done()
  126. return
  127. }
  128. rackFound := false
  129. scanner := bufio.NewScanner(strings.NewReader(gemfileLockContent))
  130. for scanner.Scan() {
  131. if strings.TrimSpace(scanner.Text()) == "GEM" {
  132. for scanner.Scan() {
  133. if strings.Contains(scanner.Text(), "rack") {
  134. rackFound = true
  135. break
  136. }
  137. }
  138. }
  139. }
  140. if rackFound {
  141. results <- struct {
  142. string
  143. bool
  144. }{rackup, true}
  145. }
  146. runtime.wg.Done()
  147. }
  148. func (runtime *rubyRuntime) detectRackupGitlab(
  149. client *gitlab.Client, owner, name, ref string, results chan struct {
  150. string
  151. bool
  152. },
  153. ) {
  154. fileContent, _, err := client.RepositoryFiles.GetRawFile(
  155. fmt.Sprintf("%s/%s", owner, name), "Gemfile.lock", &gitlab.GetRawFileOptions{
  156. Ref: gitlab.String(ref),
  157. })
  158. if err != nil {
  159. runtime.wg.Done()
  160. return
  161. }
  162. gemfileLockContent := string(fileContent)
  163. rackFound := false
  164. scanner := bufio.NewScanner(strings.NewReader(gemfileLockContent))
  165. for scanner.Scan() {
  166. if strings.TrimSpace(scanner.Text()) == "GEM" {
  167. for scanner.Scan() {
  168. if strings.Contains(scanner.Text(), "rack") {
  169. rackFound = true
  170. break
  171. }
  172. }
  173. }
  174. }
  175. if rackFound {
  176. results <- struct {
  177. string
  178. bool
  179. }{rackup, true}
  180. }
  181. runtime.wg.Done()
  182. }
  183. func (runtime *rubyRuntime) detectRake(gemfileContent string, results chan struct {
  184. string
  185. bool
  186. }) {
  187. rakeFound := false
  188. quotes := `["']`
  189. rakeRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %srake%s`, quotes, quotes))
  190. scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
  191. for scanner.Scan() {
  192. line := []byte(scanner.Text())
  193. if rakeRe.Match(line) {
  194. rakeFound = true
  195. break
  196. }
  197. }
  198. if rakeFound {
  199. results <- struct {
  200. string
  201. bool
  202. }{rake, true}
  203. }
  204. runtime.wg.Done()
  205. }
  206. func (runtime *rubyRuntime) DetectGithub(
  207. client *github.Client,
  208. directoryContent []*github.RepositoryContent,
  209. owner, name, path string,
  210. repoContentOptions github.RepositoryContentGetOptions,
  211. paketo, heroku *BuilderInfo,
  212. ) error {
  213. gemfileFound := false
  214. gemfileLockFound := false
  215. configRuFound := false
  216. rakefileFound := false
  217. for i := range directoryContent {
  218. name := directoryContent[i].GetName()
  219. if name == "Gemfile" {
  220. gemfileFound = true
  221. } else if name == "Gemfile.lock" {
  222. gemfileLockFound = true
  223. } else if name == "config.ru" {
  224. configRuFound = true
  225. } else if name == "Rakefile" || name == "Rakefile.rb" || name == "rakefile" || name == "rakefile.rb" {
  226. rakefileFound = true
  227. }
  228. }
  229. paketoBuildpackInfo := BuildpackInfo{
  230. Name: "Ruby",
  231. Buildpack: "gcr.io/paketo-buildpacks/ruby",
  232. }
  233. herokuBuildpackInfo := BuildpackInfo{
  234. Name: "Ruby",
  235. Buildpack: "heroku/ruby",
  236. }
  237. if !gemfileFound {
  238. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  239. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  240. return nil
  241. }
  242. fileContent, _, _, err := client.Repositories.GetContents(context.Background(), owner, name, "Gemfile", &repoContentOptions)
  243. if err != nil {
  244. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  245. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  246. return fmt.Errorf("error fetching contents of Gemfile for %s/%s: %v", owner, name, err)
  247. }
  248. gemfileContent, err := fileContent.GetContent()
  249. if err != nil {
  250. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  251. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  252. return fmt.Errorf("error calling GetContent() on Gemfile for %s/%s: %v", owner, name, err)
  253. }
  254. count := 6
  255. if !configRuFound {
  256. // unicorn needs config.ru
  257. count -= 1
  258. if !gemfileLockFound {
  259. // rackup needs one of Gemfile.lock or config.ru
  260. count -= 1
  261. }
  262. }
  263. if !rakefileFound {
  264. count -= 1
  265. }
  266. results := make(chan struct {
  267. string
  268. bool
  269. }, count)
  270. runtime.wg.Add(count)
  271. go runtime.detectPuma(gemfileContent, results)
  272. go runtime.detectThin(gemfileContent, results)
  273. if configRuFound {
  274. {
  275. // FIXME: find a better, more readable way of doing this
  276. results <- struct {
  277. string
  278. bool
  279. }{rackup, true}
  280. runtime.wg.Done()
  281. }
  282. go runtime.detectUnicorn(gemfileContent, results)
  283. }
  284. go runtime.detectPassenger(gemfileContent, results)
  285. if !configRuFound && gemfileLockFound {
  286. go runtime.detectRackupGithub(client, owner, name, repoContentOptions, results)
  287. }
  288. if rakefileFound {
  289. go runtime.detectRake(gemfileContent, results)
  290. }
  291. runtime.wg.Wait()
  292. close(results)
  293. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  294. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  295. return nil
  296. }
  297. func (runtime *rubyRuntime) DetectGitlab(
  298. client *gitlab.Client,
  299. tree []*gitlab.TreeNode,
  300. owner, name, path, ref string,
  301. paketo, heroku *BuilderInfo,
  302. ) error {
  303. gemfileFound := false
  304. gemfileLockFound := false
  305. configRuFound := false
  306. rakefileFound := false
  307. for i := range tree {
  308. name := tree[i].Name
  309. if name == "Gemfile" {
  310. gemfileFound = true
  311. } else if name == "Gemfile.lock" {
  312. gemfileLockFound = true
  313. } else if name == "config.ru" {
  314. configRuFound = true
  315. } else if name == "Rakefile" || name == "Rakefile.rb" || name == "rakefile" || name == "rakefile.rb" {
  316. rakefileFound = true
  317. }
  318. }
  319. paketoBuildpackInfo := BuildpackInfo{
  320. Name: "Ruby",
  321. Buildpack: "gcr.io/paketo-buildpacks/ruby",
  322. }
  323. herokuBuildpackInfo := BuildpackInfo{
  324. Name: "Ruby",
  325. Buildpack: "heroku/ruby",
  326. }
  327. if !gemfileFound {
  328. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  329. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  330. return nil
  331. }
  332. fileContent, _, err := client.RepositoryFiles.GetRawFile(
  333. fmt.Sprintf("%s/%s", owner, name), "Gemfile", &gitlab.GetRawFileOptions{
  334. Ref: gitlab.String(ref),
  335. })
  336. if err != nil {
  337. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  338. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  339. return fmt.Errorf("error fetching contents of Gemfile for %s/%s: %v", owner, name, err)
  340. }
  341. gemfileContent := string(fileContent)
  342. count := 6
  343. if !configRuFound {
  344. // unicorn needs config.ru
  345. count -= 1
  346. if !gemfileLockFound {
  347. // rackup needs one of Gemfile.lock or config.ru
  348. count -= 1
  349. }
  350. }
  351. if !rakefileFound {
  352. count -= 1
  353. }
  354. results := make(chan struct {
  355. string
  356. bool
  357. }, count)
  358. runtime.wg.Add(count)
  359. go runtime.detectPuma(gemfileContent, results)
  360. go runtime.detectThin(gemfileContent, results)
  361. if configRuFound {
  362. {
  363. // FIXME: find a better, more readable way of doing this
  364. results <- struct {
  365. string
  366. bool
  367. }{rackup, true}
  368. runtime.wg.Done()
  369. }
  370. go runtime.detectUnicorn(gemfileContent, results)
  371. }
  372. go runtime.detectPassenger(gemfileContent, results)
  373. if !configRuFound && gemfileLockFound {
  374. go runtime.detectRackupGitlab(client, owner, name, ref, results)
  375. }
  376. if rakefileFound {
  377. go runtime.detectRake(gemfileContent, results)
  378. }
  379. runtime.wg.Wait()
  380. close(results)
  381. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  382. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  383. return nil
  384. }