Class: Bibliothecary::Parsers::Go

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

Constant Summary collapse

PLATFORM_NAME =
'go'

Class Method Summary collapse

Class Method Details

.analyse(folder_path, file_list) ⇒ Object



24
25
26
27
28
# File 'lib/bibliothecary/parsers/go.rb', line 24

def self.analyse(folder_path, file_list)
  [analyse_glide_yaml(folder_path, file_list),
  analyse_glide_lockfile(folder_path, file_list),
  analyse_godep_json(folder_path, file_list)]
end

.analyse_glide_lockfile(folder_path, file_list) ⇒ Object



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

def self.analyse_glide_lockfile(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^glide\.lock$/) }
  return unless path

  manifest = YAML.load File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_glide_lockfile(manifest)
  }
end

.analyse_glide_yaml(folder_path, file_list) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bibliothecary/parsers/go.rb', line 43

def self.analyse_glide_yaml(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^glide\.yaml$/) }
  return unless path

  manifest = YAML.load File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_glide_yaml(manifest)
  }
end

.analyse_godep_json(folder_path, file_list) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bibliothecary/parsers/go.rb', line 30

def self.analyse_godep_json(folder_path, file_list)
  path = file_list.find{|path| path.gsub(folder_path, '').gsub(/^\//, '').match(/^Godeps\/Godeps\.json$/) }
  return unless path

  manifest = JSON.parse File.open(path).read

  {
    platform: PLATFORM_NAME,
    path: path,
    dependencies: parse_godep_json(manifest)
  }
end

.parse(filename, file_contents) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bibliothecary/parsers/go.rb', line 9

def self.parse(filename, file_contents)
  if filename.match(/^glide\.yaml$/)
    yaml = YAML.load file_contents
    parse_glide_yaml(yaml)
  elsif filename.match(/^glide\.lock$/)
    yaml = YAML.load file_contents
    parse_glide_lockfile(yaml)
  elsif filename.match(/^Godeps\/Godeps\.json$/)
    json = JSON.parse file_contents
    parse_godep_json(json)
  else
    []
  end
end

.parse_glide_lockfile(manifest) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/bibliothecary/parsers/go.rb', line 95

def self.parse_glide_lockfile(manifest)
  manifest.fetch('imports',[]).map do |dependency|
    {
      name: dependency['name'],
      requirement: dependency['version'] || '*',
      type: 'runtime'
    }
  end
end

.parse_glide_yaml(manifest) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bibliothecary/parsers/go.rb', line 79

def self.parse_glide_yaml(manifest)
  manifest.fetch('import',[]).map do |dependency|
    {
      name: dependency['package'],
      requirement: dependency['version'] || '*',
      type: 'runtime'
    }
  end + manifest.fetch('devImports',[]).map do |dependency|
    {
      name: dependency['package'],
      requirement: dependency['version'] || '*',
      type: 'development'
    }
  end
end

.parse_godep_json(manifest) ⇒ Object



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

def self.parse_godep_json(manifest)
  manifest.fetch('Deps',[]).map do |dependency|
    {
      name: dependency['ImportPath'],
      requirement: dependency['Rev'],
      type: 'runtime'
    }
  end
end