Skip to content

Instantly share code, notes, and snippets.

@gavinballard
Last active August 29, 2015 14:07
Show Gist options
  • Save gavinballard/a508b17197b86bc4dc83 to your computer and use it in GitHub Desktop.
Save gavinballard/a508b17197b86bc4dc83 to your computer and use it in GitHub Desktop.
A Git hook to automatically update the Shopify Theme Gem's config.yml on a branch switch.
#!/usr/bin/env/ruby
require 'yaml'
# Get the "type" of checkout from the arguments Git passes to us.
# Possible values for this are "0" for a file-only checkout (which we dont' care about)
# or "1" for a full branch checkout (which we do).
checkout_type = ARGV[2]
if checkout_type == "1"
# Get the name of the current branch and the absolute path to our git root. Trim whitespace.
current_branch_name = `git rev-parse --abbrev-ref HEAD`.gsub(/\s+/, "")
root_directory = `git rev-parse --show-toplevel`.gsub(/\s+/, "")
# Convert the branch name to a symbol for hash lookups.
branch = current_branch_name.to_sym
# Find and update any config.yml files.
Dir.glob("#{root_directory}/**/config.yml").each do |config_file|
config = YAML.load_file(config_file)
# Check to see if we've specified a known theme ID for this branch.
if config.has_key?(branch)
config[:theme_id] = config.fetch(branch)
File.open(config_file, 'w') do |f|
f.write config.to_yaml
end
end
end
end
@gavinballard
Copy link
Author

To use this hook, your config.yml just needs to have corresponding theme IDs specified for each branch.

For example, a config.yml when on the master branch might look like:

---
:api_key: 017067fa522c24d4ea471bfb4f090d17
:password: 2c281e893c488b75c09e8baff84e23e6
:store: example-store
:theme_id: 999123
:master: 999123
:sandbox: 999456

Then, after executing git checkout sandbox, the hook would rewrite it to be:

---
:api_key: 017067fa522c24d4ea471bfb4f090d17
:password: 2c281e893c488b75c09e8baff84e23e6
:store: example-store
:theme_id: 999456
:master: 999123
:sandbox: 999456

You should make sure that theme watch is stopped while switching branches, as it doesn't detect changes to configuration files and reload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment