basiszwo-reflection 0.5.1
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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.yardoc +0 -0
- data/History.rdoc +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +117 -0
- data/Rakefile +75 -0
- data/Reflection.gemspec +135 -0
- data/TODO.rdoc +38 -0
- data/VERSION +1 -0
- data/bin/reflection +5 -0
- data/doc/Reflection/CLI.html +153 -0
- data/doc/Reflection/Command/Apply.html +266 -0
- data/doc/Reflection/Command/Base.html +385 -0
- data/doc/Reflection/Command/Stash.html +342 -0
- data/doc/Reflection/Command.html +85 -0
- data/doc/Reflection/Config.html +902 -0
- data/doc/Reflection/ConfigArgumentError.html +92 -0
- data/doc/Reflection/Directory/Base.html +657 -0
- data/doc/Reflection/Directory/Stash.html +411 -0
- data/doc/Reflection/Directory.html +85 -0
- data/doc/Reflection/Rails.html +409 -0
- data/doc/Reflection/Repository.html +745 -0
- data/doc/Reflection/Support/Home.html +182 -0
- data/doc/Reflection/Support/Log.html +222 -0
- data/doc/Reflection/Support.html +141 -0
- data/doc/Reflection/Validations.html +135 -0
- data/doc/Reflection.html +285 -0
- data/doc/_index.html +267 -0
- data/doc/class_list.html +197 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +23 -0
- data/doc/css/style.css +261 -0
- data/doc/file.README.html +200 -0
- data/doc/file_list.html +29 -0
- data/doc/index.html +200 -0
- data/doc/js/app.js +91 -0
- data/doc/js/full_list.js +39 -0
- data/doc/js/jquery.js +19 -0
- data/doc/method_list.html +572 -0
- data/doc/top-level-namespace.html +80 -0
- data/lib/reflection/cli.rb +35 -0
- data/lib/reflection/command/apply.rb +71 -0
- data/lib/reflection/command/base.rb +28 -0
- data/lib/reflection/command/stash.rb +64 -0
- data/lib/reflection/command.rb +7 -0
- data/lib/reflection/config.rb +139 -0
- data/lib/reflection/directory/base.rb +58 -0
- data/lib/reflection/directory/stash.rb +30 -0
- data/lib/reflection/directory.rb +6 -0
- data/lib/reflection/rails/database.rb +96 -0
- data/lib/reflection/rails.rb +40 -0
- data/lib/reflection/repository.rb +71 -0
- data/lib/reflection/support/home.rb +17 -0
- data/lib/reflection/support/log.rb +20 -0
- data/lib/reflection/support.rb +12 -0
- data/lib/reflection/validations.rb +23 -0
- data/lib/reflection.rb +32 -0
- data/spec/reflection/cli_spec.rb +41 -0
- data/spec/reflection/command/stash_spec.rb +104 -0
- data/spec/reflection/config_spec.rb +126 -0
- data/spec/reflection/directory/base_spec.rb +38 -0
- data/spec/reflection/directory/stash_spec.rb +39 -0
- data/spec/reflection/rails/database_spec.rb +161 -0
- data/spec/reflection/rails_spec.rb +62 -0
- data/spec/reflection/repository_spec.rb +71 -0
- data/spec/reflection/support/home_spec.rb +30 -0
- data/spec/reflection/support_spec.rb +4 -0
- data/spec/reflection_spec.rb +21 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +15 -0
- metadata +157 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Command
|
3
|
+
class Apply < Reflection::Command::Base
|
4
|
+
|
5
|
+
def validate!
|
6
|
+
validate.existence_of config.directory
|
7
|
+
if config.rails_root
|
8
|
+
validate.existence_of config.rails_root
|
9
|
+
Reflection::Rails.validate_environment(config)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def run!
|
14
|
+
stash_directory = Directory::Stash.new(Reflection::Repository.new(config.repository), 'apply')
|
15
|
+
target_directory = Directory::Base.new(config.directory)
|
16
|
+
|
17
|
+
unless config.force
|
18
|
+
get_user_approval_for_cleaning_target(target_directory)
|
19
|
+
get_user_approval_for_apply_database_dump if config.rails_root
|
20
|
+
end
|
21
|
+
|
22
|
+
verify_that_target_is_not_a_repository(target_directory)
|
23
|
+
|
24
|
+
Reflection.log.info "Applying '#{config.repository}' >> '#{config.directory}'.."
|
25
|
+
|
26
|
+
target_directory.clean!
|
27
|
+
|
28
|
+
if stash_directory.exists?
|
29
|
+
stash_directory.validate_repository
|
30
|
+
stash_directory.copy_git_index_to(target_directory.path)
|
31
|
+
repo = Repository.new_from_path(target_directory.path)
|
32
|
+
repo.reset!
|
33
|
+
repo.pull
|
34
|
+
stash_directory.get_git_index_from(target_directory.path)
|
35
|
+
else
|
36
|
+
stash_directory.clone_repository
|
37
|
+
stash_directory.move_content_to(target_directory.path)
|
38
|
+
# stash_directory.get_git_index_from(target_directory.path)
|
39
|
+
end
|
40
|
+
|
41
|
+
Reflection::Rails.apply(config, target_directory)
|
42
|
+
Reflection.log.info "Apply Command done."
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def get_user_approval_for_cleaning_target(target_directory)
|
49
|
+
puts "\nIn order to get a fresh copy of your files, "
|
50
|
+
puts "Reflection will have to remove all files under '#{target_directory.path}'."
|
51
|
+
puts "If you are sure, hit <enter> to proceed.."
|
52
|
+
|
53
|
+
unless STDIN.getc == 10
|
54
|
+
puts "Aborting.."
|
55
|
+
exit
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_user_approval_for_apply_database_dump
|
60
|
+
puts "\nCaution: You've enabled Rails database dumping/applying."
|
61
|
+
puts "In order to apply a fresh dump, Reflection will override your database."
|
62
|
+
puts "If you are sure, hit <enter> to proceed.."
|
63
|
+
|
64
|
+
unless STDIN.getc == 10
|
65
|
+
puts "Aborting.."
|
66
|
+
exit
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Command
|
3
|
+
class Base
|
4
|
+
attr_accessor :config
|
5
|
+
|
6
|
+
def self.run!(config)
|
7
|
+
command = self.new(config)
|
8
|
+
command.validate! if command.respond_to?(:validate!)
|
9
|
+
command.run!
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(new_config)
|
13
|
+
self.config = new_config
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate
|
17
|
+
Reflection::Validations
|
18
|
+
end
|
19
|
+
|
20
|
+
def verify_that_target_is_not_a_repository(target_directory)
|
21
|
+
if Repository.exists?(target_directory.path)
|
22
|
+
Support.exit_with_error "The specified --directory is a repository. Reflection is afraid of breaking something, so it won't touch it. Pleace specify another one.."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Command
|
3
|
+
class Stash < Reflection::Command::Base
|
4
|
+
|
5
|
+
def validate!
|
6
|
+
validate.existence_of config.directory
|
7
|
+
if config.rails_root
|
8
|
+
validate.existence_of config.rails_root
|
9
|
+
Reflection::Rails.validate_environment(config)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def run!
|
14
|
+
Reflection.log.info "Stashing '#{config.directory}'.."
|
15
|
+
|
16
|
+
stash_directory = Directory::Stash.new(Reflection::Repository.new(config.repository), 'stash')
|
17
|
+
target_directory = Directory::Base.new(config.directory)
|
18
|
+
|
19
|
+
verify_that_target_is_not_a_repository(target_directory)
|
20
|
+
|
21
|
+
prepare_stash_repository(stash_directory)
|
22
|
+
stash_directory_into_repository(stash_directory, target_directory)
|
23
|
+
|
24
|
+
Reflection.log.info "Stash Command done."
|
25
|
+
end
|
26
|
+
|
27
|
+
def prepare_stash_repository(stash_directory)
|
28
|
+
Reflection.log.debug "Preparing stash repository.."
|
29
|
+
|
30
|
+
if stash_directory.exists?
|
31
|
+
stash_directory.validate_repository
|
32
|
+
else
|
33
|
+
stash_directory.clone_repository
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def stash_directory_into_repository(stash_directory, target_directory)
|
38
|
+
move_stash_repository_git_index_to_target(stash_directory.git_index, target_directory.path)
|
39
|
+
Reflection::Rails.stash(config, target_directory) if config.rails_root
|
40
|
+
commit_and_push_files(target_directory.path, target_directory.name)
|
41
|
+
move_stash_repository_git_index_back(target_directory.git_index, stash_directory.path)
|
42
|
+
Reflection::Rails.clean_target(config, target_directory) if config.rails_root
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def move_stash_repository_git_index_to_target(source, target)
|
49
|
+
%x(mv #{source} #{target})
|
50
|
+
end
|
51
|
+
|
52
|
+
def commit_and_push_files(repository_path, target)
|
53
|
+
repository = Repository.new_from_path(repository_path)
|
54
|
+
repository.commit_all_new_files
|
55
|
+
repository.push
|
56
|
+
end
|
57
|
+
|
58
|
+
def move_stash_repository_git_index_back(source, target)
|
59
|
+
%x(mv #{source} #{target})
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module Reflection
|
6
|
+
|
7
|
+
class ConfigArgumentError < StandardError; end
|
8
|
+
|
9
|
+
class Config
|
10
|
+
|
11
|
+
attr_accessor :command
|
12
|
+
attr_accessor :directory
|
13
|
+
attr_accessor :repository
|
14
|
+
attr_accessor :rails_root
|
15
|
+
attr_accessor :rails_environment
|
16
|
+
attr_accessor :store_configuration_path
|
17
|
+
attr_accessor :verbose
|
18
|
+
attr_accessor :force
|
19
|
+
|
20
|
+
def self.parse(args = [])
|
21
|
+
config = Config.new
|
22
|
+
case
|
23
|
+
when !args.empty? && File.exists?(args.first)
|
24
|
+
config.read!(args.first)
|
25
|
+
when !args.empty?
|
26
|
+
config.parse_command_line_options(args)
|
27
|
+
config.write(config.store_configuration_path) if config.store_configuration_path
|
28
|
+
else
|
29
|
+
config.read!(File.join(Dir.pwd, 'reflection.yml'))
|
30
|
+
end
|
31
|
+
config
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_hash
|
35
|
+
{
|
36
|
+
:command => self.command,
|
37
|
+
:directory => self.directory,
|
38
|
+
:repository => self.repository,
|
39
|
+
:rails_root => self.rails_root,
|
40
|
+
:rails_environment => self.rails_environment,
|
41
|
+
:verbose => self.verbose,
|
42
|
+
:force => self.force
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def from_hash(hash)
|
47
|
+
self.command = hash[:command]
|
48
|
+
self.directory = hash[:directory]
|
49
|
+
self.repository = hash[:repository]
|
50
|
+
self.rails_root = hash[:rails_root]
|
51
|
+
self.rails_environment = hash[:rails_environment]
|
52
|
+
self.verbose = Reflection.verbose = hash[:verbose]
|
53
|
+
self.force = hash[:force]
|
54
|
+
end
|
55
|
+
|
56
|
+
def write(path)
|
57
|
+
begin
|
58
|
+
io = File.open(path, 'w')
|
59
|
+
YAML.dump(self.to_hash, io)
|
60
|
+
io.close
|
61
|
+
rescue => e
|
62
|
+
Reflection::Support.exit_with_error("Writing of config file to '#{path}' failed: #{e.message}")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def read!(path)
|
67
|
+
begin
|
68
|
+
if File.exist?(path)
|
69
|
+
options = YAML.load_file(path)
|
70
|
+
raise(Reflection::ConfigArgumentError, "Config file is invalid.") unless options.is_a?(Hash)
|
71
|
+
self.from_hash(options)
|
72
|
+
end
|
73
|
+
rescue => e
|
74
|
+
Reflection::Support.exit_with_error("Parsing of config file '#{path}' failed: #{e.message}")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse_command_line_options(args)
|
79
|
+
opt_parser = OptionParser.new do |opts|
|
80
|
+
opts.banner = "Usage: reflection --COMMAND --repository=GIT_REPO --directory=PATH\n"
|
81
|
+
opts.banner << " -or-\n"
|
82
|
+
opts.banner << "Usage: reflection /path/to/reflection-config-file.yml"
|
83
|
+
|
84
|
+
opts.separator " "
|
85
|
+
opts.separator "On the server side:"
|
86
|
+
|
87
|
+
opts.on("-s", "--stash", "Store your assets and/or a database dump in a git-repository.") do |stash|
|
88
|
+
self.command = :stash
|
89
|
+
end
|
90
|
+
|
91
|
+
opts.separator "On the client side:"
|
92
|
+
opts.on("-a", "--apply", "Apply assets and/or a database dump loaded from a git-repository.") do |apply|
|
93
|
+
self.command = :apply
|
94
|
+
end
|
95
|
+
|
96
|
+
opts.separator " "
|
97
|
+
opts.separator "Required options for both:"
|
98
|
+
opts.on("-r", "--repository GIT_URL", "A Git repository(url) to be used as storage") do |repository|
|
99
|
+
self.repository = repository
|
100
|
+
end
|
101
|
+
|
102
|
+
opts.on("-d", "--directory PATH", "Path to your asset directory") do |directory|
|
103
|
+
self.directory = directory
|
104
|
+
end
|
105
|
+
|
106
|
+
opts.separator " "
|
107
|
+
opts.separator "Additional options for both:"
|
108
|
+
|
109
|
+
opts.on("--rails [RAILS_ROOT]", "Enable dumping/applying of a Rails managed MySQL database") do |path|
|
110
|
+
self.rails_root = path || nil
|
111
|
+
end
|
112
|
+
|
113
|
+
opts.on("--rails-env [ENV]", "Rails environment to instrument") do |environment|
|
114
|
+
self.rails_environment = environment || nil
|
115
|
+
end
|
116
|
+
|
117
|
+
opts.on("--write [FILE]", "Create a configuration FILE from the current commandline options") do |config_file_path|
|
118
|
+
self.store_configuration_path = config_file_path if config_file_path
|
119
|
+
end
|
120
|
+
|
121
|
+
opts.on("--force", "Hide tedious warnings") do
|
122
|
+
self.force = true
|
123
|
+
end
|
124
|
+
|
125
|
+
opts.on("-v", "--verbose", "Include debug information in output") do
|
126
|
+
self.verbose = true
|
127
|
+
Reflection.verbose = true
|
128
|
+
end
|
129
|
+
|
130
|
+
opts.on("--version", "Show version") do
|
131
|
+
self.command = :show_version
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
opt_parser.parse!(args)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Directory
|
3
|
+
class Base
|
4
|
+
|
5
|
+
attr_accessor :path
|
6
|
+
|
7
|
+
def initialize(new_path)
|
8
|
+
self.path = new_path
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
self.path
|
13
|
+
end
|
14
|
+
|
15
|
+
def exists?
|
16
|
+
File.exist?(self.path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def clean!
|
20
|
+
Reflection.log.debug "Cleaning #{self.path}/.."
|
21
|
+
%x(rm -r #{self.path}/*)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parent
|
25
|
+
Base.new(File.expand_path(self.path.split('/')[0..-2].join('/')))
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
self.path.split('/').last
|
30
|
+
end
|
31
|
+
|
32
|
+
def git_index
|
33
|
+
File.expand_path(File.join(self.path, '.git'))
|
34
|
+
end
|
35
|
+
|
36
|
+
def copy_git_index_to(target_path)
|
37
|
+
Reflection.log.debug "Copying git-index '#{self.git_index}' to #{target_path}"
|
38
|
+
%x(cp -R #{self.git_index} #{target_path})
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_git_index_from(target_path)
|
42
|
+
Reflection.log.debug "Getting git-index from #{target_path}"
|
43
|
+
|
44
|
+
%x(rm -rf #{self.git_index}) if File.exists?(self.git_index)
|
45
|
+
%x(mkdir -p #{self.path})
|
46
|
+
%x(mv -f #{File.join(target_path, '/.git')} #{File.join(self.path, "/")})
|
47
|
+
end
|
48
|
+
|
49
|
+
def move_content_to(target_path)
|
50
|
+
Reflection.log.debug "Moving content to '#{target_path}'.."
|
51
|
+
%x(mv #{File.join(self.path, '/*')} #{File.join(target_path, '/')})
|
52
|
+
# %x(cp -R #{File.join(self.path, '/.')} #{target_path})
|
53
|
+
# %x(rm -rf #{self.path})
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Directory
|
3
|
+
class Stash < Directory::Base
|
4
|
+
|
5
|
+
attr_accessor :repository
|
6
|
+
|
7
|
+
def initialize(repository, identifier_prefix)
|
8
|
+
@repository = repository
|
9
|
+
@identifier_prefix = identifier_prefix
|
10
|
+
end
|
11
|
+
|
12
|
+
def path
|
13
|
+
@path = File.join(Reflection.home.path, @identifier_prefix, repository.identifier)
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_repository
|
17
|
+
if Reflection::Repository.exists?(self.path) && !@repository.same_in_path?(self.path)
|
18
|
+
Reflection::Support.exit_with_error "The stash directory '#{self.path}' is a repository, but not the one you specified (--repository)."
|
19
|
+
else
|
20
|
+
Reflection.log.debug "Directory '#{self.path}' valid."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def clone_repository
|
25
|
+
Reflection.log.debug "Cloning repository '#{self.repository.url}' to '#{self.path}'"
|
26
|
+
@repository.clone(self.path)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Rails
|
3
|
+
class Database
|
4
|
+
|
5
|
+
attr_accessor :configuration
|
6
|
+
attr_accessor :environment
|
7
|
+
attr_accessor :rails_root
|
8
|
+
|
9
|
+
def initialize(new_rails_root, new_environment)
|
10
|
+
self.rails_root = new_rails_root
|
11
|
+
self.environment = new_environment
|
12
|
+
self.configuration = read_configuration_yml
|
13
|
+
end
|
14
|
+
|
15
|
+
def dump_to_directory(target_directory)
|
16
|
+
Reflection.log.debug "dumping database.."
|
17
|
+
run "mysqldump #{command_line_options} --skip-lock-tables > #{dump_file_path(target_directory)}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_dump_from_file(target_directory)
|
21
|
+
Reflection.log.debug "Loading dump.."
|
22
|
+
run("mysql #{command_line_options(:no_rehash => true)} < #{dump_file_path(target_directory)}")
|
23
|
+
end
|
24
|
+
|
25
|
+
def clean_dump_file(target_directory)
|
26
|
+
dump_file = dump_file_path(target_directory)
|
27
|
+
if File.exist?(dump_file)
|
28
|
+
run "rm #{dump_file}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def recreate!
|
33
|
+
Reflection.log.debug "Recreating database.."
|
34
|
+
drop
|
35
|
+
create
|
36
|
+
end
|
37
|
+
|
38
|
+
def create
|
39
|
+
run("echo \"CREATE DATABASE #{self.configuration['database']};\" | mysql #{command_line_options(:skip => :database)}")
|
40
|
+
end
|
41
|
+
|
42
|
+
def drop
|
43
|
+
run("echo \"DROP DATABASE #{self.configuration['database']};\" | mysql #{command_line_options(:skip => :database)}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def migrate!
|
47
|
+
Reflection.log.debug "Migrating database.."
|
48
|
+
run("(cd #{self.rails_root} && RAILS_ENV=#{self.environment} rake db:migrate)")
|
49
|
+
end
|
50
|
+
|
51
|
+
def read_configuration_yml
|
52
|
+
begin
|
53
|
+
if configuration = YAML.load_file(database_config_file_path)[self.environment]
|
54
|
+
return configuration
|
55
|
+
else
|
56
|
+
return Support.exit_with_error("Rails database configuration for '#{self.environment}' isn't available in #{database_config_file_path}")
|
57
|
+
end
|
58
|
+
rescue => e
|
59
|
+
return Support.exit_with_error("Error while parsing Rails database configuration: #{e}")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def command_line_options(opts = {})
|
64
|
+
options = []
|
65
|
+
options << "-A" if opts[:no_rehash] && opts[:no_rehash] == true
|
66
|
+
options << "-h #{configuration['host']}" if configuration['host'] && !configuration['host'].empty?
|
67
|
+
options << "-u#{configuration['username']}"
|
68
|
+
options << "-p#{configuration['password']}" if configuration['password'] && !configuration['password'].empty?
|
69
|
+
options << "#{configuration['database']}" unless opts[:skip] && opts[:skip] == :database
|
70
|
+
options.join(' ')
|
71
|
+
end
|
72
|
+
|
73
|
+
def run(command)
|
74
|
+
# Reflection.log.debug "-- #{command}"
|
75
|
+
%x(#{command})
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def log_error_and_return_false(message)
|
82
|
+
Reflection.log.error(message)
|
83
|
+
return false
|
84
|
+
end
|
85
|
+
|
86
|
+
def database_config_file_path
|
87
|
+
@database_config_file_path ||= File.join(self.rails_root, "config/database.yml")
|
88
|
+
end
|
89
|
+
|
90
|
+
def dump_file_path(directory)
|
91
|
+
File.join(directory, "_rails_database_dump.sql")
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Rails
|
3
|
+
|
4
|
+
autoload :Database, 'reflection/rails/database'
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def validate_environment(config)
|
9
|
+
env_file_path = File.join(config.rails_root, 'config/environments', "#{config.rails_environment}.rb")
|
10
|
+
unless File.exist?(env_file_path)
|
11
|
+
Reflection::Support.exit_with_error("Rails environment '#{config.rails_environment}' doesn't exist in #{env_file_path}.")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# TODO:
|
16
|
+
# Method is obsolete and has moved to Rails::Database
|
17
|
+
# Cannot be removed atm because Command::Stash depends on it
|
18
|
+
def clean_target(config, target_directory)
|
19
|
+
database = Database.new(config.rails_root, config.rails_environment)
|
20
|
+
database.clean_dump_file(target_directory.path)
|
21
|
+
end
|
22
|
+
|
23
|
+
def stash(config, target_directory)
|
24
|
+
Reflection.log.debug "Stashing database dump.."
|
25
|
+
database = Database.new(config.rails_root, config.rails_environment)
|
26
|
+
database.dump_to_directory(target_directory.path)
|
27
|
+
end
|
28
|
+
|
29
|
+
def apply(config, target_directory)
|
30
|
+
Reflection.log.debug "Applying database dump.."
|
31
|
+
database = Database.new(config.rails_root, config.rails_environment)
|
32
|
+
database.recreate!
|
33
|
+
database.load_dump_from_file(target_directory.path)
|
34
|
+
database.clean_dump_file(target_directory.path)
|
35
|
+
database.migrate!
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'git'
|
3
|
+
|
4
|
+
module Reflection
|
5
|
+
class Repository
|
6
|
+
|
7
|
+
attr_accessor :url
|
8
|
+
attr_accessor :path
|
9
|
+
|
10
|
+
def self.new_from_path(path)
|
11
|
+
unless self.exists?(path)
|
12
|
+
raise "'#{path}' is not a valid Git repository"
|
13
|
+
end
|
14
|
+
|
15
|
+
repo = Git.open(path)
|
16
|
+
self.new(repo.remote.url, path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.exists?(path)
|
20
|
+
begin
|
21
|
+
Git.open(path)
|
22
|
+
rescue ArgumentError
|
23
|
+
return false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(new_url, path = nil)
|
28
|
+
self.url = new_url
|
29
|
+
self.path = path
|
30
|
+
end
|
31
|
+
|
32
|
+
def identifier
|
33
|
+
Digest::MD5.hexdigest(self.url)
|
34
|
+
end
|
35
|
+
|
36
|
+
def same_in_path?(path)
|
37
|
+
git_repo = Git.open(path)
|
38
|
+
(git_repo.remote && git_repo.remote.url == self.url) || false
|
39
|
+
end
|
40
|
+
|
41
|
+
def reset!
|
42
|
+
Reflection.log.debug "Resetting target to HEAD"
|
43
|
+
repo = Git.open(self.path)
|
44
|
+
repo.reset_hard
|
45
|
+
end
|
46
|
+
|
47
|
+
def clone(path)
|
48
|
+
Git.clone(self.url, path)
|
49
|
+
end
|
50
|
+
|
51
|
+
def commit_all_new_files
|
52
|
+
repo = Git.open(self.path)
|
53
|
+
Reflection.log.debug "Committing all changes.."
|
54
|
+
Reflection.log.debug(repo.add)
|
55
|
+
Reflection.log.debug(repo.commit_all("Updated stash.")) rescue true
|
56
|
+
end
|
57
|
+
|
58
|
+
def push
|
59
|
+
# repo = Git.open(self.path)
|
60
|
+
Reflection.log.debug "Pushing commit.."
|
61
|
+
Reflection.log.debug(%x((cd #{self.path} && git push --force)))
|
62
|
+
# Reflection.log.debug(repo.push)
|
63
|
+
end
|
64
|
+
|
65
|
+
def pull
|
66
|
+
Reflection.log.debug "Pulling in #{self.path}.."
|
67
|
+
Reflection.log.debug(%x((cd #{self.path} && git pull --rebase)))
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Support
|
3
|
+
class Log
|
4
|
+
|
5
|
+
attr_accessor :verbose
|
6
|
+
|
7
|
+
def debug(message)
|
8
|
+
puts "** #{message}" if Reflection.verbose == true && message && !message.empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
def info(message)
|
12
|
+
puts "** #{message}" if message && !message.empty?
|
13
|
+
end
|
14
|
+
|
15
|
+
def error(message)
|
16
|
+
puts "!! #{message}" if message && !message.empty?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Reflection
|
2
|
+
module Validations
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def existence_of(path)
|
6
|
+
if File.exist?(path)
|
7
|
+
return true
|
8
|
+
else
|
9
|
+
Reflection::Support.exit_with_error "Option validation failed: #{path} does not exist."
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# def presense_of(option)
|
14
|
+
# if !option.nil? && !option.empty?
|
15
|
+
# return true
|
16
|
+
# else
|
17
|
+
# exit_with_error "Option validation failed: "
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|