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

require "thor"
require "yaml"

class Monk < Thor
  include Thor::Actions

  [:skip, :pretend, :force, :quiet].each do |task|
    class_options.delete task
  end

  desc "init", "Initialize a Monk application"
  method_option :skeleton, :type => :string, :aliases => "-s"
  def init(target = ".")
    if clone(source(options[:skeleton] || "default") || options[:skeleton], target)
      cleanup(target)
      rvmrc appname(target), target
      display_success_banner appname(target)
    else
      say_status(:error, clone_error(target))
    end
  end

  desc "rvmrc", "Create an .rvmrc file"
  def rvmrc(gemset, target = '.', version = '1.9.2')
    key = [version, gemset].join('@')

    say "Generating an .rvmrc file in your project."
    response = ask("Enter gemset name and ruby version (default: `%s`):" % key)

    key = response unless response.to_s.empty?

    create_file File.join(target, '.rvmrc'), "rvm --create use %s\n" % key
  end

  desc "show NAME", "Display the repository address for NAME"
  def show(name)
    say_status name, source(name) || "repository not found"
  end

  desc "list", "Lists the configured repositories"
  def list
    monk_config.keys.sort.each do |key|
      show(key)
    end
  end

  desc "add NAME REPOSITORY", "Add the repository to the configuration file"
  def add(name, repository)
    monk_config[name] = repository
    write_monk_config_file
  end

  desc "rm NAME", "Remove the repository from the configuration file"
  def rm(name)
    monk_config.delete(name)
    write_monk_config_file
  end

private

  def clone(source, target)
    if Dir["#{target}/*"].empty?
      say_status :fetching, source
      system "git clone -q --depth 1 #{source} #{target}"
      $?.success?
    end
  end

  def cleanup(target)
    inside(target) { remove_file ".git" }
    say_status :initialized, target
  end

  def source(name = "default")
    monk_config[name]
  end

  def monk_config_file
    @monk_config_file ||= File.join(monk_home, ".monk")
  end

  def monk_config
    @monk_config ||= begin
      write_monk_config_file unless File.exists?(monk_config_file)
      YAML.load_file(monk_config_file)
    end
  end

  def write_monk_config_file
    remove_file(monk_config_file, :verbose => false)
    create_file(monk_config_file, nil, :verbose => false) do
      config = @monk_config || { "default" => "git://github.com/monkrb/skeleton.git" }
      config.to_yaml
    end
  end

  def self.source_root
    "."
  end

  def clone_error(target)
    "Couldn't clone repository into target directory '#{target}'. " +
    "You must have git installed and the target directory must be empty."
  end

  def monk_home
    ENV["MONK_HOME"] || File.join(Thor::Util.user_home)
  end

  def display_success_banner
    say "\n"
    say "=" * 50
    say "-> You have successfully generated #{name}"
    say "=" * 50
    say "\n"
   
    if File.exist?(File.join(target, 'default.gems'))
      say "The skeleton you used comes with a `default.gems` file."
      say "To import the gems just run `rvm gemset import`."
      say "You may also clear out the existing gemset by doing `rvm gemset clear`."
    end
  end

  def appname(target)
    target == '.' ? File.basename(FileUtils.pwd) : target
  end
end
<commit_msg>Implement monk install, monk rvmrc, monk lock and monk unpack.<commit_after>#! /usr/bin/env ruby

require "thor"
require "yaml"

class Monk < Thor
  include Thor::Actions

  [:skip, :pretend, :force, :quiet].each do |task|
    class_options.delete task
  end

  desc "init", "Initialize a Monk application"
  method_option :skeleton, :type => :string, :aliases => "-s"
  def init(target = ".")
    check_for_rvm

    repo = source(options[:skeleton] || "default") || options[:skeleton]

    if clone(repo, target)
      cleanup(target)
      rvmrc appname(target), target
    else
      say_status(:error, clone_error(target))
    end
  end

  desc "rvmrc GEMSET [TARGET] [VERSION]", "Create an .rvmrc file"
  def rvmrc(gemset, target = ".", version = '1.9.2')
    key = [version, gemset].join('@')

    say "Generating an .rvmrc file in your project."
    response = ask("Enter gemset name and ruby version (default: `%s`):" % key)

    key = response unless response.to_s.empty?

    inside(target) do
      run "rvm --rvmrc --create %s && rvm rvmrc trust" % key
    end

    display_readme(target)
  end

  desc "install --clean", "Install all dependencies."
  method_option :clean, :type => :boolean
  def install(manifest = ".gems")
    run("rvm rvmrc load")
    run("rvm --force gemset empty") if options.clean?

    File.read(manifest).split("\n").each do |gem|
      if gem =~ /\A(.*?) -v(.*?)\z/
        gem_install($1, $2)
      end
    end
  end

  desc "lock", "Lock the current dependencies to the gem manifest file."
  def lock
    run("rvm gemset export .gems")
  end

  desc "unpack", "Freeze the current dependencies."
  def unpack
    run("rvm gemset unpack")
  end

  desc "show NAME", "Display the repository address for NAME"
  def show(name)
    say_status name, source(name) || "repository not found"
  end

  desc "list", "Lists the configured repositories"
  def list
    monk_config.keys.sort.each do |key|
      show(key)
    end
  end

  desc "add NAME REPOSITORY", "Add the repository to the configuration file"
  def add(name, repository)
    monk_config[name] = repository
    write_monk_config_file
  end

  desc "rm NAME", "Remove the repository from the configuration file"
  def rm(name)
    monk_config.delete(name)
    write_monk_config_file
  end

private
  def clone(source, target)
    if Dir["#{target}/*"].empty?
      say_status :fetching, source
      system "git clone -q --depth 1 #{source} #{target}"
      $?.success?
    end
  end

  def cleanup(target)
    inside(target) { remove_file ".git" }
    say_status :initialized, target
  end

  def source(name = "default")
    monk_config[name]
  end

  def monk_config_file
    @monk_config_file ||= File.join(monk_home, ".monk")
  end

  def monk_config
    @monk_config ||= begin
      write_monk_config_file unless File.exists?(monk_config_file)
      YAML.load_file(monk_config_file)
    end
  end

  def write_monk_config_file(default = "git://github.com/monkrb/skeleton.git")
    remove_file(monk_config_file, :verbose => false)
    create_file(monk_config_file, nil, :verbose => false) do
      config = @monk_config || { "default" => default }
      config.to_yaml
    end
  end

  def self.source_root
    "."
  end

  def clone_error(target)
    "Couldn't clone repository into target directory '#{target}'. " +
    "You must have git installed and the target directory must be empty."
  end

  def monk_home
    ENV["MONK_HOME"] || File.join(Thor::Util.user_home)
  end

  def appname(target)
    target == '.' ? File.basename(FileUtils.pwd) : target
  end

  def gem_install(lib, version, command = "gem install #{lib} -v#{version}")
    require "rubygems"

    if Gem.available?(lib, version) || vendored?(lib, version)
      say " " * 14 + command
    else
      run command
    end
  end

  def vendored?(lib, version)
    File.exist?("./vendor/gems/#{lib}-#{version}")
  end

  def check_for_rvm
    begin
      `rvm`
    rescue Errno::ENOENT
      puts RVM_REQUIRED_MESSAGE
      exit
    end
  end

  def display_readme(target)
    puts "\n" + File.read(Dir[File.join(target, "README*")].first)
  end

  RVM_REQUIRED_MESSAGE = (<<-EOT).gsub(/^ {4}/, "")

    !! OOPS... Monk requires RVM to be installed in your machine.
    !! The easiest way to install RVM is by using curl, as follows:

      bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

    You can also head up to http://rvm.beginrescueend.com/rvm/install/
    to read about the installation process of RVM.

  EOT
end


{'fraction_non_alphanumeric': 0.12175137626424273, 'fraction_numerical': 0.0021764178722314684, 'mean_word_length': 3.1050972149238043, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '166879', 'n_tokens_mistral': 2712, 'n_tokens_neox': 2608, 'n_words': 758}