forked from paketo-buildpacks/rails-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemfile_parser.go
More file actions
43 lines (35 loc) · 862 Bytes
/
gemfile_parser.go
File metadata and controls
43 lines (35 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package railsassets
import (
"bufio"
"fmt"
"os"
"regexp"
)
// GemfileParser parses the Gemfile to confirm that the application is using
// Rails.
type GemfileParser struct{}
// NewGemfileParser initializes a GemfileParser instance.
func NewGemfileParser() GemfileParser {
return GemfileParser{}
}
// Parse scans the Gemfile to find the "rails" gem.
func (p GemfileParser) Parse(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("failed to parse Gemfile: %w", err)
}
defer file.Close()
quotes := `["']`
railsRe := regexp.MustCompile(fmt.Sprintf(`gem %srails%s`, quotes, quotes))
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := []byte(scanner.Text())
if railsRe.Match(line) {
return true, nil
}
}
return false, nil
}