social_snippet 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +8 -8
  2. data/.travis.yml +1 -2
  3. data/Rakefile +12 -0
  4. data/lib/social_snippet/api.rb +4 -4
  5. data/lib/social_snippet/api/completion_api.rb +1 -1
  6. data/lib/social_snippet/api/config_api.rb +3 -3
  7. data/lib/social_snippet/api/insert_snippet_api.rb +1 -1
  8. data/lib/social_snippet/api/install_repository_api.rb +5 -5
  9. data/lib/social_snippet/api/manifest_api.rb +5 -5
  10. data/lib/social_snippet/api/registry_api.rb +1 -1
  11. data/lib/social_snippet/api/search_api.rb +2 -2
  12. data/lib/social_snippet/api/show_api.rb +1 -1
  13. data/lib/social_snippet/api/update_repository_api.rb +9 -9
  14. data/lib/social_snippet/command_line/command.rb +2 -2
  15. data/lib/social_snippet/command_line/ssnip/main_command.rb +1 -1
  16. data/lib/social_snippet/command_line/sspm/sub_commands/complete_command.rb +1 -1
  17. data/lib/social_snippet/command_line/sspm/sub_commands/config_command.rb +3 -3
  18. data/lib/social_snippet/command_line/sspm/sub_commands/info_command.rb +1 -1
  19. data/lib/social_snippet/command_line/sspm/sub_commands/init_command.rb +1 -1
  20. data/lib/social_snippet/command_line/sspm/sub_commands/install_command.rb +4 -4
  21. data/lib/social_snippet/command_line/sspm/sub_commands/publish_command.rb +2 -2
  22. data/lib/social_snippet/command_line/sspm/sub_commands/search_command.rb +1 -1
  23. data/lib/social_snippet/command_line/sspm/sub_commands/update_command.rb +2 -2
  24. data/lib/social_snippet/config.rb +3 -3
  25. data/lib/social_snippet/context.rb +32 -11
  26. data/lib/social_snippet/registry/registry_client.rb +4 -4
  27. data/lib/social_snippet/registry/registry_resources/base.rb +13 -13
  28. data/lib/social_snippet/repository/repository_installer.rb +6 -6
  29. data/lib/social_snippet/repository/repository_manager.rb +11 -12
  30. data/lib/social_snippet/resolvers/base_resolver.rb +25 -11
  31. data/lib/social_snippet/resolvers/dep_resolver.rb +4 -4
  32. data/lib/social_snippet/resolvers/insert_resolver.rb +14 -13
  33. data/lib/social_snippet/snippet.rb +76 -55
  34. data/lib/social_snippet/tag.rb +28 -7
  35. data/lib/social_snippet/tag_parser.rb +47 -38
  36. data/lib/social_snippet/version.rb +1 -1
  37. data/spec/lib/api_spec.rb +4 -4
  38. data/spec/lib/command_line/sspm_config_spec.rb +3 -3
  39. data/spec/lib/command_line/sspm_init_spec.rb +1 -1
  40. data/spec/lib/command_line/sspm_install_spec.rb +5 -5
  41. data/spec/lib/command_line/sspm_search_spec.rb +1 -1
  42. data/spec/lib/context_spec.rb +29 -22
  43. data/spec/lib/core_spec.rb +12 -12
  44. data/spec/lib/insert_resolver_spec.rb +7 -7
  45. data/spec/lib/repository/repository_manager_spec.rb +1 -0
  46. data/spec/spec_helper.rb +15 -15
  47. data/test/core_test.rb +1062 -428
  48. metadata +2 -2
@@ -1,5 +1,7 @@
1
1
  class SocialSnippet::Tag
2
2
 
3
+ require "pathname"
4
+
3
5
  attr_reader :path
4
6
  attr_reader :repo
5
7
  attr_reader :ref
@@ -11,15 +13,15 @@ class SocialSnippet::Tag
11
13
  #
12
14
  # @param s [String] tag line text
13
15
  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)
16
+ set_path self.class.get_path(s)
17
+ @repo = self.class.get_repo(s)
18
+ @ref = self.class.get_ref(s)
19
+ @prefix = self.class.get_prefix(s)
20
+ @suffix = self.class.get_suffix(s)
21
+ @spaces = self.class.get_spaces(s)
20
22
 
21
23
  # to normalize repo's path
22
- set_path SocialSnippet::Tag.get_path(s)
24
+ set_path self.class.get_path(s)
23
25
  end
24
26
 
25
27
  # Set information by another tag
@@ -31,12 +33,19 @@ class SocialSnippet::Tag
31
33
  self
32
34
  end
33
35
 
36
+ def filename
37
+ ::Pathname.new(path).basename.to_s
38
+ end
39
+
34
40
  # Set path
35
41
  def set_path(new_path)
36
42
  @path = normalize_path(new_path)
37
43
  end
38
44
 
39
45
  def normalize_path(path)
46
+ # ././foo/bar => foo/bar
47
+ path.gsub! /^\.\//, "" while /^\.\// === path
48
+
40
49
  # repo:/path/to/file -> repo:path/to/file
41
50
  path[0] = "" if has_repo? && path[0] == "/"
42
51
 
@@ -93,6 +102,18 @@ class SocialSnippet::Tag
93
102
 
94
103
  class << self
95
104
 
105
+ def is_control_tag?(s)
106
+ is_no_tag_line?(s)
107
+ end
108
+
109
+ def is_no_tag_line?(s)
110
+ /@no_tag/ === s
111
+ end
112
+
113
+ def is_cut?(s)
114
+ /@cut/ === s
115
+ end
116
+
96
117
  def is_begin_cut?(s)
97
118
  /@begin_cut/ === s
98
119
  end
@@ -1,51 +1,60 @@
1
- class SocialSnippet::TagParser
1
+ module SocialSnippet
2
2
 
3
- class << self
3
+ class TagParser
4
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
- find_lines(s) {|line| ::SocialSnippet::Tag.is_snip_tag_line(line) }
11
- end
5
+ class << self
12
6
 
13
- # Find `@snippet` tags from text
14
- #
15
- # @param s [String or Array] parsed text
16
- # @return [Array] found `@snippet` tags with line_no
17
- def find_snippet_tags(s)
18
- find_lines(s) {|line| ::SocialSnippet::Tag.is_snippet_tag_line(line) }
19
- end
7
+ # Find `@snip` tags from text
8
+ #
9
+ # @param s [String or Array] parsed text
10
+ # @return [Array] found `@snip` tags with line_no
11
+ def find_snip_tags(s)
12
+ find_lines(s) {|line| Tag.is_snip_tag_line(line) }
13
+ end
20
14
 
21
- private
22
-
23
- def find_lines(s, &comparator)
24
- get_lines(s).each.with_index.inject([]) do |found_lines, (line, i)|
25
- if comparator.call(line)
26
- found_lines.push(
27
- {
28
- :line_no => i,
29
- :tag => ::SocialSnippet::Tag.new(line),
30
- }
31
- )
15
+ # Find `@snippet` tags from text
16
+ #
17
+ # @param s [String or Array] parsed text
18
+ # @return [Array] found `@snippet` tags with line_no
19
+ def find_snippet_tags(s)
20
+ find_lines(s) {|line| Tag.is_snippet_tag_line(line) }
21
+ end
22
+
23
+ def find_no_tags(s)
24
+ find_lines(s) {|line| Tag.is_no_tag_line?(line) }
25
+ end
26
+
27
+ private
28
+
29
+ def find_lines(s, &comparator)
30
+ get_lines(s).each.with_index.inject([]) do |found_lines, (line, i)|
31
+ if comparator.call(line)
32
+ found_lines.push(
33
+ {
34
+ :line_no => i,
35
+ :tag => Tag.new(line),
36
+ }
37
+ )
38
+ end
39
+ found_lines
32
40
  end
33
- found_lines
34
41
  end
35
- end
36
42
 
37
- def get_lines(s)
38
- if s.is_a?(String)
39
- s.split($/)
40
- elsif s.is_a?(Array)
41
- s
42
- elsif s.is_a?(Enumerator)
43
- s
44
- else
45
- raise "error unknown data"
43
+ def get_lines(s)
44
+ if s.is_a?(::String)
45
+ s.split($/)
46
+ elsif s.is_a?(::Array)
47
+ s
48
+ elsif s.is_a?(::Enumerator)
49
+ s
50
+ else
51
+ raise "error unknown data"
52
+ end
46
53
  end
54
+
47
55
  end
48
56
 
49
57
  end
50
58
 
51
59
  end
60
+
@@ -1,6 +1,6 @@
1
1
  module SocialSnippet
2
2
 
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
 
5
5
  module Version
6
6
 
data/spec/lib/api_spec.rb CHANGED
@@ -38,7 +38,7 @@ module SocialSnippet
38
38
  let(:install_command_logger) { ::SocialSnippet::Logger.new install_command_output }
39
39
 
40
40
  before do
41
- allow(fake_social_snippet).to receive(:logger).and_return install_command_logger
41
+ allow(fake_core).to receive(:logger).and_return install_command_logger
42
42
  install_command.init
43
43
  install_command.run
44
44
  expect(install_command_output.string).to match /Success/
@@ -48,7 +48,7 @@ module SocialSnippet
48
48
 
49
49
  let(:string_io) { ::StringIO.new }
50
50
  let(:string_logger) { ::SocialSnippet::Logger.new(string_io) }
51
- before { allow(fake_social_snippet).to receive(:logger).and_return string_logger }
51
+ before { allow(fake_core).to receive(:logger).and_return string_logger }
52
52
 
53
53
  #
54
54
  # tests from here
@@ -70,7 +70,7 @@ module SocialSnippet
70
70
  ].join($/) + $/
71
71
  end
72
72
 
73
- before { fake_social_snippet.api.insert_snippet input }
73
+ before { fake_core.api.insert_snippet input }
74
74
  it { expect(string_io.string).to eq expected }
75
75
 
76
76
  end # $ ssnip / without snip
@@ -83,7 +83,7 @@ module SocialSnippet
83
83
  ].join($/)
84
84
  end
85
85
 
86
- before { fake_social_snippet.api.insert_snippet input }
86
+ before { fake_core.api.insert_snippet input }
87
87
 
88
88
  # last update: 2014-10-28
89
89
  it { expect(string_io.string).to match(/@snippet/) }
@@ -8,7 +8,7 @@ module SocialSnippet::CommandLine
8
8
 
9
9
  let(:sspm_config_command) { SSpm::SubCommands::ConfigCommand.new ["key"] }
10
10
 
11
- it { expect(fake_social_snippet.api).to receive(:config_get).with("key").once }
11
+ it { expect(fake_core.api).to receive(:config_get).with("key").once }
12
12
 
13
13
  after do
14
14
  sspm_config_command.init
@@ -21,7 +21,7 @@ module SocialSnippet::CommandLine
21
21
 
22
22
  let(:sspm_config_command) { SSpm::SubCommands::ConfigCommand.new ["key", "value"] }
23
23
 
24
- it { expect(fake_social_snippet.api).to receive(:config_set).with("key", "value").once }
24
+ it { expect(fake_core.api).to receive(:config_set).with("key", "value").once }
25
25
 
26
26
  after do
27
27
  sspm_config_command.init
@@ -34,7 +34,7 @@ module SocialSnippet::CommandLine
34
34
 
35
35
  let(:sspm_config_command) { SSpm::SubCommands::ConfigCommand.new ["key=value"] }
36
36
 
37
- it { expect(fake_social_snippet.api).to receive(:config_set).with("key", "value").once }
37
+ it { expect(fake_core.api).to receive(:config_set).with("key", "value").once }
38
38
 
39
39
  after do
40
40
  sspm_config_command.init
@@ -9,7 +9,7 @@ module SocialSnippet::CommandLine
9
9
  let(:sspm_init_command) { SSpm::SubCommands::InitCommand .new [] }
10
10
 
11
11
  example do
12
- expect(fake_social_snippet.api).to receive(:init_manifest).with({}).once
12
+ expect(fake_core.api).to receive(:init_manifest).with({}).once
13
13
  sspm_init_command.init
14
14
  sspm_init_command.run
15
15
  end
@@ -108,7 +108,7 @@ module SocialSnippet::CommandLine
108
108
  #
109
109
  let(:install_command_output) { ::StringIO.new }
110
110
  let(:install_command_logger) { ::SocialSnippet::Logger.new install_command_output }
111
- before { allow(fake_social_snippet).to receive(:logger).and_return install_command_logger }
111
+ before { allow(fake_core).to receive(:logger).and_return install_command_logger }
112
112
 
113
113
  describe "$ sspm install" do
114
114
 
@@ -126,8 +126,8 @@ module SocialSnippet::CommandLine
126
126
 
127
127
  example do
128
128
  install_command = SSpm::SubCommands::InstallCommand.new([])
129
- expect(fake_social_snippet.api).to receive(:install_repository_by_name).with("foo", "1.2.3", kind_of(Hash)).once
130
- expect(fake_social_snippet.api).to receive(:install_repository_by_name).with("bar", "0.0.1", kind_of(Hash)).once
129
+ expect(fake_core.api).to receive(:install_repository_by_name).with("foo", "1.2.3", kind_of(Hash)).once
130
+ expect(fake_core.api).to receive(:install_repository_by_name).with("bar", "0.0.1", kind_of(Hash)).once
131
131
  install_command.run
132
132
  end
133
133
 
@@ -142,7 +142,7 @@ module SocialSnippet::CommandLine
142
142
 
143
143
  before do
144
144
  install_command_logger.level = ::SocialSnippet::Logger::Severity::INFO
145
- allow(fake_social_snippet).to receive(:logger).and_return install_command_logger
145
+ allow(fake_core).to receive(:logger).and_return install_command_logger
146
146
  install_command = SSpm::SubCommands::InstallCommand.new ["my-repo"]
147
147
  install_command.init
148
148
  install_command.run
@@ -162,7 +162,7 @@ module SocialSnippet::CommandLine
162
162
 
163
163
  subject do
164
164
  install_command_logger.level = ::SocialSnippet::Logger::Severity::INFO
165
- allow(fake_social_snippet).to receive(:logger).and_return second_install_command_logger
165
+ allow(fake_core).to receive(:logger).and_return second_install_command_logger
166
166
  install_command = SSpm::SubCommands::InstallCommand.new ["my-repo"]
167
167
  install_command.init
168
168
  install_command.run
@@ -29,7 +29,7 @@ module SocialSnippet::CommandLine
29
29
 
30
30
  let(:search_command_output) { ::StringIO.new }
31
31
  let(:search_command_logger) { ::SocialSnippet::Logger.new search_command_output }
32
- before { allow(fake_social_snippet).to receive(:logger).and_return search_command_logger }
32
+ before { allow(fake_core).to receive(:logger).and_return search_command_logger }
33
33
 
34
34
  describe "$ search repo" do
35
35
 
@@ -2,45 +2,52 @@ require "spec_helper"
2
2
 
3
3
  describe SocialSnippet::Context do
4
4
 
5
- let(:context) { SocialSnippet::Context.new("path/to/file.cpp") }
6
-
7
5
  describe "#move" do
8
6
 
9
- context "move ./file2.cpp" do
10
-
11
- before { context.move "./file2.cpp" }
7
+ context "start from path/to/file1.cpp" do
12
8
 
13
- it { expect(context.path).to eq "path/to/file2.cpp" }
9
+ let(:context) { SocialSnippet::Context.new("path/to/file1.cpp") }
10
+ it { expect(context.path).to eq "path/to/file1.cpp" }
11
+ it { expect(context.dirname).to eq "path/to" }
14
12
  it { expect(context.repo).to be_nil }
15
13
 
16
- context "move path/to/file.cpp, repo" do
14
+ context "move into ./file2.cpp" do
15
+
16
+ before { context.move "./file2.cpp" }
17
+ it { expect(context.path).to eq "path/to/file2.cpp" }
18
+ it { expect(context.dirname).to eq "path/to" }
19
+ it { expect(context.repo).to be_nil }
17
20
 
18
- before { context.move "path/to/file.cpp", "repo" }
21
+ context "move into subdir/file3.cpp" do
19
22
 
20
- it { expect(context.path).to eq "path/to/file.cpp" }
21
- it { expect(context.repo).to eq "repo" }
23
+ before { context.move "subdir/file3.cpp" }
24
+ it { expect(context.path).to eq "path/to/subdir/file3.cpp" }
25
+ it { expect(context.dirname).to eq "path/to/subdir" }
26
+ it { expect(context.repo).to be_nil }
22
27
 
23
- context "move subdir/file.cpp" do
28
+ end # move into subdir/file3.cpp
24
29
 
25
- before { context.move "subdir/file.cpp" }
30
+ context "move into path/to/repo/file4.cpp at repo" do
26
31
 
27
- it { expect(context.path).to eq "path/to/subdir/file.cpp" }
32
+ before { context.move "path/to/repo/file4.cpp", "repo" }
33
+ it { expect(context.path).to eq "path/to/repo/file4.cpp" }
34
+ it { expect(context.dirname).to eq "path/to/repo" }
28
35
  it { expect(context.repo).to eq "repo" }
29
36
 
30
- end # move subdir/file.cpp
31
-
32
- end # move path/to/file.cpp, repo
37
+ context "move into subdir/file5.cpp" do
33
38
 
34
- context "move subdir/file.cpp" do
39
+ before { context.move "subdir/file5.cpp" }
40
+ it { expect(context.path).to eq "path/to/repo/subdir/file5.cpp" }
41
+ it { expect(context.dirname).to eq "path/to/repo/subdir" }
42
+ it { expect(context.repo).to eq "repo" }
35
43
 
36
- before { context.move "subdir/file.cpp" }
44
+ end # move into subdir/file5.cpp
37
45
 
38
- it { expect(context.path).to eq "path/to/subdir/file.cpp" }
39
- it { expect(context.repo).to be_nil }
46
+ end # move into path/to/reop/file3.cpp at repo
40
47
 
41
- end # move subdir/file.cpp
48
+ end # move into file2.cpp
42
49
 
43
- end # move file2.cpp
50
+ end # start from path/to/file1.cpp
44
51
 
45
52
  end # move
46
53
 
@@ -16,15 +16,15 @@ module SocialSnippet
16
16
  before do
17
17
  repo_name = "my-repo"
18
18
 
19
- FileUtils.mkdir_p "#{repo_path}"
20
- FileUtils.mkdir_p "#{repo_path}/my-repo"
21
- FileUtils.mkdir_p "#{repo_path}/my-repo/.git"
22
- FileUtils.mkdir_p "#{repo_path}/my-repo/src"
23
- FileUtils.touch "#{repo_path}/my-repo/snippet.json"
24
- FileUtils.touch "#{repo_path}/my-repo/src/get_42.cpp"
19
+ ::FileUtils.mkdir_p "#{repo_path}"
20
+ ::FileUtils.mkdir_p "#{repo_path}/my-repo"
21
+ ::FileUtils.mkdir_p "#{repo_path}/my-repo/.git"
22
+ ::FileUtils.mkdir_p "#{repo_path}/my-repo/src"
23
+ ::FileUtils.touch "#{repo_path}/my-repo/snippet.json"
24
+ ::FileUtils.touch "#{repo_path}/my-repo/src/get_42.cpp"
25
25
 
26
26
  # snippet.json
27
- File.write "#{repo_path}/my-repo/snippet.json", [
27
+ ::File.write "#{repo_path}/my-repo/snippet.json", [
28
28
  '{',
29
29
  ' "name": "my-repo",',
30
30
  ' "language": "C++",',
@@ -33,7 +33,7 @@ module SocialSnippet
33
33
  ].join($/)
34
34
 
35
35
  # src/get_42.cpp
36
- File.write "#{repo_path}/my-repo/src/get_42.cpp", [
36
+ ::File.write "#{repo_path}/my-repo/src/get_42.cpp", [
37
37
  'int get_42() {',
38
38
  ' return 42;',
39
39
  '}',
@@ -48,8 +48,8 @@ module SocialSnippet
48
48
  repo
49
49
  end
50
50
 
51
- allow(fake_social_snippet.repo_manager).to receive(:find_repository).with("my-repo") { repo_config.call }
52
- allow(fake_social_snippet.repo_manager).to receive(:find_repository).with("my-repo", short_commit_id) { repo_config.call }
51
+ allow(fake_core.repo_manager).to receive(:find_repository).with("my-repo") { repo_config.call }
52
+ allow(fake_core.repo_manager).to receive(:find_repository).with("my-repo", short_commit_id) { repo_config.call }
53
53
  end # prepare for my-repo
54
54
 
55
55
  context "there are no @snip tags" do
@@ -76,7 +76,7 @@ module SocialSnippet
76
76
  ].join($/)
77
77
  end
78
78
 
79
- subject { fake_social_snippet.api.insert_snippet(input) }
79
+ subject { fake_core.api.insert_snippet(input) }
80
80
  it { should eq output }
81
81
 
82
82
  end # there is no @snip tags
@@ -112,7 +112,7 @@ module SocialSnippet
112
112
  ].join($/)
113
113
  end
114
114
 
115
- subject { fake_social_snippet.api.insert_snippet(input) }
115
+ subject { fake_core.api.insert_snippet(input) }
116
116
  it { should eq output }
117
117
 
118
118
  end # there is a @snip tag
@@ -5,10 +5,10 @@ describe SocialSnippet::Resolvers::InsertResolver do
5
5
  context "prepare stubs" do
6
6
 
7
7
  before do
8
- allow(fake_social_snippet.repo_manager).to receive(:resolve_snippet_path) do |c, t|
8
+ allow(fake_core.repo_manager).to receive(:resolve_snippet_path) do |c, t|
9
9
  t.repo
10
10
  end
11
- allow(fake_social_snippet.repo_manager).to receive(:get_snippet) do |c, t|
11
+ allow(fake_core.repo_manager).to receive(:get_snippet) do |c, t|
12
12
  ::SocialSnippet::Snippet.new_text(t.repo)
13
13
  end
14
14
  end
@@ -18,7 +18,7 @@ describe SocialSnippet::Resolvers::InsertResolver do
18
18
  describe "ruby's require()" do
19
19
 
20
20
  before do
21
- allow(fake_social_snippet.repo_manager).to receive(:get_snippet) do |c, t|
21
+ allow(fake_core.repo_manager).to receive(:get_snippet) do |c, t|
22
22
  ::SocialSnippet::Snippet.new_text([
23
23
  "def foo",
24
24
  " 42",
@@ -28,7 +28,7 @@ describe SocialSnippet::Resolvers::InsertResolver do
28
28
  end # prepare snippet
29
29
 
30
30
  let(:resolver) do
31
- ::SocialSnippet::Resolvers::InsertResolver.new(fake_social_snippet)
31
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core)
32
32
  end
33
33
 
34
34
  let(:input) do
@@ -69,7 +69,7 @@ describe SocialSnippet::Resolvers::InsertResolver do
69
69
  context "no options" do
70
70
 
71
71
  let(:resolver) do
72
- ::SocialSnippet::Resolvers::InsertResolver.new(fake_social_snippet)
72
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core)
73
73
  end
74
74
 
75
75
  let(:input) do
@@ -99,7 +99,7 @@ describe SocialSnippet::Resolvers::InsertResolver do
99
99
  context ":margin_top => 3" do
100
100
 
101
101
  let(:resolver) do
102
- ::SocialSnippet::Resolvers::InsertResolver.new(fake_social_snippet, {
102
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, {
103
103
  :margin_top => 3,
104
104
  })
105
105
  end
@@ -140,7 +140,7 @@ describe SocialSnippet::Resolvers::InsertResolver do
140
140
  context ":margin_bottom => 3" do
141
141
 
142
142
  let(:resolver) do
143
- ::SocialSnippet::Resolvers::InsertResolver.new(fake_social_snippet, {
143
+ ::SocialSnippet::Resolvers::InsertResolver.new(fake_core, {
144
144
  :margin_bottom => 3,
145
145
  })
146
146
  end