ssource 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ddf1f3e17e7c2bed07024eb1c436dde98b82e7b5
4
+ data.tar.gz: 8d755340ebcf8602fe862a8d1ee37c8c2e0e8aa6
5
+ SHA512:
6
+ metadata.gz: 392ee99ed712d0b12e565c963efc2a0eaa9e5689f0d111015211e606efb86087e5b072a274bdf0e0e474435bf36a7e605bb4250575e21130f7f2000319d3548b
7
+ data.tar.gz: e562719e5dfa8dc38fc72249367734f9f1e3beb8f7987f6f5ed0c8c244c45fb687df40479c3063d33e511caeb36a538c5373b71e2f677ae25f855db270db78c3
data/LICENSE ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Ssource
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ssource`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ssource'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ssource
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ssource. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/bin/ss ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if $PROGRAM_NAME == __FILE__
4
+ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
5
+ require 'bundler/setup'
6
+ end
7
+
8
+ require 'ssource'
9
+
10
+ Ssource::Command.run(ARGV)
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ module Ssource
6
+ class Command
7
+ class Show < Command
8
+ self.summary = 'Shows an overview of a swift file in a YAML representation.'
9
+
10
+ def initialize(argv)
11
+ self.file_path = argv.shift_argument
12
+ super
13
+ end
14
+
15
+ def run
16
+ @root = Ssource::Source.from file_path
17
+ puts sections * "\n\n"
18
+ end
19
+
20
+ private
21
+
22
+ def sections
23
+ [].tap do |sections|
24
+ @root.pretty_print.each do |key, value|
25
+ sections << yamlize(value).prepend(key.green)
26
+ end
27
+ end
28
+ end
29
+
30
+ def yamlize(value)
31
+ yaml = value.to_yaml
32
+ yaml.gsub!(/^---$/, '')
33
+ yaml.gsub!(/^--- \[\]$/, '')
34
+ yaml.gsub!(/^-/, "\n-")
35
+ yaml
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require 'claide'
2
+ require 'colorize'
3
+
4
+ module Ssource
5
+ class Command < CLAide::Command
6
+ require 'ssource/command/show'
7
+
8
+ self.abstract_command = true
9
+ self.command = 'ss'
10
+ self.version = VERSION
11
+ self.description = 'Ssrouce lets you parse swift file from Ruby.'
12
+
13
+ def initialize(argv)
14
+ super
15
+ end
16
+
17
+ attr_reader :file_path
18
+
19
+ def file_path=(path)
20
+ @file_path = path && Pathname.new(path).expand_path
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ module Ssource
2
+ module Source
3
+ class Element
4
+ attr_reader :name, :kind, :accessibility, :elements
5
+ def initialize(json)
6
+ @name = json['name']
7
+ @accessibility = json['accessibility']
8
+ @kind = json['kind']
9
+
10
+ @elements = json.fetch('substructure', []).reduce([]) do |arr, structure|
11
+ arr << Factory.build(structure)
12
+ end
13
+ end
14
+
15
+ def display_name
16
+ name
17
+ end
18
+
19
+ def to_hash
20
+ { 'name' => display_name }
21
+ end
22
+
23
+ def pretty_print
24
+ dispay_name
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'klass'
2
+
3
+ module Ssource
4
+ module Source
5
+ class Extension < Klass
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'extension'
2
+
3
+ module Ssource
4
+ module Source
5
+ module Factory
6
+ def self.build(json)
7
+ const = case json['kind']
8
+ when ->(kind) { kind.include? 'decl.var' } then Variable
9
+ when ->(kind) { kind.include? 'decl.class' } then Klass
10
+ when ->(kind) { kind.include? 'decl.function' } then Method
11
+ when ->(kind) { kind.include? 'decl.extension' } then Extension
12
+ else Element
13
+ end
14
+ const.new json
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ require_relative 'element'
2
+ require_relative 'method'
3
+ require_relative 'variable'
4
+
5
+ module Ssource
6
+ module Source
7
+ class Klass < Element
8
+ def initialize(json)
9
+ super
10
+ end
11
+
12
+ def methods
13
+ elements.select { |element| element.is_a? Method }
14
+ end
15
+
16
+ def instance_methods
17
+ methods.select(&:instance?)
18
+ end
19
+
20
+ def static_methods
21
+ methods.select(&:static?)
22
+ end
23
+
24
+ def class_methods
25
+ methods.select(&:class?)
26
+ end
27
+
28
+ def variables
29
+ elements.select { |element| element.is_a? Variable }
30
+ end
31
+
32
+ def to_hash
33
+ elements_variables.each_with_object(super) do |method, hash|
34
+ collections = send(method).map(&:to_hash)
35
+ hash[instance_variable.to_s.capitalize] = collections unless collections.empty?
36
+ end
37
+ end
38
+
39
+ def pretty_print
40
+ result = elements_variables.each_with_object(super) do |method, hash|
41
+ collections = send(method).map(&:pretty_print)
42
+ hash[instance_variable.to_s.capitalize] = collections unless collections.empty?
43
+ end
44
+ { display_name => result }
45
+ end
46
+
47
+ def elements_variables
48
+ %i[variables instance_methods static_methods class_methods]
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,35 @@
1
+ module Ssource
2
+ module Source
3
+ class Method < Element
4
+ def initialize(json)
5
+ super
6
+ end
7
+
8
+ def instance?
9
+ kind.end_with? 'instance'
10
+ end
11
+
12
+ def class?
13
+ kind.end_with? 'class'
14
+ end
15
+
16
+ def static?
17
+ kind.end_with? 'static'
18
+ end
19
+
20
+ def parameters
21
+ elements.select { |element| element.is_a? Variable }
22
+ end
23
+
24
+ def to_hash
25
+ hash = super
26
+ hash[:parameters] = parameters.map(&:to_hash) unless parameters.empty?
27
+ hash
28
+ end
29
+
30
+ def pretty_print
31
+ display_name
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'factory'
2
+ require_relative 'klass'
3
+
4
+ module Ssource
5
+ module Source
6
+ class Root
7
+ extend Forwardable
8
+
9
+ attr_reader :elements
10
+
11
+ def initialize(file)
12
+ elements = Ssource::SourceKitten.structure file
13
+ @elements = elements.map { |element| Factory.build element }
14
+ end
15
+
16
+ def_delegators :@elements, :[], :first
17
+
18
+ def self.from(file)
19
+ Root.new(file)
20
+ end
21
+
22
+ def pretty_print
23
+ %i[classes functions extensions].each_with_object({}) do |instance_variable, hash|
24
+ collections = send(instance_variable).map(&:pretty_print)
25
+ hash[instance_variable.to_s.capitalize] = collections unless collections.empty?
26
+ end
27
+ end
28
+
29
+ def classes
30
+ elements.select { |e| e.is_a? Klass }
31
+ end
32
+
33
+ def extensions
34
+ elements.select { |e| e.is_a? Extension }
35
+ end
36
+
37
+ def functions
38
+ elements.select { |e| e.is_a? Method }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ module Ssource
2
+ module Source
3
+ class Variable < Element
4
+ attr_reader :typename
5
+ def initialize(json)
6
+ super
7
+ @typename = json['typename']
8
+ end
9
+
10
+ def to_hash
11
+ "#{name}: #{typename}"
12
+ end
13
+
14
+ def pretty_print
15
+ to_hash
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'source/root'
2
+
3
+ module Ssource
4
+ module Source
5
+ def self.from(file)
6
+ Root.new(file)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+ require 'active_support/core_ext'
3
+
4
+ class Hash
5
+ def purify_hash
6
+ hash = transform_keys do |key|
7
+ return value unless key.start_with? 'key.'
8
+ key[4..-1]
9
+ end
10
+ substructure = hash['substructure']
11
+ return hash unless substructure
12
+ hash['substructure'] = substructure.map(&:purify_hash)
13
+ hash
14
+ end
15
+ end
16
+
17
+ module Ssource
18
+ module SourceKitten
19
+ class << self
20
+ def syntax(file)
21
+ JSON.parse `sourcekitten syntax --file #{file}`
22
+ end
23
+
24
+ def structure(file)
25
+ json = JSON.parse `sourcekitten structure --file #{file}`
26
+ json['key.substructure'].map(&:purify_hash)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Ssource
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/ssource.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'active_support'
2
+ require 'ssource/version'
3
+
4
+ module Ssource
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :SourceKitten
8
+ autoload :Source
9
+ autoload :Command
10
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - draveness
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: claide
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.1
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: colorize
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.8.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.14'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.14'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ description: A ruby wrapper for SourceKitten.
104
+ email:
105
+ - i@draveness.me
106
+ executables:
107
+ - ss
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - LICENSE
112
+ - README.md
113
+ - bin/ss
114
+ - lib/ssource.rb
115
+ - lib/ssource/command.rb
116
+ - lib/ssource/command/show.rb
117
+ - lib/ssource/source.rb
118
+ - lib/ssource/source/element.rb
119
+ - lib/ssource/source/extension.rb
120
+ - lib/ssource/source/factory.rb
121
+ - lib/ssource/source/klass.rb
122
+ - lib/ssource/source/method.rb
123
+ - lib/ssource/source/root.rb
124
+ - lib/ssource/source/variable.rb
125
+ - lib/ssource/source_kitten.rb
126
+ - lib/ssource/version.rb
127
+ homepage: https://github.com/Draveness/ssource
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.6.11
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: A ruby wrapper for SourceKitten.
151
+ test_files: []