<commit_before># run with:
# rails new my_app -m ./react_starter_app/template.rb
# rails new my_app -m https://raw.githubusercontent.com/atomicjolt/react_starter_app/master/template.rb

require "fileutils"
require "securerandom"


#repo = "<EMAIL>:atomicjolt/react_starter_app.git"
repo = "https://github.com/atomicjolt/react_starter_app.git"

working_dir = destination_root

###########################################################
# 
# Overrides
#
def source_paths
  puts "*******************************************************************"
  puts File.expand_path(File.dirname(__FILE__))
  puts "*******************************************************************"
  Array(super) + 
    [File.expand_path(File.dirname(__FILE__)), destination_root]
end

###########################################################
# 
# Helper methods
# 
def git_repo_url
  @git_repo_url ||= ask_with_default("What is the Github or bitbucket remote URL for this project?", :blue, "skip")
end

def git_repo_specified?
  git_repo_url != "skip" && !git_repo_url.strip.empty?
end

def gsub_file(path, regexp, *args, &block)
  content = File.read(path).gsub(regexp, *args, &block)
  File.open(path, 'wb') { |file| file.write(content) }
end

def ask_with_default(question, color, default)
  return default unless $stdin.tty?
  question = (question.split("?") << " [#{default}]?").join
  answer = ask(question, color)
  answer.to_s.strip.empty? ? default : answer
end

def app_dir
  destination_root.split('/').last
end

###########################################################
# 
# Gather information
# 
app_name = app_dir
url_safe_name = app_name.parameterize.gsub("-", "").gsub("_", "")
git_repo_url
rails_port = ask_with_default("Port for Rails?", :blue, 3000)
assets_port = ask_with_default("Port for assets server?", :blue, 8000)


###########################################################
# 
# Clone and add remote
# 
FileUtils.rm_rf("#{working_dir}/.")               # Get rid of the rails generated code.
run "cd .. && git clone #{repo} #{working_dir}"   # Replace it with our repo.
git remote: "set-url origin #{git_repo_url}" if git_repo_specified?
git remote: "add upstream #{repo}"


###########################################################
# 
# Database.yml
#
inside 'config' do 
  copy_file "database.example.yml", "database.yml"
end


###########################################################
# 
# secrets.yml
# 
inside 'config' do
  copy_file "secrets.example.yml", "secrets.yml"

  gsub_file("secrets.yml", "<Run rake secret to get a value to put here>") do |match|
    SecureRandom.hex(64)
  end
end


###########################################################
# 
# .env
# 
create_file '.env' do <<-EOF
APP_SUBDOMAIN=#{url_safe_name}
APP_URL=ngrok.io
APP_PORT=#{rails_port}
ASSETS_SUBDOMAIN=#{url_safe_name}assets
ASSETS_PORT=#{assets_port}
ASSETS_URL=https://#{url_safe_name}assets.ngrok.io
APP_DEFAULT_CANVAS_URL=https://atomicjolt.instructure.com
EOF
end


###########################################################
# 
# Modify application name
#
allowed = [".rb", ".js", ".yml", ".erb", ".json", ".md", ".jsx", ".example"]
modify_files = Dir.glob("#{working_dir}/**/*").reject { |f| File.directory?(f) || !allowed.include?(File.extname(f)) }
modify_files << ".env"
modify_files << "Gemfile"
modify_files << ".ruby-gemset"

modify_files.each do |f|
  
  gsub_file(f, "react_starter_app") do |match|
    app_name.underscore
  end

  gsub_file(f, "ReactStarterApp") do |match|
    app_name.classify
  end

  gsub_file(f, "reactstarterapp") do |match|
    url_safe_name
  end

  gsub_file(f, "React Starter App") do |match|
    app_name.titleize
  end<commit_msg>Fixes using template directly from github<commit_after># run with:
# rails new my_app -m ./react_starter_app/template.rb
# rails new my_app -m https://raw.githubusercontent.com/atomicjolt/react_starter_app/master/template.rb

require "fileutils"
require "securerandom"

#repo = "<EMAIL>:atomicjolt/react_starter_app.git"
repo = "https://github.com/atomicjolt/react_starter_app.git"

# keep track if the initial directory
@working_dir = destination_root

###########################################################
# 
# Overrides
#
def source_paths
  paths = Array(super) + 
    [File.expand_path(File.dirname(__FILE__))]
  paths << @working_dir
  paths
end

###########################################################
# 
# Helper methods
# 
def git_repo_url
  @git_repo_url ||= ask_with_default("What is the Github or bitbucket remote URL for this project?", :blue, "skip")
end

def git_repo_specified?
  git_repo_url != "skip" && !git_repo_url.strip.empty?
end

def gsub_file(path, regexp, *args, &block)
  content = File.read(path).gsub(regexp, *args, &block)
  File.open(path, 'wb') { |file| file.write(content) }
end

def ask_with_default(question, color, default)
  return default unless $stdin.tty?
  question = (question.split("?") << " [#{default}]?").join
  answer = ask(question, color)
  answer.to_s.strip.empty? ? default : answer
end

def app_dir
  @working_dir.split('/').last
end

###########################################################
# 
# Gather information
#
app_name = app_dir
url_safe_name = app_name.parameterize.gsub("-", "").gsub("_", "")
git_repo_url
rails_port = ask_with_default("Port for Rails?", :blue, 3000)
assets_port = ask_with_default("Port for assets server?", :blue, 8000)


###########################################################
# 
# Clone and add remote
# 
FileUtils.rm_rf("#{@working_dir}/.")               # Get rid of the rails generated code.
run "cd .. && git clone #{repo} #{@working_dir}"   # Replace it with our repo.
git remote: "set-url origin #{git_repo_url}" if git_repo_specified?
git remote: "add upstream #{repo}"


###########################################################
# 
# Database.yml
#
inside 'config' do
  copy_file "database.example.yml", "database.yml"
end


###########################################################
# 
# secrets.yml
# 
inside 'config' do
  copy_file "secrets.example.yml", "secrets.yml"

  gsub_file("secrets.yml", "<Run rake secret to get a value to put here>") do |match|
    SecureRandom.hex(64)
  end
end


###########################################################
# 
# .env
# 
create_file '.env' do <<-EOF
APP_SUBDOMAIN=#{url_safe_name}
APP_URL=ngrok.io
APP_PORT=#{rails_port}
ASSETS_SUBDOMAIN=#{url_safe_name}assets
ASSETS_PORT=#{assets_port}
ASSETS_URL=https://#{url_safe_name}assets.ngrok.io
APP_DEFAULT_CANVAS_URL=https://atomicjolt.instructure.com
EOF
end


###########################################################
# 
# Modify application name
#
allowed = [".rb", ".js", ".yml", ".erb", ".json", ".md", ".jsx", ".example"]
modify_files = Dir.glob("#{@working_dir}/**/*").reject { |f| File.directory?(f) || !allowed.include?(File.extname(f)) }
modify_files << ".env"
modify_files << "Gemfile"
modify_files << ".ruby-gemset"

modify_files.each do |f|
  
  gsub_file(f, "react_starter_app") do |match|
    app_name.underscore
  end

  gsub_file(f, "ReactStarterApp") do |match|
    app_name.classify
  end

  gsub_file(f, "reactstarterapp") do |match|
    url_safe_name
  end

  gsub_file(f, "React Starter App") do |match|
    app_name.titleize
  end
{'fraction_non_alphanumeric': 0.27720171436471724, 'fraction_numerical': 0.0027651043826904466, 'mean_word_length': 5.57039055404178, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 7, 'https://': 8, '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': '4838576', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2377, 'n_words': 505}