Sha256: cddb184f54798ebc38222bd55439157da5caeb1b4e39142fcb431fce852f6859

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

#!/usr/bin/env ruby
require 'fileutils'

class Generator
  attr_reader :spec, :app_dir, :example_dir

  def initialize(app_name)
    @spec = Gem::Specification.find_by_name('shopify-sinatra-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.exists? 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("SHOPIFY_REDIRECT_URI=your_redirect_uri\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 'shopify-sinatra-app', github: 'kevinhughes27/shopify-sinatra-app'", "gem 'shopify-sinatra-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:\nshopify-sinatra-app-generator new <app_name>"
else
  app_name = ARGV[1]
  Generator.new(app_name).run
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shopify-sinatra-app-0.3.2 bin/shopify-sinatra-app-generator
shopify-sinatra-app-0.3.1 bin/shopify-sinatra-app-generator
shopify-sinatra-app-0.3.0 bin/shopify-sinatra-app-generator
shopify-sinatra-app-0.2.0 bin/shopify-sinatra-app-generator