social_snippet 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +2 -0
  3. data/.travis.yml +22 -3
  4. data/Gemfile +25 -2
  5. data/Guardfile +9 -0
  6. data/README.md +6 -1
  7. data/Rakefile +51 -1
  8. data/appveyor.yml +36 -0
  9. data/bin/ssnip +10 -0
  10. data/bin/sspm +10 -0
  11. data/lib/social_snippet.rb +15 -4
  12. data/lib/social_snippet/api.rb +238 -0
  13. data/lib/social_snippet/command_line.rb +4 -0
  14. data/lib/social_snippet/command_line/command.rb +158 -0
  15. data/lib/social_snippet/command_line/ssnip.rb +2 -0
  16. data/lib/social_snippet/command_line/ssnip/main_command.rb +26 -0
  17. data/lib/social_snippet/command_line/sspm.rb +3 -0
  18. data/lib/social_snippet/command_line/sspm/main_command.rb +63 -0
  19. data/lib/social_snippet/command_line/sspm/sub_commands.rb +16 -0
  20. data/lib/social_snippet/command_line/sspm/sub_commands/complete_command.rb +28 -0
  21. data/lib/social_snippet/command_line/sspm/sub_commands/info_command.rb +28 -0
  22. data/lib/social_snippet/command_line/sspm/sub_commands/install_command.rb +103 -0
  23. data/lib/social_snippet/command_line/sspm/sub_commands/publish_command.rb +51 -0
  24. data/lib/social_snippet/command_line/sspm/sub_commands/search_command.rb +32 -0
  25. data/lib/social_snippet/command_line/sspm/sub_commands/update_command.rb +37 -0
  26. data/lib/social_snippet/config.rb +92 -0
  27. data/lib/social_snippet/context.rb +89 -0
  28. data/lib/social_snippet/core.rb +40 -0
  29. data/lib/social_snippet/inserter.rb +64 -0
  30. data/lib/social_snippet/logger.rb +9 -0
  31. data/lib/social_snippet/registry.rb +3 -0
  32. data/lib/social_snippet/registry/registry_client.rb +15 -0
  33. data/lib/social_snippet/registry/registry_resources.rb +3 -0
  34. data/lib/social_snippet/registry/registry_resources/base.rb +80 -0
  35. data/lib/social_snippet/registry/registry_resources/repositories.rb +23 -0
  36. data/lib/social_snippet/repository.rb +6 -0
  37. data/lib/social_snippet/repository/drivers.rb +3 -0
  38. data/lib/social_snippet/repository/drivers/base_repository.rb +192 -0
  39. data/lib/social_snippet/repository/drivers/git_repository.rb +76 -0
  40. data/lib/social_snippet/repository/repository_errors.rb +5 -0
  41. data/lib/social_snippet/repository/repository_factory.rb +59 -0
  42. data/lib/social_snippet/repository/repository_installer.rb +86 -0
  43. data/lib/social_snippet/repository/repository_manager.rb +177 -0
  44. data/lib/social_snippet/resolvers.rb +4 -0
  45. data/lib/social_snippet/resolvers/base_resolver.rb +103 -0
  46. data/lib/social_snippet/resolvers/dep_resolver.rb +61 -0
  47. data/lib/social_snippet/resolvers/insert_resolver.rb +100 -0
  48. data/lib/social_snippet/snippet.rb +14 -0
  49. data/lib/social_snippet/tag.rb +198 -0
  50. data/lib/social_snippet/tag_parser.rb +61 -0
  51. data/lib/social_snippet/version.rb +26 -1
  52. data/social_snippet.gemspec +18 -3
  53. data/spec/helpers/codeclimate_helper.rb +4 -0
  54. data/spec/helpers/fakefs_helper.rb +15 -0
  55. data/spec/helpers/webmock_helper.rb +16 -0
  56. data/spec/lib/api_spec.rb +106 -0
  57. data/spec/lib/command_line/sspm_install_spec.rb +224 -0
  58. data/spec/lib/command_line/sspm_search_spec.rb +167 -0
  59. data/spec/lib/command_line/sspm_spec.rb +81 -0
  60. data/spec/lib/config_spec.rb +56 -0
  61. data/spec/lib/context_spec.rb +48 -0
  62. data/spec/lib/core_spec.rb +126 -0
  63. data/spec/lib/inserter_spec.rb +177 -0
  64. data/spec/lib/registry_client_spec.rb +173 -0
  65. data/spec/lib/repository/base_repository_spec.rb +104 -0
  66. data/spec/lib/repository/git_repository_spec.rb +83 -0
  67. data/spec/lib/repository/repository_factory_spec.rb +31 -0
  68. data/spec/lib/repository/repository_installer_spec.rb +63 -0
  69. data/spec/lib/repository/repository_manager_spec.rb +201 -0
  70. data/spec/lib/tag_parser_spec.rb +173 -0
  71. data/spec/lib/tag_spec.rb +93 -0
  72. data/spec/spec_helper.rb +106 -0
  73. data/test/base_repository_test.rb +375 -0
  74. data/test/command_test.rb +39 -0
  75. data/test/context_test.rb +31 -0
  76. data/test/core_test.rb +2091 -0
  77. data/test/git_repository_test.rb +114 -0
  78. data/test/install_command_test.rb +28 -0
  79. data/test/repository_manager_test.rb +109 -0
  80. data/test/tag_parser_test.rb +47 -0
  81. data/test/tag_test.rb +217 -0
  82. data/test/version_test.rb +56 -0
  83. metadata +271 -14
@@ -0,0 +1,14 @@
1
+ class SocialSnippet::Snippet
2
+
3
+ attr_reader :path
4
+ attr_reader :code
5
+ attr_reader :lines
6
+
7
+ # Constructor
8
+ def initialize(snippet_path)
9
+ @path = snippet_path
10
+ @code = ::File.read(@path)
11
+ @lines = @code.split("\n")
12
+ end
13
+
14
+ end
@@ -0,0 +1,198 @@
1
+ class SocialSnippet::Tag
2
+
3
+ attr_reader :path
4
+ attr_reader :repo
5
+ attr_reader :ref
6
+ attr_reader :prefix
7
+ attr_reader :suffix
8
+ attr_reader :spaces
9
+
10
+ # Create instance
11
+ #
12
+ # @param s [String] tag line text
13
+ def initialize(s)
14
+ @path = SocialSnippet::Tag.get_path(s)
15
+ @repo = SocialSnippet::Tag.get_repo(s)
16
+ @ref = SocialSnippet::Tag.get_ref(s)
17
+ @prefix = SocialSnippet::Tag.get_prefix(s)
18
+ @suffix = SocialSnippet::Tag.get_suffix(s)
19
+ @spaces = SocialSnippet::Tag.get_spaces(s)
20
+
21
+ # to normalize repo's path
22
+ set_path SocialSnippet::Tag.get_path(s)
23
+ end
24
+
25
+ # Set information by another tag
26
+ def set_by_tag(base_tag)
27
+ return self if base_tag.nil?
28
+ @prefix = base_tag.prefix
29
+ @suffix = base_tag.suffix
30
+ @spaces = base_tag.spaces
31
+ self
32
+ end
33
+
34
+ # Set path
35
+ def set_path(new_path)
36
+ @path = normalize_path(new_path)
37
+ end
38
+
39
+ def normalize_path(path)
40
+ # repo:/path/to/file -> repo:path/to/file
41
+ path[0] = "" if has_repo? && path[0] == "/"
42
+
43
+ path
44
+ end
45
+
46
+ # Set repo
47
+ def set_repo(new_repo)
48
+ @repo = new_repo
49
+ end
50
+
51
+ # Set ref
52
+ def set_ref(new_ref)
53
+ @ref = new_ref
54
+ end
55
+
56
+ # Check to have ref
57
+ def has_ref?
58
+ return ref.nil? === false && ref != ""
59
+ end
60
+
61
+ # Check to have repository
62
+ def has_repo?
63
+ return repo != ""
64
+ end
65
+
66
+ # Get path text
67
+ def to_path
68
+ if has_repo?
69
+ if has_ref?
70
+ "#{repo}##{ref}:#{path}"
71
+ else
72
+ "#{repo}:#{path}"
73
+ end
74
+ else
75
+ "#{path}"
76
+ end
77
+ end
78
+
79
+ # Get tag text by given tag text
80
+ def to_tag_text(tag_text)
81
+ "#{prefix}#{tag_text}#{spaces}<#{to_path}>#{suffix}"
82
+ end
83
+
84
+ # Get @snip tag text
85
+ def to_snip_tag
86
+ return to_tag_text("@snip")
87
+ end
88
+
89
+ # Get @snippet tag text
90
+ def to_snippet_tag
91
+ return to_tag_text("@snippet")
92
+ end
93
+
94
+ class << self
95
+
96
+ # Check given line to match @snip tag
97
+ def is_snip_tag_line(s)
98
+ return /@snip\s*<.*?>/ === s
99
+ end
100
+
101
+ # Check given line to match @snippet tag
102
+ def is_snippet_tag_line(s)
103
+ return /@snippet\s*<.*?>/ === s
104
+ end
105
+
106
+ # Check given line to match @snip or @snippet tag
107
+ def is_snip_or_snippet_tag_line(s)
108
+ return is_snip_tag_line(s) || is_snippet_tag_line(s)
109
+ end
110
+
111
+ # Check given line to have `#` character
112
+ def has_ref_text(s)
113
+ return /<.*?#(.*?):/ === s
114
+ end
115
+
116
+ # Check given line to match `:` character
117
+ def has_colon(s)
118
+ return /:/ === s
119
+ end
120
+
121
+ # Check given line to match snippet tag with repo
122
+ def is_tag_line_with_repository(s)
123
+ return is_snip_or_snippet_tag_line(s) && has_colon(s)
124
+ end
125
+
126
+ def is_tag_line_with_repository_have_ref(s)
127
+ return is_tag_line_with_repository(s) && has_ref_text(s)
128
+ end
129
+
130
+ # Get spaces from given line
131
+ def get_spaces(s)
132
+ if is_snip_or_snippet_tag_line(s)
133
+ # return spaces
134
+ return /(@snip|@snippet)(\s*?)</.match(s)[2]
135
+ end
136
+
137
+ # return empty string
138
+ return ""
139
+ end
140
+
141
+ # Get suffix from given line
142
+ def get_suffix(s)
143
+ if is_snip_or_snippet_tag_line(s)
144
+ # return suffix text
145
+ return />(.*)/.match(s)[1]
146
+ end
147
+
148
+ # return empty string
149
+ return ""
150
+ end
151
+
152
+ # Get prefix from given line
153
+ def get_prefix(s)
154
+ if is_snip_or_snippet_tag_line(s)
155
+ # return prefix text
156
+ return /(.*?)@/.match(s)[1]
157
+ end
158
+
159
+ # return empty string
160
+ return ""
161
+ end
162
+
163
+ # Get path from given line
164
+ def get_path(s)
165
+ if is_snip_or_snippet_tag_line(s)
166
+ # return snippet path (without repo name)
167
+ path = ::Pathname.new(/<(.*?:)?(.*?)>/.match(s)[2])
168
+ return path.cleanpath.to_s
169
+ end
170
+
171
+ # return empty string
172
+ return ""
173
+ end
174
+
175
+ # Get repo's ref from given line
176
+ def get_ref(s)
177
+ if is_tag_line_with_repository_have_ref(s)
178
+ # return ref text
179
+ return /<.+?#(.*?):/.match(s)[1]
180
+ end
181
+
182
+ return ""
183
+ end
184
+
185
+ # Get repository name from given line
186
+ def get_repo(s)
187
+ if is_tag_line_with_repository(s)
188
+ # return repository name
189
+ return /<(.*?)[:#]/.match(s)[1]
190
+ end
191
+
192
+ # return empty string
193
+ return ""
194
+ end
195
+
196
+ end
197
+
198
+ end
@@ -0,0 +1,61 @@
1
+ class SocialSnippet::TagParser
2
+
3
+ class << self
4
+
5
+ # Find `@snip` tags from text
6
+ #
7
+ # @param s [String or Array] parsed text
8
+ # @return [Array] found `@snip` tags with line_no
9
+ def find_snip_tags(s)
10
+ found_lines = []
11
+
12
+ lines = get_lines(s)
13
+
14
+ lines.each.with_index do |line, i|
15
+ if ::SocialSnippet::Tag.is_snip_tag_line(line)
16
+ found_lines.push(
17
+ {
18
+ :line_no => i,
19
+ :tag => ::SocialSnippet::Tag.new(line),
20
+ }
21
+ )
22
+ end
23
+ end
24
+
25
+ return found_lines
26
+ end
27
+
28
+ # Find `@snippet` tags from text
29
+ #
30
+ # @param s [String or Array] parsed text
31
+ # @return [Array] found `@snippet` tags with line_no
32
+ def find_snippet_tags(s)
33
+ found_lines = []
34
+
35
+ lines = get_lines(s)
36
+
37
+ lines.each.with_index do |line, i|
38
+ if ::SocialSnippet::Tag.is_snippet_tag_line(line)
39
+ found_lines.push(
40
+ {
41
+ :line_no => i,
42
+ :tag => ::SocialSnippet::Tag.new(line),
43
+ }
44
+ )
45
+ end
46
+ end
47
+
48
+ return found_lines
49
+ end
50
+
51
+ def get_lines(s)
52
+ if s.is_a?(String)
53
+ return s.split("\n")
54
+ elsif s.is_a?(Array)
55
+ return s
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -1,3 +1,28 @@
1
1
  module SocialSnippet
2
- VERSION = "0.0.1"
2
+
3
+ VERSION = "0.0.2"
4
+
5
+ module Version
6
+
7
+ class << self
8
+
9
+ # Check given text matches version pattern
10
+ def is_matched_version_pattern(pattern, version)
11
+ return true if pattern == "" || pattern.nil?
12
+ return true if pattern == version
13
+
14
+ # "2.1.0" and "2.1.1" match "2.1"
15
+ # "2.11.0" and "2.11.1" do not match "2.1"
16
+ return version.start_with?("#{pattern}.")
17
+ end
18
+
19
+ # Check given text is version string
20
+ def is_version(s)
21
+ return /^([0]|[1-9][0-9]*)\.([0]|[1-9][0-9]*)\.([0]|[1-9][0-9]*)$/ === s
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
3
28
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = SocialSnippet::VERSION
9
9
  spec.authors = ["Hiroyuki Sano"]
10
10
  spec.email = ["sh19910711@gmail.com"]
11
- spec.summary = %q{Social Snippet System}
11
+ spec.summary = %q{Share and use snippet libraries for the online judges}
12
12
  spec.homepage = "https://github.com/social-snippet/social-snippet"
13
13
  spec.license = "MIT"
14
14
 
@@ -17,7 +17,22 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency "bundler", "~> 1.6"
21
- spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_runtime_dependency "bundler"
21
+ spec.add_runtime_dependency "rake"
22
+ spec.add_runtime_dependency "version_sorter"
23
+ spec.add_runtime_dependency "rugged"
24
+ spec.add_runtime_dependency "rest-client"
25
+
26
+ spec.add_development_dependency "yard"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "guard"
29
+ spec.add_development_dependency "guard-shell"
30
+ spec.add_development_dependency "pry-byebug"
31
+
22
32
  spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "fakefs"
34
+ spec.add_development_dependency "webmock"
35
+ spec.add_development_dependency "codeclimate-test-reporter"
36
+
37
+
23
38
  end
@@ -0,0 +1,4 @@
1
+ if ENV["TRAVIS"] == "true"
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ end
@@ -0,0 +1,15 @@
1
+ module FakeFSHelpers
2
+
3
+ require "fakefs/safe"
4
+
5
+ def disable_fakefs
6
+ FakeFS.deactivate!
7
+ FakeFS::FileSystem.clear
8
+ end
9
+
10
+ def enable_fakefs
11
+ FakeFS.activate!
12
+ end
13
+
14
+ end
15
+
@@ -0,0 +1,16 @@
1
+ require "webmock"
2
+
3
+ RSpec.configure do |config|
4
+ config.before do
5
+ WebMock.disable_net_connect!(
6
+ :allow => [
7
+ "codeclimate.com"
8
+ ]
9
+ )
10
+ end
11
+
12
+ config.after do
13
+ WebMock.reset!
14
+ end
15
+ end
16
+
@@ -0,0 +1,106 @@
1
+ require "spec_helper"
2
+
3
+ module SocialSnippet
4
+
5
+ describe Api do
6
+
7
+ describe "#insert_snippet()", :without_fakefs => true do
8
+
9
+ let(:example_repo_info) do
10
+ {
11
+ "name" => "example-repo",
12
+ "desc" => "This is my repository.",
13
+ "url" => "https://github.com/social-snippet/example-repo",
14
+ "dependencies" => {
15
+ },
16
+ }
17
+ end # example_repo_info
18
+
19
+ before do
20
+ WebMock
21
+ .stub_request(
22
+ :get,
23
+ "https://api.server/api/v0/repositories/example-repo",
24
+ )
25
+ .to_return(
26
+ :status => 200,
27
+ :body => example_repo_info.to_json,
28
+ :headers => {
29
+ "Content-Type" => "application/json",
30
+ },
31
+ )
32
+ end # GET /repositories/my-repo
33
+
34
+ context "$ sspm install example-repo" do
35
+
36
+ let(:install_command) { CommandLine::SSpm::SubCommands::InstallCommand.new ["example-repo"] }
37
+ let(:install_command_output) { ::StringIO.new }
38
+ let(:install_command_logger) { ::SocialSnippet::Logger.new install_command_output }
39
+
40
+ before do
41
+ allow(fake_social_snippet).to receive(:logger).and_return install_command_logger
42
+ install_command.init
43
+ install_command.run
44
+ expect(install_command_output.string).to match /Success/
45
+ end # install example-repo
46
+
47
+ context "create instance" do
48
+
49
+ let(:string_io) { ::StringIO.new }
50
+ let(:string_logger) { ::SocialSnippet::Logger.new(string_io) }
51
+ before { allow(fake_social_snippet).to receive(:logger).and_return string_logger }
52
+
53
+ #
54
+ # tests from here
55
+ #
56
+
57
+ context "$ ssnip / without snip" do
58
+
59
+ let(:input) do
60
+ [
61
+ 'hello',
62
+ 'world',
63
+ ].join($/)
64
+ end
65
+
66
+ let(:expected) do
67
+ [
68
+ 'hello',
69
+ 'world',
70
+ ].join($/) + $/
71
+ end
72
+
73
+ before { fake_social_snippet.api.insert_snippet input }
74
+ it { expect(string_io.string).to eq expected }
75
+
76
+ end # $ ssnip / without snip
77
+
78
+ context "$ ssnip / with a snip tag" do
79
+
80
+ let(:input) do
81
+ [
82
+ '// @snip <example-repo:func.cpp>'
83
+ ].join($/)
84
+ end
85
+
86
+ before { fake_social_snippet.api.insert_snippet input }
87
+
88
+ # last update: 2014-10-28
89
+ it { expect(string_io.string).to match(/@snippet/) }
90
+ it { expect(string_io.string).to match(/example-repo#.*:func\.cpp/) }
91
+ it { expect(string_io.string).to match(/example-repo#.*:func\/sub_func_1\.cpp/) }
92
+ it { expect(string_io.string).to match(/example-repo#.*:func\/sub_func_2\.cpp/) }
93
+ it { expect(string_io.string).to match(/example-repo#.*:func\/sub_func_3\.cpp/) }
94
+
95
+ end # $ ssnip / with a snip tag
96
+
97
+ end # create instance
98
+
99
+ end # $ sspm install example-repo
100
+
101
+ end # insert_snippet()
102
+
103
+ end # Api
104
+
105
+ end # SocialSnippet::CommandLine
106
+