Skip to content

Instantly share code, notes, and snippets.

@mulder
Created May 18, 2016 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mulder/de168f24faa0be9d98b545de588e7e50 to your computer and use it in GitHub Desktop.
Save mulder/de168f24faa0be9d98b545de588e7e50 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require File.expand_path('../../lib/themer', __FILE__)
Themer::CLI.start
require 'fileutils'
require 'thor'
module Themer
class CLI < Thor
include Thor::Actions
LOGIN_AUTH_FILE = '.themer_auth'
class_option :quiet, type: :boolean
class_option :force_login, type: :boolean, aliases: '-l'
desc 'login', "Write shop user / pass to .shopify_themer file so you don't have to login each time"
def login(myshopify_name)
if yes?("[Auth] Write login credentials to ./#{LOGIN_AUTH_FILE}?")
shopify_shop_url = auth_from_prompt(myshopify_name)
create_file LOGIN_AUTH_FILE, shopify_shop_url
end
end
desc 'pull MYSHOPIFY_NAME', 'Pull current theme from the specified shop and store it in the current directory'
method_option :overwrite, type: :boolean, aliases: '-f'
def pull(myshopify_name = nil)
shopify_shop_url = auth(myshopify_name)
if options[:overwrite] || yes?("Overwrite files in local directory?")
say_status 'overwrite', "Forcing pull on local directory" if options[:overwrite]
Themer::Sync.new(local_path: FileUtils.pwd, shopify_shop_url: shopify_shop_url).pull
end
end
desc 'push MYSHOPIFY_NAME', 'Push files from local directory to specified shop'
method_option :overwrite, type: :boolean, aliases: '-f'
def push(myshopify_name = nil)
shopify_shop_url = auth(myshopify_name)
if options[:overwrite] || yes?("Overwrite files in remote shop?")
say_status 'overwrite', "Forcing push to remote shop" if options[:overwrite]
Themer::Sync.new(local_path: FileUtils.pwd, shopify_shop_url: shopify_shop_url).push
end
end
private
def auth(myshopify_name)
auth_from_file || auth_from_prompt(myshopify_name)
end
def auth_from_file
return open(LOGIN_AUTH_FILE).read if !options[:force_login] && File.exists?(LOGIN_AUTH_FILE)
end
def auth_from_prompt(myshopify_name)
user = ask("[Auth] Login: ")
password = ask("[Auth] Password: ", echo: false)
puts "\n\n"
"https://#{user}:#{password}@#{myshopify_name}.myshopify.com/admin"
end
end
end
module Themer
class Sync
attr_reader :shopify_shop_url, :local_path
def initialize(local_path: '', shopify_shop_url: '')
@shopify_shop_url = shopify_shop_url
@local_path = local_path
end
def pull_theme_from_shopify_to_local_path
puts "Pulling theme code from #{shopify_shop_url} current theme."
end
alias_method :pull, :pull_theme_from_shopify_to_local_path
def push_theme_from_local_path_to_shopify
puts "Pushing theme code from #{local_path} to #{shopify_shop_url}."
end
alias_method :push, :push_theme_from_local_path_to_shopify
end
end
require File.expand_path('../themer/sync', __FILE__)
require File.expand_path('../themer/cli', __FILE__)
Gem::Specification.new do |s|
s.name = 'themer'
s.version = '0.0.1'
s.date = '2016-05-17'
s.summary = "A simple gem to demo a thor binary"
s.description = "A simple gem to demo a thor binary"
s.authors = ["Nick Mulder"]
s.email = 'mulder@shopify.com'
s.files = ["lib/themer.rb", "lib/themer/sync.rb", "lib/themer/cli.rb"]
s.executables << 'themer'
s.homepage =
'http://rubygems.org/gems/themer'
s.license = 'MIT'
end
# Run ME
# gem build themer.gemspec
# gem install ./themer-0.0.1.gem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment