Class: Bibliothecary::Parsers::Go

Inherits:
Object
  • Object
show all
Includes:
Analyser
Defined in:
lib/bibliothecary/parsers/go.rb

Constant Summary collapse

GPM_REGEXP =
/^(.+)\s+(.+)$/
GOMOD_REGEX =
/^(.+)\s+(.+)$/
GOMOD_IGNORABLE_REGEX =
/^(module\s|require\s+\(|go\s|\))/m
GOSUM_REGEX =
/^(.+)\s+(.+)\s+(.+)$/

Class Method Summary collapse

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.map_dependencies(manifest, attr_name, dep_attr_name, version_attr_name, type) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/bibliothecary/parsers/go.rb', line 138

def self.map_dependencies(manifest, attr_name, dep_attr_name, version_attr_name, type)
  manifest.fetch(attr_name,[]).map do |dependency|
    {
      name: dependency[dep_attr_name],
      requirement: dependency[version_attr_name]  || '*',
      type: type
    }
  end
end

.mappingObject



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bibliothecary/parsers/go.rb', line 14

def self.mapping
  {
    match_filename("go.mod") => {
      kind: 'manifest',
      parser: :parse_go_mod
    },
    match_filename("go.sum") => {
      kind: 'lockfile',
      parser: :parse_go_sum
    },
    match_filename("glide.yaml") => {
      kind: 'manifest',
      parser: :parse_glide_yaml
    },
    match_filename("glide.lock") => {
      kind: 'lockfile',
      parser: :parse_glide_lockfile
    },
    match_filename("Godeps/Godeps.json") => {
      kind: 'manifest',
      parser: :parse_godep_json
    },
    match_filename("Godeps", case_insensitive: true) => {
      kind: 'manifest',
      parser: :parse_gpm
    },
    match_filename("vendor/manifest") => {
      kind: 'manifest',
      parser: :parse_gb_manifest
    },
    match_filename("vendor/vendor.json") => {
      kind: 'manifest',
      parser: :parse_govendor
    },
    match_filename("Gopkg.toml") => {
      kind: 'manifest',
      parser: :parse_dep_toml
    },
    match_filename("Gopkg.lock") => {
      kind: 'lockfile',
      parser: :parse_dep_lockfile
    }
  }
end

.parse_dep_lockfile(file_contents) ⇒ Object



104
105
106
107
# File 'lib/bibliothecary/parsers/go.rb', line 104

def self.parse_dep_lockfile(file_contents)
  manifest = TomlRB.parse file_contents
  map_dependencies(manifest, 'projects', 'name', 'revision', 'runtime')
end

.parse_dep_toml(file_contents) ⇒ Object



99
100
101
102
# File 'lib/bibliothecary/parsers/go.rb', line 99

def self.parse_dep_toml(file_contents)
  manifest = TomlRB.parse file_contents
  map_dependencies(manifest, 'constraint', 'name', 'version', 'runtime')
end

.parse_gb_manifest(file_contents) ⇒ Object



94
95
96
97
# File 'lib/bibliothecary/parsers/go.rb', line 94

def self.parse_gb_manifest(file_contents)
  manifest = JSON.parse file_contents
  map_dependencies(manifest, 'dependencies', 'importpath', 'revision', 'runtime')
end

.parse_glide_lockfile(file_contents) ⇒ Object



89
90
91
92
# File 'lib/bibliothecary/parsers/go.rb', line 89

def self.parse_glide_lockfile(file_contents)
  manifest = YAML.load file_contents
  map_dependencies(manifest, 'imports', 'name', 'version', 'runtime')
end

.parse_glide_yaml(file_contents) ⇒ Object



83
84
85
86
87
# File 'lib/bibliothecary/parsers/go.rb', line 83

def self.parse_glide_yaml(file_contents)
  manifest = YAML.load file_contents
  map_dependencies(manifest, 'import', 'package', 'version', 'runtime') +
  map_dependencies(manifest, 'devImports', 'package', 'version', 'development')
end

.parse_go_mod(file_contents) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bibliothecary/parsers/go.rb', line 109

def self.parse_go_mod(file_contents)
  deps = []
  file_contents.lines.map(&:strip).each do |line|
    next if line.match(GOMOD_IGNORABLE_REGEX)
    if match = line.gsub(/(\/\/(.*))/, '').match(GOMOD_REGEX)
      deps << {
        name: match[1].strip,
        requirement: match[2].strip || '*',
        type: 'runtime'
      }
    end
  end
  deps
end

.parse_go_sum(file_contents) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bibliothecary/parsers/go.rb', line 124

def self.parse_go_sum(file_contents)
  deps = []
  file_contents.lines.map(&:strip).each do |line|
    if match = line.match(GOSUM_REGEX)
      deps << {
        name: match[1].strip,
        requirement: match[2].strip.split('/').first || '*',
        type: 'runtime'
      }
    end
  end
  deps.uniq
end

.parse_godep_json(file_contents) ⇒ Object



59
60
61
62
# File 'lib/bibliothecary/parsers/go.rb', line 59

def self.parse_godep_json(file_contents)
  manifest = JSON.parse file_contents
  map_dependencies(manifest, 'Deps', 'ImportPath', 'Rev', 'runtime')
end

.parse_govendor(file_contents) ⇒ Object



78
79
80
81
# File 'lib/bibliothecary/parsers/go.rb', line 78

def self.parse_govendor(file_contents)
  manifest = JSON.load file_contents
  map_dependencies(manifest, 'package', 'path', 'revision', 'runtime')
end

.parse_gpm(file_contents) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bibliothecary/parsers/go.rb', line 64

def self.parse_gpm(file_contents)
  deps = []
  file_contents.split("\n").each do |line|
    match = line.gsub(/(\#(.*))/, '').match(GPM_REGEXP)
    next unless match
    deps << {
      name: match[1].strip,
      requirement: match[2].strip || '*',
      type: 'runtime'
    }
  end
  deps
end