Class: Bibliothecary::Parsers::Go

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

Constant Summary collapse

GPM_REGEXP =
/^(.+)\s+(.+)$/

Class Method Summary collapse

Methods included from Analyser

included

Class Method Details

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



98
99
100
101
102
103
104
105
106
# File 'lib/bibliothecary/parsers/go.rb', line 98

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



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
44
45
46
# File 'lib/bibliothecary/parsers/go.rb', line 11

def self.mapping
  {
    /^glide\.yaml$/ => {
      kind: 'manifest',
      parser: :parse_glide_yaml
    },
    /^glide\.lock$/ => {
      kind: 'lockfile',
      parser: :parse_glide_lockfile
    },
    /^Godeps\/Godeps\.json$/ => {
      kind: 'manifest',
      parser: :parse_godep_json
    },
    /^Godeps$/i => {
      kind: 'manifest',
      parser: :parse_gpm
    },
    /^vendor\/manifest$/ => {
      kind: 'manifest',
      parser: :parse_gb_manifest
    },
    /^vendor\/vendor.json$/ => {
      kind: 'manifest',
      parser: :parse_govendor
    },
    /^Gopkg\.toml$/ => {
      kind: 'manifest',
      parser: :parse_dep_toml
    },
    /^Gopkg\.lock$/ => {
      kind: 'lockfile',
      parser: :parse_dep_lockfile
    },
  }
end

.parse_dep_lockfile(file_contents) ⇒ Object



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

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



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

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



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

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



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

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



72
73
74
75
76
# File 'lib/bibliothecary/parsers/go.rb', line 72

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_godep_json(file_contents) ⇒ Object



48
49
50
51
# File 'lib/bibliothecary/parsers/go.rb', line 48

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



67
68
69
70
# File 'lib/bibliothecary/parsers/go.rb', line 67

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

.parse_gpm(file_contents) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bibliothecary/parsers/go.rb', line 53

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