rdm 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/Gemfile.lock +80 -0
  4. data/bin/rdm +78 -10
  5. data/example/.rdm/helpers/render_helper.rb +12 -0
  6. data/example/.rdm/templates/package/.gitignore +1 -0
  7. data/example/.rdm/templates/package/.rspec +2 -0
  8. data/example/.rdm/templates/package/<%=package_subdir_name%>/<%=package_name%>.rb +3 -0
  9. data/example/.rdm/templates/package/<%=package_subdir_name%>/<%=package_name%>/.gitkeep +0 -0
  10. data/{lib/rdm/templates/package/package.rb.erb → example/.rdm/templates/package/Package.rb} +0 -0
  11. data/{lib/rdm/templates/package/bin/console_irb → example/.rdm/templates/package/bin/console} +0 -0
  12. data/example/.rdm/templates/package/spec/spec_helper.rb +10 -0
  13. data/example/.rdm/templates/repository/dao/<%=name%>_dao.rb +4 -0
  14. data/example/.rdm/templates/repository/mapper/<%=name%>_mapper.rb +2 -0
  15. data/example/.rdm/templates/repository/repository/<%=name%>_repository.rb +2 -0
  16. data/example/.rdm/templates/repository/views/users.html.erb +2 -0
  17. data/example/Gemfile.lock +50 -0
  18. data/example/Rdm.packages +1 -0
  19. data/example/tests/diff_run +0 -0
  20. data/example/tests/run +0 -0
  21. data/lib/rdm.rb +40 -8
  22. data/lib/rdm/cli/compile_package.rb +56 -0
  23. data/lib/rdm/cli/dependencies_controller.rb +30 -0
  24. data/lib/rdm/cli/diff_package.rb +21 -0
  25. data/lib/rdm/cli/gen_package.rb +24 -32
  26. data/lib/rdm/cli/init.rb +20 -27
  27. data/lib/rdm/cli/template_generator.rb +38 -0
  28. data/lib/rdm/errors.rb +37 -0
  29. data/lib/rdm/gen/init.rb +29 -44
  30. data/lib/rdm/gen/package.rb +24 -78
  31. data/lib/rdm/git/diff_command.rb +13 -0
  32. data/lib/rdm/git/diff_manager.rb +30 -0
  33. data/lib/rdm/git/repository_locator.rb +23 -0
  34. data/lib/rdm/handlers/dependencies_handler.rb +110 -0
  35. data/lib/rdm/handlers/diff_package_handler.rb +48 -0
  36. data/lib/rdm/handlers/template_handler.rb +118 -0
  37. data/lib/rdm/helpers/path_helper.rb +15 -0
  38. data/lib/rdm/package.rb +6 -0
  39. data/lib/rdm/package_importer.rb +1 -1
  40. data/lib/rdm/packages/compiler_service.rb +78 -0
  41. data/lib/rdm/packages/locator.rb +28 -0
  42. data/lib/rdm/settings.rb +14 -1
  43. data/lib/rdm/spec_runner.rb +5 -0
  44. data/lib/rdm/spec_runner/command_generator.rb +28 -0
  45. data/lib/rdm/spec_runner/command_params.rb +3 -0
  46. data/lib/rdm/spec_runner/package_fetcher.rb +13 -0
  47. data/lib/rdm/spec_runner/runner.rb +122 -0
  48. data/lib/rdm/spec_runner/view.rb +20 -0
  49. data/lib/rdm/templates/init/.rdm/helpers/render_helper.rb +12 -0
  50. data/lib/rdm/templates/init/{Gemfile.erb → Gemfile} +0 -0
  51. data/lib/rdm/templates/init/{Rdm.packages.erb → Rdm.packages} +0 -0
  52. data/lib/rdm/templates/init/{Readme.md.erb → Readme.md} +0 -0
  53. data/lib/rdm/templates/init/tests/diff_run +29 -0
  54. data/lib/rdm/templates/init/tests/run +7 -210
  55. data/lib/rdm/templates/package/<%=package_subdir_name%>/<%=package_name%>.rb +3 -0
  56. data/lib/rdm/templates/package/<%=package_subdir_name%>/<%=package_name%>/.gitkeep +0 -0
  57. data/lib/rdm/templates/package/Package.rb +8 -0
  58. data/lib/rdm/templates/package/bin/console +16 -0
  59. data/lib/rdm/templates/template_detector.rb +32 -0
  60. data/lib/rdm/templates/template_renderer.rb +49 -0
  61. data/lib/rdm/utils/file_utils.rb +20 -0
  62. data/lib/rdm/utils/render_util.rb +24 -0
  63. data/lib/rdm/utils/string_utils.rb +16 -0
  64. data/lib/rdm/version.rb +1 -1
  65. data/rdm.gemspec +1 -1
  66. data/spec/helpers/example_project_helper.rb +217 -0
  67. data/spec/helpers/git_commands_helper.rb +13 -0
  68. data/spec/rdm/cli/compile_package_spec.rb +114 -0
  69. data/spec/rdm/cli/dependencies_controller_spec.rb +50 -0
  70. data/spec/rdm/cli/diff_package_spec.rb +5 -0
  71. data/spec/rdm/cli/gen_package_spec.rb +60 -86
  72. data/spec/rdm/cli/init_spec.rb +53 -70
  73. data/spec/rdm/gen/init_spec.rb +21 -38
  74. data/spec/rdm/gen/package_spec.rb +70 -51
  75. data/spec/rdm/git/diff_manager_spec.rb +81 -0
  76. data/spec/rdm/git/repository_locator_spec.rb +31 -0
  77. data/spec/rdm/handlers/dependencies_handler_spec.rb +84 -0
  78. data/spec/rdm/handlers/diff_package_handler_spec.rb +78 -0
  79. data/spec/rdm/handlers/template_handler_spec.rb +94 -0
  80. data/spec/rdm/helpers/path_helper_spec.rb +52 -0
  81. data/spec/rdm/package/compiler_service_spec.rb +124 -0
  82. data/spec/rdm/package/locator_spec.rb +31 -0
  83. data/spec/rdm/rdm_spec.rb +2 -2
  84. data/spec/rdm/spec_runner/runner_spec.rb +12 -0
  85. data/spec/rdm/templates/template_detector_spec.rb +39 -0
  86. data/spec/rdm/templates/template_renderer_spec.rb +42 -0
  87. data/spec/spec_helper.rb +31 -25
  88. metadata +84 -17
  89. data/lib/rdm/gen/concerns/template_handling.rb +0 -81
  90. data/lib/rdm/support/colorize.rb +0 -106
  91. data/lib/rdm/support/render.rb +0 -17
  92. data/lib/rdm/support/template.rb +0 -30
  93. data/lib/rdm/templates/package/main_module_file.rb.erb +0 -3
  94. data/spec/rdm/support/colorize_spec.rb +0 -24
  95. data/spec/rdm/support/template_spec.rb +0 -20
@@ -1,40 +1,24 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Rdm::Gen::Package do
4
- before :all do
5
- Rdm::Gen::Package.disable_logger!
6
- end
7
- include SetupHelper
8
-
9
- def generate_package!
10
- Rdm::Gen::Package.generate(
11
- current_dir: project_dir,
12
- package_name: "some",
13
- package_relative_path: "domain/some",
14
- skip_tests: false
15
- )
16
- end
4
+ include ExampleProjectHelper
17
5
 
18
- def ensure_exists(file)
19
- expect(File.exists?(file)).to be true
20
- end
21
-
22
- def ensure_content(file, content)
23
- expect(File.read(file)).to match(content)
24
- end
6
+ subject { described_class }
7
+
8
+ before { initialize_example_project }
9
+ after { reset_example_project }
25
10
 
26
11
  context "sample package" do
27
- before :all do
28
- fresh_project
29
- generate_package!
30
- end
12
+ it "has generated correct files" do
13
+ FileUtils.rm_rf(File.join(example_project_path, '.rdm'))
31
14
 
32
- after :all do
33
- clean_tmp
34
- end
15
+ subject.generate(
16
+ current_path: example_project_path,
17
+ package_name: "some",
18
+ local_path: "domain/some",
19
+ )
35
20
 
36
- it "has generated correct files" do
37
- FileUtils.cd(project_dir) do
21
+ FileUtils.cd(example_project_path) do
38
22
  ensure_exists("domain/some/Package.rb")
39
23
  ensure_exists("domain/some/package/some.rb")
40
24
  ensure_exists("domain/some/package/some/")
@@ -42,46 +26,81 @@ describe Rdm::Gen::Package do
42
26
  ensure_exists("domain/some/spec/spec_helper.rb")
43
27
  ensure_exists("domain/some/.rspec")
44
28
  ensure_exists("domain/some/.gitignore")
45
- ensure_content("domain/some/package/some.rb", "module Some\n\nend\n")
46
29
  end
47
30
  end
48
31
 
49
- it "has added new entry to Rdm.packages" do
50
- FileUtils.cd(project_dir) do
51
- ensure_content("Rdm.packages", "package 'domain/some'")
32
+ it "takes template from '.rdm' directory primarily" do
33
+ File.open(File.join(example_project_path, '.rdm/templates/package/Package.rb'), 'w') do |f|
34
+ f.write('# modified file for .rdm tempalates directory')
35
+ end
36
+
37
+ subject.generate(
38
+ current_path: example_project_path,
39
+ package_name: "some",
40
+ local_path: "domain/some",
41
+ )
42
+
43
+ FileUtils.cd(example_project_path) do
44
+ ensure_content("domain/some/Package.rb", "# modified file for .rdm tempalates directory")
45
+ expect(Dir['domain/some/'].size).to eq(1)
52
46
  end
53
47
  end
54
48
 
55
- it "has logged useful output" do
56
- Rdm::Gen::Package.enable_logger!
57
- expect {
58
- fresh_project
59
- generate_package!
60
- }.to output(Regexp.new("Generated: domain/some/Package.rb")).to_stdout
61
- Rdm::Gen::Package.disable_logger!
49
+ it "has added new entry to Rdm.packages" do
50
+ subject.generate(
51
+ current_path: example_project_path,
52
+ package_name: "some",
53
+ local_path: "domain/some",
54
+ )
55
+
56
+ FileUtils.cd(example_project_path) do
57
+ ensure_content("Rdm.packages", "package 'domain/some'")
58
+ end
62
59
  end
63
60
  end
64
61
 
65
62
  context "prevents double execution" do
66
- after :all do
67
- clean_tmp
68
- end
69
-
70
63
  it "raises on second package generation" do
71
- fresh_project
72
- generate_package!
64
+ subject.generate(
65
+ current_path: example_project_path,
66
+ package_name: "some",
67
+ local_path: "domain/some",
68
+ )
69
+
73
70
  expect {
74
- generate_package!
71
+ subject.generate(
72
+ current_path: example_project_path,
73
+ package_name: "some",
74
+ local_path: "domain/some",
75
+ )
75
76
  }.to raise_error(Rdm::Errors::PackageDirExists)
76
77
  end
77
78
 
78
79
  it "raises on second package generation, if Rdm.packages includes the package (and folder does not exist)" do
79
- fresh_project
80
- generate_package!
81
- FileUtils.rm_rf(File.join(project_dir, "domain/some"))
80
+ subject.generate(
81
+ current_path: example_project_path,
82
+ package_name: "some",
83
+ local_path: "domain/some",
84
+ )
82
85
  expect {
83
- generate_package!
86
+ subject.generate(
87
+ current_path: example_project_path,
88
+ package_name: "some",
89
+ local_path: "some",
90
+ )
84
91
  }.to raise_error(Rdm::Errors::PackageExists)
85
92
  end
93
+
94
+
95
+ it "add package record to Rdm.packages file" do
96
+ subject.generate(
97
+ package_name: "database",
98
+ current_path: example_project_path,
99
+ local_path: "infrastructure/database"
100
+ )
101
+ expect(
102
+ Rdm::SourceParser.read_and_init_source(rdm_source_file).packages.keys
103
+ ).to include('database')
104
+ end
86
105
  end
87
106
  end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ describe Rdm::Git::DiffManager do
4
+ include GitCommandsHelper
5
+ subject { described_class }
6
+
7
+ describe "::get_diffs" do
8
+ before :each do
9
+ @git_example_path = File.join("/tmp", 'git_example')
10
+ FileUtils.mkdir_p(File.join(@git_example_path, 'files'))
11
+
12
+ File.open(File.join(@git_example_path, 'files/file_to_modify.rb'), 'w') { |f| f.write('I will be modified') }
13
+ File.open(File.join(@git_example_path, 'file_to_delete.rb'), 'w') { |f| f.write('I will be deleted') }
14
+ end
15
+
16
+ context "contains all modified files if present" do
17
+ before do
18
+ git_initialize_repository(@git_example_path)
19
+ git_commit_changes(@git_example_path)
20
+
21
+ @new_filename = File.join(@git_example_path, 'new_file.rb')
22
+ File.open(@new_filename, 'w') { |f| f.write('I am new file') }
23
+
24
+ @modified_filename = File.join(@git_example_path, 'file_to_modify.rb')
25
+ File.open(@modified_filename, 'a') { |f| f.write('I am modified now!') }
26
+
27
+ @deleted_filename = File.join(@git_example_path, 'file_to_delete.rb')
28
+ FileUtils.rm(@deleted_filename)
29
+
30
+ @nested_file = File.join(@git_example_path, 'files/nested_file.rb')
31
+ File.open(@nested_file, 'w') { |f| f.write("I'm nested file!") }
32
+
33
+ %x( cd #{@git_example_path} && git init && git add . )
34
+ end
35
+
36
+ it "shows new files" do
37
+ expect(subject.run(path: @git_example_path, revision: 'HEAD')).to include(@new_filename)
38
+ end
39
+
40
+ it "shows edited files" do
41
+ expect(subject.run(path: @git_example_path, revision: 'HEAD')).to include(@modified_filename)
42
+ end
43
+
44
+ it "shows deleted files" do
45
+ expect(subject.run(path: @git_example_path, revision: 'HEAD')).to include(@deleted_filename)
46
+ end
47
+
48
+ it "shows files with folder structure" do
49
+ expect(subject.run(path: @git_example_path, revision: 'HEAD')).to include(@nested_file)
50
+ end
51
+
52
+ it "returns array" do
53
+ expect(subject.run(path: @git_example_path, revision: 'HEAD')).to be_a(Array)
54
+ expect(subject.run(path: @git_example_path, revision: 'HEAD').size).to eq(4)
55
+ end
56
+ end
57
+
58
+ context "does't contain any modified files if not present" do
59
+ before do
60
+ git_initialize_repository(@git_example_path)
61
+ git_commit_changes(@git_example_path)
62
+ end
63
+
64
+ it "returns empty array of modified files" do
65
+ expect(subject.run(path: @git_example_path, revision: 'HEAD')).to eq([])
66
+ end
67
+ end
68
+
69
+ context "if git repository was not initialized" do
70
+ it "raises Rdm::Errors::GitRepositoryNotInitialized" do
71
+ expect{
72
+ subject.run(path: @git_example_path, revision: 'HEAD')
73
+ }.to raise_error(Rdm::Errors::GitRepositoryNotInitialized)
74
+ end
75
+ end
76
+
77
+ after :each do
78
+ FileUtils.rm_r(@git_example_path)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe Rdm::Git::RepositoryLocator do
4
+ include GitCommandsHelper
5
+ include ExampleProjectHelper
6
+
7
+ subject { described_class }
8
+
9
+ context "::locate" do
10
+ before { initialize_example_project }
11
+ after { reset_example_project }
12
+
13
+ it "returns root of git repository if initialized for existing path" do
14
+ %x( cd #{example_project_path} && git init )
15
+
16
+ expect(subject.locate(File.join(example_project_path, "application"))).to eq(example_project_path)
17
+ end
18
+
19
+ it "returns root of git repository if initialized for non existing path" do
20
+ %x( cd #{example_project_path} && git init )
21
+
22
+ expect(subject.locate(File.join(example_project_path, "non_existing_path"))).to eq(example_project_path)
23
+ end
24
+
25
+ it "raises error Rdm::Errors::GitRepositoryNotInitialized if not initialized" do
26
+ expect{
27
+ subject.locate(File.join(example_project_path, "application"))
28
+ }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rdm::Handlers::DependenciesHandler do
4
+ include ExampleProjectHelper
5
+
6
+ subject { described_class }
7
+
8
+ before { initialize_example_project }
9
+ after { reset_example_project }
10
+
11
+ context ":show_names" do
12
+ it "returns array of dependencies names" do
13
+ expect(
14
+ subject.show_names(
15
+ package_name: 'web',
16
+ project_path: example_project_path
17
+ )
18
+ ).to match(["core", "repository", "web"])
19
+ end
20
+ end
21
+
22
+ context ":show_packages" do
23
+ let(:result) {
24
+ subject.show_packages(
25
+ package_name: 'web',
26
+ project_path: example_project_path
27
+ )
28
+ }
29
+ it "returns array with proper size" do
30
+ expect(result.count).to eq(3)
31
+ end
32
+
33
+ it "returns array with proper size" do
34
+ expect(result.first).to be_a Rdm::Package
35
+ end
36
+ end
37
+
38
+ context ":format_for_draw" do
39
+ context "for simple case" do
40
+ let(:result) {
41
+ subject.draw(
42
+ package_name: 'web',
43
+ project_path: example_project_path
44
+ )
45
+ }
46
+ it "returns hash structure of dependencies" do
47
+ expect(result).to match(
48
+ [
49
+ "web",
50
+ "└── core",
51
+ " └── repository"
52
+ ]
53
+ )
54
+ end
55
+ end
56
+
57
+ context "for cycle dependencies" do
58
+ before do
59
+ core_package_file = File.join(example_project_path, "domain/core", Rdm::PACKAGE_FILENAME)
60
+ Rdm::Utils::FileUtils.change_file(core_package_file) do |line|
61
+ line.include?('import "repository"') ? ' import "web"' : line
62
+ end
63
+ end
64
+
65
+ let(:result) {
66
+ subject.draw(
67
+ package_name: 'web',
68
+ project_path: example_project_path
69
+ )
70
+ }
71
+
72
+ it "use one at time" do
73
+ expect(result).to match(
74
+ [
75
+ "web",
76
+ "└── core",
77
+ " └── web",
78
+ " └── ..."
79
+ ]
80
+ )
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe Rdm::Handlers::DiffPackageHandler do
4
+ include ExampleProjectHelper
5
+ include GitCommandsHelper
6
+
7
+ subject { described_class }
8
+
9
+ context "::handle" do
10
+ before { initialize_example_project }
11
+ after { reset_example_project }
12
+
13
+ context "when git changes present" do
14
+ context "for package without dependencies" do
15
+ before do
16
+ git_initialize_repository(example_project_path)
17
+ git_commit_changes(example_project_path)
18
+
19
+ File.open(File.join(example_project_path, 'server/package/new_server.rb'), 'w') { |f| f.write '' }
20
+
21
+ git_index_changes(example_project_path)
22
+ end
23
+
24
+ it "returns only modified packages" do
25
+ expect(subject.handle(path: example_project_path, revision: 'HEAD')).to match(["server"])
26
+ end
27
+ end
28
+
29
+ context "for package with dependencies" do
30
+ before do
31
+ git_initialize_repository(example_project_path)
32
+ git_commit_changes(example_project_path)
33
+
34
+ File.open(File.join(example_project_path, 'domain/core/package/core/new_service.rb'), 'w') do |f|
35
+ f.write <<~EOF
36
+ class Core::NewService
37
+ end
38
+ EOF
39
+ end
40
+ end
41
+
42
+ it "returns array of modified and dependend packages for indexed changes" do
43
+ git_index_changes(example_project_path)
44
+
45
+ expect(subject.handle(path: example_project_path, revision: 'HEAD')).to match(["core", "server", "web"].sort)
46
+ end
47
+ end
48
+
49
+ context "for file without package" do
50
+ before do
51
+ git_initialize_repository(example_project_path)
52
+ git_commit_changes(example_project_path)
53
+
54
+ File.open(File.join(example_project_path, 'domain/missing_package_class.rb'), 'w') do |f|
55
+ f.write <<~EOF
56
+ class MissingPackageClass
57
+ end
58
+ EOF
59
+ end
60
+ end
61
+
62
+ it "returns empty array" do
63
+ git_index_changes(example_project_path)
64
+
65
+ expect(subject.handle(path: example_project_path, revision: 'HEAD')).to eq([])
66
+ end
67
+ end
68
+ end
69
+
70
+ context "when git repository is not initialized" do
71
+ it "raises Rdm::Errors::GitRepositoryNotInitialized error" do
72
+ expect{
73
+ subject.handle(path: example_project_path, revision: 'HEAD')
74
+ }.to raise_error(Rdm::Errors::GitRepositoryNotInitialized)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rdm::Handlers::TemplateHandler do
4
+ include ExampleProjectHelper
5
+
6
+ subject { Rdm::Handlers::TemplateHandler }
7
+
8
+ let(:template_name) { 'repository' }
9
+ let(:local_path) { 'domain/infrastructure' }
10
+ let(:created_abs_path) { File.join(example_project_path, local_path) }
11
+ let(:stdout) { SpecLogger.new }
12
+
13
+ before { initialize_example_project }
14
+ after { reset_example_project }
15
+
16
+ describe "::generate" do
17
+ context "for existing template" do
18
+ it "creates all files from template to destination folder" do
19
+ subject.generate(
20
+ template_name: template_name,
21
+ local_path: local_path,
22
+ current_path: example_project_path,
23
+ locals: {
24
+ name: 'users'
25
+ }
26
+ )
27
+
28
+ ensure_exists(File.join(created_abs_path, 'dao/users_dao.rb'))
29
+ ensure_exists(File.join(created_abs_path, 'mapper/users_mapper.rb'))
30
+ ensure_exists(File.join(created_abs_path, 'repository/users_repository.rb'))
31
+ end
32
+
33
+ it "creates files with proper content" do
34
+ subject.generate(
35
+ template_name: template_name,
36
+ local_path: local_path,
37
+ current_path: example_project_path,
38
+ locals: {
39
+ name: 'users'
40
+ }
41
+ )
42
+
43
+ ensure_content(
44
+ File.join(created_abs_path, 'dao/users_dao.rb'),
45
+ "require 'users_repository'\n\nclass Users\nend"
46
+ )
47
+ end
48
+
49
+ it "skips erb files" do
50
+ subject.generate(
51
+ template_name: template_name,
52
+ local_path: local_path,
53
+ current_path: example_project_path,
54
+ locals: {
55
+ name: 'users'
56
+ }
57
+ )
58
+
59
+ ensure_content(
60
+ File.join(created_abs_path, 'views/users.html.erb'),
61
+ "class <%=name%>\nend"
62
+ )
63
+ end
64
+
65
+ context "asks for undefined variables" do
66
+ before do
67
+ subject.generate(
68
+ template_name: template_name,
69
+ local_path: local_path,
70
+ current_path: example_project_path,
71
+ locals: {},
72
+ stdout: stdout,
73
+ stdin: SpecLogger.new(stdin: "order\n")
74
+ )
75
+ end
76
+
77
+ it "output ask variable text" do
78
+ expect(stdout.output).to match([
79
+ "Undefined variables were found:",
80
+ " 1. name", "Type value for 'name': "
81
+ ])
82
+ end
83
+
84
+ it "creates file with correct variable" do
85
+ ensure_content(
86
+ File.join(created_abs_path, 'dao/order_dao.rb'),
87
+ "require 'order_repository'\n\nclass Order\nend"
88
+ )
89
+ end
90
+
91
+ end
92
+ end
93
+ end
94
+ end