rdm 0.1.12 → 0.1.13

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.rubocop.yml +17 -0
  4. data/.travis.yml +13 -0
  5. data/Gemfile +5 -1
  6. data/LICENSE.txt +1 -1
  7. data/README.md +84 -28
  8. data/bin/rdm +72 -0
  9. data/bin/rubocop +6 -0
  10. data/docs/_config.yml +1 -0
  11. data/docs/index.md +98 -0
  12. data/docs/interface_brainstorming.md +31 -0
  13. data/example/Rdm.packages +1 -1
  14. data/example/Readme.md +1 -3
  15. data/lib/rdm.rb +29 -20
  16. data/lib/rdm/cli/gen_package.rb +48 -0
  17. data/lib/rdm/cli/init.rb +43 -0
  18. data/lib/rdm/config.rb +9 -1
  19. data/lib/rdm/config_manager.rb +5 -5
  20. data/lib/rdm/config_scope.rb +3 -3
  21. data/lib/rdm/errors.rb +2 -0
  22. data/lib/rdm/gen/concerns/template_handling.rb +81 -0
  23. data/lib/rdm/gen/init.rb +69 -0
  24. data/lib/rdm/gen/package.rb +99 -0
  25. data/lib/rdm/package.rb +17 -16
  26. data/lib/rdm/package_importer.rb +89 -79
  27. data/lib/rdm/package_parser.rb +25 -5
  28. data/lib/rdm/settings.rb +21 -20
  29. data/lib/rdm/source.rb +1 -1
  30. data/lib/rdm/source_locator.rb +31 -0
  31. data/lib/rdm/source_parser.rb +83 -68
  32. data/lib/rdm/support/colorize.rb +106 -0
  33. data/lib/rdm/support/render.rb +17 -0
  34. data/lib/rdm/support/template.rb +30 -0
  35. data/lib/rdm/templates/init/Gemfile.erb +25 -0
  36. data/lib/rdm/templates/init/Rdm.packages.erb +18 -0
  37. data/lib/rdm/templates/init/Readme.md.erb +24 -0
  38. data/lib/rdm/templates/{tests → init/tests}/run +27 -31
  39. data/lib/rdm/templates/package/.gitignore +1 -0
  40. data/lib/rdm/templates/{.rspec → package/.rspec} +0 -0
  41. data/lib/rdm/templates/{bin → package/bin}/console_irb +4 -5
  42. data/lib/rdm/templates/{main_module_file.rb.erb → package/main_module_file.rb.erb} +0 -0
  43. data/lib/rdm/templates/{package.rb.erb → package/package.rb.erb} +0 -0
  44. data/lib/rdm/templates/{spec → package/spec}/spec_helper.rb +0 -0
  45. data/lib/rdm/version.rb +1 -1
  46. data/rdm.gemspec +3 -0
  47. data/spec/fixtures/SampleSource.rb +3 -1
  48. data/spec/fixtures/sample_prj/Rdm.packages +12 -0
  49. data/spec/fixtures/sample_prj/infrastructure/web/Package.rb +16 -0
  50. data/spec/rdm/cli/gen_package_spec.rb +130 -0
  51. data/spec/rdm/cli/init_spec.rb +97 -0
  52. data/spec/rdm/config_manager_spec.rb +37 -1
  53. data/spec/rdm/gen/init_spec.rb +63 -0
  54. data/spec/rdm/gen/package_spec.rb +87 -0
  55. data/spec/rdm/package_importer_spec.rb +5 -1
  56. data/spec/rdm/package_parser_spec.rb +10 -24
  57. data/spec/rdm/rdm_spec.rb +42 -0
  58. data/spec/rdm/source_locator_spec.rb +45 -0
  59. data/spec/rdm/source_parser_spec.rb +45 -3
  60. data/spec/rdm/support/colorize_spec.rb +24 -0
  61. data/spec/rdm/support/template_spec.rb +20 -0
  62. data/spec/spec_helper.rb +45 -0
  63. metadata +92 -16
  64. data/bin/rdm-generate +0 -55
  65. data/bin/rdm-install +0 -13
  66. data/lib/rdm/auto_updater.rb +0 -41
  67. data/lib/rdm/package_generator.rb +0 -121
  68. data/lib/rdm/source_installer.rb +0 -35
  69. data/lib/rdm/templates/.gitignore +0 -1
@@ -8,107 +8,117 @@ class Rdm::PackageImporter
8
8
  # @param group [Optional<String>] Dependency group
9
9
  # @return [Rdm::Package] Current package
10
10
  def import_file(package_path, group: nil)
11
- ensure_updated(package_path)
12
- if File.directory?(package_path)
13
- package_path = File.join(package_path, Rdm::PACKAGE_LOCK_FILENAME)
14
- end
15
- package_content = File.open(package_path).read
16
- package = package_parser.parse(package_content)
17
-
18
- source = read_and_init_source(package.source)
19
-
20
- # Init Rdm.root based on Rdm.packages directory
21
- Rdm.root = File.dirname(package.source)
11
+ instance.import_file(package_path, group: group)
12
+ end
22
13
 
23
- # Import package and it's dependencies
24
- import_package(package.name, source: source, group: group.to_s)
14
+ # Import package and initialize module
15
+ def import_package(package_name, source:, group: nil)
16
+ instance.import_package(package_name, source: source, group: group)
17
+ end
25
18
 
26
- package
19
+ def instance
20
+ @instance ||= new
27
21
  end
28
22
 
29
- def ensure_updated(package_path)
30
- Rdm::AutoUpdater.update(package_path)
23
+ def reset!
24
+ @instance = nil
31
25
  end
26
+ end
32
27
 
33
- # Import package and initialize module
34
- def import_package(package_name, source:, imported_packages: [], imported_configs: [], group: nil)
35
- return if imported_packages.include?(package_name.to_s)
36
- package = source.packages[package_name.to_s]
28
+ # Initialize current package using Package.rb
29
+ # @param package_path [String] Package.rb file path
30
+ # @param group [Optional<String>] Dependency group
31
+ # @return [Rdm::Package] Current package
32
+ def import_file(package_path, group: nil)
33
+ package = Rdm::PackageParser.parse_file(package_path)
34
+ source = read_and_init_source(package.source)
37
35
 
38
- if package == nil
39
- raise "Can't find package with name: #{package_name.to_s}"
40
- end
36
+ # Init Rdm.root based on Rdm.packages directory
37
+ Rdm.root = File.dirname(package.source)
41
38
 
42
- init_package(package, group: group)
43
- imported_packages << package_name
39
+ # Import package and it's dependencies
40
+ import_package(package.name, source: source, group: group.to_s)
44
41
 
45
- # also import local dependencies
46
- package.local_dependencies(group).each do |dependency|
47
- import_package(dependency, source: source, imported_packages: imported_packages)
48
- end
42
+ package
43
+ end
49
44
 
50
- # also import config dependencies
51
- package.config_dependencies(group).each do |dependency|
52
- unless imported_configs.include?(dependency)
53
- import_config(dependency, source: source)
54
- end
55
- imported_configs << dependency
56
- end
45
+ def imported_packages
46
+ @imported_packages ||= []
47
+ end
57
48
 
58
- # only after importing dependencies - require package itself
59
- begin
60
- require package_name
61
- rescue LoadError => e
62
- unless Rdm.settings.silence_missing_package_file
63
- package_require_path = "#{package_name}/#{package_subdir_name}/#{package_name}.rb"
64
- puts "WARNING: Can't require package #{package_name}, please make sure that file #{package_require_path} exists and it's valid."
65
- raise e
66
- end
67
- end
49
+ def imported_configs
50
+ @imported_configs ||= []
51
+ end
52
+
53
+ # Import package and initialize module
54
+ def import_package(package_name, source:, group: nil)
55
+ return imported_packages if imported_packages.include?(package_name.to_s)
56
+ package = source.packages[package_name.to_s]
57
+
58
+ raise "Can't find package with name: #{package_name}" if package.nil?
59
+
60
+ init_package(package, group: group)
61
+ imported_packages << package_name
68
62
 
69
- imported_packages
63
+ # also import local dependencies
64
+ package.local_dependencies(group).each do |dependency|
65
+ import_package(dependency, source: source)
70
66
  end
71
67
 
72
- private
73
- def source_parser
74
- Rdm::SourceParser
75
- end
68
+ # also import config dependencies
69
+ package.config_dependencies(group).each do |dependency|
70
+ import_config(dependency, source: source)
71
+ end
76
72
 
77
- def package_parser
78
- Rdm::PackageParser
73
+ # only after importing dependencies - require package itself
74
+ begin
75
+ require package_name
76
+ rescue LoadError => e
77
+ unless Rdm.settings.silence_missing_package_file
78
+ package_require_path = "#{package_name}/#{package_subdir_name}/#{package_name}.rb"
79
+ puts "WARNING: Can't require package #{package_name}, please make sure that file #{package_require_path} exists and it's valid."
80
+ raise e
79
81
  end
82
+ end
83
+ imported_packages
84
+ end
80
85
 
81
- def package_subdir_name
82
- Rdm.settings.package_subdir_name.to_s
83
- end
86
+ private
84
87
 
85
- def init_package(package, group:)
86
- package_dir_name = File.join(package.path, package_subdir_name)
87
- $LOAD_PATH.push(package_dir_name)
88
+ def source_parser
89
+ Rdm::SourceParser
90
+ end
88
91
 
89
- package.external_dependencies(group).each do |dependency|
90
- require dependency
91
- end
92
+ def package_subdir_name
93
+ Rdm.settings.package_subdir_name.to_s
94
+ end
92
95
 
93
- package.file_dependencies(group).each do |file_path|
94
- require File.join(package.path, file_path)
95
- end
96
+ def init_package(package, group:)
97
+ package_dir_name = File.join(package.path, package_subdir_name)
98
+ $LOAD_PATH.push(package_dir_name)
96
99
 
97
- if !ActiveSupport::Dependencies.autoload_paths.include?(package_dir_name)
98
- ActiveSupport::Dependencies.autoload_paths << package_dir_name
99
- end
100
- end
100
+ package.external_dependencies(group).each do |dependency|
101
+ require dependency
102
+ end
101
103
 
102
- def import_config(config_name, source:)
103
- config = source.configs[config_name.to_s]
104
- if config == nil
105
- raise "Can't find config with name: #{config_name.to_s}"
106
- end
107
- Rdm.config.load_config(config, source: source)
108
- end
104
+ package.file_dependencies(group).each do |file_path|
105
+ require File.join(package.path, file_path)
106
+ end
109
107
 
110
- def read_and_init_source(source_path)
111
- source_parser.read_and_init_source(source_path)
112
- end
108
+ unless ActiveSupport::Dependencies.autoload_paths.include?(package_dir_name)
109
+ ActiveSupport::Dependencies.autoload_paths << package_dir_name
110
+ end
111
+ end
112
+
113
+ def import_config(config_name, source:)
114
+ return if imported_configs.include?(config_name)
115
+ config = source.configs[config_name.to_s]
116
+ raise "Can't find config with name: #{config_name}" if config.nil?
117
+ Rdm.config.load_config(config, source: source)
118
+ imported_configs << config_name
119
+ end
120
+
121
+ def read_and_init_source(source_path)
122
+ source_parser.read_and_init_source(source_path)
113
123
  end
114
124
  end
@@ -1,7 +1,27 @@
1
1
  class Rdm::PackageParser
2
- def self.parse(package_content)
3
- spec = Rdm::Package.new
4
- spec.instance_eval(package_content)
5
- spec
2
+ class << self
3
+ def parse_file(package_path)
4
+ if File.directory?(package_path)
5
+ package_path = File.join(package_path, Rdm::PACKAGE_FILENAME)
6
+ end
7
+ package_content = File.read(package_path)
8
+ package = parse(package_content)
9
+ package.path = File.dirname(package_path)
10
+ package.source(source_path(package_path))
11
+
12
+ package
13
+ end
14
+
15
+ private
16
+
17
+ def parse(package_content)
18
+ spec = Rdm::Package.new
19
+ spec.instance_eval(package_content)
20
+ spec
21
+ end
22
+
23
+ def source_path(path)
24
+ Rdm::SourceLocator.locate(path)
25
+ end
6
26
  end
7
- end
27
+ end
@@ -2,15 +2,15 @@ class Rdm::Settings
2
2
  SETTING_KEYS = [
3
3
  :role, :package_subdir_name, :configs_dir, :config_path, :role_config_path,
4
4
  :silence_missing_package_file, :silence_missing_package
5
- ]
5
+ ].freeze
6
6
 
7
- SETTING_VARIABLES = [:role, :configs_dir, :config_path, :role_config_path]
7
+ SETTING_VARIABLES = [:role, :configs_dir, :config_path, :role_config_path].freeze
8
8
 
9
9
  # Default settings
10
10
  def initialize
11
11
  silence_missing_package(false)
12
12
  silence_missing_package_file(false)
13
- package_subdir_name("package")
13
+ package_subdir_name('package')
14
14
  configs_dir('configs')
15
15
  end
16
16
 
@@ -40,24 +40,25 @@ class Rdm::Settings
40
40
  end
41
41
 
42
42
  private
43
- def write_setting(key, value)
44
- @settings ||= {}
45
- @settings[key.to_s] = value
46
- end
47
43
 
48
- def replace_variables(value, except: nil, additional_vars: {})
49
- variables_keys = SETTING_VARIABLES - [except.to_sym]
50
- new_value = value
51
- additional_vars.each do |key, variable|
52
- if new_value.match(":#{key.to_s}")
53
- new_value = new_value.gsub(":#{key.to_s}", variable)
54
- end
44
+ def write_setting(key, value)
45
+ @settings ||= {}
46
+ @settings[key.to_s] = value
47
+ end
48
+
49
+ def replace_variables(value, except: nil, additional_vars: {})
50
+ variables_keys = SETTING_VARIABLES - [except.to_sym]
51
+ new_value = value
52
+ additional_vars.each do |key, variable|
53
+ if new_value.match(":#{key}")
54
+ new_value = new_value.gsub(":#{key}", variable)
55
55
  end
56
- variables_keys.each do |key|
57
- if new_value.match(":#{key}")
58
- new_value = new_value.gsub(":#{key}", read_setting(key))
59
- end
56
+ end
57
+ variables_keys.each do |key|
58
+ if new_value.match(":#{key}")
59
+ new_value = new_value.gsub(":#{key}", read_setting(key))
60
60
  end
61
- new_value
62
61
  end
63
- end
62
+ new_value
63
+ end
64
+ end
@@ -45,4 +45,4 @@ class Rdm::Source
45
45
  def configs
46
46
  @configs || {}
47
47
  end
48
- end
48
+ end
@@ -0,0 +1,31 @@
1
+ module Rdm
2
+ class SourceLocator
3
+ def self.locate(path)
4
+ Rdm::SourceLocator.new(path).locate
5
+ end
6
+
7
+ attr_accessor :path
8
+ def initialize(path)
9
+ @path = path
10
+ end
11
+
12
+ def locate
13
+ find_source_path_in_hierarchy(path)
14
+ end
15
+
16
+ def find_source_path_in_hierarchy(some_path)
17
+ some_path = File.expand_path(some_path)
18
+ raise Rdm::Errors::SourceFileDoesNotExist, path if some_path == '/'
19
+ return potential_file(some_path) if present?(some_path)
20
+ find_source_path_in_hierarchy(File.dirname(some_path))
21
+ end
22
+
23
+ def present?(some_path)
24
+ File.exist?(potential_file(some_path))
25
+ end
26
+
27
+ def potential_file(some_path)
28
+ File.join(some_path, Rdm::SOURCE_FILENAME)
29
+ end
30
+ end
31
+ end
@@ -3,83 +3,98 @@ class Rdm::SourceParser
3
3
  end
4
4
 
5
5
  class << self
6
-
7
- # Read source file, parse and init it's packages and configs
8
- # @param source_path [String] Source file path
9
- # @return [Rdm::Source] Source
10
6
  def read_and_init_source(source_path)
11
- root_path = File.dirname(source_path)
12
- source_content = File.open(source_path).read
13
- source = parse(source_content, root_path: root_path)
7
+ Rdm::SourceParser.new(source_path).read_and_init_source
8
+ end
9
+ end
14
10
 
15
- # Setup Rdm
16
- if block = source.setup_block
17
- Rdm.setup(&block)
18
- end
19
- validate_rdm_settings!
20
-
21
- # Init and set packages
22
- packages = {}
23
- source.package_paths.each do |package_path|
24
- package_full_path = File.join(root_path, package_path)
25
- if File.exists?(package_full_path)
26
- package_rb_path = File.join(package_full_path, Rdm::PACKAGE_FILENAME)
27
- package_content = File.open(package_rb_path).read
28
- package = package_parser.parse(package_content)
29
- package.path = package_full_path
30
- packages[package.name] = package
31
- elsif !settings.silence_missing_package
32
- raise "Missing package at folder: #{package_full_path}"
33
- end
34
- end
11
+ attr_accessor :source_path
12
+
13
+ def initialize(source_path)
14
+ @source_path = source_path
15
+ end
16
+
17
+ # Read source file, parse and init it's packages and configs
18
+ # @param source_path [String] Source file path
19
+ # @return [Rdm::Source] Source
20
+ def read_and_init_source
21
+ source = parse_source_content
22
+
23
+ # Setup Rdm
24
+ if block = source.setup_block
25
+ Rdm.setup(&block)
26
+ end
27
+ validate_rdm_settings!
28
+
29
+ init_and_set_packages(source)
30
+ init_and_set_configs(source)
31
+ source.init_with(packages: packages, configs: configs)
32
+ source
33
+ end
34
+
35
+ # Parse source file and return Source object
36
+ # @return [Rdm::Source] Source
37
+ def parse_source_content
38
+ source = Rdm::Source.new(root_path: root_path)
39
+ source.instance_eval(source_content)
40
+ source
41
+ end
42
+
43
+ private
35
44
 
36
- # Init and set configs
37
- configs = {}
38
- source.config_names.each do |config_name|
39
- default_path = settings.read_setting(:config_path, vars: {config_name: config_name})
40
- role_path = settings.read_setting(:role_config_path, vars: {config_name: config_name})
41
-
42
- config = Rdm::Config.new
43
- config.default_path = default_path
44
- config.role_path = role_path
45
- config.name = config_name
46
- configs[config_name] = config
45
+ def init_and_set_packages(source)
46
+ source.package_paths.each do |package_path|
47
+ package_full_path = File.join(root_path, package_path)
48
+ if File.exist?(package_full_path)
49
+ package_rb_path = File.join(package_full_path, Rdm::PACKAGE_FILENAME)
50
+ package = Rdm::PackageParser.parse_file(package_rb_path)
51
+ packages[package.name] = package
52
+ elsif !settings.silence_missing_package
53
+ raise "Missing package at folder: #{package_full_path}"
47
54
  end
55
+ end
56
+ end
48
57
 
49
- source.init_with(packages: packages, configs: configs)
50
- source
58
+ def init_and_set_configs(source)
59
+ source.config_names.each do |config_name|
60
+ default_path = settings.read_setting(:config_path, vars: { config_name: config_name })
61
+ role_path = settings.read_setting(:role_config_path, vars: { config_name: config_name })
62
+ config = Rdm::Config.new
63
+ config.default_path = default_path
64
+ config.role_path = role_path
65
+ config.name = config_name
66
+ configs[config_name] = config
51
67
  end
68
+ end
52
69
 
53
- # Parse source file and return Source object
54
- # @param source_content [String] Source file content
55
- # @return [Rdm::Source] Source
56
- def parse(source_content, root_path: nil)
57
- source = Rdm::Source.new(root_path: root_path)
58
- source.instance_eval(source_content)
59
- source
70
+ # Make sure that all required settings are in place
71
+ def validate_rdm_settings!
72
+ if settings.read_setting(:role).nil?
73
+ raise SourceValidationError, "Please add `role` setting in Rdm.packages. E.g. \r\n setup do\r\n role { ENV['RAILS_ENV'] }\r\n end"
74
+ end
75
+ if settings.read_setting(:config_path).nil?
76
+ raise SourceValidationError, "Please add `config_path` setting in Rdm.packages. E.g. \r\n setup do\r\n config_path :configs_dir/:config_name/default.yml'\r\n end"
60
77
  end
78
+ end
61
79
 
62
- private
63
- # Make sure that all required settings are in place
64
- def validate_rdm_settings!
65
- if settings.read_setting(:role).nil?
66
- raise SourceValidationError.new(
67
- "Please add `role` setting in Rdm.packages. E.g. \r\n setup do\r\n role { ENV['RAILS_ENV'] }\r\n end"
68
- )
69
- end
70
- if settings.read_setting(:config_path).nil?
71
- raise SourceValidationError.new(
72
- "Please add `config_path` setting in Rdm.packages. E.g. \r\n setup do\r\n config_path :configs_dir/:config_name/default.yml'\r\n end"
73
- )
74
- end
75
- end
80
+ def root_path
81
+ File.dirname(source_path)
82
+ end
76
83
 
77
- def settings
78
- Rdm.settings
79
- end
84
+ # [String] Source file content
85
+ def source_content
86
+ @source_content ||= File.read(source_path)
87
+ end
80
88
 
81
- def package_parser
82
- Rdm::PackageParser
83
- end
89
+ def packages
90
+ @packages ||= {}
91
+ end
92
+
93
+ def configs
94
+ @configs ||= {}
95
+ end
96
+
97
+ def settings
98
+ Rdm.settings
84
99
  end
85
- end
100
+ end