Sha256: 612ffd785d2f6e13cb829b48a3d62f682a20f6a8a662ff302febd7a0910c8e8e

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

#!/usr/bin/env ruby

require 'fileutils'

# Generator
class Generator
  attr_reader :spec, :app_dir, :example_dir

  def initialize(app_name)
    @spec = Gem::Specification.find_by_name('sinatra-embedded-shopify-app')
    gem_root = spec.gem_dir
    @example_dir = "#{gem_root}/example"

    working_dir = Dir.pwd
    @app_dir = "#{working_dir}/#{app_name}"

    log "Generating new app: #{app_name}"
    log "fullpath: #{app_dir}"
  end

  def run
    if Dir.exist? app_dir
      log 'App directory alread exists, pick a new app name or delete the existing folder'
      return
    end

    copy_example_app
    create_dot_env
    sub_latest_version
    bundle_install
    migrate_database
  end

  private

  def copy_example_app
    FileUtils.cp_r(example_dir, app_dir)
  end

  def create_dot_env
    FileUtils.touch("#{app_dir}/.env")
    file = File.open("#{app_dir}/.env", 'w')
    file.write("SHOPIFY_API_KEY=your_api_key\n")
    file.write("SHOPIFY_SHARED_SECRET=your_shared_secret\n")
    file.write("SECRET=random_string_to_encrypt_credentials_with\n")
    file.close
  end

  def sub_latest_version
    file_name = "#{app_dir}/Gemfile"
    text = File.read(file_name)
    new_contents = text.gsub("gem 'sinatra-embedded-shopify-app', path: '../'",
                             "gem 'sinatra-embedded-shopify-app', '~> #{spec.version}'")
    File.open(file_name, 'w') { |file| file.puts new_contents }
  end

  def bundle_install
    Dir.chdir(app_dir)

    pipe = IO.popen('bundle install')
    while (line = pipe.gets)
      print line
    end
  end

  def migrate_database
    Dir.chdir(app_dir)

    pipe = IO.popen('bundle exec rake db:migrate')
    while (line = pipe.gets)
      print line
    end
  end

  def log(msg)
    puts msg
  end
end

if ARGV.length < 2
  puts "Usage:\nsinatra-embedded-shopify-app-generator new <app_name>"
else
  app_name = ARGV[1]
  Generator.new(app_name).run
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-embedded-shopify-app-0.5.16 bin/sinatra-embedded-shopify-app-generator