class Shopifly::Syncer

Public Class Methods

new() click to toggle source
# File lib/shopifly/syncer.rb, line 7
def initialize
  @config = Shopifly::Config.new
  @store_config = @config.store_config
  @shared_config = @config.shared_config
end

Public Instance Methods

create_theme(branch) click to toggle source
# File lib/shopifly/syncer.rb, line 96
def create_theme(branch)
  ShopifyAPI::Theme.create(name: branch)
end
default_theme() click to toggle source
# File lib/shopifly/syncer.rb, line 62
def default_theme
  @default_theme ||= begin
    themes_array = ShopifyAPI::Theme.find(:all)

    themes_array.select do |theme|
      theme.attributes["role"] == "main"
    end.first
  end
end
delete_settings_json() click to toggle source
# File lib/shopifly/syncer.rb, line 78
def delete_settings_json
  p "Removing local settings"

  `rm #{@shared_config["directory"]}config/settings_data.json`
end
deploy_theme() click to toggle source
# File lib/shopifly/syncer.rb, line 45
def deploy_theme
  p "Uploading theme!"
  system @config.deploy_command
end
get_settings_json() click to toggle source
# File lib/shopifly/syncer.rb, line 72
def get_settings_json
  p "Downloading settings"

  system("theme download config/settings_data.json")
end
set_config_for(theme, allow_config_override = false) click to toggle source
# File lib/shopifly/syncer.rb, line 100
    def set_config_for(theme, allow_config_override = false)
      p "Setting config to point to #{theme.attributes[:name]}, #{@config.store_url}"

      ignore_files = allow_config_override ? "[]" : @shared_config["ignore_files"]

      File.open("config.yml", "w") do |file|
        file.write <<~CONFIG
          build:
            password: #{@config.password}
            theme_name: "#{theme.attributes[:name]}"
            theme_id: #{theme.attributes[:id]}
            store: #{@config.store_url}
            directory: #{@shared_config['directory']}
            ignore_files: #{ignore_files}

          development:
            password: #{@config.password}
            theme_name: "#{theme.attributes[:name]}"
            theme_id: #{theme.attributes[:id]}
            store: #{@config.store_url}
            directory: #{@shared_config['directory']}
            ignore_files: #{ignore_files}

          production:
            password: #{@config.password}
            theme_name: "#{theme.attributes[:name]}"
            theme_id: #{theme.attributes[:id]}
            store: #{@config.store_url}
            directory: #{@shared_config['directory']}
            ignore_files: #{ignore_files}
        CONFIG
      end
    end
sync() click to toggle source
# File lib/shopifly/syncer.rb, line 13
def sync
  with_settings = ARGV.include? "--with-settings"

  themes_array = ShopifyAPI::Theme.find(:all)

  current_theme = themes_array.select do |theme|
    theme.attributes["name"] == @config.current_branch
  end.first

  if current_theme.nil?
    p "Theme doesn't exist, creating..."
    current_theme = create_theme(@config.current_branch)

    if current_theme.errors.present?
      return p "Errors: #{current_theme.errors.first}"
    end

    with_settings_json(current_theme, default_theme) do
      deploy_theme if set_config_for(current_theme, true)
    end
  else
    p "Theme already exists: #{@config.current_branch}, #{@config.store_url}"
    if with_settings
      with_settings_json(current_theme, default_theme) do
        set_config_for(current_theme, false)
      end
    else
      set_config_for(current_theme, false)
    end
  end
end
upload_settings_json() click to toggle source
# File lib/shopifly/syncer.rb, line 84
def upload_settings_json
  p "Uploading settings"

  config = File.read("config.yml")

  if config.include? "theme_id: #{@default_theme.attributes['id']}"
    raise "Refusing to push settings_data.json to currently published branch"
  end

  system("theme deploy config/settings_data.json")
end
with_settings_json(current_theme, default_theme) { || ... } click to toggle source
# File lib/shopifly/syncer.rb, line 50
def with_settings_json(current_theme, default_theme)
  yield

  set_config_for(default_theme, true)
  get_settings_json

  set_config_for(current_theme, true)
  upload_settings_json

  delete_settings_json
end