<commit_before>## -------------------------------------------------------------------
##
## Erlang Toolbox: Bootstrap file
## Copyright (c) 2008 The Hive.  All rights reserved.
##
## -------------------------------------------------------------------

require 'set'
require 'pathname'
require 'rubygems'

gem 'rake'
require 'rake'
require 'rake/clean'

libdir = Pathname(__FILE__).dirname
$:.unshift(libdir) unless $:.include?(libdir) || $:.include?(libdir.expand_path)

verbose false

## -------------------------------------------------------------------
## Constants

PWD = Dir.getwd

def debug?
  !ENV['debug'].nil?
end

puts "WARNING: Debugging is enabled." if debug?

ERL_SRC = FileList['src/*.erl']
ERL_BEAM = ERL_SRC.pathmap("%{src,ebin}X.beam")
ERL_PATH = FileList.new
APP_FILE = FileList["#{PWD}/ebin/*.app"][0]

ERL_INCLUDE = "./include"
ERLC_FLAGS = %W( -I#{ERL_INCLUDE} -W )
ERLC_FLAGS << '+debug_info' if debug?

UNIT_TEST_FLAGS = []
INT_TEST_FLAGS  = []
PERF_TEST_FLAGS = []

TEST_ROOT = "."
TEST_LOG_DIR = "#{TEST_ROOT}/logs"

UNIT_TEST_DIR = "#{PWD}/#{TEST_ROOT}/test"
INT_TEST_DIR  = "#{PWD}/#{TEST_ROOT}/int_test"
PERF_TEST_DIR = "#{PWD}/#{TEST_ROOT}/perf_test"

CLEAN.include %w( **/*.beam **/erl_crash.dump )
CLOBBER.include TEST_LOG_DIR, 'doc'

## -------------------------------------------------------------------
## Rules

directory 'ebin'

rule ".beam" => ["%{ebin,src}X.erl"] do |t|
  puts "compiling #{t.source}..."
  sh "erlc #{print_flags(ERLC_FLAGS)} #{expand_path(ERL_PATH)} -o ebin #{t.source}"
end

## -------------------------------------------------------------------
## Tasks

desc "Compile Erlang sources to .beam files"
task :compile => ['ebin'] + ERL_BEAM do
  do_validate_app(APP_FILE)
end

desc "Do a fresh build from scratch"
task :rebuild => [:clean, :compile]

desc "Generate Edoc documentation"
task :doc do
  app = erl_app_name(APP_FILE)
  sh %Q(erl -noshell -run edoc_run application #{app} '"."' "[]"  -s init stop)
end

namespace :test do

  desc "Test preparation run before all tests"
  task :prepare do
    fail "No tests defined" unless File.directory?(TEST_ROOT)
    Dir.mkdir(TEST_LOG_DIR) unless File.directory?(TEST_LOG_DIR)

    # Always compile tests with debug info
    puts "Debugging is enabled for test builds."
    ERLC_FLAGS << '+debug_info'
  end

  task :results do
    `open #{TEST_LOG_DIR}/index.html`
  end

  ['unit', 'int', 'perf'].each do |type|
    desc "Run #{type} tests"
    task type => "test:#{type}:prepare" do
      check_and_run_tests(type, false)
    end

    namespace type do
      desc "Prepare #{type} tests"
      task :prepare => ['^prepare', 'compile']

      desc "Compile #{type} tests"
      task :compile => 'rake:compile' do
        compile_tests(type)
      end

      desc "Run #{type} tests with coverage"
      task :cover => :prepare do
        check_and_run_tests(type, true)
      end
    end
  end

  desc "Run all tests"
  task :all => [:unit, :int, :perf]

end

# Convenience tasks for backward compatibility
task :test => 'test:unit'
task :int_test => 'test:int'
task :perf_test => 'test:perf'

desc "Package app for publication to a faxien repo"
task :package => [:compile] do
  target_dir = package_dir
  FileUtils.rm_rf target_dir
  Dir.mkdir target_dir
  FileUtils.cp_r 'ebin', target_dir
  FileUtils.cp_r 'include', target_dir
  FileUtils.cp_r 'src', target_dir
  FileUtils.cp_r 'priv', target_dir if File.exist?('priv')
  puts "Packaged to #{target_dir}"
end

task :default => [:compile]

## -------------------------------------------------------------------
## Public functions

def append_flags(flags, value)
  flags << value
end

def erl_run(script, args = "")
  `erl -eval '#{script}' -s erlang halt #{args} -noshell 2>&1`.strip
end

def erl_where(lib)
  script = <<-ERL
      io:format("~s\n", [filename:join(code:lib_dir(#{lib}), "include")])
      ERL
  erl_run(script)
end

def erl_app_version(app)
  script = <<-ERL
     ok = application:load(#{app}),
     {ok, Vsn} = application:get_key(#{app}, vsn),
     io:format("~s\\n", [Vsn]).
     ERL
  output = erl_run(script, "-pa ebin")
  output.strip()
end

def erl_app_name(app_file)
  File.basename(app_file, ".app")
end

def package_dir
  app = erl_app_name(APP_FILE)
  vsn = erl_app_version(app)
  "#{app}-#{vsn}"
end


## -------------------------------------------------------------------
## Private functions

def print_flags(flags)
  flags.join(' ')
end

def erl_app_modules(app)
  script = <<-ERL
      ok = application:load(#{app}),
      {ok, M} = application:get_key(#{app}, modules),
      [io:format("~s\\n", [Mod]) || Mod <- M].
      ERL

  output = erl_run(script, "-pa ebin")
  if output[/badmatch/]
    fail "Error processing .app file: ", output
  else
    output.split("\n")
  end
end<commit_msg>Fixed a Rakefile error that occurred when .app files were invalid.<commit_after>## -------------------------------------------------------------------
##
## Erlang Toolbox: Bootstrap file
## Copyright (c) 2008 The Hive.  All rights reserved.
##
## -------------------------------------------------------------------

require 'set'
require 'pathname'
require 'rubygems'

gem 'rake'
require 'rake'
require 'rake/clean'

libdir = Pathname(__FILE__).dirname
$:.unshift(libdir) unless $:.include?(libdir) || $:.include?(libdir.expand_path)

verbose false

## -------------------------------------------------------------------
## Constants

PWD = Dir.getwd

def debug?
  !ENV['debug'].nil?
end

puts "WARNING: Debugging is enabled." if debug?

ERL_SRC = FileList['src/*.erl']
ERL_BEAM = ERL_SRC.pathmap("%{src,ebin}X.beam")
ERL_PATH = FileList.new
APP_FILE = FileList["#{PWD}/ebin/*.app"][0]

ERL_INCLUDE = "./include"
ERLC_FLAGS = %W( -I#{ERL_INCLUDE} -W )
ERLC_FLAGS << '+debug_info' if debug?

UNIT_TEST_FLAGS = []
INT_TEST_FLAGS  = []
PERF_TEST_FLAGS = []

TEST_ROOT = "."
TEST_LOG_DIR = "#{TEST_ROOT}/logs"

UNIT_TEST_DIR = "#{PWD}/#{TEST_ROOT}/test"
INT_TEST_DIR  = "#{PWD}/#{TEST_ROOT}/int_test"
PERF_TEST_DIR = "#{PWD}/#{TEST_ROOT}/perf_test"

CLEAN.include %w( **/*.beam **/erl_crash.dump )
CLOBBER.include TEST_LOG_DIR, 'doc'

## -------------------------------------------------------------------
## Rules

directory 'ebin'

rule ".beam" => ["%{ebin,src}X.erl"] do |t|
  puts "compiling #{t.source}..."
  sh "erlc #{print_flags(ERLC_FLAGS)} #{expand_path(ERL_PATH)} -o ebin #{t.source}"
end

## -------------------------------------------------------------------
## Tasks

desc "Compile Erlang sources to .beam files"
task :compile => ['ebin'] + ERL_BEAM do
  do_validate_app(APP_FILE)
end

desc "Do a fresh build from scratch"
task :rebuild => [:clean, :compile]

desc "Generate Edoc documentation"
task :doc do
  app = erl_app_name(APP_FILE)
  sh %Q(erl -noshell -run edoc_run application #{app} '"."' "[]"  -s init stop)
end

namespace :test do

  desc "Test preparation run before all tests"
  task :prepare do
    fail "No tests defined" unless File.directory?(TEST_ROOT)
    Dir.mkdir(TEST_LOG_DIR) unless File.directory?(TEST_LOG_DIR)

    # Always compile tests with debug info
    puts "Debugging is enabled for test builds."
    ERLC_FLAGS << '+debug_info'
  end

  task :results do
    `open #{TEST_LOG_DIR}/index.html`
  end

  ['unit', 'int', 'perf'].each do |type|
    desc "Run #{type} tests"
    task type => "test:#{type}:prepare" do
      check_and_run_tests(type, false)
    end

    namespace type do
      desc "Prepare #{type} tests"
      task :prepare => ['^prepare', 'compile']

      desc "Compile #{type} tests"
      task :compile => 'rake:compile' do
        compile_tests(type)
      end

      desc "Run #{type} tests with coverage"
      task :cover => :prepare do
        check_and_run_tests(type, true)
      end
    end
  end

  desc "Run all tests"
  task :all => [:unit, :int, :perf]

end

# Convenience tasks for backward compatibility
task :test => 'test:unit'
task :int_test => 'test:int'
task :perf_test => 'test:perf'

desc "Package app for publication to a faxien repo"
task :package => [:compile] do
  target_dir = package_dir
  FileUtils.rm_rf target_dir
  Dir.mkdir target_dir
  FileUtils.cp_r 'ebin', target_dir
  FileUtils.cp_r 'include', target_dir
  FileUtils.cp_r 'src', target_dir
  FileUtils.cp_r 'priv', target_dir if File.exist?('priv')
  puts "Packaged to #{target_dir}"
end

task :default => [:compile]

## -------------------------------------------------------------------
## Public functions

def append_flags(flags, value)
  flags << value
end

def erl_run(script, args = "")
  `erl -eval '#{script}' -s erlang halt #{args} -noshell 2>&1`.strip
end

def erl_where(lib)
  script = <<-ERL
      io:format("~s\n", [filename:join(code:lib_dir(#{lib}), "include")])
      ERL
  erl_run(script)
end

def erl_app_version(app)
  script = <<-ERL
     ok = application:load(#{app}),
     {ok, Vsn} = application:get_key(#{app}, vsn),
     io:format("~s\\n", [Vsn]).
     ERL
  output = erl_run(script, "-pa ebin")
  output.strip()
end

def erl_app_name(app_file)
  File.basename(app_file, ".app")
end

def package_dir
  app = erl_app_name(APP_FILE)
  vsn = erl_app_version(app)
  "#{app}-#{vsn}"
end


## -------------------------------------------------------------------
## Private functions

def print_flags(flags)
  flags.join(' ')
end

def erl_app_modules(app)
  script = <<-ERL
      ok = application:load(#{app}),
      {ok, M} = application:get_key(#{app}, modules),
      [io:format("~s\\n", [Mod]) || Mod <- M].
      ERL

  output = erl_run(script, "-pa ebin")
  if output[/badmatch/]
    puts "Error processing .app file: #{output}"
  else
    output.split("\n")
  end
end
{'fraction_non_alphanumeric': 0.24117158288325716, 'fraction_numerical': 0.0014540922309929372, 'mean_word_length': 2.51937134502924, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '451305', 'n_tokens_mistral': 3325, 'n_tokens_neox': 3267, 'n_words': 829}