Last active
August 29, 2015 14:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 themaster
branch might look like:Then, after executing
git checkout sandbox
, the hook would rewrite it to be:You should make sure that
theme watch
is stopped while switching branches, as it doesn't detect changes to configuration files and reload.