Class: SocialSnippet::Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_social_snippet) ⇒ Api

Constructor


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

def initialize(new_social_snippet)
  @social_snippet = new_social_snippet
end

Instance Attribute Details

#social_snippetObject (readonly)

Returns the value of attribute social_snippet


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

def social_snippet
  @social_snippet
end

Instance Method Details

#add_url(url, options = {}) ⇒ Object


172
173
174
175
# File 'lib/social_snippet/api.rb', line 172

def add_url(url, options = {})
  ret = social_snippet.registry_client.repositories.add_url(url)
  output ret
end

#cli_complete_snippet_path(keyword) ⇒ Object


154
155
156
157
158
# File 'lib/social_snippet/api.rb', line 154

def cli_complete_snippet_path(keyword)
  complete_snippet_path(keyword).each do |cand_repo|
    output cand_repo
  end
end

#complete_snippet_path(keyword) ⇒ Object


150
151
152
# File 'lib/social_snippet/api.rb', line 150

def complete_snippet_path(keyword)
  social_snippet.repo_manager.complete(keyword)
end

#insert_snippet(src) ⇒ Object

Insert snippets to given text


15
16
17
18
19
20
# File 'lib/social_snippet/api.rb', line 15

def insert_snippet(src)
  resolver = ::SocialSnippet::Resolvers::InsertResolver.new(social_snippet)
  res = resolver.insert(src)
  output res
  res
end

#install_repository(repo_name, repo_ref, repo, options = {}) ⇒ Object

Install repository


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/social_snippet/api.rb', line 73

def install_repository(repo_name, repo_ref, repo, options = {})
  display_name = repo_name

  if repo_ref.nil?
    repo_ref = resolve_reference(repo)

    if repo.has_versions?
      output "Resolving #{display_name}'s version"
    else
      output "No versions, use current reference"
    end
  end

  display_name = "#{repo_name}\##{repo_ref}"

  output "Installing: #{display_name}"
  unless options[:dry_run]
    social_snippet.repo_manager.install repo_name, repo_ref, repo, options
  end

  output "Success: #{display_name}"

  # install dependencies
  if has_dependencies?(repo)
    output "Finding #{display_name}'s dependencies"
    install_missing_dependencies repo.dependencies, options
    output "Finish finding #{display_name}'s dependencies"
  end
end

#install_repository_by_name(repo_name, repo_ref, options = {}) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/social_snippet/api.rb', line 22

def install_repository_by_name(repo_name, repo_ref, options = {})
  installed_as = repo_name
  installed_as = options[:name] unless options[:name].nil?

  unless installed_as === repo_name
    output "Installing as #{installed_as}"
  end

  if social_snippet.repo_manager.exists?(installed_as, repo_ref)
    output "#{installed_as} is already installed"
    return
  end

  output "Finding: #{repo_name}"
  info = social_snippet.registry_client.repositories.find(repo_name)
  output "Found at: #{info["url"]}"

  output "Cloning repository..."
  repo = ::SocialSnippet::Repository::RepositoryFactory.clone(info["url"])

  install_repository installed_as, repo_ref, repo
end

#install_repository_by_path(repo_path, repo_ref, options = {}) ⇒ Object


51
52
53
54
55
# File 'lib/social_snippet/api.rb', line 51

def install_repository_by_path(repo_path, repo_ref, options = {})
  output "Cloning repository..."
  repo = ::SocialSnippet::Repository::RepositoryFactory.clone_local(repo_path)
  install_repository_by_repo repo, repo_ref, options
end

#install_repository_by_repo(repo, repo_ref, options) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/social_snippet/api.rb', line 57

def install_repository_by_repo(repo, repo_ref, options)
  installed_as = repo.name
  installed_as = options[:name] unless options[:name].nil?
  output "Installing as #{installed_as}"

  if social_snippet.repo_manager.exists?(installed_as)
    output "#{installed_as} is already installed"
    return
  end

  install_repository installed_as, repo_ref, repo
end

#install_repository_by_url(repo_url, repo_ref, options = {}) ⇒ Object


45
46
47
48
49
# File 'lib/social_snippet/api.rb', line 45

def install_repository_by_url(repo_url, repo_ref, options = {})
  output "Cloning repository..."
  repo = ::SocialSnippet::Repository::RepositoryFactory.clone(repo_url)
  install_repository_by_repo repo, repo_ref, options
end

#search_repositories(query, options = {}) ⇒ Object


165
166
167
168
169
170
# File 'lib/social_snippet/api.rb', line 165

def search_repositories(query, options = {})
  format_text = search_result_format(options)
  social_snippet.registry_client.repositories.search(query).each do |repo|
    output format_text % search_result_list(repo, options)
  end
end

#show_info(repo_name) ⇒ Object


160
161
162
163
# File 'lib/social_snippet/api.rb', line 160

def show_info(repo_name)
  repo_info = social_snippet.registry_client.repositories.find(repo_name)
  output ::JSON.pretty_generate(repo_info)
end

#update_all_repositories(options = {}) ⇒ Object


144
145
146
147
148
# File 'lib/social_snippet/api.rb', line 144

def update_all_repositories(options = {})
  social_snippet.repo_manager.each_installed_repo do |repo_name|
    update_repository repo_name, options
  end
end

#update_repository(repo_name, options = {}) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/social_snippet/api.rb', line 103

def update_repository(repo_name, options = {})
  unless social_snippet.repo_manager.exists?(repo_name)
    output "ERROR: #{repo_name} is not installed"
    return
  end

  output "Fetching update for #{repo_name}"
  ret = social_snippet.repo_manager.fetch(repo_name, options)

  repo = social_snippet.repo_manager.find_repository(repo_name)
  display_name = repo_name

  # nothing to update
  if ret == 0 && social_snippet.repo_manager.exists?(repo_name, repo.latest_version)
    output "Everything up-to-date"
    return
  end

  output "Updating #{repo_name}"
  unless options[:dry_run]
    version = repo.latest_version
    if repo.has_versions? && repo.current_ref != version
      output "Bumping version into #{version}"
      display_name = "#{repo_name}\##{version}"
      repo.checkout version
      social_snippet.repo_manager.update repo_name, version, repo, options
    end
    social_snippet.repo_manager.cache_installing_repo repo
  end

  output "Success #{display_name}"

  # update deps
  if social_snippet.repo_manager.has_deps?(repo_name)
    output "Updating #{display_name}'s dependencies"
    deps = social_snippet.repo_manager.deps(repo_name)
    install_missing_dependencies deps, options
    output "Finish updating #{display_name}'s dependencies"
  end
end