cbratools 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1e5877a1de1630bd28985889be2fb086d4af2579
4
+ data.tar.gz: 1b140d3a317fbf23333b99abb30a2af170faf621
5
+ SHA512:
6
+ metadata.gz: 777778e2171f462a6697ad501f5e85b7580c28ac490e9c868a4bc8d3d7288f8971ae69f232af67c96d88443fc855aca37bae6b66487b114d06b2aa5e3f055742
7
+ data.tar.gz: f5ab655b97fac07031e152326d9fcc764d27b252ec25225083cbac49d7f882e58b52416f5e70548e3ae603312410345ce352aadaaba528ec66c1224d9959f40e
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # cbratools [![Build Status](https://travis-ci.org/shageman/cbratools.svg?branch=master)](https://travis-ci.org/shageman/cbratools) [![Gem Version](https://badge.fury.io/rb/cbratools.svg)](http://badge.fury.io/rb/cbratools) [![Code Climate](https://codeclimate.com/github/shageman/cbratools.png)](https://codeclimate.com/github/shageman/cbratools) [![Dependency Status](https://gemnasium.com/shageman/cbratools.svg)](https://gemnasium.com/shageman/cbratools)
2
+
3
+ A set of tools to help with refactorings in component-based Rails applications. Specifically,
4
+
5
+ * rnc: Renames a component and its references within a CBRA application
6
+ * rnm: Creates renaming migrations of all component tables for a rename
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'cbratools'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cbratools
21
+
22
+ ## Usage
23
+
24
+ ### rnc
25
+
26
+ rnc [-v] CurrentName NewName PATH
27
+
28
+ Rename CBRA components.
29
+
30
+ Pass no options to see this help text.
31
+
32
+ Option -v is for verbose output.
33
+
34
+ ### rnm
35
+
36
+ rnm [-v] CurrentName NewName MIGRATIONS_PATH SCHEMA_FILE_PATH
37
+
38
+ Create migrations to support CBRA component rename.
39
+
40
+ Pass no options to see this help text.
41
+
42
+ Option -v is for verbose output.
43
+
44
+ ## Todos
45
+
46
+ _None yet_
47
+
48
+ ## License
49
+
50
+ Copyright (c) 2015 Stephan Hagemann, stephan.hagemann@gmail.com, [@shageman](http://twitter.com/shageman)
51
+
52
+ Released under the MIT license. See LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/cbratools.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'pathname'
2
+ require 'fileutils'
3
+
4
+ module Cbratools
5
+ require_relative("cbratools/rename_component")
6
+ require_relative("cbratools/component_name_change_migrations")
7
+ require_relative("cbratools/string")
8
+ end
@@ -0,0 +1,58 @@
1
+ module Cbratools
2
+ class ComponentNameChangeMigrations
3
+ def initialize(current_name, new_name, migrations_path, db_state_file)
4
+ @current_name = current_name
5
+ @new_name = new_name
6
+ @migrations_path = migrations_path
7
+ @db_state_file = db_state_file
8
+
9
+ @name_changes = [
10
+ [current_name, new_name],
11
+ [Cbratools::String.underscore(current_name),
12
+ Cbratools::String.underscore(new_name)]
13
+ ]
14
+ end
15
+
16
+ def add
17
+ file = File.read(@db_state_file)
18
+ p file
19
+ tables = []
20
+ file.each_line do |line|
21
+ tables << find_all_potential_table_names(line)
22
+ end
23
+ component_tables = tables.compact.select do |table|
24
+ table.start_with?(@name_changes.last.first)
25
+ end
26
+ component_tables.each do |component_table|
27
+ p component_table
28
+ table_name = component_table.gsub("#{@name_changes.last.first}_", "")
29
+ migration_name = Time.now.strftime("%Y%m%d%H%M%S") + migration_title(table_name)
30
+ filename = File.join(@migrations_path, Cbratools::String.underscore(migration_name)) + ".rb"
31
+ File.open(filename, "w") do |f|
32
+ f.write(migration_content(table_name))
33
+ end
34
+ end
35
+ end
36
+
37
+ def find_all_potential_table_names(line)
38
+ result = /create_table (?:"|')([^"']+)(?:"|')/.match line
39
+ result && result[1]
40
+ end
41
+
42
+ private
43
+
44
+ def migration_title(table_name)
45
+ table_name = Cbratools::String.camelcase(table_name)
46
+ "Move#{table_name}From#{@name_changes.first.first}To#{@name_changes.first.last}"
47
+ end
48
+
49
+ def migration_content(table_name)
50
+ "class #{migration_title(table_name)} < ActiveRecord::Migration
51
+ def change
52
+ rename_table :#{@name_changes.last.first}_#{table_name}, :#{@name_changes.last.last}_#{table_name}
53
+ end
54
+ end
55
+ "
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,126 @@
1
+ module Cbratools
2
+ class RenameComponent
3
+ def initialize(current_name, new_name, path, verbose_output)
4
+ @current_name = current_name
5
+ @new_name = new_name
6
+ @path = path
7
+ @verbose_output = verbose_output
8
+
9
+ @name_changes = [
10
+ [current_name, new_name],
11
+ [Cbratools::String.underscore(current_name),
12
+ Cbratools::String.underscore(new_name)]
13
+ ]
14
+ end
15
+
16
+ def run
17
+ change_component_name_in_files
18
+ change_component_name_in_folder_names
19
+ change_component_name_in_file_names
20
+ end
21
+
22
+ def change_component_name_in_files
23
+ selectors = [
24
+ ->(filename) { !File.directory?(filename) },
25
+ ->(filename) { !filename.include?(".log") },
26
+ ->(filename) { !filename.include?(".sqlite") },
27
+ ->(filename) { !filename.include?("schema.rb") },
28
+ ->(filename) { !filename.include?("structure.sql") },
29
+ ->(filename) { !filename.include?("sprockets") },
30
+ ->(filename) { !filename.include?("tmp/cache") },
31
+ ->(filename) { !filename.include?("assets/images") },
32
+ ->(filename) { !filename.include?("db/migrate") }
33
+ ]
34
+
35
+ FilesAndFoldersSelector.new(@path, @name_changes, selectors).each do |f|
36
+ FileRefactorer.new(f, @name_changes).refactor
37
+ end
38
+ end
39
+
40
+ def change_component_name_in_folder_names
41
+ selectors = [
42
+ ->(filename) { File.directory?(filename) },
43
+ ->(filename) { filename.end_with?(@name_changes.last.first) }
44
+ ]
45
+ FilesAndFoldersSelector.new(@path, @name_changes, selectors).each do |f|
46
+ FolderRenamer.new(f, @name_changes).refactor
47
+ end
48
+ end
49
+
50
+ def change_component_name_in_file_names
51
+ selectors = [
52
+ ->(filename) { !File.directory?(filename) },
53
+ ->(filename) { !filename.include?("db/migrate") },
54
+ ->(filename) { File.split(filename).last.include?(@name_changes.last.first) }
55
+ ]
56
+ FilesAndFoldersSelector.new(@path, @name_changes, selectors).each do |f|
57
+ FilesRenamer.new(f, @name_changes).refactor
58
+ end
59
+ end
60
+
61
+ class FilesAndFoldersSelector
62
+ def initialize(path, name_changes, selectors)
63
+ @path = path
64
+ @name_changes = name_changes
65
+ @selectors = selectors
66
+ end
67
+
68
+ def each
69
+ all.each { |a| yield a }
70
+ end
71
+
72
+ private
73
+
74
+ def all
75
+ Dir.glob(@path + "/**/*").select do |filename|
76
+ @selectors.all? { |selector| selector.call(filename) }
77
+ end.sort do |a, b|
78
+ b.length <=> a.length
79
+ end
80
+ end
81
+ end
82
+
83
+ class FileRefactorer
84
+ def initialize(file, name_changes)
85
+ @file = file
86
+ @name_changes = name_changes
87
+ end
88
+
89
+ def refactor()
90
+ file = File.read(@file)
91
+ @name_changes.each do |name_change|
92
+ file = file.gsub(name_change.first, name_change.last)
93
+ end
94
+ File.open(@file, "w") do |f|
95
+ f.write(file)
96
+ end
97
+ end
98
+ end
99
+
100
+ class FolderRenamer
101
+ def initialize(folder, name_changes)
102
+ @folder = folder
103
+ @name_changes = name_changes
104
+ end
105
+
106
+ def refactor
107
+ new_folder = @folder.gsub(/#{@name_changes.last.first}$/, @name_changes.last.last)
108
+ FileUtils.move(@folder, new_folder)
109
+ end
110
+ end
111
+
112
+ class FilesRenamer
113
+ def initialize(file, name_changes)
114
+ @file = file
115
+ @name_changes = name_changes
116
+ end
117
+
118
+ def refactor
119
+ file = File.split(@file)
120
+ new_file_name = file.last.gsub(/#{@name_changes.last.first}/, @name_changes.last.last)
121
+ new_file = File.join(file.first, new_file_name)
122
+ FileUtils.move(@file, new_file)
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,16 @@
1
+ module Cbratools
2
+ class String
3
+ def self.underscore(string)
4
+ string.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
7
+ tr("-", "_").
8
+ downcase
9
+ end
10
+
11
+ def self.camelcase(string)
12
+ return string if string !~ /_/ && string =~ /[A-Z]+.*/
13
+ string.split('_').map { |e| e.capitalize }.join
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbratools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephan Hagemann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.14'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.14.1
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '2.14'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.14.1
47
+ description: A set of tools to help with refactorings in component-based Rails applications.
48
+ email:
49
+ - stephan.hagemann@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - README.md
55
+ - Rakefile
56
+ - lib/cbratools.rb
57
+ - lib/cbratools/component_name_change_migrations.rb
58
+ - lib/cbratools/rename_component.rb
59
+ - lib/cbratools/string.rb
60
+ homepage: https://github.com/shageman/cbratools
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: cbratools
84
+ test_files: []