arduino-library 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88454f721c5525155a7c7c211b3ad5be0ff9bd18
4
- data.tar.gz: 1bbec07173a6e26da1f04068aaaeb9a38f39611e
3
+ metadata.gz: 3b5c76ad31ff11fa46db661d9c15bdd0491d371a
4
+ data.tar.gz: 15c6b0b9857623d65d41bd3f6b0364f42ffc34c6
5
5
  SHA512:
6
- metadata.gz: 6be6be182453fc56d7c34a3d2698e6a643dbd7b3f10b209f4b5c686bac9924b5f6c61bc2d1f63aa96978eebd279644f9d8ae6479760d9275c5d136d4d1279a2d
7
- data.tar.gz: 6f6694663d6516970d0d36d96ffc3918c795706df451a1b76ec9fa016af83119137776324e9bf2405c3a951ab466838f5ba8479e3e46f989897be883a763284b
6
+ metadata.gz: 233b52e36e5128b7ca9dc5373d5d2f5254e79c25226261d8c0cbbdaf5257275b1d2649e1fcddfc83f1928fc94274826b94ed26cdc8775f68fb9cdaee4930dbfa
7
+ data.tar.gz: 4ccc781670970f5ba2178842e08cbe9233c5415c8bf226e8e5b26e7b08ac364449b980a3a33802c93b2ee6e65018fb611ecf146cd11622ff27fa0f2a17ef27e2
data/README.md CHANGED
@@ -36,6 +36,49 @@ Or install it yourself as:
36
36
  Current version only contains Ruby-based API and is meant to be consumed by other projects (in particularly, check out [Arli](https://github.com/kigster/arli) — a command-line tool and an Arduino Library Manager and installer). This project is invaluable if you are you using, for example, [arduino-cmake](https://github.com/arduino-cmake/arduino-cmake) project to build and upload your Arduino Code.
37
37
 
38
38
 
39
+ ### Configuration
40
+
41
+ The gem database can be configured to download the default database from a custom URL,
42
+ keep a local cache in a custom file. It always downloads the index locally, and next time
43
+ it's invoked, the local file is used (if and only if it's size is identical
44
+ to the remote file).
45
+
46
+ You can change the configuration in two ways:
47
+
48
+ 1. Set environment variables before invoking the gem
49
+ 2. Configure the `DefaultDatabase` class variables
50
+
51
+ #### Environment Variables
52
+
53
+ * `ARDUINO_CUSTOM_LIBRARY_PATH` can be used to change local top-level path to the libraries folder.
54
+ * `ARDUINO_LIBRARY_INDEX_PATH` can be used to change the location of the cached index file.
55
+
56
+ #### Class Variables
57
+
58
+ ```ruby
59
+ require 'arduino/library'
60
+ Arduino::Library::DefaultDatabase.library_index_path =
61
+ Arduino::Library::DefaultDatabase.library_index_url =
62
+ Arduino::Library::DefaultDatabase.library_path =
63
+
64
+ # then reload the database:
65
+ Arduino::Library::DefaultDatabase.instance.setup
66
+ ```
67
+
68
+ #### Default Values:
69
+
70
+ ```ruby
71
+ DEFAULT_ARDUINO_LIBRARY_INDEX_URL =
72
+ 'http://downloads.arduino.cc/libraries/library_index.json.gz'
73
+
74
+ DEFAULT_ARDUINO_LIBRARY_PATH =
75
+ ENV['ARDUINO_CUSTOM_LIBRARY_PATH'] || (ENV['HOME'] + '/Documents/Arduino/Libraries')
76
+
77
+ DEFAULT_ARDUINO_LIBRARY_INDEX_PATH =
78
+ ENV['ARDUINO_LIBRARY_INDEX_PATH'] ||
79
+ (ENV['HOME'] + '/Documents/Arduino/Libraries/index.json.gz')
80
+ ```
81
+
39
82
  ### Using the top-level module
40
83
 
41
84
  If you prefer not to have hard-coded dependencies on the `Arduino::Library::*` sub-classes and sub-modules, you can use the top level module, which proxies several shortcut methods.
@@ -146,7 +189,7 @@ database = Arduino::Library::Database.from(
146
189
  or, since the above link happens to be the default location of Arduino-maintained librarie index file, you can use the `default` method instead:
147
190
 
148
191
  ```ruby
149
- database = Arduino::Library::Database.default
192
+ database = Arduino::Library::DefaultDatabase.instance
150
193
  ```
151
194
 
152
195
  or, load the list from a local JSON file, that can be optionally gzipped (just like the URL):
@@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'dry-configurable'
25
25
  spec.add_dependency 'dry-types'
26
26
  spec.add_dependency 'dry-struct'
27
+ spec.add_dependency 'colored2'
28
+ spec.add_dependency 'httparty'
27
29
 
28
30
  spec.add_development_dependency 'simplecov'
29
31
  spec.add_development_dependency 'bundler', '~> 1.15'
@@ -2,7 +2,13 @@ require 'open-uri'
2
2
 
3
3
  module Arduino
4
4
  module Library
5
- DEFAULT_ARDUINO_LIBRARY_INDEX_URL = 'http://downloads.arduino.cc/libraries/library_index.json.gz'
5
+ DEFAULT_ARDUINO_LIBRARY_INDEX_URL =
6
+ 'http://downloads.arduino.cc/libraries/library_index.json.gz'
7
+ DEFAULT_ARDUINO_LIBRARY_PATH =
8
+ ENV['ARDUINO_CUSTOM_LIBRARY_PATH'] || (ENV['HOME'] + '/Documents/Arduino/Libraries')
9
+ DEFAULT_ARDUINO_LIBRARY_INDEX_PATH =
10
+ ENV['ARDUINO_LIBRARY_INDEX_PATH'] ||
11
+ (ENV['HOME'] + '/Documents/Arduino/Libraries/index.json.gz')
6
12
  end
7
13
  end
8
14
 
@@ -12,6 +18,7 @@ require 'arduino/library/types'
12
18
  # noinspection RubyResolve
13
19
  require 'arduino/library/model'
14
20
  require 'arduino/library/database'
21
+ require 'arduino/library/default_database'
15
22
 
16
23
  module Arduino
17
24
  module Library
@@ -21,7 +28,7 @@ module Arduino
21
28
  end
22
29
 
23
30
  def db_default
24
- Database.default
31
+ DefaultDatabase.instance
25
32
  end
26
33
 
27
34
  #
@@ -1,33 +1,30 @@
1
- require 'open-uri'
2
- require 'zlib'
3
1
  require 'json'
4
2
  require 'forwardable'
5
- require 'arduino/library'
6
3
  require 'arduino/library/model'
4
+ require 'arduino/library/utilities'
5
+ require 'colored2'
7
6
 
8
7
  module Arduino
9
8
  module Library
10
-
11
9
  # This class represents a single entry into the library-index.json file,
12
10
  # in other words — a `library.properties` file.
13
11
  class Database
14
- extend Forwardable
15
- include Utilities
16
12
 
17
- def_delegators :@db_list, *(Array.new.methods - Object.methods - [:search])
13
+ include Utilities
14
+ extend Forwardable
18
15
 
19
16
  class << self
20
17
  alias from new
21
-
22
- def default
23
- @default ||= new
24
- end
25
18
  end
26
19
 
20
+ def_delegators :@db_list,
21
+ *(Array.new.methods -
22
+ Object.methods - [:search])
23
+
27
24
  attr_accessor :local_file,
28
25
  :db_list
29
26
 
30
- def initialize(file_or_url = Arduino::Library::DEFAULT_ARDUINO_LIBRARY_INDEX_URL)
27
+ def initialize(file_or_url)
31
28
  self.local_file = read_file_or_url(file_or_url)
32
29
  load_json
33
30
  end
@@ -48,25 +45,25 @@ module Arduino
48
45
  match_list
49
46
  end
50
47
 
51
- private
48
+ protected
52
49
 
53
50
  def entry_matches?(entry, opts)
54
51
  matches = true
55
52
  opts.each_pair do |attr, check|
56
53
  value = entry.send(attr)
57
54
  matches &= case check
58
- when String
59
- value == check
60
- when Regexp
61
- value =~ check
62
- when Array
63
- value = value.split(',') unless value.is_a?(Array)
64
- value.eql?(check) || value.include?(check) || value.first == '*'
65
- when Proc
66
- check.call(value)
67
- else
68
- raise InvalidArgument, "Class #{check.class.name} is unsupported for value checks"
69
- end
55
+ when String
56
+ value == check
57
+ when Regexp
58
+ value =~ check
59
+ when Array
60
+ value = value.split(',') unless value.is_a?(Array)
61
+ value.eql?(check) || value.include?(check) || value.first == '*'
62
+ when Proc
63
+ check.call(value)
64
+ else
65
+ raise InvalidArgument, "Class #{check.class.name} is unsupported for value checks"
66
+ end
70
67
  break unless matches
71
68
  end
72
69
  end
@@ -74,7 +71,7 @@ module Arduino
74
71
  private
75
72
 
76
73
  def load_json
77
- hash = JSON.load(local_file.read)
74
+ hash = JSON.load(local_file.read)
78
75
  self.db_list = hash['libraries'].map { |lib| Model.from_hash(lib) }
79
76
  end
80
77
  end
@@ -0,0 +1,60 @@
1
+ require 'httparty'
2
+ require 'open-uri'
3
+ require_relative 'database'
4
+
5
+ module Arduino
6
+ module Library
7
+ # This class represents a single entry into the library-index.json file,
8
+ # in other words — a `library.properties` file.
9
+
10
+ class DefaultDatabase < Database
11
+
12
+ class << self
13
+ attr_accessor :library_index_path,
14
+ :library_path,
15
+ :library_index_url
16
+
17
+ def instance
18
+ @default ||= self.send(:new)
19
+ end
20
+ end
21
+
22
+ self.library_index_path ||= DEFAULT_ARDUINO_LIBRARY_INDEX_PATH
23
+ self.library_index_url ||= DEFAULT_ARDUINO_LIBRARY_INDEX_URL
24
+ self.library_path ||= DEFAULT_ARDUINO_LIBRARY_PATH
25
+
26
+ attr_accessor :url, :path
27
+
28
+ def initialize
29
+ setup
30
+ end
31
+
32
+ def setup
33
+ self.url = self.class.library_index_url
34
+ self.path = self.class.library_index_path
35
+
36
+ FileUtils.mkpath(File.dirname(path))
37
+
38
+ download_if_needed!
39
+
40
+ self.local_file = open_plain_or_gzipped(path)
41
+
42
+ load_json
43
+ end
44
+
45
+ def download_if_needed!
46
+ if File.exist?(path)
47
+ resp = HTTParty.head(url)
48
+ remote_size = resp['content-length'].to_i
49
+ local_size = File.size(path)
50
+ debug("remote: #{remote_size}, local #{local_size}")
51
+ return if remote_size == local_size
52
+ backup_previous_library(path)
53
+ end
54
+
55
+ download(url, path)
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -39,7 +39,7 @@ module Arduino
39
39
  end
40
40
 
41
41
  def database
42
- @database ||= Database.default
42
+ @database ||= DefaultDatabase.instance
43
43
  end
44
44
 
45
45
  def find(**opts)
@@ -56,7 +56,7 @@ module Arduino
56
56
  #
57
57
  # Arduino::Library::Model.database = Database.from(file)
58
58
  #
59
- # otherwise it defaults to the default database, +Database.default+.
59
+ # otherwise it defaults to the default database, +DefaultDatabase.instance+.
60
60
  #
61
61
  # @param [Hash] opts — search parameters to the current database
62
62
  #
@@ -1,19 +1,52 @@
1
1
  require 'open-uri'
2
+ require 'fileutils'
3
+ require 'zlib'
2
4
 
3
5
  module Arduino
4
6
  module Library
5
-
6
7
  module Utilities
8
+
7
9
  def read_file_or_url(file_or_url)
8
10
  raise ArgumentError, 'Empty file_or_url provided' unless file_or_url
9
11
  temp_file = open(file_or_url)
12
+ open_plain_or_gzipped(file_or_url, temp_file)
13
+ end
14
+
15
+ def open_plain_or_gzipped(file_or_url, temp_file = nil)
10
16
  if file_or_url =~ /\.gz$/i
11
- Zlib::GzipReader.new(temp_file)
17
+ Zlib::GzipReader.new(temp_file || File.open(file_or_url))
12
18
  else
13
19
  temp_file
14
20
  end
15
21
  end
16
- end
17
22
 
23
+ def backup_previous_library(path)
24
+ debug "backup previous library: #{path.bold.green}"
25
+ new_name = nil
26
+ index = 0
27
+
28
+ loop do
29
+ index += 1
30
+
31
+ new_name = "#{path}.#{index}"
32
+ break unless File.exist?(new_name)
33
+ debug "file #{new_name.bold.green} exists, next..."
34
+ raise 'Too many backup versions created, delete some first' if index > 20
35
+ end
36
+
37
+ debug "moving #{path.bold.green}", "to #{new_name.bold.blue}"
38
+ FileUtils.move(path, new_name)
39
+ end
40
+
41
+ def download(url, path)
42
+ open(path, 'wb') do |file|
43
+ file << open(url).read
44
+ end
45
+ end
46
+
47
+ def debug(*msgs)
48
+ puts "\n" + msgs.join("\n") if ENV['DEBUG']
49
+ end
50
+ end
18
51
  end
19
52
  end
@@ -1,6 +1,6 @@
1
1
  module Arduino
2
2
  module Library
3
- VERSION = '0.3.1'
3
+ VERSION = '0.3.2'
4
4
  DESCRIPTION = <<-eof
5
5
  This gem encapsulates many concepts related to how Arduino Libraries are indexed, how their metadata is validated, or .properties file generated. It supports searching the Arduino library database for any terms. This gem is used by Arli — Arduino Installer CLI toolkit.
6
6
  eof
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arduino-library
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Gredeskoul
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-22 00:00:00.000000000 Z
11
+ date: 2017-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colored2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: httparty
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: simplecov
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -144,6 +172,7 @@ files:
144
172
  - bin/setup
145
173
  - lib/arduino/library.rb
146
174
  - lib/arduino/library/database.rb
175
+ - lib/arduino/library/default_database.rb
147
176
  - lib/arduino/library/include.rb
148
177
  - lib/arduino/library/model.rb
149
178
  - lib/arduino/library/presenters/properties.rb