Class: Gill::Import

Inherits:
Object
  • Object
show all
Includes:
Helper, Settings
Defined in:
lib/gill/import.rb

Instance Method Summary collapse

Methods included from Helper

#ask, #basename, #blue, #folder_empty?, #green, #parent, #red

Constructor Details

#initializeImport

Returns a new instance of Import.



10
11
12
13
# File 'lib/gill/import.rb', line 10

def initialize
  @updated_cache = Gill.config.cache
  STDOUT.puts "Please wait..."
end

Instance Method Details

#add(category, repo_name, git_uri, path) ⇒ Object

Add Repository

Parameters:

  • category (String)

    Repository Category

  • repo_name (String)

    Repository Name

  • git_uri (String)

    Repository URI

  • path (String)

    Repository Path



144
145
146
147
148
149
150
# File 'lib/gill/import.rb', line 144

def add(category,repo_name,git_uri,path)
  if @updated_cache[:categories].has_key?(category.to_s.downcase.to_sym)
    @updated_cache[:categories][category.to_s.downcase.to_sym].merge!({repo_name.to_sym => {:repo => git_uri, :path => path}})
  else
    @updated_cache[:categories][category.to_s.downcase.to_sym] = { repo_name.to_sym => { :repo => git_uri, :path => path }}
  end
end

#check_move_import(basename, repo_name, path) ⇒ Object

Check, Move & import

Parameters:

  • basename (String)

    Basename Of The Repository Path

  • repo_name (String)

    Repository Name

  • path (Object)

    Pathname Parent Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gill/import.rb', line 75

def check_move_import(basename,repo_name,path)
  git_uri = git_uri_for(path)

  if repo_in_root(basename)
    prompt = "Do you want to move #{basename(Gill.config.source_path)}/#{basename(repo_name)} to",
    "#{basename(Gill.config.source_path)}/#{basename(Gill.config.default_path)}/#{basename(repo_name)}?"
    
    ask prompt, :default => false do
      move_root_repos_to_default_path(path,File.join(Gill.config.source_path, basename(Gill.config.default_path), repo_name))
      add(basename(Gill.config.default_path),repo_name,git_uri,
      File.join(Gill.config.source_path,
      basename(Gill.config.default_path), 
      repo_name))
    end
    
  else
    add(basename,repo_name,git_uri,path)
  end

end

#find_repo(path, repo_name, parent) ⇒ Object

Find Repo

Parameters:

  • path (String)

    Folder Path

  • repo_name (String)

    Repository Name

  • parent (Object)

    Pathname Parent Pbject



40
41
42
43
44
45
46
47
48
# File 'lib/gill/import.rb', line 40

def find_repo(path,repo_name,parent)

  if has_git_folder?(path)
    check_move_import(basename(parent.path), repo_name, path) if has_git_config?(path)
  else
    import(path)
  end

end

#git_uri_for(path) ⇒ String

Git URI For Path

Parameters:

  • path (String)

    Repository Path

Returns:

  • (String)

    Return the git uri for a path.



125
126
127
128
129
130
131
# File 'lib/gill/import.rb', line 125

def git_uri_for(path)
  File.open(File.join(path, '.git', 'config'), 'r') do |file|
    @git_uri = file.read[/url\s+\=\s+(\S+)/m,1]
    file.close
  end
  @git_uri
end

#has_git_config?(path) ⇒ true, false

Has Git?

Parameters:

  • path (String)

    Folder Path

Returns:

  • (true, false)

    Retrun true if the .git folder is found.



103
104
105
106
# File 'lib/gill/import.rb', line 103

def has_git_config?(path)
  return true if File.exist?(File.join(path, '.git', 'config'))
  false
end

#has_git_folder?(path) ⇒ true, false

Has Git Folder

path contains a .git folder.

Parameters:

  • path (String)

    Folder Path

Returns:

  • (true, false)

    Return true if the folder



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gill/import.rb', line 178

def has_git_folder?(path)
  temp = Dir.new(path)

  if temp.entries.include?('.git')
    temp.close
    return true
  else
    return false
  end

end

#import(path = nil) ⇒ Object

Import

Parameters:

  • path (String) (defaults to: nil)

    Override Default Import Path



20
21
22
23
24
25
26
27
28
29
# File 'lib/gill/import.rb', line 20

def import(path=nil)

  if path.nil?
    folder = Gill.config.source_path
  else
    folder = path
  end

  search_folder(folder)
end

#move_root_repos_to_default_path(source, destination) ⇒ Object

Move Repository

Parameters:

  • source (String)

    Repository Source Path

  • destination (String)

    Repository Destination Path



114
115
116
# File 'lib/gill/import.rb', line 114

def move_root_repos_to_default_path(source,destination)
  FileUtils.mv(source,destination)
end

#repo_in_root(folder) ⇒ true, false

Repository In Root

is in the root dir.

Parameters:

  • folder (String)

    Folder Path

Returns:

  • (true, false)

    Return true if the Repository



165
166
167
168
# File 'lib/gill/import.rb', line 165

def repo_in_root(folder)
  return true if folder.to_s == basename(Gill.config.source_path).to_s
  false
end

#search_folder(folder) ⇒ Object

Search Folder

Parameters:

  • folder (String)

    Folder Path



55
56
57
58
59
60
61
62
63
64
# File 'lib/gill/import.rb', line 55

def search_folder(folder)
  current_folder = Dir.new(folder)
  
  current_folder.entries.each do |file|
    next if %w(. ..).include?(file)
    path = File.join(current_folder.path, file)
    find_repo(path,file,current_folder) if File.directory?(path)
  end

end

#update_cacheObject



152
153
154
155
# File 'lib/gill/import.rb', line 152

def update_cache
  STDOUT.puts green("Import Completed Successfully.")
  Gill.config.write_cache(@updated_cache)
end