ruby-elm 0.6.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dca5811a16c26814915e18ece649c317225c7c38
4
- data.tar.gz: 9f7fa319c3f2d9b6f9e6a1f55bfe59220e76527c
3
+ metadata.gz: e03118f2609e6358434579a31cc37560564e7fe4
4
+ data.tar.gz: 4972d9e8e1163095883e23d9cf2dbdd7c6d87330
5
5
  SHA512:
6
- metadata.gz: 526c718a0eac10dfc22ed76b393f3d4f4d2722c3ff29c7155ccbaec262785ccb5d28fe3e78decb8c93f0b08d2543fd194a4198e1e582d88bc12effa9a6bfca8d
7
- data.tar.gz: 9b183da7f8139edb2c4fb87e9e8481ff6a8a4ef33bdae218721aacc2aadff41ab066dd2b4c11b4e9e5b6da4d2c83c8b63c9b96e4849bece88b09a353e447b00b
6
+ metadata.gz: 934f528bf98757909f8c9243a26a4609ef99e8c49022c0a4e159113bd878194b54c93980b61c95268eeaacb343d31431d251fdb2883605aa000c874b9a7edb48
7
+ data.tar.gz: fbc3abf86d9c5930af507196d8fcea8c18778ff347647a86dd71b86568a67217e17020570c27b49f76951f5cfc2d0ee6ab1e4f66475e7c372958b80af2e83d02
data/lib/elm.rb CHANGED
@@ -5,6 +5,7 @@ require 'elm/compiler'
5
5
  require 'elm/opt_parser'
6
6
  require 'elm/options'
7
7
  require 'elm/bin'
8
+ require 'elm/dependencies'
8
9
 
9
10
  # Elm ruby wrapper
10
11
  module Elm
@@ -0,0 +1,131 @@
1
+ require 'contracts'
2
+ require 'json'
3
+
4
+ module Elm
5
+ # When a dependency is not local or file is missing
6
+ class DependencyNotFound
7
+ end
8
+
9
+ # Existing dependency file
10
+ class DependencyFound
11
+ include Contracts::Core
12
+ include Contracts::Builtin
13
+
14
+ attr_reader :path
15
+
16
+ Contract String => DependencyFound
17
+ def initialize(path)
18
+ @path = path
19
+
20
+ self
21
+ end
22
+
23
+ Contract None => String
24
+ def content
25
+ File.read @path
26
+ end
27
+ end
28
+
29
+ # Get files in dependencies from an Elm file or code
30
+ class Dependencies
31
+ include Contracts::Core
32
+ include Contracts::Builtin
33
+
34
+ Contract String => ArrayOf[String]
35
+ def self.from_file(file)
36
+ # Error if not exists
37
+ dep = new.init File.read(file), [File.dirname(file)]
38
+ only_existing_files dep.dependencies
39
+ end
40
+
41
+ Contract String => ArrayOf[String]
42
+ def self.from_content(content)
43
+ dep = new.init content
44
+ only_existing_files dep.dependencies
45
+ end
46
+
47
+ Contract ArrayOf[String] => ArrayOf[String]
48
+ def self.only_existing_files(dependencies)
49
+ dependencies
50
+ end
51
+
52
+ Contract String => Elm::Dependencies
53
+ def init(content)
54
+ init content, find_dirs
55
+
56
+ self
57
+ end
58
+
59
+ # rubocop:disable Lint/DuplicateMethods
60
+ Contract String, ArrayOf[String] => Elm::Dependencies
61
+ def init(content, dirs)
62
+ @content = content
63
+ @dirs = dirs
64
+
65
+ self
66
+ end
67
+ # rubocop:enable Lint/DuplicateMethods
68
+
69
+ Contract None => ArrayOf[String]
70
+ def dependencies
71
+ extract_dependencies(@content).uniq
72
+ end
73
+
74
+ private
75
+
76
+ Contract None => Elm::Dependencies
77
+ def initialize
78
+ self
79
+ end
80
+
81
+ Contract None => ArrayOf[String]
82
+ def find_dirs
83
+ if File.exist? 'elm-package.json'
84
+ read_elm_package_source_dirs
85
+ else
86
+ ['']
87
+ end
88
+ end
89
+
90
+ Contract None => ArrayOf[String]
91
+ def read_elm_package_source_dirs
92
+ config = JSON.parse(File.read('elm-package.json'))
93
+ config['source-directories']
94
+ end
95
+
96
+ Contract String => ArrayOf[String]
97
+ def extract_dependencies(src)
98
+ deps = []
99
+ src.each_line do |l|
100
+ next unless l =~ /^import[[:blank:]]+([\w.]+)/
101
+ file = import_to_file Regexp.last_match(1)
102
+ if file.is_a? DependencyFound
103
+ deps << file.path
104
+ deps.concat extract_dependencies(file.content)
105
+ end
106
+ end
107
+ deps
108
+ end
109
+
110
+ Contract String => Or[DependencyFound, DependencyNotFound]
111
+ def import_to_file(directive)
112
+ relatives = @dirs.map { |dir| to_relative_file(dir, to_path(directive)) }
113
+ files = relatives.select { |path| File.exist? path }
114
+ if files.empty?
115
+ DependencyNotFound.new
116
+ else
117
+ DependencyFound.new files.first
118
+ end
119
+ end
120
+
121
+ Contract String => String
122
+ def to_path(directive)
123
+ directive.tr('.', '/') + '.elm'
124
+ end
125
+
126
+ Contract String, String => String
127
+ def to_relative_file(dir, directive)
128
+ File.join(dir, directive).gsub(%r{^/}, '')
129
+ end
130
+ end
131
+ end
@@ -3,6 +3,10 @@ require 'optparse'
3
3
  require 'elm/options'
4
4
 
5
5
  module Elm
6
+ # Error raised when trying to parse invalid option
7
+ class InvalidOptionError < RuntimeError
8
+ end
9
+
6
10
  # Parse command line options
7
11
  class OptParser
8
12
  include Contracts::Core
@@ -57,7 +61,11 @@ module Elm
57
61
  opts.separator 'Examples:'
58
62
  end
59
63
 
60
- opt_parser.parse! args
64
+ begin
65
+ opt_parser.parse! args
66
+ rescue OptionParser::InvalidOption => err
67
+ raise InvalidOptionError, err.message
68
+ end
61
69
  options
62
70
  end
63
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-elm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yves Brissaud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-12 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -177,6 +177,7 @@ files:
177
177
  - lib/elm.rb
178
178
  - lib/elm/bin.rb
179
179
  - lib/elm/compiler.rb
180
+ - lib/elm/dependencies.rb
180
181
  - lib/elm/files.rb
181
182
  - lib/elm/opt_parser.rb
182
183
  - lib/elm/options.rb