ruby_npm 0.1.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyNPM
6
+ module Commands
7
+ class Install < Base
8
+ # @!visibility private
9
+ def subcommands
10
+ %w[install]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'commands/install'
4
+
5
+ module RubyNPM
6
+ module Commands
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNPM
4
+ module Errors
5
+ class ExecutionError < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'errors/execution_error'
4
+
5
+ module RubyNPM
6
+ module Errors
7
+ end
8
+ end
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'immutable-struct'
4
+
5
+ require_relative 'name'
6
+ require_relative 'types'
7
+ require_relative 'values'
8
+
9
+ module RubyNPM
10
+ module Options
11
+ # rubocop:disable Metrics/ClassLength
12
+ class Definition < ImmutableStruct.new(
13
+ :name,
14
+ :option_type,
15
+ :value_type,
16
+ :override_keys,
17
+ :extra_keys,
18
+ :separator,
19
+ :placement,
20
+ :repeatable?
21
+ )
22
+ # rubocop:disable Metrics/MethodLength
23
+ def initialize(opts)
24
+ raise 'Missing name.' unless opts[:name]
25
+
26
+ super(
27
+ name: Name.new(opts[:name]),
28
+ option_type: Types.resolve(opts[:option_type]) || Types::Standard,
29
+ value_type: Values.resolve(opts[:value_type]) || Values::String,
30
+ repeatable: opts[:repeatable] || false,
31
+ separator: opts[:separator],
32
+ placement: opts[:placement],
33
+ extra_keys:
34
+ { singular: [], plural: [] }
35
+ .merge(opts[:extra_keys] || {}),
36
+ override_keys:
37
+ { singular: nil, plural: nil }
38
+ .merge(opts[:override_keys] || {})
39
+ )
40
+ end
41
+ # rubocop:enable Metrics/MethodLength
42
+
43
+ def matches?(name)
44
+ @name == Name.new(name)
45
+ end
46
+
47
+ def build(parameters)
48
+ build_singular_options(parameters) + build_plural_options(parameters)
49
+ end
50
+
51
+ private
52
+
53
+ def resolved_singular_key
54
+ if override_keys[:singular] == false
55
+ nil
56
+ else
57
+ override_keys[:singular] || name.as_singular_key
58
+ end
59
+ end
60
+
61
+ def all_singular_keys
62
+ ([resolved_singular_key] + extra_keys[:singular]).compact
63
+ end
64
+
65
+ def resolved_plural_key
66
+ if override_keys[:plural] == false
67
+ nil
68
+ else
69
+ override_keys[:plural] || name.as_plural_key
70
+ end
71
+ end
72
+
73
+ def all_plural_keys
74
+ ([resolved_plural_key] + extra_keys[:plural]).compact
75
+ end
76
+
77
+ def too_many_values?(values)
78
+ !repeatable? && values.length > 1
79
+ end
80
+
81
+ def values(parameters, keys)
82
+ keys.map { |k| parameters[k] }.compact
83
+ end
84
+
85
+ def needs_plural?(value)
86
+ repeatable? && !value.nil?
87
+ end
88
+
89
+ def only_singular?(value)
90
+ !needs_plural?(value)
91
+ end
92
+
93
+ def key_valued?(value)
94
+ value.respond_to?(:keys)
95
+ end
96
+
97
+ def multi_valued?(value)
98
+ value.respond_to?(:each)
99
+ end
100
+
101
+ def build_option(value)
102
+ option_type.new(name, value, separator: separator, placement: placement)
103
+ end
104
+
105
+ def build_value(value)
106
+ value_type.new(value)
107
+ end
108
+
109
+ def build_key_value(key, value)
110
+ Values::KeyValue.new(key, build_value(value))
111
+ end
112
+
113
+ def build_singular(value)
114
+ build_single_option(value)
115
+ end
116
+
117
+ def build_singulars(values)
118
+ values.map { |p| build_singular(p) }.flatten
119
+ end
120
+
121
+ def build_singular_options(parameters)
122
+ keys = all_singular_keys
123
+ values = values(parameters, keys)
124
+
125
+ if too_many_values?(values)
126
+ raise "Multiple values provided for '#{name}' " \
127
+ "(with keys #{keys}) and option not repeatable."
128
+ end
129
+
130
+ build_singulars(values)
131
+ end
132
+
133
+ def build_plural(value)
134
+ if only_singular?(value)
135
+ build_no_options
136
+ elsif key_valued?(value)
137
+ build_key_value_options(value)
138
+ elsif multi_valued?(value)
139
+ build_multiple_options(value)
140
+ else
141
+ build_single_option(value)
142
+ end
143
+ end
144
+
145
+ def build_plurals(values)
146
+ values.map { |p| build_plural(p) }.flatten
147
+ end
148
+
149
+ def build_plural_options(parameters)
150
+ keys = all_plural_keys
151
+ values = values(parameters, keys)
152
+
153
+ build_plurals(values)
154
+ end
155
+
156
+ def build_key_value_options(value)
157
+ value.map { |k, v| build_option(build_key_value(k, v)) }
158
+ end
159
+
160
+ def build_multiple_options(value)
161
+ value.map { |v| build_option(build_value(v)) }
162
+ end
163
+
164
+ def build_single_option(value)
165
+ [build_option(build_value(value))]
166
+ end
167
+
168
+ def build_no_options
169
+ []
170
+ end
171
+ end
172
+ # rubocop:enable Metrics/ClassLength
173
+ end
174
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNPM
4
+ module Options
5
+ DEFINITIONS = [].freeze
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'name'
4
+
5
+ module RubyNPM
6
+ module Options
7
+ class Factory
8
+ def initialize(definitions)
9
+ @definitions = definitions
10
+ end
11
+
12
+ def resolve(names, parameters)
13
+ names
14
+ .map { |name| Name.new(name) }
15
+ .inject([]) do |options, name|
16
+ options + resolve_name(name, parameters)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def resolve_name(name, parameters)
23
+ @definitions.find { |d| d.matches?(name) }.build(parameters)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'immutable-struct'
4
+
5
+ module RubyNPM
6
+ module Options
7
+ class Name < ImmutableStruct.new(:name)
8
+ def initialize(name)
9
+ super(name: name.to_s)
10
+ end
11
+
12
+ def name
13
+ "-#{without_prefix}"
14
+ end
15
+
16
+ alias to_s name
17
+
18
+ def as_singular_key
19
+ snake_case.to_sym
20
+ end
21
+
22
+ def as_plural_key
23
+ "#{snake_case}s".to_sym
24
+ end
25
+
26
+ private
27
+
28
+ def without_prefix
29
+ @name.sub(/^-+/, '')
30
+ end
31
+
32
+ def snake_case
33
+ without_prefix.gsub('-', '_')
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNPM
4
+ module Options
5
+ module Sets
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNPM
4
+ module Options
5
+ module Types
6
+ class Flag < ImmutableStruct.new(
7
+ :name,
8
+ :value
9
+ )
10
+ def initialize(name, value, **_opts)
11
+ super(name: name, value: value)
12
+ end
13
+
14
+ def apply(builder)
15
+ value.resolve ? builder.with_flag(name) : builder
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNPM
4
+ module Options
5
+ module Types
6
+ class Standard < ImmutableStruct.new(
7
+ :name,
8
+ :value,
9
+ :separator,
10
+ :placement
11
+ )
12
+ def initialize(name, value, **opts)
13
+ super(
14
+ name: name, value: value,
15
+ separator: opts[:separator],
16
+ placement: opts[:placement]
17
+ )
18
+ end
19
+
20
+ def apply(builder)
21
+ builder.with_option(
22
+ name,
23
+ value.render,
24
+ separator: separator,
25
+ placement: placement
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'types/standard'
4
+ require_relative 'types/flag'
5
+
6
+ module RubyNPM
7
+ module Options
8
+ module Types
9
+ def self.standard(name, value, **opts)
10
+ Standard.new(name, value, **opts)
11
+ end
12
+
13
+ def self.flag(name, value)
14
+ Flag.new(name, value)
15
+ end
16
+
17
+ def self.resolve(type)
18
+ case type
19
+ when :standard then Types::Standard
20
+ when :flag then Types::Flag
21
+ else type
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'immutable-struct'
4
+
5
+ module RubyNPM
6
+ module Options
7
+ module Values
8
+ class Base < ImmutableStruct.new(:value)
9
+ def initialize(value)
10
+ super(value: value)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyNPM
6
+ module Options
7
+ module Values
8
+ class Boolean < Base
9
+ def resolve
10
+ return nil if @value.nil?
11
+ return @value if a_boolean?(@value)
12
+ return true if true_as_string?(@value)
13
+
14
+ false
15
+ end
16
+
17
+ def render
18
+ resolve&.to_s
19
+ end
20
+
21
+ private
22
+
23
+ def a_boolean?(value)
24
+ value.is_a?(TrueClass) || value.is_a?(FalseClass)
25
+ end
26
+
27
+ def true_as_string?(value)
28
+ value.respond_to?(:downcase) && value.downcase == 'true'
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require_relative 'base'
6
+
7
+ module RubyNPM
8
+ module Options
9
+ module Values
10
+ class Complex < Base
11
+ def resolve
12
+ @value
13
+ end
14
+
15
+ def render
16
+ @value.is_a?(::String) ? @value : JSON.generate(value)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'immutable-struct'
4
+
5
+ module RubyNPM
6
+ module Options
7
+ module Values
8
+ class KeyValue < ImmutableStruct.new(:key, :value)
9
+ def initialize(key, value)
10
+ super(key: key, value: value)
11
+ end
12
+
13
+ def resolve
14
+ { key => value.resolve }
15
+ end
16
+
17
+ def render
18
+ "'#{key}=#{value.render}'"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module RubyNPM
6
+ module Options
7
+ module Values
8
+ class String < Base
9
+ def resolve
10
+ @value.to_s
11
+ end
12
+
13
+ alias render resolve
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'values/boolean'
4
+ require_relative 'values/string'
5
+ require_relative 'values/complex'
6
+ require_relative 'values/key_value'
7
+
8
+ module RubyNPM
9
+ module Options
10
+ module Values
11
+ def self.boolean(value)
12
+ Boolean.new(value)
13
+ end
14
+
15
+ def self.string(value)
16
+ String.new(value)
17
+ end
18
+
19
+ def self.complex(value)
20
+ Complex.new(value)
21
+ end
22
+
23
+ def self.key_value(key, value)
24
+ KeyValue.new(key, value)
25
+ end
26
+
27
+ def self.resolve(type)
28
+ case type
29
+ when :string then Values::String
30
+ when :boolean then Values::Boolean
31
+ when :complex then Values::Complex
32
+ when :key_value then Values::KeyValue
33
+ else type
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'options/name'
4
+ require_relative 'options/types'
5
+ require_relative 'options/values'
6
+ require_relative 'options/definition'
7
+ require_relative 'options/factory'
8
+ require_relative 'options/sets'
9
+
10
+ module RubyNPM
11
+ module Options
12
+ def self.name(name)
13
+ Name.new(name)
14
+ end
15
+
16
+ def self.definition(opts)
17
+ Definition.new(opts)
18
+ end
19
+
20
+ def self.types
21
+ Types
22
+ end
23
+
24
+ def self.values
25
+ Values
26
+ end
27
+ end
28
+ end
29
+
30
+ require_relative 'options/definitions'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNPM
4
+ VERSION = '0.1.0.pre.1'
5
+ end
data/lib/ruby_npm.rb ADDED
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ruby_npm/options'
4
+ require_relative 'ruby_npm/version'
5
+ require_relative 'ruby_npm/commands'
6
+ require 'logger'
7
+
8
+ module RubyNPM
9
+ class << self
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def configure
15
+ yield(configuration)
16
+ end
17
+
18
+ def reset!
19
+ @configuration = nil
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+ def install(parameters = {}, invocation_options = {})
25
+ exec(RubyNPM::Commands::Install,
26
+ parameters, invocation_options)
27
+ end
28
+
29
+ private
30
+
31
+ def exec(command_class, parameters, invocation_options)
32
+ command_class.new.execute(parameters, invocation_options)
33
+ end
34
+ end
35
+
36
+ extend ClassMethods
37
+
38
+ def self.included(other)
39
+ other.extend(ClassMethods)
40
+ end
41
+
42
+ class Configuration
43
+ attr_accessor :binary, :logger, :options, :stdin, :stdout, :stderr
44
+
45
+ def default_logger
46
+ logger = Logger.new($stdout)
47
+ logger.level = Logger::INFO
48
+ logger
49
+ end
50
+
51
+ def default_options
52
+ Options::Factory.new(Options::DEFINITIONS)
53
+ end
54
+
55
+ def initialize
56
+ @binary = 'npm'
57
+ @logger = default_logger
58
+ @options = default_options
59
+ @stdin = ''
60
+ @stdout = $stdout
61
+ @stderr = $stderr
62
+ end
63
+ end
64
+ end
data/ruby_npm.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ruby_npm/version'
6
+ require 'date'
7
+
8
+ files = %w[
9
+ bin
10
+ lib
11
+ CODE_OF_CONDUCT.md
12
+ ruby_npm.gemspec
13
+ Gemfile
14
+ LICENSE.txt
15
+ Rakefile
16
+ README.md
17
+ ]
18
+
19
+ Gem::Specification.new do |spec|
20
+ spec.name = 'ruby_npm'
21
+ spec.version = RubyNPM::VERSION
22
+ spec.authors = ['InfraBlocks Maintainers']
23
+ spec.email = ['maintainers@infrablocks.io']
24
+
25
+ spec.summary = 'A simple Ruby wrapper for invoking NPM commands.'
26
+ spec.description =
27
+ 'Wraps the NPM CLI so that NPM can be invoked from a Ruby ' \
28
+ 'script or Rakefile.'
29
+ spec.homepage = 'https://github.com/infrablocks/ruby_npm'
30
+ spec.license = 'MIT'
31
+
32
+ spec.files = `git ls-files -z`.split("\x0").select do |f|
33
+ f.match(/^(#{files.map { |g| Regexp.escape(g) }.join('|')})/)
34
+ end
35
+ spec.bindir = 'exe'
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.required_ruby_version = '>= 2.7'
40
+
41
+ spec.add_dependency 'immutable-struct', '~> 2.4'
42
+ spec.add_dependency 'lino', '~> 3.0'
43
+
44
+ spec.add_development_dependency 'bundler'
45
+ spec.add_development_dependency 'faker'
46
+ spec.add_development_dependency 'gem-release'
47
+ spec.add_development_dependency 'guard'
48
+ spec.add_development_dependency 'guard-rspec'
49
+ spec.add_development_dependency 'rake'
50
+ spec.add_development_dependency 'rake_circle_ci'
51
+ spec.add_development_dependency 'rake_github'
52
+ spec.add_development_dependency 'rake_gpg'
53
+ spec.add_development_dependency 'rake_ssh'
54
+ spec.add_development_dependency 'rspec'
55
+ spec.add_development_dependency 'rubocop'
56
+ spec.add_development_dependency 'rubocop-rake'
57
+ spec.add_development_dependency 'rubocop-rspec'
58
+ spec.add_development_dependency 'simplecov'
59
+ spec.add_development_dependency 'terminal-notifier-guard'
60
+ spec.add_development_dependency 'yard'
61
+
62
+ spec.metadata['rubygems_mfa_required'] = 'false'
63
+ end