<commit_before>#!/usr/bin/env ruby

def info(str)
  STDERR.puts str
end

# OS helpers.
module OS
  module_function

  # Returns false if the current OS is not Mac OS X.
  def mac?
    RUBY_PLATFORM.downcase.include? 'darwin'
  end
end

# Homebrew is a package manager for OS X. This module provides some functions
# to check if it is installed, to install it and to install its packages.
module Homebrew
  module_function

  # Returns true if Homebrew is installed.
  def installed?
    system 'command -v brew >/dev/null 2>&1'
  end

  # Installs Homebrew.
  def install
    system 'ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"'
  end

  # Returns true if a Homebrew package is installed.
  def pkg_installed?(pkg)
    system "brew list #{pkg} >/dev/null 2>&1"
  end

  # Installs a Homebrew package.
  def install_pkg(pkg)
    system "brew install #{pkg}"
  end

  # Clean up old Homebrew packages.
  def cleanup
    system 'brew cleanup >/dev/null 2>&1'
  end
end

# Cask is a package manager for GUI apps on OS X. It is an extension for
# Homebrew.
module Cask
  module_function

  # Returns true if a Cask package is installed.
  def pkg_installed?(pkg)
    system "brew cask list #{pkg} >/dev/null 2>&1"
  end

  # Installs a Cask package.
  def install_pkg(pkg)
    system "brew cask install --appdir=/Applications #{pkg}"
  end

  # Clean up old Cask packages.
  def cleanup
    system 'brew cask cleanup >/dev/null 2>&1'
  end
end

abort 'This script only works on Mac OS X' unless OS.mac?

if Homebrew.installed?
  info 'Homebrew is already installed, skipping'
else
  info 'Installing Homebrew...'

  if Homebrew.install
    info 'Homebrew installed'
  else
    abort 'Homebrew was not installed due to a problem'
  end
end

packages = File.readlines('packages').map(&:strip)
casks = File.readlines('casks').map(&:strip)

info 'Installing Homebrew packages...'

packages.each do |pkg|
  if Homebrew.pkg_installed?(pkg)
    info "#{pkg} is already installed, skipping"
  else
    info "Installing #{pkg}"

    if Homebrew.install_pkg(pkg) && Homebrew.pkg_installed?(pkg)
      info "#{pkg} installed"
    else
      abort "#{pkg} was not installed due to a problem"
    end
  end
end

info 'Installing cask packages...'

casks.each do |pkg|
  if Cask.pkg_installed?(pkg)
    info "#{pkg} is already installed, skipping"
  else
    info "Installing #{pkg}"

    if Cask.install_pkg(pkg) && Cask.pkg_installed?(pkg)
      info "#{pkg} installed"
    else
      abort "#{pkg} was not installed due to a problem"
    end
  end
end

info 'Cleaning old packages...'

Homebrew.cleanup
Cask.cleanup

info 'Done!'<commit_msg>Extract common code to a function method<commit_after>#!/usr/bin/env ruby

def info(str)
  STDERR.puts str
end

# OS helpers.
module OS
  module_function

  # Returns false if the current OS is not Mac OS X.
  def mac?
    RUBY_PLATFORM.downcase.include? 'darwin'
  end
end

# Homebrew is a package manager for OS X. This module provides some functions
# to check if it is installed, to install it and to install its packages.
module Homebrew
  module_function

  # Returns true if Homebrew is installed.
  def installed?
    system 'command -v brew >/dev/null 2>&1'
  end

  # Installs Homebrew.
  def install
    system 'ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"'
  end

  # Returns true if a Homebrew package is installed.
  def pkg_installed?(pkg)
    system "brew list #{pkg} >/dev/null 2>&1"
  end

  # Installs a Homebrew package.
  def install_pkg(pkg)
    system "brew install #{pkg}"
  end

  # Clean up old Homebrew packages.
  def cleanup
    system 'brew cleanup >/dev/null 2>&1'
  end
end

# Cask is a package manager for GUI apps on OS X. It is an extension for
# Homebrew.
module Cask
  module_function

  # Returns true if a Cask package is installed.
  def pkg_installed?(pkg)
    system "brew cask list #{pkg} >/dev/null 2>&1"
  end

  # Installs a Cask package.
  def install_pkg(pkg)
    system "brew cask install --appdir=/Applications #{pkg}"
  end

  # Clean up old Cask packages.
  def cleanup
    system 'brew cask cleanup >/dev/null 2>&1'
  end
end

abort 'This script only works on Mac OS X' unless OS.mac?

if Homebrew.installed?
  info 'Homebrew is already installed, skipping'
else
  info 'Installing Homebrew...'

  if Homebrew.install
    info 'Homebrew installed'
  else
    abort 'Homebrew was not installed due to a problem'
  end
end

# Installs a package. "check" is a lambda used to check if the package is
# installed and "install" is a lambda that performs the installation.
def install_pkg(pkg, check, install)
  if check.call(pkg)
    info "#{pkg} is already installed, skipping"
  else
    info "Installing #{pkg}"

    if install.call(pkg) && check.call(pkg)
      info "#{pkg} installed"
    else
      abort "#{pkg} was not installed due to a problem"
    end
  end
end

info 'Installing Homebrew packages...'

packages = File.readlines('packages').map(&:strip)
packages.each do |pkg|
  perform_install(
    pkg,
    ->(p) { Homebrew.pkg_installed?(p) },
    ->(p) { Homebrew.install_pkg(p) }
  )
end

info 'Installing cask packages...'

casks = File.readlines('casks').map(&:strip)
casks.each do |pkg|
  perform_install(
    pkg,
    ->(p) { Cask.pkg_installed?(p) },
    ->(p) { Cask.install_pkg(p) }
  )
end

info 'Cleaning old packages...'

Homebrew.cleanup
Cask.cleanup

info 'Done!'
{'fraction_non_alphanumeric': 0.10157682434910158, 'fraction_numerical': 0.003667033370003667, 'mean_word_length': 3.2385392385392384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7004167', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1772, 'n_words': 658}