<commit_before>defmodule WhiteBread.CommandLine.ContextLoader do

  def load_context_files(context_path) do
    path_pattern = context_path <> "**"
    IO.puts "loading all contexts from #{path_pattern}"
    path_pattern
      |> Path.wildcard()
      |> Enum.filter(&is_script?/1)
      |> Enum.map(&Code.require_file/1)
  end

  def load_create_context([first | _] = context_options)
  when is_list(context_options)
  do
    existing_context = context_options
      |> Stream.filter(&File.exists?/1)
      |> Enum.take(1)
    case existing_context do
      [context] -> load_context_file(context)
      []        -> create_context(first)
    end
  end

  def context_from_string(context_name) do
    {context, []} = Code.eval_string(context_name)
    context
  end

  defp create_context(context) do
    context_text = WhiteBread.CodeGenerator.Context.empty_context
    IO.puts "Default context module not found in #{context}. "
    IO.puts "Create one [Y/n]? "
    acceptance = IO.read(:stdio, :line)

    unless acceptance == "n" <> "\n" do
      File.write(context, context_text)
    end
  end

  defp load_context_file(path) do
    IO.puts "loading #{path}"
    [{context_module, _} | _] = Code.load_file(path)
    context_module
  end

  defp is_script?(file_path) do
    fn(file_path) -> file_path |> String.ends_with?(".exs") end
  end
<commit_msg>rename is_script? to script?<commit_after>defmodule WhiteBread.CommandLine.ContextLoader do

  def load_context_files(context_path) do
    path_pattern = context_path <> "**"
    IO.puts "loading all contexts from #{path_pattern}"
    path_pattern
      |> Path.wildcard()
      |> Enum.filter(&script?/1)
      |> Enum.map(&Code.require_file/1)
  end

  def load_create_context([first | _] = context_options)
  when is_list(context_options)
  do
    existing_context = context_options
      |> Stream.filter(&File.exists?/1)
      |> Enum.take(1)
    case existing_context do
      [context] -> load_context_file(context)
      []        -> create_context(first)
    end
  end

  def context_from_string(context_name) do
    {context, []} = Code.eval_string(context_name)
    context
  end

  defp create_context(context) do
    context_text = WhiteBread.CodeGenerator.Context.empty_context
    IO.puts "Default context module not found in #{context}. "
    IO.puts "Create one [Y/n]? "
    acceptance = IO.read(:stdio, :line)

    unless acceptance == "n" <> "\n" do
      File.write(context, context_text)
    end
  end

  defp load_context_file(path) do
    IO.puts "loading #{path}"
    [{context_module, _} | _] = Code.load_file(path)
    context_module
  end

  defp script?(file_path) do
    fn(file_path) -> file_path |> String.ends_with?(".exs") end
  end

{'fraction_non_alphanumeric': 0.118986412045538, 'fraction_numerical': 0.0029379360998898272, 'mean_word_length': 3.323809523809524, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, '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': '2003935', 'n_tokens_mistral': 936, 'n_tokens_neox': 906, 'n_words': 185}