<commit_before>task "extras:copy_terms" => :environment do
  ['privacy', 'terms', 'jobs'].each do |type|
    str = "<!-- auto-generated app/views/shared/_#{type}.html.erb -->\n"
    str += File.read("./app/views/shared/_#{type}.html.erb")
    File.open("./app/frontend/app/templates/#{type}.hbs", 'w') do |f|
      f.puts str
    end
  end
end

task "extras:assert_js" do
  `mkdir -p ./app/frontend/dist/assets`
  `cp -n ./app/frontend/frontend-placeholder.js ./app/frontend/dist/assets/frontend.js`
  `touch ./app/frontend/dist/assets/vendor.js`
  `cd app/assets/javascripts/ && ln -sf ../../frontend/dist/assets/frontend.js frontend.js`
  `cd app/assets/javascripts/ && ln -sf ../../frontend/dist/assets/vendor.js vendor.js`
end

task "extras:jobs_list" do
  require 'worker'
  puts "default queue"
  puts Worker.scheduled_actions('default')
  puts "priority queue"
  puts Worker.scheduled_actions('priority')
  puts "slow queue"
  puts Worker.scheduled_actions('slow')
end

task "extras:clear_report_tallies" => :environment do
  RedisInit.default.del('missing_words')
  RedisInit.default.del('missing_symbols')
end

task "extras:deploy_notification", [:system, :level, :version] => :environment do |t, args|
  message = "Something got deployed!"
  if !args[:system] && ARGV.length > 1
    ARGV.each { |a| task a.to_sym do ; end }    
    args = {
      :system => ARGV[1],
      :level => ARGV[2],
      :version => ARGV[3]
    }
  end
  if args[:system] && args[:system].downcase == 'android'
    if args[:level] && (args[:level].downcase == 'beta' || args[:level].downcase == 'alpha')
      message = "#{args[:level]} version pushed out for testing on Android\nplease kick the tires when you have a chance"
    else
      message = "An update on the Google Play Store is going live"
      message += " (#{args[:version]})" if args[:version]
      message += "\nif people start reporting bugs, that is probably why"
    end
    message += "\n<https://play.google.com/store/apps/details?id=com.mycoughdrop.coughdrop|app store link>"
  elsif args[:system] && args[:system].downcase == 'ios'
    if args[:level] && (args[:level].downcase == 'beta' || args[:level].downcase == 'alpha')
      message "#{args[:level]} version submitted to the iOS App Store"
    else
      message = "An update has been submitted to the iOS App Store"
      message += " (#{args[:version]})" if args[:version]
      message += "\nit typically takes 7-10 days to get approved, so sit tight"
    end
    message += "\n<https://itunes.apple.com/us/app/coughdrop/id1021384570|app store link>"
  elsif args[:system] && (args[:system].downcase == 'kindle' || args[:system].downcase == 'amazon')
    message = "An update on the Amazon App Store is going live"
    message += " (#{args[:version]})" if args[:version]
    message += "\nif people start reporting bugs, that is probably why"
    message += "\n<https://www.amazon.com/CoughDrop-Inc-AAC/dp/B01BU8RUEY/ref=sr_1_1?s=mobile-apps&ie=UTF8&qid=1478539872&sr=1-1&keywords=coughdrop|app store link>"
  elsif args[:system] && args[:system].downcase == 'windows'
    message = "New version of the Windows app is available"
    message += " (#{args[:version]})" if args[:version]
    message += "\n<https://www.mycoughdrop.com/download|download links>"
  elsif args[:system]
    raise "unrecognized system, #{args[:system]}"
  else
    str = File.read('./app/assets/javascripts/application-preload.js')
    match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+\w*)\";/)
    version = match && match[1]
    message = "New version deployed to servers (#{version})"
    message += "\n<https://github.com/CoughDrop/coughdrop/blob/master/CHANGELOG.md|change notes> | <https://github.com/CoughDrop/coughdrop/commits/master|detailed log>"
  end
  json = {"username": "deploy-bot", "icon_emoji": ":cuttlefish:", "text":message}
  `curl -X POST -H 'Content-type: application/json' --data '#{json.to_json}' #{ENV['SLACK_NOTIFICATION_URL']}`
  #SLACK_NOTIFICATION_URL
end

task "extras:version" => :environment do
  str = File.read('./app/assets/javascripts/application-preload.js')
  match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+)(\w*)\";/)
  version = match[1]
  revision = match[2]
  date_version = Date.today.strftime('%Y.%m.%d')
  if version == date_version
    if revision == ""
      revision = 'a'
    elsif revision[-1] == 'z'
      revision = revision[0..-2] + 'aa'
    else
      revision = revision[0..-2] + (revision[-1].ord + 1).chr
    end
    date_version += revision
  end
  str.sub!(/window\.app_version\s*=\s*\"[^\"]*\";/, "window.app_version = \"#{date_version}\";")
  File.write('./app/assets/javascripts/application-preload.js', str)
  puts date_version
end

task "extras:desktop" => :environment do
  folder = 'coughdrop_desktop'
  js = nil
  css = nil
  Dir.glob('./public/assets/application-*') do |fn|
    if fn.match(/\.js$/)
      js = fn
    elsif fn.match(/\.css$/)
      css = fn
    end
  end
  if !js || !css
    raise "need both a js and css to be created"
  end
  puts "copying static assets"
  puts `cp ./public/images/* ../#{folder}/www/images`
  puts `cp ./public/images/logos/* ../#{folder}/www/images/logos`
  puts `cp ./public/fonts/* ../#{folder}/www/fonts`
  puts `cp ./public/icons/* ../#{folder}/www/assets/icons`
  puts `cp #{js} ../#{folder}/www/app.js`
  puts `cp #{css} ../#{folder}/www/css/app.css`

  puts "updating index file"
  content = File.read("../#{folder}/www/desktop_index.html")
  str = ""
  if ENV['TRACK_JS_TOKEN']
    str += "<script type=\"text/javascript\" async src=\"//d2zah9y47r7bi2.cloudfront.net/releases/current/tracker.js\" data-token=\"#{ ENV['TRACK_JS_TOKEN'] }\"></script>"
  end
  str += "\n<div id='enabled_frontend_features' data-list='#{FeatureFlags::ENABLED_FRONTEND_FEATURES.join(',')}'></div>"

  pre, chunk = content.split(/<!-- begin generated content -->/)
  chunk, post = chunk.split(/<!-- end generated content -->/)
  content = pre + "<!-- begin generated content -->\n" + str + "\n\n<!-- end generated content -->" + post
  File.write("../#{folder}/www/desktop_index.html", content)

  puts "updating electron version"
  
  str = File.read('./app/assets/javascripts/application-preload.js')
  match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+\w*)\";/)
  str = File.read("../#{folder}/package.json")
  full_version = (match && match[1]) || Date.today.strftime('%Y.%m.%d')
  full_version = full_version[2..-1].gsub(/[a-z]+/, '')
  str = str.sub(/\"version\"\s*:\s*\"[^\"]+\"/, "\"version\": \"#{full_version}\"");
  File.write("../#{folder}/package.json", str)

  str = File.read('./app/assets/javascripts/application-preload.js')
  match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+\w*)\";/)
  str = File.read("../#{folder}/www/init.js")
  full_version = (match && match[1]) || Date.today.strftime('%Y.%m.%d')
  str = str.sub(/window\.app_version\s*=\s*\"[^\"]+\"/, "window.app_version = \"#{full_version}\"");
  File.write("../#{folder}/www/init.js", str)
end

task "extras:mobile" => :environment do
  folder = 'coughdrop_mobile'
  js = nil
  css = nil
  Dir.glob('./public/assets/application-*') do |fn|
    if fn.match(/\.js$/)
      js = fn
    elsif fn.match(/\.css$/)
      css = fn
    end
  end
  if !js || !css
    raise "need both a js and css to be created"
  end
  puts "copying static assets"
  puts `cp ./public/images/* ../#{folder}/www/images`
  puts `cp ./public/images/logos/* ../#{folder}/www/images/logos`<commit_msg>desktop doesn't like leading zeroes anymore in some needed library<commit_after>task "extras:copy_terms" => :environment do
  ['privacy', 'terms', 'jobs'].each do |type|
    str = "<!-- auto-generated app/views/shared/_#{type}.html.erb -->\n"
    str += File.read("./app/views/shared/_#{type}.html.erb")
    File.open("./app/frontend/app/templates/#{type}.hbs", 'w') do |f|
      f.puts str
    end
  end
end

task "extras:assert_js" do
  `mkdir -p ./app/frontend/dist/assets`
  `cp -n ./app/frontend/frontend-placeholder.js ./app/frontend/dist/assets/frontend.js`
  `touch ./app/frontend/dist/assets/vendor.js`
  `cd app/assets/javascripts/ && ln -sf ../../frontend/dist/assets/frontend.js frontend.js`
  `cd app/assets/javascripts/ && ln -sf ../../frontend/dist/assets/vendor.js vendor.js`
end

task "extras:jobs_list" do
  require 'worker'
  puts "default queue"
  puts Worker.scheduled_actions('default')
  puts "priority queue"
  puts Worker.scheduled_actions('priority')
  puts "slow queue"
  puts Worker.scheduled_actions('slow')
end

task "extras:clear_report_tallies" => :environment do
  RedisInit.default.del('missing_words')
  RedisInit.default.del('missing_symbols')
end

task "extras:deploy_notification", [:system, :level, :version] => :environment do |t, args|
  message = "Something got deployed!"
  if !args[:system] && ARGV.length > 1
    ARGV.each { |a| task a.to_sym do ; end }    
    args = {
      :system => ARGV[1],
      :level => ARGV[2],
      :version => ARGV[3]
    }
  end
  if args[:system] && args[:system].downcase == 'android'
    if args[:level] && (args[:level].downcase == 'beta' || args[:level].downcase == 'alpha')
      message = "#{args[:level]} version pushed out for testing on Android\nplease kick the tires when you have a chance"
    else
      message = "An update on the Google Play Store is going live"
      message += " (#{args[:version]})" if args[:version]
      message += "\nif people start reporting bugs, that is probably why"
    end
    message += "\n<https://play.google.com/store/apps/details?id=com.mycoughdrop.coughdrop|app store link>"
  elsif args[:system] && args[:system].downcase == 'ios'
    if args[:level] && (args[:level].downcase == 'beta' || args[:level].downcase == 'alpha')
      message "#{args[:level]} version submitted to the iOS App Store"
    else
      message = "An update has been submitted to the iOS App Store"
      message += " (#{args[:version]})" if args[:version]
      message += "\nit typically takes 7-10 days to get approved, so sit tight"
    end
    message += "\n<https://itunes.apple.com/us/app/coughdrop/id1021384570|app store link>"
  elsif args[:system] && (args[:system].downcase == 'kindle' || args[:system].downcase == 'amazon')
    message = "An update on the Amazon App Store is going live"
    message += " (#{args[:version]})" if args[:version]
    message += "\nif people start reporting bugs, that is probably why"
    message += "\n<https://www.amazon.com/CoughDrop-Inc-AAC/dp/B01BU8RUEY/ref=sr_1_1?s=mobile-apps&ie=UTF8&qid=1478539872&sr=1-1&keywords=coughdrop|app store link>"
  elsif args[:system] && args[:system].downcase == 'windows'
    message = "New version of the Windows app is available"
    message += " (#{args[:version]})" if args[:version]
    message += "\n<https://www.mycoughdrop.com/download|download links>"
  elsif args[:system]
    raise "unrecognized system, #{args[:system]}"
  else
    str = File.read('./app/assets/javascripts/application-preload.js')
    match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+\w*)\";/)
    version = match && match[1]
    message = "New version deployed to servers (#{version})"
    message += "\n<https://github.com/CoughDrop/coughdrop/blob/master/CHANGELOG.md|change notes> | <https://github.com/CoughDrop/coughdrop/commits/master|detailed log>"
  end
  json = {"username": "deploy-bot", "icon_emoji": ":cuttlefish:", "text":message}
  `curl -X POST -H 'Content-type: application/json' --data '#{json.to_json}' #{ENV['SLACK_NOTIFICATION_URL']}`
  #SLACK_NOTIFICATION_URL
end

task "extras:version" => :environment do
  str = File.read('./app/assets/javascripts/application-preload.js')
  match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+)(\w*)\";/)
  version = match[1]
  revision = match[2]
  date_version = Date.today.strftime('%Y.%m.%d')
  if version == date_version
    if revision == ""
      revision = 'a'
    elsif revision[-1] == 'z'
      revision = revision[0..-2] + 'aa'
    else
      revision = revision[0..-2] + (revision[-1].ord + 1).chr
    end
    date_version += revision
  end
  str.sub!(/window\.app_version\s*=\s*\"[^\"]*\";/, "window.app_version = \"#{date_version}\";")
  File.write('./app/assets/javascripts/application-preload.js', str)
  puts date_version
end

task "extras:desktop" => :environment do
  folder = 'coughdrop_desktop'
  js = nil
  css = nil
  Dir.glob('./public/assets/application-*') do |fn|
    if fn.match(/\.js$/)
      js = fn
    elsif fn.match(/\.css$/)
      css = fn
    end
  end
  if !js || !css
    raise "need both a js and css to be created"
  end
  puts "copying static assets"
  puts `cp ./public/images/* ../#{folder}/www/images`
  puts `cp ./public/images/logos/* ../#{folder}/www/images/logos`
  puts `cp ./public/fonts/* ../#{folder}/www/fonts`
  puts `cp ./public/icons/* ../#{folder}/www/assets/icons`
  puts `cp #{js} ../#{folder}/www/app.js`
  puts `cp #{css} ../#{folder}/www/css/app.css`

  puts "updating index file"
  content = File.read("../#{folder}/www/desktop_index.html")
  str = ""
  if ENV['TRACK_JS_TOKEN']
    str += "<script type=\"text/javascript\" async src=\"//d2zah9y47r7bi2.cloudfront.net/releases/current/tracker.js\" data-token=\"#{ ENV['TRACK_JS_TOKEN'] }\"></script>"
  end
  str += "\n<div id='enabled_frontend_features' data-list='#{FeatureFlags::ENABLED_FRONTEND_FEATURES.join(',')}'></div>"

  pre, chunk = content.split(/<!-- begin generated content -->/)
  chunk, post = chunk.split(/<!-- end generated content -->/)
  content = pre + "<!-- begin generated content -->\n" + str + "\n\n<!-- end generated content -->" + post
  File.write("../#{folder}/www/desktop_index.html", content)

  puts "updating electron version"
  
  str = File.read('./app/assets/javascripts/application-preload.js')
  match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+\w*)\";/)
  str = File.read("../#{folder}/package.json")
  full_version = (match && match[1]) || Date.today.strftime('%Y.%m.%d')
  full_version = full_version[2..-1].gsub(/[a-z]+/, '').gsub(/\.0+/, '.')
  str = str.sub(/\"version\"\s*:\s*\"[^\"]+\"/, "\"version\": \"#{full_version}\"");
  File.write("../#{folder}/package.json", str)

  str = File.read('./app/assets/javascripts/application-preload.js')
  match = str.match(/window\.app_version\s+=\s+\"([0-9\.]+\w*)\";/)
  str = File.read("../#{folder}/www/init.js")
  full_version = (match && match[1]) || Date.today.strftime('%Y.%m.%d')
  str = str.sub(/window\.app_version\s*=\s*\"[^\"]+\"/, "window.app_version = \"#{full_version}\"");
  File.write("../#{folder}/www/init.js", str)
end

task "extras:mobile" => :environment do
  folder = 'coughdrop_mobile'
  js = nil
  css = nil
  Dir.glob('./public/assets/application-*') do |fn|
    if fn.match(/\.js$/)
      js = fn
    elsif fn.match(/\.css$/)
      css = fn
    end
  end
  if !js || !css
    raise "need both a js and css to be created"
  end
  puts "copying static assets"
  puts `cp ./public/images/* ../#{folder}/www/images`
  puts `cp ./public/images/logos/* ../#{folder}/www/images/logos`
{'fraction_non_alphanumeric': 0.2039443000866147, 'fraction_numerical': 0.008461589712838963, 'mean_word_length': 3.4186046511627906, 'pattern_counts': {'":': 10, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 12, 'lorem ipsum': 0, 'www.': 28, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1510659', 'n_tokens_mistral': 5369, 'n_tokens_neox': 5305, 'n_words': 1133}