See more posts

Automating code updates within a rails app

I find myself running git pull master && bundle && rake db:migrate && yarn etc.. many times throughout the day. So, I wrote this quick script which takes sha1 digests of the files before a git pull and conditionally runs the related commands to update my dependencies.

I just tuck it away in ~/.dotfiles/bin/rails-pull and make sure that the bin path is part of my shell's $PATH .

ruby
#!/usr/bin/env ruby

require 'digest/sha1'
require 'json'

def path(*parts)
  File.join(Dir.pwd, *parts)
end

bundle_paths = {
  path('Gemfile.lock') => nil,
}

yarn_paths = {
  path('yarn.lock') => nil,
}

npm_paths = {
  path('package.lock') => nil,
}

migrate_paths = {
  path('db', 'schema.rb') => nil,
  path('db', 'structure.sql') => nil,
}

tasks = Hash.new { false }

[
  bundle_paths,
  yarn_paths,
  npm_paths,
  migrate_paths,
].each do |paths|
  paths.each do |path, digest|
    if File.exist? path
      paths[path] = Digest::SHA1.hexdigest(File.read(path))
    end
  end
end

system 'git pull'

[
  bundle_paths,
  yarn_paths,
  npm_paths,
  migrate_paths,
].each do |paths|
  paths.each do |path, digest|
    if File.exist? path
      if digest == Digest::SHA1.hexdigest(File.read(path))
        paths[path] = false # did not change
      else
        paths[path] = true # changed
      end
    end
  end
end

system 'bundle'                       if bundle_paths.values.any? { |changed| changed }
system 'yarn'                         if yarn_paths.values.any? { |changed| changed }
system 'npm i'                        if npm_paths.values.any? { |changed| changed }
system 'bundle exec rake db:migrate'  if migrate_paths.values.any? { |changed| changed }

Thanks for reading! See more posts Leave feedback