class PressPass::Cli::NewProjectGenerator

Public Class Methods

new(command = "new") click to toggle source
# File lib/presspass/cli/new_project_generator.rb, line 9
def initialize(command = "new")
  @options = {:php_port => 8000}

  @app_name = ARGV.first

  OptionParser.new do |opts|
    opts.banner = "Usage: presspass #{command} <app_name> [options]"

    opts.on("--port NUMBER", "Port to run PHP on") do |php_port|
      @options[:php_port] = php_port
    end

  end.parse!(ARGV)

end

Public Instance Methods

run() click to toggle source
# File lib/presspass/cli/new_project_generator.rb, line 25
def run
  create_project_directory

  download_wordpress

  extract_wordpress_into_project_directory

  install_foreman(php_port: @options[:php_port])

  puts "WordPress installation created at #{@app_name}."
  puts "You can now run:"
  puts
  puts "    $ gem install foreman"
  puts "    $ cd #{@app_name}/"
  puts "    $ foreman start"
  puts
  puts "And access your WordPress site at http://localhost:8000!"
end

Private Instance Methods

create_project_directory() click to toggle source
# File lib/presspass/cli/new_project_generator.rb, line 54
def create_project_directory
  if File.exist?(@app_name)
    puts "A file or directory with name #{@app_name} already exists."
    exit 1
  end
end
download_wordpress() click to toggle source
# File lib/presspass/cli/new_project_generator.rb, line 61
def download_wordpress
  if !File.exists?("/tmp/wordpress-#{PressPass::WORDPRESS_VERSION}.tar.gz")
    filename = "wordpress-#{PressPass::WORDPRESS_VERSION}.tar.gz"
    puts "Downloading #{filename}..."

    `curl -o /tmp/#{filename} https://wordpress.org/#{filename}`
  end

  puts "#{filename} downloaded."
end
extract_wordpress_into_project_directory() click to toggle source
# File lib/presspass/cli/new_project_generator.rb, line 72
def extract_wordpress_into_project_directory
  filestamp = Time.now.to_i
  download_location = File.join('/tmp', "wordpress-#{PressPass::WORDPRESS_VERSION}.tar.gz")
  tmp_dir = "/tmp/wordpress-latest-#{filestamp}"

  Dir.mkdir(tmp_dir)
  `cd #{tmp_dir}; tar -xzf #{download_location}`

  FileUtils.mv("#{tmp_dir}/wordpress", @app_name)
  FileUtils.rm_r(tmp_dir)
end
install_foreman(php_port: 8000) click to toggle source
# File lib/presspass/cli/new_project_generator.rb, line 46
def install_foreman(php_port: 8000)
  foreman_config = "web: php -S 0.0.0.0:#{php_port}\n"

  File.open(File.join(@app_name, "Procfile"), "w+") do |f|
    f.write(foreman_config)
  end
end