social_snippet 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,3 @@
1
+ module SocialSnippet::Registry::RegistryResources; end
2
+ require_relative "registry_resources/base"
3
+ require_relative "registry_resources/repositories"
@@ -0,0 +1,80 @@
1
+ module SocialSnippet::Registry::RegistryResources
2
+
3
+ require "rest_client"
4
+ require "json"
5
+
6
+ class Base
7
+
8
+ attr_reader :social_snippet
9
+ attr_reader :rest_client
10
+ attr_reader :protocol
11
+ attr_reader :host
12
+ attr_reader :api_version
13
+ attr_reader :end_point
14
+ attr_reader :cookies
15
+ attr_reader :default_headers
16
+
17
+ def initialize(new_social_snippet)
18
+ @social_snippet = new_social_snippet
19
+ @host = social_snippet.config.sspm_host
20
+ @api_version = social_snippet.config.sspm_version
21
+ @protocol = social_snippet.config.sspm_protocol
22
+ @end_point = "#{protocol}://#{host}/api/#{api_version}"
23
+ @rest_client = ::RestClient::Resource.new(end_point)
24
+ @cookies = {}
25
+ @default_headers = {
26
+ :accept => :json,
27
+ }
28
+
29
+ social_snippet.logger.debug "registry: end-point = #{end_point}"
30
+ end
31
+
32
+ def post(req_path, params, headers = {})
33
+ social_snippet.logger.debug "registry: post: #{req_path}"
34
+ social_snippet.logger.debug params
35
+ csrf_token = fetch_csrf_token
36
+
37
+ # set headers
38
+ headers.merge! default_headers
39
+ headers["Content-Type"] = "application/json"
40
+ headers["X-CSRF-Token"] = csrf_token
41
+
42
+ # debug output
43
+ social_snippet.logger.debug "registry: post: csrf_token = #{csrf_token}"
44
+ social_snippet.logger.debug "registry: post: headers:"
45
+ social_snippet.logger.debug headers
46
+
47
+ parse_response rest_client[req_path].post(params.to_json, headers)
48
+ end
49
+
50
+ def get(req_path, headers = {})
51
+ social_snippet.logger.debug "registry: get #{req_path}"
52
+ headers.merge! default_headers
53
+ social_snippet.logger.debug "registry: headers:"
54
+ social_snippet.logger.debug headers
55
+ parse_response rest_client[req_path].get(headers)
56
+ end
57
+
58
+ private
59
+
60
+ def fetch_csrf_token
61
+ unless @csrf_token
62
+ @csrf_token = get("token")
63
+ default_headers.merge! :cookies => cookies
64
+ end
65
+ @csrf_token
66
+ end
67
+
68
+ def parse_response(res)
69
+ cookies.merge! res.cookies
70
+ s = res.to_s
71
+ begin
72
+ ::JSON.parse s
73
+ rescue ::JSON::ParserError
74
+ s
75
+ end
76
+ end
77
+
78
+ end # Base
79
+
80
+ end
@@ -0,0 +1,23 @@
1
+ module SocialSnippet::Registry::RegistryResources
2
+
3
+ class Repositories < Base
4
+
5
+ def all
6
+ get "repositories"
7
+ end
8
+
9
+ def search(query)
10
+ get "repositories?q=#{query}"
11
+ end
12
+
13
+ def find(repo_name)
14
+ get "repositories/#{repo_name}"
15
+ end
16
+
17
+ def add_url(repo_url)
18
+ post "repositories", :url => repo_url
19
+ end
20
+
21
+ end # Repositories
22
+
23
+ end # SocialSnippet
@@ -0,0 +1,6 @@
1
+ module SocialSnippet::Repository; end
2
+ require_relative "repository/drivers"
3
+ require_relative "repository/repository_factory"
4
+ require_relative "repository/repository_manager"
5
+ require_relative "repository/repository_errors"
6
+ require_relative "repository/repository_installer"
@@ -0,0 +1,3 @@
1
+ module SocialSnippet::Repository::Drivers; end
2
+ require_relative "drivers/base_repository"
3
+ require_relative "drivers/git_repository"
@@ -0,0 +1,192 @@
1
+ module SocialSnippet::Repository::Drivers
2
+
3
+ require "version_sorter"
4
+ require "json"
5
+
6
+ # Repository base class
7
+ # usage: class GitRepository < BaseRepository
8
+ class BaseRepository
9
+
10
+ attr_reader :path
11
+ attr_reader :cache_path
12
+ attr_reader :name
13
+ attr_reader :desc
14
+ attr_reader :main
15
+ attr_reader :ref
16
+ attr_reader :dependencies
17
+ attr_reader :ref
18
+ attr_reader :url
19
+
20
+ # Constructor
21
+ #
22
+ # @param path [String] The path of repository
23
+ def initialize(new_path, new_ref = nil)
24
+ @path = new_path
25
+ @cache_path = nil
26
+ @ref = new_ref
27
+
28
+ if ( not ref.nil? ) && ( not refs.empty? ) && ( not refs.include?(ref) )
29
+ raise Errors::NotExistRef
30
+ end
31
+
32
+ @url = nil
33
+ end
34
+
35
+ # Set repo's URL
36
+ def set_url(new_url)
37
+ @url = new_url
38
+ end
39
+
40
+ def load_cache(base_cache_path)
41
+ cache_path_cand = "#{base_cache_path}/#{name}/#{short_commit_id}"
42
+
43
+ if ::Dir.exists?(cache_path_cand)
44
+ return @cache_path = cache_path_cand
45
+ end
46
+
47
+ return @cache_path = nil
48
+ end
49
+
50
+ # Create cache of repository
51
+ #
52
+ # @param cache_path [String] The path of cache dir
53
+ # @return [String] the path
54
+ def create_cache(base_cache_path)
55
+ @cache_path = "#{base_cache_path}/#{name}/#{short_commit_id}"
56
+
57
+ return if ::Dir.exists?(cache_path)
58
+
59
+ ::FileUtils.mkdir_p "#{base_cache_path}/#{name}"
60
+ ::FileUtils.cp_r path, cache_path
61
+
62
+ return cache_path
63
+ end
64
+
65
+ # Load snippet.json file
66
+ def load_snippet_json
67
+ text = ::File.read(::File.join(path, "snippet.json"))
68
+ snippet_json = ::JSON.parse(text)
69
+ @name = snippet_json["name"]
70
+ @desc = snippet_json["desc"]
71
+ @main = snippet_json["main"] || ""
72
+ @dependencies = snippet_json["dependencies"] || {}
73
+ end
74
+
75
+ # Returns json text from repo instance
76
+ #
77
+ # @return json text
78
+ def to_snippet_json
79
+ required = [
80
+ :name,
81
+ ]
82
+
83
+ optional = [
84
+ :desc,
85
+ :main,
86
+ :dependencies,
87
+ ]
88
+
89
+ data = {}
90
+ required.each do |key|
91
+ data[key] = send(key)
92
+ end
93
+ optional.each do |key|
94
+ val = send(key)
95
+ if val.nil? === false && val != ""
96
+ data[key] = send(key)
97
+ end
98
+ end
99
+
100
+ return data.to_json
101
+ end
102
+
103
+ # Returns latest version
104
+ #
105
+ # @param pattern [String] The pattern of version
106
+ # @return [String] The version
107
+ def latest_version(pattern = "")
108
+ pattern = "" if pattern.nil?
109
+ matched_versions = versions.select {|ref| ::SocialSnippet::Version.is_matched_version_pattern(pattern, ref)}
110
+ return ::VersionSorter.rsort(matched_versions).first
111
+ end
112
+
113
+ # Returns all versions
114
+ #
115
+ # @return [Array<String>] All versions of repository
116
+ def versions
117
+ refs.select {|ref| ::SocialSnippet::Version.is_version(ref) }
118
+ end
119
+
120
+ def current_ref
121
+ raise "not implete current_ref"
122
+ end
123
+
124
+ # Returns all refs
125
+ #
126
+ # @return [Array<String>] All references of repository
127
+ def refs
128
+ raise "not implement refs"
129
+ end
130
+
131
+ # Returns short current ref's commit id
132
+ #
133
+ # @return [String]
134
+ def short_commit_id
135
+ commit_id[0..7]
136
+ end
137
+
138
+ # Returns current ref's commit id
139
+ #
140
+ # @return [String]
141
+ def commit_id
142
+ raise "not implement commit_id"
143
+ end
144
+
145
+ # Returns the directory or file names within repository
146
+ #
147
+ # @param glob_path [String]
148
+ def glob(glob_path)
149
+ root_path = real_path("/")
150
+ ::Dir.glob("#{root_path}/#{glob_path}")
151
+ end
152
+
153
+ # Returns the real path of given path
154
+ #
155
+ # @param target_path [String] The real path of repo's file or directory
156
+ def real_path(target_path)
157
+ # TODO: normalize path
158
+ if is_cached?
159
+ "#{cache_path}/#{main}/#{target_path}"
160
+ else
161
+ "#{path}/#{main}/#{target_path}"
162
+ end
163
+ end
164
+
165
+ # Checkout to ref
166
+ #
167
+ # @param ref [String] The reference of repository
168
+ def checkout(new_ref)
169
+ raise "not implement checkout"
170
+ end
171
+
172
+ # Check repository has version ref
173
+ #
174
+ # @return [Boolean]
175
+ def has_versions?
176
+ versions.empty? === false
177
+ end
178
+
179
+ # Check repository is cached
180
+ #
181
+ # @return [Boolean]
182
+ def is_cached?
183
+ @cache_path.nil? === false
184
+ end
185
+
186
+ def update(options = {})
187
+ throw "not implement update"
188
+ end
189
+
190
+ end # BaseRepository
191
+
192
+ end # SocialSnippet
@@ -0,0 +1,76 @@
1
+ module SocialSnippet::Repository::Drivers
2
+
3
+ require "rugged"
4
+
5
+ class GitRepository < BaseRepository
6
+
7
+ attr_reader :rugged_repo
8
+
9
+ def initialize(repo_path, new_ref = nil)
10
+ @rugged_repo = ::Rugged::Repository.new(repo_path)
11
+ super(repo_path)
12
+ end
13
+
14
+ def current_ref
15
+ rugged_repo.head.name.gsub /^refs\/heads\//, ""
16
+ end
17
+
18
+ def update
19
+ # TODO: write tests
20
+ fetch_status = rugged_repo.fetch("origin")
21
+ fetch_status[:received_bytes]
22
+ end
23
+
24
+ def commit_id
25
+ rugged_repo.head.target_id
26
+ end
27
+
28
+ def checkout(ref_name)
29
+ rugged_repo.checkout ref_name, :strategy => [:force]
30
+ end
31
+
32
+ def refs
33
+ refs = []
34
+ refs.concat remote_refs
35
+ refs.concat tags
36
+ return refs
37
+ end
38
+
39
+ def remote_refs
40
+ rugged_repo.references
41
+ .select {|ref| /^refs\/remotes\/origin\// === ref.name }
42
+ .map {|ref| /^refs\/remotes\/origin\/(.*)/.match(ref.name)[1] }
43
+ end
44
+
45
+ def tags
46
+ rugged_repo.references
47
+ .select {|ref| /^refs\/tags\// === ref.name }
48
+ .map {|ref| /^refs\/tags\/(.*)/.match(ref.name)[1] }
49
+ end
50
+
51
+ class << self
52
+
53
+ # @return path
54
+ def download(uri, dest_path = nil)
55
+ if dest_path.nil?
56
+ dest_path = Dir.mktmpdir
57
+ end
58
+
59
+ # git clone
60
+ cloned_repo = ::Rugged::Repository.clone_at uri.to_s, dest_path
61
+ cloned_repo_files = cloned_repo.head.target.tree.map {|item| item[:name] }
62
+
63
+ # check snippet.json
64
+ unless cloned_repo_files.include?("snippet.json")
65
+ raise "ERROR: Not found snippet.json in the repository"
66
+ end
67
+
68
+ cloned_repo.close
69
+ dest_path
70
+ end
71
+
72
+ end # class << self
73
+
74
+ end # GitRepository
75
+
76
+ end # SocialSnippet
@@ -0,0 +1,5 @@
1
+ module SocialSnippet::Repository::Errors
2
+
3
+ class NotExistRef < StandardError; end
4
+
5
+ end
@@ -0,0 +1,59 @@
1
+ module SocialSnippet::Repository::RepositoryFactory
2
+
3
+ class << self
4
+
5
+ def clone(repo_url)
6
+ uri = URI.parse repo_url
7
+ if is_git_repo(uri)
8
+ path = ::SocialSnippet::Repository::Drivers::GitRepository.download uri
9
+ repo = create_git_repo(path)
10
+ repo.set_url repo_url
11
+ repo.load_snippet_json
12
+ return repo
13
+ else
14
+ raise "unknown repository type"
15
+ end
16
+ end
17
+
18
+ def clone_local(repo_path)
19
+ if has_git_dir?(repo_path)
20
+ cloned_path = ::SocialSnippet::Repository::Drivers::GitRepository.download repo_path
21
+ repo = create_git_repo(cloned_path)
22
+ repo.set_url repo_path
23
+ repo.load_snippet_json
24
+ return repo
25
+ else
26
+ raise "unknown local repository type"
27
+ end
28
+ end
29
+
30
+ # Create suitable Repository class instance from path
31
+ #
32
+ # @param path [String] The path of repository
33
+ def create(path, ref = nil, options = {})
34
+ if has_git_dir?(path)
35
+ repo = create_git_repo(path, ref)
36
+ repo.load_snippet_json
37
+ return repo
38
+ end
39
+
40
+ return nil
41
+ end
42
+
43
+ def create_git_repo(path, ref = nil)
44
+ ::SocialSnippet::Repository::Drivers::GitRepository.new(path, ref)
45
+ end
46
+
47
+ def has_git_dir?(dir_path)
48
+ Dir.exists?("#{dir_path}/.git")
49
+ end
50
+
51
+ def is_git_repo(uri)
52
+ return true if uri.scheme === "git"
53
+ return true if uri.host === "github.com"
54
+ return false
55
+ end
56
+
57
+ end # class << self
58
+
59
+ end # RepositoryFactory