Class: RailsPushAndMigrate::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-push-and-migrate/base.rb

Direct Known Subclasses

Dokku, Heroku

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote, branch) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
# File 'lib/rails-push-and-migrate/base.rb', line 4

def initialize(remote, branch)
  @branch = branch
  @remote = remote
  diff_files
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



3
4
5
# File 'lib/rails-push-and-migrate/base.rb', line 3

def branch
  @branch
end

#remoteObject

Returns the value of attribute remote.



3
4
5
# File 'lib/rails-push-and-migrate/base.rb', line 3

def remote
  @remote
end

Instance Method Details

#assets_changed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rails-push-and-migrate/base.rb', line 35

def assets_changed?
  diff_files.select {|f| f.include? "app/assets"}.any?
end

#diff_filesObject



14
15
16
17
18
19
20
21
# File 'lib/rails-push-and-migrate/base.rb', line 14

def diff_files
  @diff_files ||= (
    with_named_repo do |repo_name|
      puts "Execute git diff #{repo_name}/#{remote_branch}..#{branch} --name-only"
      `git diff #{repo_name}/#{remote_branch}..#{branch} --name-only`.split("\n")
    end
  )
end

#git_push_cmdObject



39
40
41
# File 'lib/rails-push-and-migrate/base.rb', line 39

def git_push_cmd
  "git push #{remote} #{branch}:#{remote_branch}"
end

#has_migration?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rails-push-and-migrate/base.rb', line 31

def has_migration?
  diff_files.include?("db/schema.rb")
end

#migrate_cmdObject

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/rails-push-and-migrate/base.rb', line 43

def migrate_cmd
  raise NotImplementedError
end


47
48
49
50
# File 'lib/rails-push-and-migrate/base.rb', line 47

def print_and_execute(cmd)
  puts "Executing #{cmd}"
  system cmd
end


52
53
54
55
56
# File 'lib/rails-push-and-migrate/base.rb', line 52

def print_and_execute!(cmd)
  unless print_and_execute(cmd)
    raise "Failed to execute #{cmd}"
  end
end

#remote_branchObject



10
11
12
# File 'lib/rails-push-and-migrate/base.rb', line 10

def remote_branch
  "master"
end

#runObject



58
59
60
61
# File 'lib/rails-push-and-migrate/base.rb', line 58

def run
  print_and_execute! git_push_cmd
  print_and_execute!(migrate_cmd) if has_migration?
end

#with_named_repoObject



23
24
25
26
27
28
29
# File 'lib/rails-push-and-migrate/base.rb', line 23

def with_named_repo
  system "git remote add auto-push-repo #{remote}"
  system "git remote update auto-push-repo"
  result = yield "auto-push-repo"
  system "git remote rm auto-push-repo"
  result
end