-
Notifications
You must be signed in to change notification settings - Fork 75
/
datashift.thor
136 lines (100 loc) · 3.69 KB
/
datashift.thor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright:: (c) Autotelik Media Ltd 2011
# Author :: Tom Statter
# Date :: Aug 2010
#
# License:: MIT - Free, OpenSource
#
# Details:: Gem::Specification for DataShift gem.
#
# Provides classes for moving data between a number of enterprise
# type applications, files and databases.
#
# Provides support for moving data between .xls (Excel/OpenOffice)
# Spreedsheets via Ruby and AR, enabling direct import/export of
# ActiveRecord models with all their associations from database.
#
# Provides support for moving data between csv files and AR, enabling direct
# import/export of AR models and their associations from database.
#
require 'thor'
$LOAD_PATH.push File.expand_path('lib')
require 'datashift'
require 'factory_bot_rails'
require 'database_cleaner'
require_relative File.join('spec', 'support/sandbox')
require_relative File.join('spec', 'support/datashift_test_helpers')
module Datashift
class Utils < Thor
include DataShift::TestHelpers
desc 'lint', 'Run in spec - Verify that FactoryBot factories are valid'
def lint
ENV['RAILS_ENV'] = 'test'
environment
begin
DatabaseCleaner.start
puts 'Running FactoryBot.lint'
FactoryBot.lint
ensure
DatabaseCleaner.clean
end
end
desc 'sandbox', 'Rebuild the dummy rails app in spec - required for testing'
def sandbox
# Need an active record DB to test against, so we manage own Rails sandbox
DataShift::Sandbox.gen_rails_sandbox( :force )
end
desc 'build', 'Build gem and install in one step'
method_option :bump, aliases: '-b', type: :string, desc: 'Bump the version', required: false
method_option :push, aliases: '-p', desc: 'Push resulting gem to rubygems.org'
method_option :install, aliases: '-i',
desc: 'Install freshly built gem locally', type: :boolean, default: false
def build
if options[:push] && (options[:bump].blank? || options[:bump] !~ /^(\d+\.)?(\d+\.)?(\*|\d+)$/)
puts 'ERROR: Please bump to a new numeric version to push to rubygems'
exit(-1)
end
if options[:bump] && options[:bump] !~ /^(\d+\.)?(\d+\.)?(\*|\d+)$/
puts 'ERROR: bump should be a valid numeric version in form x.x.x'
exit(-1)
end
version = options[:bump] || DataShift::VERSION
# Bump the VERSION file in library
if options[:bump].present?
File.open( File.join('lib/datashift/version.rb'), 'w') do |f|
f << "module DataShift\n"
f << " VERSION = '#{version}'.freeze unless defined?(VERSION)\n"
f << "end\n"
end
end
build_cmd = 'gem build datashift.gemspec'
puts "\n*** Running build cmd [#{build_cmd}]"
system(build_cmd)
gem = "#{DataShift.gem_name}-#{version}.gem"
if(options[:install])
puts "Installing : #{gem}"
cmd = "gem install --no-ri --no-rdoc #{gem}"
system(cmd)
end
if(options[:push])
puts "Pushing version #{version} to rubygems"
cmd = "gem push #{gem}"
system(cmd)
end
end
no_commands do
def environment
env = File.expand_path('dummy/config/environment.rb')
if File.exist?(env)
begin
require env
rescue StandardError => e
logger.error("Failed to initialise ActiveRecord : #{e.message}")
raise ConnectionError, "Failed to initialise ActiveRecord : #{e.message}"
end
else
raise DataShift::PathError, 'No config/environment.rb found - cannot initialise ActiveRecord'
end
end
end
end
end