social_snippet 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjdkNTQyNTZmNWMzYmFmOWM0MGU1NDhjOTRlNjIxMGNmNGNkYTU5YQ==
4
+ NDExZWFlYjUyMzUyOGQwYWFkNjg0YjRhZDg0ZDdmMzA1MTViMjIxMA==
5
5
  data.tar.gz: !binary |-
6
- YjQ3MDAwNWI3MDliOWFjNjY0YjA3MTdmODYyNmQ1MDk0NjEzNGY2NA==
6
+ OGMyMTAyZjdmODQ2NzJiNzM4YzAyMGJlZDRmMjQwZjEyYmIxMmJiZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTQxZGJmOGQwNDYzNjUwN2E3ZjM5ODI4NWFlNDMxNDFkZTJlYzBhMzkzZjkw
10
- MzE3NzBjMzk3MjYxMTgyZGQxMTRjMzFlN2Y1MjFhMjMyMTI0MmU4Yjg1NDJj
11
- YjY4NWYzZjY0NDM3NzE5YjViMmExNjY3YTg4MTI2ZmRkZjVkZjQ=
9
+ MTg1MWU1OGM5ODUxMjdhNTZlZDhhMmU5ZTMzYmM2N2ExMWNjNDNmYjM2MmM2
10
+ MTE2MzU0NzYyNGU4NWU1NDljNmRjNzE5MTI5NmQ3Njc2Y2NhZGRlNzNiNThj
11
+ YzRmNTA0ZDVhYmVlNmI4ZGZjMWI0MjNmYzE1ZjVhNjk0ZThmMWQ=
12
12
  data.tar.gz: !binary |-
13
- ZWMwZjM4NGQ2MWIwMmYzOGNjMTQ5NzVjNTNlZjg4NWUyNDJkMmEyYzEyMmIy
14
- NzE5M2FhOTkwNDFiODU3YTNlMmQ3NDBiNmRmMzMxZWIzYTNkODFkYTkzMDdj
15
- MTU4ZjRmNjAzZDc1OTY3MmViMmIxYTA5ZGRhZTQ5NWRlYmU2NGU=
13
+ YjY0NjczODE3NWUxZjRhNGFmMzliY2IyYTFmODJjM2I3OGU5MTJjZmVjNGUz
14
+ NTY4MTE1Y2ZlN2FlMGU1ZmNjMTZlMjc2ZGJkNGFjNjAyM2M3MjZkN2JjZGZm
15
+ MmY3MmFhYWM4ZGExNjA0YWZlYjI4ZDU0MDI0MGI1MDRjNGFlNjQ=
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /vendor/
11
+ .ruby-version
11
12
  .pryrc
12
13
  *.bundle
13
14
  *.so
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Social Snippet
2
2
 
3
- [![Gem Version](https://img.shields.io/gem/v/social_snippet.svg)](https://rubygems.org/gems/social_snippet)
4
- [![Build Status](https://img.shields.io/travis/social-snippet/social-snippet.svg)](https://travis-ci.org/social-snippet/social-snippet)
5
- [![Code Climate](https://img.shields.io/codeclimate/github/social-snippet/social-snippet.svg)](https://codeclimate.com/github/social-snippet/social-snippet)
6
- [![Test Coverage](https://img.shields.io/codeclimate/coverage/github/social-snippet/social-snippet.svg)](https://codeclimate.com/github/social-snippet/social-snippet)
7
- [![API Doc](http://img.shields.io/badge/RubyDocs-API-green.svg)](http://www.rubydoc.info/github/social-snippet/social-snippet)
3
+ [![Gem Version](https://img.shields.io/gem/v/social_snippet.svg?style=flat)](https://rubygems.org/gems/social_snippet)
4
+ [![Build Status](https://img.shields.io/travis/social-snippet/social-snippet.svg?style=flat)](https://travis-ci.org/social-snippet/social-snippet)
5
+ [![Code Climate](https://img.shields.io/codeclimate/github/social-snippet/social-snippet.svg?style=flat)](https://codeclimate.com/github/social-snippet/social-snippet)
6
+ [![Test Coverage](https://img.shields.io/codeclimate/coverage/github/social-snippet/social-snippet.svg?style=flat)](https://codeclimate.com/github/social-snippet/social-snippet)
7
+ [![API Doc](http://img.shields.io/badge/RubyDocs-API-green.svg?style=flat)](http://www.rubydoc.info/github/social-snippet/social-snippet)
8
8
 
9
9
  TODO: Write a gem description
10
10
 
@@ -0,0 +1,13 @@
1
+ module SocialSnippet::Api::CompletionApi
2
+
3
+ def complete_snippet_path(keyword)
4
+ social_snippet.repo_manager.complete(keyword)
5
+ end
6
+
7
+ def cli_complete_snippet_path(keyword)
8
+ complete_snippet_path(keyword).each do |cand_repo|
9
+ output cand_repo
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,14 @@
1
+ module SocialSnippet::Api::ConfigApi
2
+
3
+ # $ sspm config key=value
4
+ def config_set(key, value)
5
+ social_snippet.config.set! key, value
6
+ end
7
+
8
+ # $ sspm config key
9
+ def config_get(key)
10
+ value = social_snippet.config.get(key)
11
+ social_snippet.logger.say "#{key}=#{value}"
12
+ end
13
+
14
+ end
@@ -0,0 +1,15 @@
1
+ module SocialSnippet::Api::InsertSnippetApi
2
+
3
+ # Insert snippets to given text.
4
+ # $ ssnip
5
+ #
6
+ # @param src [String] The text of source code
7
+ #
8
+ def insert_snippet(src, options = {})
9
+ resolver = ::SocialSnippet::Resolvers::InsertResolver.new(social_snippet, options)
10
+ res = resolver.insert(src)
11
+ output res
12
+ res
13
+ end
14
+
15
+ end
@@ -0,0 +1,109 @@
1
+ module SocialSnippet::Api::InstallRepositoryApi
2
+
3
+ # Install repository (Core API)
4
+ #
5
+ # @param repo [::SocialSnippet::Repository::Drivers::BaseRepository]
6
+ def install_repository(repo_name, repo_ref, repo, options = {})
7
+ display_name = repo_name
8
+
9
+ if repo_ref.nil?
10
+ repo_ref = resolve_reference(repo)
11
+
12
+ if repo.has_versions?
13
+ output "Resolving #{display_name}'s version"
14
+ else
15
+ output "No versions, use current reference"
16
+ end
17
+ end
18
+
19
+ display_name = "#{repo_name}\##{repo_ref}"
20
+
21
+ output "Installing: #{display_name}"
22
+ unless options[:dry_run]
23
+ social_snippet.repo_manager.install repo_name, repo_ref, repo, options
24
+ end
25
+
26
+ output "Success: #{display_name}"
27
+
28
+ # install dependencies
29
+ if has_dependencies?(repo)
30
+ output "Finding #{display_name}'s dependencies"
31
+ install_missing_dependencies repo.dependencies, options
32
+ output "Finish finding #{display_name}'s dependencies"
33
+ end
34
+ end
35
+
36
+ # $ sspm install name
37
+ def install_repository_by_name(repo_name, repo_ref, options = {})
38
+ installed_as = repo_name
39
+ installed_as = options[:name] unless options[:name].nil?
40
+
41
+ unless installed_as === repo_name
42
+ output "Installing as #{installed_as}"
43
+ end
44
+
45
+ if social_snippet.repo_manager.exists?(installed_as, repo_ref)
46
+ output "#{installed_as} is already installed"
47
+ return
48
+ end
49
+
50
+ output "Finding: #{repo_name}"
51
+ info = social_snippet.registry_client.repositories.find(repo_name)
52
+ output "Found at: #{info["url"]}"
53
+
54
+ output "Cloning repository..."
55
+ repo = ::SocialSnippet::Repository::RepositoryFactory.clone(info["url"])
56
+
57
+ install_repository installed_as, repo_ref, repo
58
+ end
59
+
60
+ # $ sspm install URL
61
+ def install_repository_by_url(repo_url, repo_ref, options = {})
62
+ output "Cloning repository..."
63
+ repo = ::SocialSnippet::Repository::RepositoryFactory.clone(repo_url)
64
+ install_repository_by_repo repo, repo_ref, options
65
+ end
66
+
67
+ # $ sspm install ./path/to/repo
68
+ def install_repository_by_path(repo_path, repo_ref, options = {})
69
+ output "Cloning repository..."
70
+ repo = ::SocialSnippet::Repository::RepositoryFactory.clone_local(repo_path)
71
+ install_repository_by_repo repo, repo_ref, options
72
+ end
73
+
74
+ def install_repository_by_repo(repo, repo_ref, options)
75
+ installed_as = repo.name
76
+ installed_as = options[:name] unless options[:name].nil?
77
+ output "Installing as #{installed_as}"
78
+
79
+ if social_snippet.repo_manager.exists?(installed_as)
80
+ output "#{installed_as} is already installed"
81
+ return
82
+ end
83
+
84
+ install_repository installed_as, repo_ref, repo
85
+ end
86
+
87
+ def install_missing_dependencies(repo_deps, options = {})
88
+ repo_deps.each do |dep_repo_name, dep_repo_ref|
89
+ unless social_snippet.repo_manager.exists?(dep_repo_name, dep_repo_ref)
90
+ install_repository_by_name dep_repo_name, dep_repo_ref, options
91
+ end
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def resolve_reference(repo)
98
+ if repo.has_versions?
99
+ repo.latest_version
100
+ else
101
+ repo.current_ref
102
+ end
103
+ end
104
+
105
+ def has_dependencies?(repo)
106
+ repo.dependencies && ( not repo.dependencies.empty? )
107
+ end
108
+
109
+ end
@@ -0,0 +1,83 @@
1
+ module SocialSnippet::Api::ManifestApi
2
+
3
+ require "json"
4
+
5
+ # Initialize the snippet.json interactively.
6
+ # $ sspm init
7
+ def init_manifest(options = {})
8
+ current_answers = load_manifest_file || {}
9
+ answer = loop_manifest_questions(current_answers)
10
+ ::File.write "snippet.json", ::JSON.pretty_generate(answer)
11
+ answer
12
+ end
13
+
14
+ private
15
+
16
+ def loop_manifest_questions(answer)
17
+ loop do
18
+ answer = ask_manifest_questions(manifest_questions(answer), answer)
19
+ social_snippet.logger.say ""
20
+ social_snippet.logger.say ::JSON.pretty_generate(answer)
21
+ social_snippet.logger.say ""
22
+ break if ask_confirm("Is this okay? [Y/N]: ")
23
+ end
24
+ answer
25
+ end
26
+
27
+ # load current configuration
28
+ def load_manifest_file
29
+ if ::File.exists?("snippet.json")
30
+ ::JSON.parse(::File.read "snippet.json")
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ def ask_confirm(message)
37
+ ret = social_snippet.prompt.ask(message) do |q|
38
+ q.limit = 1
39
+ q.validate = /^[yn]$/i
40
+ end
41
+ /y/i === ret
42
+ end
43
+
44
+ def ask_manifest_questions(questions, obj)
45
+ questions.inject(obj) do |obj, q|
46
+ obj[q[:key]] = ask_manifest_question(q)
47
+ obj
48
+ end
49
+ end
50
+
51
+ def ask_manifest_question(question)
52
+ if question[:type] === :string
53
+ social_snippet.prompt.ask("#{question[:key]}: ") do |q|
54
+ q.default = question[:default]
55
+ if question[:validate].is_a?(Regexp)
56
+ q.validate = question[:validate]
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def manifest_questions(answer)
63
+ [
64
+ {
65
+ :key => "name",
66
+ :type => :string,
67
+ :validate => /[a-zA-Z0-9\.\-_]+/,
68
+ :default => answer["name"],
69
+ },
70
+ {
71
+ :key => "description",
72
+ :type => :string,
73
+ :default => answer["description"],
74
+ },
75
+ {
76
+ :key => "license",
77
+ :default => answer["license"] || "MIT",
78
+ :type => :string,
79
+ },
80
+ ]
81
+ end
82
+
83
+ end
@@ -0,0 +1,8 @@
1
+ module SocialSnippet::Api::RegistryApi
2
+
3
+ def add_url(url, options = {})
4
+ ret = social_snippet.registry_client.repositories.add_url(url)
5
+ output ret
6
+ end
7
+
8
+ end
@@ -0,0 +1,44 @@
1
+ module SocialSnippet::Api::SearchApi
2
+
3
+ # $ sspm search query
4
+ def search_repositories(query, options = {})
5
+ format_text = search_result_format(options)
6
+ social_snippet.registry_client.repositories.search(query).each do |repo|
7
+ output format_text % search_result_list(repo, options)
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def search_result_list(repo, options)
14
+ list = []
15
+ list.push repo["name"] if options[:name]
16
+ list.push "\"#{repo["desc"]}\"" if options[:desc]
17
+ list.push repo["url"] if options[:url]
18
+ list.push search_result_installed(repo) if options[:installed]
19
+ return list
20
+ end
21
+
22
+ def search_result_installed(repo)
23
+ if social_snippet.repo_manager.exists?(repo["name"])
24
+ "#installed"
25
+ else
26
+ ""
27
+ end
28
+ end
29
+
30
+ def search_result_format(options)
31
+ keys = [ # TODO: improve (change order by option, etc...)
32
+ :name,
33
+ :desc,
34
+ :url,
35
+ :installed,
36
+ ]
37
+
38
+ list = []
39
+ keys.each {|key| list.push "%s" if options[key] }
40
+
41
+ return list.join(" ")
42
+ end
43
+
44
+ end
@@ -0,0 +1,10 @@
1
+ module SocialSnippet::Api::ShowApi
2
+
3
+ require "json"
4
+
5
+ def show_info(repo_name)
6
+ repo_info = social_snippet.registry_client.repositories.find(repo_name)
7
+ output ::JSON.pretty_generate(repo_info)
8
+ end
9
+
10
+ end
@@ -0,0 +1,54 @@
1
+ module SocialSnippet::Api::UpdateRepositoryApi
2
+
3
+ # Update a repository
4
+ # $ sspm update repo-name
5
+ def update_repository(repo_name, options = {})
6
+ unless social_snippet.repo_manager.exists?(repo_name)
7
+ output "ERROR: #{repo_name} is not installed"
8
+ return
9
+ end
10
+
11
+ output "Fetching update for #{repo_name}"
12
+ ret = social_snippet.repo_manager.fetch(repo_name, options)
13
+
14
+ repo = social_snippet.repo_manager.find_repository(repo_name)
15
+ display_name = repo_name
16
+
17
+ # nothing to update
18
+ if ret == 0 && social_snippet.repo_manager.exists?(repo_name, repo.latest_version)
19
+ output "Everything up-to-date"
20
+ return
21
+ end
22
+
23
+ output "Updating #{repo_name}"
24
+ unless options[:dry_run]
25
+ version = repo.latest_version
26
+ if repo.has_versions? && repo.current_ref != version
27
+ output "Bumping version into #{version}"
28
+ display_name = "#{repo_name}\##{version}"
29
+ repo.checkout version
30
+ social_snippet.repo_manager.update repo_name, version, repo, options
31
+ end
32
+ social_snippet.repo_manager.cache_installing_repo repo
33
+ end
34
+
35
+ output "Success #{display_name}"
36
+
37
+ # update deps
38
+ if social_snippet.repo_manager.has_deps?(repo_name)
39
+ output "Updating #{display_name}'s dependencies"
40
+ deps = social_snippet.repo_manager.deps(repo_name)
41
+ install_missing_dependencies deps, options
42
+ output "Finish updating #{display_name}'s dependencies"
43
+ end
44
+ end
45
+
46
+ # Update all installed repositories
47
+ # $ sspm update
48
+ def update_all_repositories(options = {})
49
+ social_snippet.repo_manager.each_installed_repo do |repo_name|
50
+ update_repository repo_name, options
51
+ end
52
+ end
53
+
54
+ end