Class: Raykit::Git::Repositories

Inherits:
Array
  • Object
show all
Defined in:
lib/raykit/git/repositories.rb

Overview

Functionality to manage a remote git repository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Repositories

Returns a new instance of Repositories.



11
12
13
14
# File 'lib/raykit/git/repositories.rb', line 11

def initialize(filename)
  @filename = filename
  open(@filename)
end

Instance Attribute Details

#filename ⇒ Object

Returns the value of attribute filename.



9
10
11
# File 'lib/raykit/git/repositories.rb', line 9

def filename
  @filename
end

Instance Method Details

#import(pattern) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/raykit/git/repositories.rb', line 50

def import(pattern)
  imported = []
  if is_remote_url(pattern)
    remote = pattern
    unless include?(remote)
      insert(-1, remote)
      imported.insert(-1, remote)
    end
  else
    git_dirs = []
    Dir.chdir(work_dir) do
      Dir.glob("**/.git") do |git_dir|
        dir = File.expand_path("..", git_dir)
        git_dirs.insert(0, dir) if dir.length.positive?
      end
    end
    Dir.chdir(Environment.home_dir) do
      Dir.glob("**/.git") do |git_dir|
        dir = File.expand_path("..", git_dir)
        git_dirs.insert(0, dir) if dir.length.positive?
      end
    end

    git_dirs.each do |git_dir|
      dir = Raykit::Git::Directory.new(git_dir)
      remote = dir.remote
      if remote.include?(pattern) && !include?(remote)
        insert(-1, remote)
        imported.insert(-1, remote)
      end
    end
  end
  save(@filename)
  imported
end

#is_remote_url(pattern) ⇒ Object



36
37
38
39
40
41
# File 'lib/raykit/git/repositories.rb', line 36

def is_remote_url(pattern)
  return true if pattern.include?("http://")
  return true if pattern.include?("https://")

  false
end

#matches(pattern) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/raykit/git/repositories.rb', line 86

def matches(pattern)
  matches = []
  REPOSITORIES.each do |url|
    matches << url if url.include?(pattern)
  end
  matches
end

#open(filename) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/raykit/git/repositories.rb', line 16

def open(filename)
  if File.exist?(filename)
    JSON.parse(File.read(filename)).each do |url|
      insert(-1, url)
    end
  else
    # puts "filename #{filename} does not exist"

  end
end

#remove(url) ⇒ Object



43
44
45
46
47
48
# File 'lib/raykit/git/repositories.rb', line 43

def remove(url)
  if include?(url)
    delete(url)
    save(@filename)
  end
end

#save(_filename) ⇒ Object



26
27
28
29
30
# File 'lib/raykit/git/repositories.rb', line 26

def save(_filename)
  File.open(@filename, "w") do |f|
    f.write(JSON.pretty_generate(self))
  end
end

#work_dir ⇒ Object



32
33
34
# File 'lib/raykit/git/repositories.rb', line 32

def work_dir
  Environment.get_dev_dir("work")
end