Class: Grpm::Gitlab

Inherits:
Object
  • Object
show all
Defined in:
lib/grpm/gitlab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, ref = 'master') ⇒ Gitlab

Returns a new instance of Gitlab.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/grpm/gitlab.rb', line 12

def initialize(repository, ref='master')
  @info = parse_uri(repository)
  @ref = ref

  token = Env.info.select {|env|
    env['host'] == @info.host
  }.first['token']

  @client = ::Gitlab.client(endpoint: @info.endpoint, private_token: token)
  @project = @client.project(@info.project)
  @id = @project.id
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/grpm/gitlab.rb', line 10

def client
  @client
end

#projectObject (readonly)

Returns the value of attribute project.



10
11
12
# File 'lib/grpm/gitlab.rb', line 10

def project
  @project
end

Instance Method Details

#installObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/grpm/gitlab.rb', line 42

def install
  package['deps'].map { |dep|
    if dep[0] == '/'
      dep
    else
      '/' + dep
    end
  }.each { |path|
    dir = Pathname.new('./grpm').join(@info.name)
    if path[1] == '~'
      path = Pathname.new('/').join(path[2..-1])
      dir = dir.join(path.dirname.to_s[1..-1])
    end

    puts "dep: #{path}"
    puts "dir: #{dir}"

    file_name = Pathname.new(path).basename.to_s
    file = @client.file_contents(@id, path)

    puts file_name

    FileUtils.mkdir_p(dir)
    File.write dir.join(file_name), file
  }
end

#packageObject



38
39
40
# File 'lib/grpm/gitlab.rb', line 38

def package
  @package ||= YAML.load @client.file_contents(@id, 'grpc.yml', @ref)
end

#parse_uri(repository) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grpm/gitlab.rb', line 25

def parse_uri(repository)
  uri = URI(repository)
  path_args = uri.path.sub('.git', '').split('/')
  uri.path = '/api/v4'

  OpenStruct.new({
    host: uri.host,
    name: path_args.last,
    endpoint: uri.to_s,
    project: path_args.select { |e| e != "" }.join('/')
  })
end