| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- package buildpacks
- import (
- "bufio"
- "context"
- "fmt"
- "regexp"
- "strings"
- "sync"
- "github.com/google/go-github/v41/github"
- "github.com/xanzy/go-gitlab"
- )
- type rubyRuntime struct {
- wg sync.WaitGroup
- }
- func NewRubyRuntime() Runtime {
- return &rubyRuntime{}
- }
- func (runtime *rubyRuntime) detectPuma(gemfileContent string, results chan struct {
- string
- bool
- },
- ) {
- pumaFound := false
- quotes := `["']`
- pumaRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %spuma%s`, quotes, quotes))
- scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
- for scanner.Scan() {
- line := []byte(scanner.Text())
- if pumaRe.Match(line) {
- pumaFound = true
- break
- }
- }
- if pumaFound {
- results <- struct {
- string
- bool
- }{puma, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) detectThin(gemfileContent string, results chan struct {
- string
- bool
- },
- ) {
- thinFound := false
- quotes := `["']`
- thinRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %sthin%s`, quotes, quotes))
- scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
- for scanner.Scan() {
- line := []byte(scanner.Text())
- if thinRe.Match(line) {
- thinFound = true
- break
- }
- }
- if thinFound {
- results <- struct {
- string
- bool
- }{thin, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) detectUnicorn(gemfileContent string, results chan struct {
- string
- bool
- },
- ) {
- unicornFound := false
- quotes := `["']`
- unicornRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %sunicorn%s`, quotes, quotes))
- scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
- for scanner.Scan() {
- line := []byte(scanner.Text())
- if unicornRe.Match(line) {
- unicornFound = true
- break
- }
- }
- if unicornFound {
- results <- struct {
- string
- bool
- }{unicorn, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) detectPassenger(gemfileContent string, results chan struct {
- string
- bool
- },
- ) {
- passengerFound := false
- quotes := `["']`
- passengerRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %spassenger%s`, quotes, quotes))
- scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
- for scanner.Scan() {
- line := []byte(scanner.Text())
- if passengerRe.Match(line) {
- passengerFound = true
- break
- }
- }
- if passengerFound {
- results <- struct {
- string
- bool
- }{passenger, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) detectRackupGithub(
- client *github.Client, owner, name string,
- repoContentOptions github.RepositoryContentGetOptions, results chan struct {
- string
- bool
- },
- ) {
- fileContent, _, _, err := client.Repositories.GetContents(context.Background(),
- owner, name, "Gemfile.lock", &repoContentOptions)
- if err != nil {
- runtime.wg.Done()
- return
- }
- gemfileLockContent, err := fileContent.GetContent()
- if err != nil {
- runtime.wg.Done()
- return
- }
- rackFound := false
- scanner := bufio.NewScanner(strings.NewReader(gemfileLockContent))
- for scanner.Scan() {
- if strings.TrimSpace(scanner.Text()) == "GEM" {
- for scanner.Scan() {
- if strings.Contains(scanner.Text(), "rack") {
- rackFound = true
- break
- }
- }
- }
- }
- if rackFound {
- results <- struct {
- string
- bool
- }{rackup, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) detectRackupGitlab(
- client *gitlab.Client, repoPath, ref string, results chan struct {
- string
- bool
- },
- ) {
- fileContent, _, err := client.RepositoryFiles.GetRawFile(
- repoPath, "Gemfile.lock", &gitlab.GetRawFileOptions{
- Ref: gitlab.String(ref),
- })
- if err != nil {
- runtime.wg.Done()
- return
- }
- gemfileLockContent := string(fileContent)
- rackFound := false
- scanner := bufio.NewScanner(strings.NewReader(gemfileLockContent))
- for scanner.Scan() {
- if strings.TrimSpace(scanner.Text()) == "GEM" {
- for scanner.Scan() {
- if strings.Contains(scanner.Text(), "rack") {
- rackFound = true
- break
- }
- }
- }
- }
- if rackFound {
- results <- struct {
- string
- bool
- }{rackup, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) detectRake(gemfileContent string, results chan struct {
- string
- bool
- },
- ) {
- rakeFound := false
- quotes := `["']`
- rakeRe := regexp.MustCompile(fmt.Sprintf(`^\s*gem %srake%s`, quotes, quotes))
- scanner := bufio.NewScanner(strings.NewReader(gemfileContent))
- for scanner.Scan() {
- line := []byte(scanner.Text())
- if rakeRe.Match(line) {
- rakeFound = true
- break
- }
- }
- if rakeFound {
- results <- struct {
- string
- bool
- }{rake, true}
- }
- runtime.wg.Done()
- }
- func (runtime *rubyRuntime) DetectGithub(
- client *github.Client,
- directoryContent []*github.RepositoryContent,
- owner, name, path string,
- repoContentOptions github.RepositoryContentGetOptions,
- paketo, heroku *BuilderInfo,
- ) error {
- gemfileFound := false
- gemfileLockFound := false
- configRuFound := false
- rakefileFound := false
- for i := range directoryContent {
- name := directoryContent[i].GetName()
- if name == "Gemfile" {
- gemfileFound = true
- } else if name == "Gemfile.lock" {
- gemfileLockFound = true
- } else if name == "config.ru" {
- configRuFound = true
- } else if name == "Rakefile" || name == "Rakefile.rb" || name == "rakefile" || name == "rakefile.rb" {
- rakefileFound = true
- }
- }
- paketoBuildpackInfo := BuildpackInfo{
- Name: "Ruby",
- Buildpack: "gcr.io/paketo-buildpacks/ruby",
- }
- herokuBuildpackInfo := BuildpackInfo{
- Name: "Ruby",
- Buildpack: "heroku/ruby",
- }
- if !gemfileFound {
- paketo.Others = append(paketo.Others, paketoBuildpackInfo)
- heroku.Others = append(heroku.Others, herokuBuildpackInfo)
- return nil
- }
- fileContent, _, _, err := client.Repositories.GetContents(context.Background(), owner, name, "Gemfile", &repoContentOptions)
- if err != nil {
- paketo.Others = append(paketo.Others, paketoBuildpackInfo)
- heroku.Others = append(heroku.Others, herokuBuildpackInfo)
- return fmt.Errorf("error fetching contents of Gemfile for %s/%s: %v", owner, name, err)
- }
- gemfileContent, err := fileContent.GetContent()
- if err != nil {
- paketo.Others = append(paketo.Others, paketoBuildpackInfo)
- heroku.Others = append(heroku.Others, herokuBuildpackInfo)
- return fmt.Errorf("error calling GetContent() on Gemfile for %s/%s: %v", owner, name, err)
- }
- count := 6
- if !configRuFound {
- // unicorn needs config.ru
- count -= 1
- if !gemfileLockFound {
- // rackup needs one of Gemfile.lock or config.ru
- count -= 1
- }
- }
- if !rakefileFound {
- count -= 1
- }
- results := make(chan struct {
- string
- bool
- }, count)
- runtime.wg.Add(count)
- go runtime.detectPuma(gemfileContent, results)
- go runtime.detectThin(gemfileContent, results)
- if configRuFound {
- {
- // FIXME: find a better, more readable way of doing this
- results <- struct {
- string
- bool
- }{rackup, true}
- runtime.wg.Done()
- }
- go runtime.detectUnicorn(gemfileContent, results)
- }
- go runtime.detectPassenger(gemfileContent, results)
- if !configRuFound && gemfileLockFound {
- go runtime.detectRackupGithub(client, owner, name, repoContentOptions, results)
- }
- if rakefileFound {
- go runtime.detectRake(gemfileContent, results)
- }
- runtime.wg.Wait()
- close(results)
- paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
- heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
- return nil
- }
- func (runtime *rubyRuntime) DetectGitlab(
- client *gitlab.Client,
- tree []*gitlab.TreeNode,
- repoPath, path, ref string,
- paketo, heroku *BuilderInfo,
- ) error {
- gemfileFound := false
- gemfileLockFound := false
- configRuFound := false
- rakefileFound := false
- for i := range tree {
- name := tree[i].Name
- if name == "Gemfile" {
- gemfileFound = true
- } else if name == "Gemfile.lock" {
- gemfileLockFound = true
- } else if name == "config.ru" {
- configRuFound = true
- } else if name == "Rakefile" || name == "Rakefile.rb" || name == "rakefile" || name == "rakefile.rb" {
- rakefileFound = true
- }
- }
- paketoBuildpackInfo := BuildpackInfo{
- Name: "Ruby",
- Buildpack: "gcr.io/paketo-buildpacks/ruby",
- }
- herokuBuildpackInfo := BuildpackInfo{
- Name: "Ruby",
- Buildpack: "heroku/ruby",
- }
- if !gemfileFound {
- paketo.Others = append(paketo.Others, paketoBuildpackInfo)
- heroku.Others = append(heroku.Others, herokuBuildpackInfo)
- return nil
- }
- fileContent, _, err := client.RepositoryFiles.GetRawFile(
- repoPath, "Gemfile", &gitlab.GetRawFileOptions{
- Ref: gitlab.String(ref),
- })
- if err != nil {
- paketo.Others = append(paketo.Others, paketoBuildpackInfo)
- heroku.Others = append(heroku.Others, herokuBuildpackInfo)
- return fmt.Errorf("error fetching contents of Gemfile for %s: %v", repoPath, err)
- }
- gemfileContent := string(fileContent)
- count := 6
- if !configRuFound {
- // unicorn needs config.ru
- count -= 1
- if !gemfileLockFound {
- // rackup needs one of Gemfile.lock or config.ru
- count -= 1
- }
- }
- if !rakefileFound {
- count -= 1
- }
- results := make(chan struct {
- string
- bool
- }, count)
- runtime.wg.Add(count)
- go runtime.detectPuma(gemfileContent, results)
- go runtime.detectThin(gemfileContent, results)
- if configRuFound {
- {
- // FIXME: find a better, more readable way of doing this
- results <- struct {
- string
- bool
- }{rackup, true}
- runtime.wg.Done()
- }
- go runtime.detectUnicorn(gemfileContent, results)
- }
- go runtime.detectPassenger(gemfileContent, results)
- if !configRuFound && gemfileLockFound {
- go runtime.detectRackupGitlab(client, repoPath, ref, results)
- }
- if rakefileFound {
- go runtime.detectRake(gemfileContent, results)
- }
- runtime.wg.Wait()
- close(results)
- paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
- heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
- return nil
- }
|