social_snippet 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +3 -0
- data/Gemfile +1 -26
- data/README.md +1 -0
- data/lib/social_snippet.rb +1 -0
- data/lib/social_snippet/api.rb +120 -36
- data/lib/social_snippet/command_line/sspm/main_command.rb +1 -1
- data/lib/social_snippet/command_line/sspm/sub_commands.rb +1 -0
- data/lib/social_snippet/command_line/sspm/sub_commands/init_command.rb +27 -0
- data/lib/social_snippet/core.rb +3 -10
- data/lib/social_snippet/inserter.rb +1 -1
- data/lib/social_snippet/repository/repository_manager.rb +1 -1
- data/lib/social_snippet/resolvers/dep_resolver.rb +1 -1
- data/lib/social_snippet/resolvers/insert_resolver.rb +18 -6
- data/lib/social_snippet/snippet.rb +2 -2
- data/lib/social_snippet/tag_parser.rb +2 -2
- data/lib/social_snippet/tsortable_hash.rb +18 -0
- data/lib/social_snippet/version.rb +1 -1
- data/social_snippet.gemspec +10 -6
- data/spec/lib/command_line/sspm_init_spec.rb +21 -0
- data/spec/lib/core_spec.rb +6 -6
- data/spec/lib/insert_resolver_spec.rb +131 -0
- data/spec/lib/inserter_spec.rb +7 -7
- data/spec/lib/repository/base_repository_spec.rb +2 -2
- data/spec/lib/repository/repository_manager_spec.rb +2 -2
- data/test/base_repository_test.rb +4 -4
- data/test/core_test.rb +163 -163
- data/test/tag_parser_test.rb +2 -2
- metadata +33 -41
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjdkNTQyNTZmNWMzYmFmOWM0MGU1NDhjOTRlNjIxMGNmNGNkYTU5YQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YjQ3MDAwNWI3MDliOWFjNjY0YjA3MTdmODYyNmQ1MDk0NjEzNGY2NA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YTQxZGJmOGQwNDYzNjUwN2E3ZjM5ODI4NWFlNDMxNDFkZTJlYzBhMzkzZjkw
|
10
|
+
MzE3NzBjMzk3MjYxMTgyZGQxMTRjMzFlN2Y1MjFhMjMyMTI0MmU4Yjg1NDJj
|
11
|
+
YjY4NWYzZjY0NDM3NzE5YjViMmExNjY3YTg4MTI2ZmRkZjVkZjQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZWMwZjM4NGQ2MWIwMmYzOGNjMTQ5NzVjNTNlZjg4NWUyNDJkMmEyYzEyMmIy
|
14
|
+
NzE5M2FhOTkwNDFiODU3YTNlMmQ3NDBiNmRmMzMxZWIzYTNkODFkYTkzMDdj
|
15
|
+
MTU4ZjRmNjAzZDc1OTY3MmViMmIxYTA5ZGRhZTQ5NWRlYmU2NGU=
|
data/.travis.yml
CHANGED
@@ -23,6 +23,9 @@ deploy:
|
|
23
23
|
secure: "hlC3/Vsqpw3RdA0g8nAuusXEpGiwLtndGQVd8eg8x7yCoubFmsruwJG2Cci4QF2aIVGovFzrrmlxPb204nndsmBCzTLZEnn2dsCqOt8TZDAyFRZf6xeZ53eT7AjOJvFIafS4YLQ975upzsQ/cW4OgWFRaqhHgckTUSapa7Dyeao="
|
24
24
|
gem:
|
25
25
|
master: social_snippet
|
26
|
+
on:
|
27
|
+
repo: social-snippet/social-snippet
|
28
|
+
ruby: "2.2"
|
26
29
|
|
27
30
|
cache:
|
28
31
|
bundler: true
|
data/Gemfile
CHANGED
@@ -1,27 +1,2 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gem "bundler"
|
4
|
-
gem "rake"
|
5
|
-
gem "version_sorter"
|
6
|
-
gem "rugged"
|
7
|
-
gem "rest-client"
|
8
|
-
|
9
|
-
group :development do
|
10
|
-
gem "yard", :require => false
|
11
|
-
gem "pry", :require => false
|
12
|
-
gem "guard", :require => false
|
13
|
-
gem "guard-shell", :require => false
|
14
|
-
end
|
15
|
-
|
16
|
-
group :test do
|
17
|
-
gem "rspec", :require => false
|
18
|
-
gem "fakefs", :require => false
|
19
|
-
gem "webmock", :require => false
|
20
|
-
gem "simplecov", :require => false
|
21
|
-
gem "codeclimate-test-reporter", :require => false
|
22
|
-
end
|
23
|
-
|
24
|
-
# should be disabled on travis-ci
|
25
|
-
group :debug do
|
26
|
-
gem "pry-byebug", :require => false
|
27
|
-
end
|
2
|
+
gemspec
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
[![Build Status](https://img.shields.io/travis/social-snippet/social-snippet.svg)](https://travis-ci.org/social-snippet/social-snippet)
|
5
5
|
[![Code Climate](https://img.shields.io/codeclimate/github/social-snippet/social-snippet.svg)](https://codeclimate.com/github/social-snippet/social-snippet)
|
6
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)
|
7
8
|
|
8
9
|
TODO: Write a gem description
|
9
10
|
|
data/lib/social_snippet.rb
CHANGED
data/lib/social_snippet/api.rb
CHANGED
@@ -9,25 +9,55 @@ class SocialSnippet::Api
|
|
9
9
|
@social_snippet = new_social_snippet
|
10
10
|
end
|
11
11
|
|
12
|
+
# $ sspm config key=value
|
12
13
|
def config_set(key, value)
|
13
14
|
social_snippet.config.set! key, value
|
14
15
|
end
|
15
16
|
|
17
|
+
# $ sspm config key
|
16
18
|
def config_get(key)
|
17
19
|
value = social_snippet.config.get(key)
|
18
20
|
social_snippet.logger.say "#{key}=#{value}"
|
19
21
|
end
|
20
22
|
|
21
|
-
#
|
23
|
+
# Initialize the snippet.json interactively.
|
24
|
+
# $ sspm init
|
25
|
+
def init_manifest(options = {})
|
26
|
+
answer = {}
|
27
|
+
json_str = "{}"
|
28
|
+
|
29
|
+
# load current configuration
|
30
|
+
if ::File.exists?("snippet.json")
|
31
|
+
answer = ::JSON.parse(::File.read "snippet.json")
|
32
|
+
end
|
33
|
+
|
34
|
+
loop do
|
35
|
+
answer = ask_manifest_questions(manifest_questions(answer), answer)
|
36
|
+
json_str = ::JSON.pretty_generate(answer)
|
37
|
+
social_snippet.logger.say ""
|
38
|
+
social_snippet.logger.say json_str
|
39
|
+
social_snippet.logger.say ""
|
40
|
+
break if ask_confirm("Is this okay? [Y/N]: ")
|
41
|
+
end
|
42
|
+
|
43
|
+
::File.write "snippet.json", json_str
|
44
|
+
|
45
|
+
answer
|
46
|
+
end
|
47
|
+
|
48
|
+
# Insert snippets to given text.
|
49
|
+
# $ ssnip
|
22
50
|
#
|
23
51
|
# @param src [String] The text of source code
|
24
|
-
|
25
|
-
|
52
|
+
#
|
53
|
+
def insert_snippet(src, options = {})
|
54
|
+
resolver = ::SocialSnippet::Resolvers::InsertResolver.new(social_snippet, options)
|
26
55
|
res = resolver.insert(src)
|
27
56
|
output res
|
28
57
|
res
|
29
58
|
end
|
30
59
|
|
60
|
+
# $ sspm install name
|
31
61
|
def install_repository_by_name(repo_name, repo_ref, options = {})
|
32
62
|
installed_as = repo_name
|
33
63
|
installed_as = options[:name] unless options[:name].nil?
|
@@ -51,12 +81,14 @@ class SocialSnippet::Api
|
|
51
81
|
install_repository installed_as, repo_ref, repo
|
52
82
|
end
|
53
83
|
|
84
|
+
# $ sspm install URL
|
54
85
|
def install_repository_by_url(repo_url, repo_ref, options = {})
|
55
86
|
output "Cloning repository..."
|
56
87
|
repo = ::SocialSnippet::Repository::RepositoryFactory.clone(repo_url)
|
57
88
|
install_repository_by_repo repo, repo_ref, options
|
58
89
|
end
|
59
90
|
|
91
|
+
# $ sspm install ./path/to/repo
|
60
92
|
def install_repository_by_path(repo_path, repo_ref, options = {})
|
61
93
|
output "Cloning repository..."
|
62
94
|
repo = ::SocialSnippet::Repository::RepositoryFactory.clone_local(repo_path)
|
@@ -76,39 +108,8 @@ class SocialSnippet::Api
|
|
76
108
|
install_repository installed_as, repo_ref, repo
|
77
109
|
end
|
78
110
|
|
79
|
-
#
|
80
|
-
#
|
81
|
-
# @param repo [::SocialSnippet::Repository::Drivers::BaseRepository]
|
82
|
-
def install_repository(repo_name, repo_ref, repo, options = {})
|
83
|
-
display_name = repo_name
|
84
|
-
|
85
|
-
if repo_ref.nil?
|
86
|
-
repo_ref = resolve_reference(repo)
|
87
|
-
|
88
|
-
if repo.has_versions?
|
89
|
-
output "Resolving #{display_name}'s version"
|
90
|
-
else
|
91
|
-
output "No versions, use current reference"
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
display_name = "#{repo_name}\##{repo_ref}"
|
96
|
-
|
97
|
-
output "Installing: #{display_name}"
|
98
|
-
unless options[:dry_run]
|
99
|
-
social_snippet.repo_manager.install repo_name, repo_ref, repo, options
|
100
|
-
end
|
101
|
-
|
102
|
-
output "Success: #{display_name}"
|
103
|
-
|
104
|
-
# install dependencies
|
105
|
-
if has_dependencies?(repo)
|
106
|
-
output "Finding #{display_name}'s dependencies"
|
107
|
-
install_missing_dependencies repo.dependencies, options
|
108
|
-
output "Finish finding #{display_name}'s dependencies"
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
111
|
+
# Update a repository
|
112
|
+
# $ sspm update repo-name
|
112
113
|
def update_repository(repo_name, options = {})
|
113
114
|
unless social_snippet.repo_manager.exists?(repo_name)
|
114
115
|
output "ERROR: #{repo_name} is not installed"
|
@@ -150,6 +151,8 @@ class SocialSnippet::Api
|
|
150
151
|
end
|
151
152
|
end
|
152
153
|
|
154
|
+
# Update all installed repositories
|
155
|
+
# $ sspm update
|
153
156
|
def update_all_repositories(options = {})
|
154
157
|
social_snippet.repo_manager.each_installed_repo do |repo_name|
|
155
158
|
update_repository repo_name, options
|
@@ -171,6 +174,7 @@ class SocialSnippet::Api
|
|
171
174
|
output ::JSON.pretty_generate(repo_info)
|
172
175
|
end
|
173
176
|
|
177
|
+
# $ sspm search query
|
174
178
|
def search_repositories(query, options = {})
|
175
179
|
format_text = search_result_format(options)
|
176
180
|
social_snippet.registry_client.repositories.search(query).each do |repo|
|
@@ -189,6 +193,86 @@ class SocialSnippet::Api
|
|
189
193
|
|
190
194
|
private
|
191
195
|
|
196
|
+
# Install repository
|
197
|
+
#
|
198
|
+
# @param repo [::SocialSnippet::Repository::Drivers::BaseRepository]
|
199
|
+
def install_repository(repo_name, repo_ref, repo, options = {})
|
200
|
+
display_name = repo_name
|
201
|
+
|
202
|
+
if repo_ref.nil?
|
203
|
+
repo_ref = resolve_reference(repo)
|
204
|
+
|
205
|
+
if repo.has_versions?
|
206
|
+
output "Resolving #{display_name}'s version"
|
207
|
+
else
|
208
|
+
output "No versions, use current reference"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
display_name = "#{repo_name}\##{repo_ref}"
|
213
|
+
|
214
|
+
output "Installing: #{display_name}"
|
215
|
+
unless options[:dry_run]
|
216
|
+
social_snippet.repo_manager.install repo_name, repo_ref, repo, options
|
217
|
+
end
|
218
|
+
|
219
|
+
output "Success: #{display_name}"
|
220
|
+
|
221
|
+
# install dependencies
|
222
|
+
if has_dependencies?(repo)
|
223
|
+
output "Finding #{display_name}'s dependencies"
|
224
|
+
install_missing_dependencies repo.dependencies, options
|
225
|
+
output "Finish finding #{display_name}'s dependencies"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def ask_confirm(message)
|
230
|
+
ret = social_snippet.prompt.ask(message) do |q|
|
231
|
+
q.limit = 1
|
232
|
+
q.validate = /^[yn]$/i
|
233
|
+
end
|
234
|
+
/y/i === ret
|
235
|
+
end
|
236
|
+
|
237
|
+
def ask_manifest_questions(questions, obj)
|
238
|
+
questions.inject(obj) do |obj, q|
|
239
|
+
obj[q[:key]] = ask_manifest_question(q)
|
240
|
+
obj
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def ask_manifest_question(question)
|
245
|
+
if question[:type] === :string
|
246
|
+
social_snippet.prompt.ask("#{question[:key]}: ") do |q|
|
247
|
+
q.default = question[:default]
|
248
|
+
if question[:validate].is_a?(Regexp)
|
249
|
+
q.validate = question[:validate]
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def manifest_questions(answer)
|
256
|
+
[
|
257
|
+
{
|
258
|
+
:key => "name",
|
259
|
+
:type => :string,
|
260
|
+
:validate => /[a-zA-Z0-9\.\-_]+/,
|
261
|
+
:default => answer["name"],
|
262
|
+
},
|
263
|
+
{
|
264
|
+
:key => "description",
|
265
|
+
:type => :string,
|
266
|
+
:default => answer["description"],
|
267
|
+
},
|
268
|
+
{
|
269
|
+
:key => "license",
|
270
|
+
:default => answer["license"] || "MIT",
|
271
|
+
:type => :string,
|
272
|
+
},
|
273
|
+
]
|
274
|
+
end
|
275
|
+
|
192
276
|
def resolve_reference(repo)
|
193
277
|
if repo.has_versions?
|
194
278
|
repo.latest_version
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module SocialSnippet::CommandLine
|
2
|
+
|
3
|
+
# TODO: $ sspm init --no-prompt
|
4
|
+
class SSpm::SubCommands::InitCommand < Command
|
5
|
+
|
6
|
+
def usage
|
7
|
+
<<EOF
|
8
|
+
Usage:
|
9
|
+
$ sspm init
|
10
|
+
-> Generate snippet.json interactively
|
11
|
+
EOF
|
12
|
+
end
|
13
|
+
|
14
|
+
def desc
|
15
|
+
"Generate snippet.json interactively"
|
16
|
+
end
|
17
|
+
|
18
|
+
def define_options
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
social_snippet.api.init_manifest options
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/social_snippet/core.rb
CHANGED
@@ -1,13 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
|
3
|
-
# Extend Hash tsortable
|
4
|
-
class Hash
|
5
|
-
include TSort
|
6
|
-
alias tsort_each_node each_key
|
7
|
-
def tsort_each_child(node, &block)
|
8
|
-
fetch(node).each(&block)
|
9
|
-
end
|
10
|
-
end
|
1
|
+
require "highline"
|
11
2
|
|
12
3
|
class SocialSnippet::Core
|
13
4
|
|
@@ -18,6 +9,7 @@ class SocialSnippet::Core
|
|
18
9
|
attr_reader :registry_client
|
19
10
|
attr_reader :logger
|
20
11
|
attr_reader :api
|
12
|
+
attr_reader :prompt
|
21
13
|
|
22
14
|
# Constructor
|
23
15
|
def initialize(new_input_stream = STDIN, new_output_stream = STDOUT)
|
@@ -25,6 +17,7 @@ class SocialSnippet::Core
|
|
25
17
|
@output_stream = new_output_stream
|
26
18
|
@config = ::SocialSnippet::Config.new(self)
|
27
19
|
@logger = ::SocialSnippet::Logger.new output_stream
|
20
|
+
@prompt = ::HighLine.new(input_stream, output_stream)
|
28
21
|
init_logger
|
29
22
|
|
30
23
|
@repo_manager = ::SocialSnippet::Repository::RepositoryManager.new(self)
|
@@ -44,7 +44,7 @@ module SocialSnippet::Repository
|
|
44
44
|
# @param context [::SocialSnippet::Context] The context of snippet
|
45
45
|
# @param tag [::SocialSnippet::Tag] The tag of snippet
|
46
46
|
def get_snippet(context, tag)
|
47
|
-
|
47
|
+
::SocialSnippet::Snippet.new(resolve_snippet_path(context, tag))
|
48
48
|
end
|
49
49
|
|
50
50
|
# Resolve snippet path from tag
|
@@ -3,13 +3,21 @@ module SocialSnippet
|
|
3
3
|
class Resolvers::InsertResolver < Resolvers::BaseResolver
|
4
4
|
|
5
5
|
attr_reader :deps_resolver
|
6
|
+
attr_reader :options
|
6
7
|
|
7
8
|
# Constructor
|
8
9
|
#
|
9
10
|
# @param social_snippet [::SocialSnippet::Core]
|
10
|
-
def initialize(social_snippet)
|
11
|
+
def initialize(social_snippet, new_options = {})
|
12
|
+
@options = new_options
|
11
13
|
@deps_resolver = Resolvers::DepResolver.new(social_snippet)
|
12
14
|
super(social_snippet)
|
15
|
+
init_options
|
16
|
+
end
|
17
|
+
|
18
|
+
def init_options
|
19
|
+
options[:margin_bottom] = options.fetch(:margin_bottom, 0)
|
20
|
+
options[:margin_top] = options.fetch(:margin_top, 0)
|
13
21
|
end
|
14
22
|
|
15
23
|
# Insert snippets to given text
|
@@ -17,14 +25,14 @@ module SocialSnippet
|
|
17
25
|
# @param src [Array<String>] The text of source code
|
18
26
|
def insert(src)
|
19
27
|
context = Context.new("")
|
20
|
-
lines = src.split(
|
28
|
+
lines = src.split($/)
|
21
29
|
|
22
30
|
TagParser.find_snippet_tags(lines).each do |tag_info|
|
23
31
|
visit tag_info[:tag]
|
24
32
|
end
|
25
33
|
|
26
34
|
dest = insert_func(lines, context)
|
27
|
-
return dest.join(
|
35
|
+
return dest.join($/)
|
28
36
|
end
|
29
37
|
|
30
38
|
private
|
@@ -51,15 +59,19 @@ module SocialSnippet
|
|
51
59
|
|
52
60
|
# Insert snippet by tag and context
|
53
61
|
def insert_by_tag_and_context!(inserter, snippet, context, tag)
|
54
|
-
src = insert_func(snippet.lines, context, tag)
|
62
|
+
src = insert_func(snippet.lines.to_a, context, tag)
|
63
|
+
|
64
|
+
options[:margin_top].times { inserter.insert "" }
|
55
65
|
inserter.insert tag.to_snippet_tag # @snip -> @snippet
|
56
66
|
inserter.insert src
|
67
|
+
options[:margin_bottom].times { inserter.insert "" }
|
68
|
+
|
57
69
|
visit tag
|
58
70
|
end
|
59
71
|
|
60
72
|
# Insert depended snippet
|
61
73
|
def insert_depended_snippets!(inserter, snippet, context, tag)
|
62
|
-
dep_tags = deps_resolver.find(snippet.lines, context, tag)
|
74
|
+
dep_tags = deps_resolver.find(snippet.lines.to_a, context, tag)
|
63
75
|
dep_tags = sort_dep_tags_by_dep(dep_tags)
|
64
76
|
|
65
77
|
dep_tags.each do |tag_info|
|
@@ -85,7 +97,7 @@ module SocialSnippet
|
|
85
97
|
end
|
86
98
|
|
87
99
|
# generate dependency graph
|
88
|
-
dep_tags_hash =
|
100
|
+
dep_tags_hash = TSortableHash.new
|
89
101
|
dep_tags.each do |tag_info|
|
90
102
|
tag = tag_info[:tag].to_path
|
91
103
|
dep_ind = dep_tags_index[tag]
|