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,51 @@
1
+ module SocialSnippet::CommandLine
2
+
3
+ class SSpm::SubCommands::PublishCommand < Command
4
+
5
+ def usage
6
+ <<EOF
7
+ Usage:
8
+ - sspm publish [options] [--] <repo-url>
9
+ - sspm publish [options] [--] <owner-id> <repo-id>
10
+ (published as "https://github.com/{owner-id}/{repo-id}.git")
11
+
12
+ Example:
13
+ $ sspm publish https://github.com/user/repo
14
+ -> published as the name written in snippet.json
15
+
16
+ [another method]
17
+ $ sspm publish user repo
18
+
19
+ Note:
20
+ - Currently the registry system supported the GitHub repositories only.
21
+
22
+ EOF
23
+ end
24
+
25
+ def desc
26
+ "Publish a repository to the registry system"
27
+ end
28
+
29
+ def define_options
30
+ end
31
+
32
+ def run
33
+ if has_next_token?
34
+ repo_url = next_token
35
+ if /^(git|http|https)\:\/\// === repo_url # url
36
+ social_snippet.api.add_url repo_url
37
+ elsif has_next_token? # {repo_owner_id} {repo_id}
38
+ owner_id = repo_url
39
+ repo_id = next_token
40
+ social_snippet.api.add_url "https://github.com/#{owner_id}/#{repo_id}.git"
41
+ else
42
+ help
43
+ end
44
+ else
45
+ help
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,32 @@
1
+ module SocialSnippet::CommandLine
2
+
3
+ class SSpm::SubCommands::SearchCommand < Command
4
+
5
+ def usage
6
+ <<EOF
7
+ Usage: sspm search [options] [--] <keyword>
8
+ EOF
9
+ end
10
+
11
+ def desc
12
+ "Search for repository by keyword"
13
+ end
14
+
15
+ def define_options
16
+ define_option :name, :type => :flag, :short => true, :default => true
17
+ define_option :desc, :type => :flag, :short => true, :default => true
18
+ define_option :url, :type => :flag, :short => true, :default => false
19
+ define_option :installed, :type => :flag, :short => true, :default => true
20
+ end
21
+
22
+ def run
23
+ if has_next_token?
24
+ social_snippet.api.search_repositories next_token, options
25
+ else
26
+ help
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,37 @@
1
+ module SocialSnippet::CommandLine
2
+
3
+ class SSpm::SubCommands::UpdateCommand < Command
4
+
5
+ def usage
6
+ <<EOF
7
+ Usage: sspm update [options] [--] [<repo> ...]
8
+
9
+ Examples:
10
+ $ sspm update
11
+ -> Update all installed repositories
12
+
13
+ $ sspm update example-repo
14
+ -> Update example-repo and Install missing dependencies
15
+ EOF
16
+ end
17
+
18
+ def desc
19
+ "Update repositories"
20
+ end
21
+
22
+ def define_options
23
+ end
24
+
25
+ def run
26
+ if has_next_token?
27
+ while has_next_token?
28
+ social_snippet.api.update_repository next_token, options
29
+ end
30
+ else
31
+ social_snippet.api.update_all_repositories options
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,92 @@
1
+ class SocialSnippet::Config
2
+
3
+ attr_reader :social_snippet
4
+
5
+ # prefix of environment variables
6
+ ENV_PREFIX = "SOCIAL_SNIPPET_"
7
+
8
+ FIELDS = [
9
+ # The path of home directory [default: "$HOME/.social-snippet"]
10
+ :home,
11
+ # Enable debug mode? [default: false]
12
+ :debug,
13
+ # Web API host [default: "sspm.herokuapp.com"]
14
+ :sspm_host,
15
+ # Web API version [default: "v0"]
16
+ :sspm_version,
17
+ # Web API protocol [default: "https"]
18
+ :sspm_protocol,
19
+ ]
20
+
21
+ # use "true" / "false"
22
+ FLAGS = [
23
+ :debug,
24
+ ]
25
+
26
+ # set accessors
27
+ FIELDS.each {|field| attr_reader field }
28
+
29
+ # Constructor
30
+ def initialize(new_social_snippet, options = {})
31
+ @social_snippet = new_social_snippet
32
+
33
+ fields = {}
34
+ fields.merge! options
35
+ load_environment_variables fields
36
+
37
+ # set default values
38
+ fields[:home] ||= "#{ENV["HOME"]}/.social-snippet"
39
+ fields[:sspm_host] ||= "sspm.herokuapp.com"
40
+ fields[:sspm_version] ||= "v0"
41
+ fields[:sspm_protocol] ||= "https"
42
+
43
+ FIELDS.each do |field_name|
44
+ key = "@#{field_name.to_s}".to_sym
45
+ instance_variable_set key, fields[field_name]
46
+ end
47
+
48
+ init_directories
49
+ end
50
+
51
+ def repository_cache_path
52
+ ::File.join home, "repo_cache"
53
+ end
54
+
55
+ def installed_repos_file
56
+ ::File.join home, "installed_repos.yml"
57
+ end
58
+
59
+ def install_path
60
+ ::File.join home, "repo"
61
+ end
62
+
63
+ def init_directories
64
+ ::FileUtils.mkdir_p home
65
+ ::FileUtils.mkdir_p install_path
66
+ ::FileUtils.mkdir_p repository_cache_path
67
+ end
68
+
69
+ def debug?
70
+ debug
71
+ end
72
+
73
+ private
74
+
75
+ def load_environment_variables(fields)
76
+ FIELDS.each do |field_sym|
77
+ fields[field_sym] ||= load_env(field_sym)
78
+ end
79
+ end
80
+
81
+ def load_env(sym)
82
+ name = sym.to_s.upcase # :foo_bar => FOO_BAR
83
+ key = "#{ENV_PREFIX}#{name}"
84
+ return nil unless ENV.has_key?(key) && (not ENV[key].nil?)
85
+ if FLAGS.include?(sym)
86
+ ENV[key] === "true"
87
+ else
88
+ ENV[key]
89
+ end
90
+ end
91
+
92
+ end
@@ -0,0 +1,89 @@
1
+ class SocialSnippet::Context
2
+
3
+ attr_reader :flag_absolute
4
+ attr_reader :path
5
+ attr_reader :repo
6
+ attr_reader :ref
7
+
8
+ # Constructor
9
+ #
10
+ # @param new_path [String] The path of context
11
+ def initialize(new_path, new_repo = nil, new_ref = nil)
12
+ @flag_absolute = is_absolute_path(new_path)
13
+ @path = new_path
14
+ @repo = new_repo
15
+ @ref = new_ref
16
+ end
17
+
18
+ # Check context in repo
19
+ #
20
+ # @return [Boolean]
21
+ def is_in_repository?
22
+ repo.nil? === false
23
+ end
24
+
25
+ # Move to new path from current path
26
+ #
27
+ # @param new_path [String] The next path
28
+ # @param new_repo [String] The next repository
29
+ # @param new_ref [String] The next reference
30
+ def move(new_path, new_repo = nil, new_ref = nil)
31
+ if new_repo.nil?
32
+ if is_absolute_path(new_path)
33
+ @flag_absolute = true
34
+ @path = new_path
35
+ else
36
+ @path = move_func(new_path)
37
+ end
38
+ else
39
+ @flag_absolute = false
40
+ @path = new_path
41
+ @repo = new_repo
42
+ @ref = new_ref
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def move_func(new_path)
49
+ source = path.split("/")
50
+ source_file = source.pop
51
+ dest = new_path.split("/")
52
+ dest_file = dest.pop
53
+
54
+ if is_absolute_path(path)
55
+ source.shift
56
+ end
57
+
58
+ dest.each do |x|
59
+ if is_dotdot(x)
60
+ source.pop
61
+ elsif ! is_dot(x)
62
+ source.push x
63
+ end
64
+ end
65
+
66
+ if flag_absolute
67
+ "/" + source.join("/") + "/" + dest_file
68
+ else
69
+ source.join("/") + "/" + dest_file
70
+ end
71
+ end
72
+
73
+ private
74
+ # Check given text is absolute path
75
+ def is_absolute_path(s)
76
+ s[0] === "/"
77
+ end
78
+
79
+ # Check given text is `.`
80
+ def is_dot(s)
81
+ s === "."
82
+ end
83
+
84
+ # Check given text is `..`
85
+ def is_dotdot(s)
86
+ s === ".."
87
+ end
88
+
89
+ end
@@ -0,0 +1,40 @@
1
+ require "tsort"
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
11
+
12
+ class SocialSnippet::Core
13
+
14
+ attr_reader :input_stream
15
+ attr_reader :output_stream
16
+ attr_reader :repo_manager
17
+ attr_reader :config
18
+ attr_reader :registry_client
19
+ attr_reader :logger
20
+ attr_reader :api
21
+
22
+ # Constructor
23
+ def initialize(new_input_stream = STDIN, new_output_stream = STDOUT)
24
+ @input_stream = new_input_stream
25
+ @output_stream = new_output_stream
26
+ @config = ::SocialSnippet::Config.new(self)
27
+ @logger = ::SocialSnippet::Logger.new output_stream
28
+ init_logger
29
+
30
+ @repo_manager = ::SocialSnippet::Repository::RepositoryManager.new(self)
31
+ @registry_client = ::SocialSnippet::Registry::RegistryClient.new(self)
32
+ @api = ::SocialSnippet::Api.new(self)
33
+ end
34
+
35
+ def init_logger
36
+ logger.level = ::SocialSnippet::Logger::Severity::INFO
37
+ logger.level = ::SocialSnippet::Logger::Severity::DEBUG if config.debug?
38
+ end
39
+
40
+ end # SocialSnippet
@@ -0,0 +1,64 @@
1
+ class SocialSnippet::Inserter
2
+
3
+ attr_reader :src_index
4
+ attr_reader :dest_index
5
+ attr_reader :src
6
+ attr_reader :dest
7
+
8
+ # Constructor
9
+ #
10
+ # @param src [Array<String>] The source code
11
+ def initialize(src)
12
+ @src_index = 0
13
+ @dest_index = -1
14
+ @src = src.clone.freeze
15
+ @dest = []
16
+ end
17
+
18
+ # Set index
19
+ #
20
+ # @param new_index [Number] The next index
21
+ def set_index(new_index)
22
+ if new_index > src.length
23
+ raise "invalid index"
24
+ end
25
+ if new_index > src_index
26
+ last_index = [new_index - 1, src.length - 1].min
27
+ insert src[src_index .. last_index]
28
+ @src_index = new_index
29
+ end
30
+ end
31
+
32
+ # Set index to last
33
+ def set_index_last
34
+ set_index src.length
35
+ end
36
+
37
+ # Ignore current line
38
+ def ignore
39
+ @src_index += 1
40
+ end
41
+
42
+ # Insert text
43
+ #
44
+ # @param line_or_lines [String or Array<String>] The inserted text
45
+ def insert(line_or_lines)
46
+ if line_or_lines.is_a?(Array)
47
+ lines = line_or_lines
48
+ dest.insert dest_index + 1, *lines
49
+ @dest_index += lines.length
50
+ else
51
+ line = line_or_lines
52
+ dest.insert dest_index + 1, line
53
+ @dest_index += 1
54
+ end
55
+ end
56
+
57
+ # Get text
58
+ #
59
+ # @return [String]
60
+ def to_s
61
+ dest.join("\n")
62
+ end
63
+
64
+ end
@@ -0,0 +1,9 @@
1
+ require "logger"
2
+
3
+ class SocialSnippet::Logger < ::Logger
4
+
5
+ def say(s)
6
+ @logdev.dev.puts s if info?
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ module SocialSnippet::Registry; end
2
+ require_relative "registry/registry_client"
3
+ require_relative "registry/registry_resources"
@@ -0,0 +1,15 @@
1
+ module SocialSnippet::Registry
2
+
3
+ class RegistryClient
4
+
5
+ attr_reader :social_snippet
6
+ attr_reader :repositories
7
+
8
+ def initialize(new_social_snippet)
9
+ @social_snippet = new_social_snippet
10
+ @repositories = RegistryResources::Repositories.new(social_snippet)
11
+ end
12
+
13
+ end
14
+
15
+ end