Class: Rails::Diff::RailsRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/diff/rails_repo.rb

Constant Summary collapse

RAILS_REPO =
"https://github.com/rails/rails.git"

Instance Method Summary collapse

Constructor Details

#initialize(logger:, cache_dir: Rails::Diff::CACHE_DIR, rails_repo: RAILS_REPO) ⇒ RailsRepo

Returns a new instance of RailsRepo.



6
7
8
9
10
# File 'lib/rails/diff/rails_repo.rb', line 6

def initialize(logger:, cache_dir: Rails::Diff::CACHE_DIR, rails_repo: RAILS_REPO)
  @logger = logger
  @cache_dir = cache_dir
  @rails_repo = rails_repo
end

Instance Method Details

#checkout(ref) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rails/diff/rails_repo.rb', line 12

def checkout(ref)
  on_rails_dir do
    logger.info "Checking out Rails (at #{ref})"
    unless Shell.run!("git", "checkout", ref, abort: false, logger:)
      Shell.run!("git", "fetch", "--depth", "1", "origin", ref, logger:)
      Shell.run!("git", "checkout", "FETCH_HEAD", logger:)
    end
  end
end

#install_dependenciesObject



33
34
35
36
37
38
39
40
41
# File 'lib/rails/diff/rails_repo.rb', line 33

def install_dependencies
  within "railties" do
    unless Shell.run!("bundle check", abort: false, logger:)
      logger.info "Installing Rails dependencies"
      Shell.run!("bundle", "config", "set", "--local", "without", "db", logger:)
      Shell.run!("bundle", "install", logger:)
    end
  end
end

#latest_commitObject



22
23
24
25
26
27
# File 'lib/rails/diff/rails_repo.rb', line 22

def latest_commit
  @latest_commit ||= on_rails_dir do
    Shell.run!("git fetch origin main", logger:)
    `git rev-parse origin/main`.strip
  end
end

#new_app(name, options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rails/diff/rails_repo.rb', line 43

def new_app(name, options)
  within "railties" do
    command = rails_new_command(name, options)
    logger.info "Generating new Rails application\n\t  > #{command.join(" ")}"
    Shell.run!(*command, logger:)
  end
end

#up_to_date?Boolean

Returns:



29
30
31
# File 'lib/rails/diff/rails_repo.rb', line 29

def up_to_date?
  File.exist?(rails_path) && on_latest_commit?
end

#within(dir, &block) ⇒ Object



51
# File 'lib/rails/diff/rails_repo.rb', line 51

def within(dir, &block) = on_rails_dir { Dir.chdir(dir, &block) }