Class: SocialSnippet::Repository::RepositoryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/social_snippet/repository/repository_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_social_snippet) ⇒ RepositoryManager

Constructor

Parameters:


13
14
15
16
17
18
19
20
21
# File 'lib/social_snippet/repository/repository_manager.rb', line 13

def initialize(new_social_snippet)
  @social_snippet = new_social_snippet
  @installer = ::SocialSnippet::Repository::RepositoryInstaller.new(social_snippet)
  @repo_cache_path = social_snippet.config.repository_cache_path
  @repo_paths = []

  init_repo_paths
  init_repo_cache_path
end

Instance Attribute Details

#installerObject (readonly)

Returns the value of attribute installer


5
6
7
# File 'lib/social_snippet/repository/repository_manager.rb', line 5

def installer
  @installer
end

#repo_cache_pathObject (readonly)

Returns the value of attribute repo_cache_path


7
8
9
# File 'lib/social_snippet/repository/repository_manager.rb', line 7

def repo_cache_path
  @repo_cache_path
end

#repo_pathsObject (readonly)

Returns the value of attribute repo_paths


6
7
8
# File 'lib/social_snippet/repository/repository_manager.rb', line 6

def repo_paths
  @repo_paths
end

#social_snippetObject (readonly)

Returns the value of attribute social_snippet


8
9
10
# File 'lib/social_snippet/repository/repository_manager.rb', line 8

def social_snippet
  @social_snippet
end

Instance Method Details

#cache_installing_repo(repo, options = {}) ⇒ Object


149
150
151
# File 'lib/social_snippet/repository/repository_manager.rb', line 149

def cache_installing_repo(repo, options = {})
  repo.create_cache repo_cache_path
end

#complete(keyword) ⇒ Object


119
120
121
122
123
124
125
126
127
# File 'lib/social_snippet/repository/repository_manager.rb', line 119

def complete(keyword)
  if is_completing_repo_name?(keyword)
    complete_repo_name(keyword)
  elsif is_completing_file_path?(keyword)
    complete_file_name(keyword)
  else
    raise "completion error"
  end
end

#complete_file_name(keyword) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/social_snippet/repository/repository_manager.rb', line 104

def complete_file_name(keyword)
  repo_name   = get_repo_name(keyword)
  repo        = find_repository(repo_name)
  file_path   = get_file_path_prefix(keyword)
  glob_path   = "#{file_path}*"

  repo.glob(glob_path).map do |cand_file_path|
    if ::File.directory?(cand_file_path)
      Pathname(cand_file_path).basename.to_s + "/"
    else
      Pathname(cand_file_path).basename.to_s + ">"
    end
  end
end

#complete_repo_name(keyword) ⇒ Object


99
100
101
102
# File 'lib/social_snippet/repository/repository_manager.rb', line 99

def complete_repo_name(keyword)
  repo_name = get_repo_name_prefix(keyword)
  find_repositories_start_with(repo_name)
end

#deps(repo_name, repo_ref = nil) ⇒ Object


32
33
34
35
36
# File 'lib/social_snippet/repository/repository_manager.rb', line 32

def deps(repo_name, repo_ref = nil)
  repo = find_repository(repo_name)
  repo.checkout(repo_ref) unless repo_ref.nil?
  repo.dependencies
end

#each_installed_repo(&block) ⇒ Object


171
172
173
# File 'lib/social_snippet/repository/repository_manager.rb', line 171

def each_installed_repo(&block)
  installer.each &block
end

#exists?(repo_name, repo_ref = nil) ⇒ Boolean

Returns:

  • (Boolean)

167
168
169
# File 'lib/social_snippet/repository/repository_manager.rb', line 167

def exists?(repo_name, repo_ref = nil)
  installer.exists? repo_name, repo_ref
end

#fetch(repo_name, options) ⇒ Object


153
154
155
# File 'lib/social_snippet/repository/repository_manager.rb', line 153

def fetch(repo_name, options)
  installer.fetch repo_name, options
end

#find_repositories_start_with(prefix) ⇒ Object


92
93
94
95
96
97
# File 'lib/social_snippet/repository/repository_manager.rb', line 92

def find_repositories_start_with(prefix)
  glob_path = ::File.join(installer.path, "#{prefix}*")
  ::Dir.glob(glob_path).map do |repopath|
    Pathname(repopath).basename.to_s
  end
end

#find_repository(name, ref = nil) ⇒ Object

Find repository by repo name

Parameters:

  • name (String)

    The name of repository


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/social_snippet/repository/repository_manager.rb', line 79

def find_repository(name, ref = nil)
  repo_paths.each do |repo_path|
    path = "#{repo_path}/#{name}"
    if ::Dir.exists?(path)
      repo = RepositoryFactory.create(path, ref)
      repo.load_cache repo_cache_path
      return repo
    end
  end

  return nil
end

#find_repository_by_tag(tag) ⇒ Object

Find repository by tag

Parameters:


68
69
70
71
72
73
74
# File 'lib/social_snippet/repository/repository_manager.rb', line 68

def find_repository_by_tag(tag)
  if tag.has_ref?
    find_repository(tag.repo, tag.ref)
  else
    find_repository(tag.repo)
  end
end

#get_file_path_prefix(keyword) ⇒ Object


137
138
139
# File 'lib/social_snippet/repository/repository_manager.rb', line 137

def get_file_path_prefix(keyword)
  /^[^@]*@[^<]+<[^#:]+:([^>]*)$/.match(keyword)[1]
end

#get_repo_name(keyword) ⇒ Object


129
130
131
# File 'lib/social_snippet/repository/repository_manager.rb', line 129

def get_repo_name(keyword)
  /^[^@]*@[^<]+<([^:#>]*[^:#>])/.match(keyword)[1]
end

#get_repo_name_prefix(keyword) ⇒ Object


133
134
135
# File 'lib/social_snippet/repository/repository_manager.rb', line 133

def get_repo_name_prefix(keyword)
  /^[^@]*@[^<]+<([^:#>]*[^:#>])$/.match(keyword)[1]
end

#get_snippet(context, tag) ⇒ Object

Get snippet

Parameters:


46
47
48
# File 'lib/social_snippet/repository/repository_manager.rb', line 46

def get_snippet(context, tag)
  return ::SocialSnippet::Snippet.new(resolve_snippet_path(context, tag))
end

#has_deps?(repo_name, repo_ref = nil) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/social_snippet/repository/repository_manager.rb', line 38

def has_deps?(repo_name, repo_ref = nil)
  not deps(repo_name, repo_ref).empty?
end

#init_repo_cache_pathObject


23
24
25
# File 'lib/social_snippet/repository/repository_manager.rb', line 23

def init_repo_cache_path
  ::FileUtils.mkdir_p repo_cache_path
end

#init_repo_pathsObject


27
28
29
30
# File 'lib/social_snippet/repository/repository_manager.rb', line 27

def init_repo_paths
  repo_paths.push installer.path
  repo_paths.each {|path| ::FileUtils.mkdir_p path }
end

#install(repo_name, repo_ref, repo, options) ⇒ Object


162
163
164
165
# File 'lib/social_snippet/repository/repository_manager.rb', line 162

def install(repo_name, repo_ref, repo, options)
  installer.copy_repository repo, options
  update repo_name, repo_ref, repo, options
end

#is_completing_file_path?(keyword) ⇒ Boolean

Returns:

  • (Boolean)

145
146
147
# File 'lib/social_snippet/repository/repository_manager.rb', line 145

def is_completing_file_path?(keyword)
  /^[^@]*@[^<]+<[^#:]+:[^>]*$/ === keyword
end

#is_completing_repo_name?(keyword) ⇒ Boolean

Returns:

  • (Boolean)

141
142
143
# File 'lib/social_snippet/repository/repository_manager.rb', line 141

def is_completing_repo_name?(keyword)
  /^[^@]*@[^<]+<[^:#>]*$/ === keyword
end

#resolve_snippet_path(context, tag) ⇒ Object

Resolve snippet path from tag

Parameters:


54
55
56
57
58
59
60
61
62
63
# File 'lib/social_snippet/repository/repository_manager.rb', line 54

def resolve_snippet_path(context, tag)
  if tag.has_repo?
    repo = find_repository_by_tag(tag)
    return repo.real_path tag.path
  end

  new_context = context.clone
  new_context.move tag.path
  return new_context.path
end

#update(repo_name, repo_ref, repo, options) ⇒ Object


157
158
159
160
# File 'lib/social_snippet/repository/repository_manager.rb', line 157

def update(repo_name, repo_ref, repo, options)
  cache_installing_repo repo, options
  installer.add repo_name, repo_ref
end