<commit_before>#! env ruby
# For use in Jenkins: Build a locally deployed demo app.

checkout_result = %x[git checkout -f]

PROJECT_DIR     = ENV['PROJECT_DIR'] || "/tmp"
COMMIT_HASH     = ENV['COMMIT_HASH'] || %x[git log -1 --format=%H][0..7]
APP_NAME        = ENV['APP_NAME']    || "my_system"

URL_PREFIX      = "/#{APP_NAME}/releases/#{COMMIT_HASH}"
RELEASE_DIR     = "#{PROJECT_DIR}#{URL_PREFIX}"
BUILD_DIR       = "build_folder"

File.open('Buildfile', 'a') do |file|
  file.puts
  file.puts "config :all,"
  file.puts "   :url_prefix => '#{URL_PREFIX}'"
end

# need to rerun bundle install probably because of git checkout -f
%x[bundle install]

def doit(command)
  puts command
  %x[#{command}]
end

# clean
doit %[rm -rf   #{BUILD_DIR}]
doit %[rm -rf   #{RELEASE_DIR}]
doit %[mkdir -p #{RELEASE_DIR}]

# build
puts "***  Building #{APP_NAME} #{COMMIT_HASH} in #{BUILD_DIR}"
doit %[bundle exec sc-build #{APP_NAME} --mode="demo" --build=#{COMMIT_HASH} -r -c --languages=en --buildroot="#{BUILD_DIR}"]

# copy the build to the distro directory
puts "***  copying #{BUILD_DIR}#{URL_PREFIX} #{RELEASE_DIR} ***"
doit %[cp -r #{BUILD_DIR}/#{URL_PREFIX}/* #{RELEASE_DIR}]

# copy the authoring files too:
puts "***  copying authoring files to #{RELEASE_DIR} ***"
doit %[cp -r public/authoring #{RELEASE_DIR}]

# symlink top level files for convinience:
doit %[ln -sfh #{RELEASE_DIR}/#{APP_NAME}/en/#{COMMIT_HASH}/index.html #{RELEASE_DIR}/#{APP_NAME}/index.html]
doit %[ln -sfh #{RELEASE_DIR}/#{APP_NAME}/index.html #{RELEASE_DIR}/index.html]
doit %[ln -sfh #{RELEASE_DIR}/#{APP_NAME}/authoring #{RELEASE_DIR}/authoring]

# link to current release:
doit %[ln -sfh #{RELEASE_DIR} #{PROJECT_DIR}/#{APP_NAME}/current]

# convinience links:<commit_msg>Raise err. to trigger build failure when appropriate.<commit_after>#! env ruby
# For use in Jenkins: Build a locally deployed demo app.

checkout_result = %x[git checkout -f]

PROJECT_DIR     = ENV['PROJECT_DIR'] || "/tmp"
COMMIT_HASH     = ENV['COMMIT_HASH'] || %x[git log -1 --format=%H][0..7]
APP_NAME        = ENV['APP_NAME']    || "my_system"

URL_PREFIX      = "/#{APP_NAME}/releases/#{COMMIT_HASH}"
RELEASE_DIR     = "#{PROJECT_DIR}#{URL_PREFIX}"
BUILD_DIR       = "build_folder"

File.open('Buildfile', 'a') do |file|
  file.puts
  file.puts "config :all,"
  file.puts "   :url_prefix => '#{URL_PREFIX}'"
end

# need to rerun bundle install probably because of git checkout -f
%x[bundle install]

def doit(command, fail_quietly = false, verbose = true)
  puts command if verbose
  %x[#{command}]
  unless fail_quietly or $?.success?
    raise "Exception in shell command: #{$?.to_s}"
  end
end


# clean
doit %[rm -rf   #{BUILD_DIR}],   true
doit %[rm -rf   #{RELEASE_DIR}], true
doit %[mkdir -p #{RELEASE_DIR}]

# build
puts "***  Building #{APP_NAME} #{COMMIT_HASH} in #{BUILD_DIR}"
doit %[bundle exec sc-build #{APP_NAME} --mode="demo" --build=#{COMMIT_HASH} -r -c --languages=en --buildroot="#{BUILD_DIR}"]

# copy the build to the distro directory
puts "***  copying #{BUILD_DIR}#{URL_PREFIX} #{RELEASE_DIR} ***"
doit %[cp -r #{BUILD_DIR}/#{URL_PREFIX}/* #{RELEASE_DIR}]

# copy the authoring files too:
puts "***  copying authoring files to #{RELEASE_DIR} ***"
doit %[cp -r public/authoring #{RELEASE_DIR}]

# symlink top level files for convinience:
doit %[ln -sfh #{RELEASE_DIR}/#{APP_NAME}/en/#{COMMIT_HASH}/index.html #{RELEASE_DIR}/#{APP_NAME}/index.html]
doit %[ln -sfh #{RELEASE_DIR}/#{APP_NAME}/index.html #{RELEASE_DIR}/index.html]
doit %[ln -sfh #{RELEASE_DIR}/#{APP_NAME}/authoring #{RELEASE_DIR}/authoring]

# link to current release:
doit %[ln -sfh #{RELEASE_DIR} #{PROJECT_DIR}/#{APP_NAME}/current]

# convinience links:
{'fraction_non_alphanumeric': 0.17874526771227692, 'fraction_numerical': 0.0016224986479177934, 'mean_word_length': 4.6045454545454545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3537444', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1392, 'n_words': 347}