<commit_before># hook order:
# configure [all]
# detect    [all]
# sync      [all]
# setup     [all]
# boxfile   [all]
# prepare   [all]
# build     [run]
# publish   [run]
# cleanup   [all]

# import some logic/helpers from lib/*.rb
include NanoBox::Engine
include NanoBox::Output

# store the original boxfile for later use if needed (this may not be needed forever)
registry(:original_boxfile, boxfile)

logtap.print(bullet('Running configure hook...'), 'debug')

# 0) temporary
logtap.print(bullet("Ensuring the engine dir exists..."), 'debug')

# ensure engine dir exists
directory "#{ENGINE_DIR}" do
  recursive true
end

# 1)
# If an engine is mounted from the workstation, let's put those in place first.
# This process will replace any default engine if the names collide.
if boxfile[:engine] and is_filepath?(boxfile[:engine])

  basename = ::File.basename(boxfile[:engine])
  path     = "#{SHARE_DIR}/engines/#{basename}"

  # if the engine has been shared with us, then let's copy it over
  if ::File.exist?(path)

    logtap.print(bullet("Detected engine from local workstation..."))

    # remove any official engine that may be in the way
    directory "#{ENGINE_DIR}/#{basename}" do
      action :delete
    end

    logtap.print(bullet("Copying engine from workstation into build container..."))

    # copy the mounted engine into place
    logtap.print(process_start('copy mounted engine'), 'debug')

    execute 'move engine into place' do
      command <<-EOF
        rsync \
          -v \
          -a \
          --exclude='.git/' \
          #{ENGINE_LIVE_DIR}/#{basename} \
          #{ENGINE_DIR}/
      EOF
      stream true
      on_data { |data| logtap.print subtask_info(data), 'debug' }
    end

    logtap.print(process_end, 'debug')
  end

  # now let's set the engine in the registry for later consumption
  registry('engine', basename)
end

# 2)
# If a custom engine is specified, and is not mounted from
# the workstation, let's fetch it from warehouse.nanobox.io.
# This process will replace any default engine if the names collide.
if boxfile[:engine] and not is_filepath?(boxfile[:engine])

  engine = engine_name(boxfile[:engine])

  # remove any official engine that may be in the way
  directory "#{ENGINE_DIR}/#{engine}" do
    action :delete
  end

  # ensure a directory for this engine exists
  directory "#{ENGINE_DIR}/#{engine}" do
    recursive true
  end

  logtap.print(process_start("Fetch #{boxfile[:engine]}"))

  execute "fetching #{engine}" do
    command <<-EOF
      nanobox \
        fetch \
        #{boxfile[:engine]} \
          | tar \
            -xzf - \
            -C #{ENGINE_DIR}/#{engine}
    EOF
    stream true
    on_stderr { |data| logtap.print subtask_info(data) }<commit_msg>Fetch custom engine with 'engine' subcommand<commit_after># hook order:
# configure [all]
# detect    [all]
# sync      [all]
# setup     [all]
# boxfile   [all]
# prepare   [all]
# build     [run]
# publish   [run]
# cleanup   [all]

# import some logic/helpers from lib/*.rb
include NanoBox::Engine
include NanoBox::Output

# store the original boxfile for later use if needed (this may not be needed forever)
registry(:original_boxfile, boxfile)

logtap.print(bullet('Running configure hook...'), 'debug')

# 0) temporary
logtap.print(bullet("Ensuring the engine dir exists..."), 'debug')

# ensure engine dir exists
directory "#{ENGINE_DIR}" do
  recursive true
end

# 1)
# If an engine is mounted from the workstation, let's put those in place first.
# This process will replace any default engine if the names collide.
if boxfile[:engine] and is_filepath?(boxfile[:engine])

  basename = ::File.basename(boxfile[:engine])
  path     = "#{SHARE_DIR}/engines/#{basename}"

  # if the engine has been shared with us, then let's copy it over
  if ::File.exist?(path)

    logtap.print(bullet("Detected engine from local workstation..."))

    # remove any official engine that may be in the way
    directory "#{ENGINE_DIR}/#{basename}" do
      action :delete
    end

    logtap.print(bullet("Copying engine from workstation into build container..."))

    # copy the mounted engine into place
    logtap.print(process_start('copy mounted engine'), 'debug')

    execute 'move engine into place' do
      command <<-EOF
        rsync \
          -v \
          -a \
          --exclude='.git/' \
          #{ENGINE_LIVE_DIR}/#{basename} \
          #{ENGINE_DIR}/
      EOF
      stream true
      on_data { |data| logtap.print subtask_info(data), 'debug' }
    end

    logtap.print(process_end, 'debug')
  end

  # now let's set the engine in the registry for later consumption
  registry('engine', basename)
end

# 2)
# If a custom engine is specified, and is not mounted from
# the workstation, let's fetch it from warehouse.nanobox.io.
# This process will replace any default engine if the names collide.
if boxfile[:engine] and not is_filepath?(boxfile[:engine])

  engine = engine_name(boxfile[:engine])

  # remove any official engine that may be in the way
  directory "#{ENGINE_DIR}/#{engine}" do
    action :delete
  end

  # ensure a directory for this engine exists
  directory "#{ENGINE_DIR}/#{engine}" do
    recursive true
  end

  logtap.print(process_start("Fetch #{boxfile[:engine]}"))

  execute "fetching #{engine}" do
    command <<-EOF
      nanobox engine fetch \
        #{boxfile[:engine]} \
          | tar \
            -xzf - \
            -C #{ENGINE_DIR}/#{engine}
    EOF
    stream true
    on_stderr { |data| logtap.print subtask_info(data) }
{'fraction_non_alphanumeric': 0.11623282718727404, 'fraction_numerical': 0.0010845986984815619, 'mean_word_length': 3.077376565954311, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '5363301', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1670, 'n_words': 574}