Last active
February 28, 2023 19:02
-
-
Save vitroz/b257227ca0d9c21700fb7f10a60d1ec7 to your computer and use it in GitHub Desktop.
Theme Version Check - Ruby Version
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
#CREATE TABLE fake_connected_accounts ( | |
# id serial PRIMARY KEY, | |
# provider_account_uid VARCHAR ( 150 ) unique NOT NULL, | |
# auth_token VARCHAR ( 150 ) NOT NULL, | |
# account_id numeric not null | |
#); | |
# Populate table with merchant sample data (singular merchant x unique theme samples) | |
# Specify templates required by the app (where app block will be installed). | |
# E.g. %w[product collection index blog customers/account list-collections search] | |
def check_theme_version(theme) | |
app_block_templates = %w[product collection index] | |
theme_assets = ShopifyAPI::Asset.find(:all, params: { theme_id: theme.id }) | |
template_json_files = theme_assets.select do |asset| | |
app_block_templates.any? { |t| asset.key == "templates/#{t}.json"} | |
end | |
return :vintage if template_json_files.size != app_block_templates.size | |
template_main_sections = template_json_files.map do |template| | |
asset = ShopifyAPI::Asset.find(template.key, params: { theme_id: theme.id }) | |
json = JSON.parse(asset.value) | |
main = json["sections"].find { |k, v| k == "main" || v["type"].start_with?("main-") } | |
if main | |
theme_assets.find { |asset| asset.key == "sections/#{main[1]["type"]}.liquid" } | |
elsif template.key == "templates/index.json" | |
theme_assets.find { |asset| asset.key == "sections/apps.liquid" } | |
else | |
nil | |
end | |
end.compact | |
sections_with_app_block = template_main_sections.map do |asset| | |
accepts_app_block = false | |
file = ShopifyAPI::Asset.find(asset.key, params: { theme_id: theme.id} ) | |
match = file.value.match(/\{\%[-\s]+schema[-\s]+\%\}([\s\S]*?)\{\%[-\s]+endschema[-\s]+\%\}/m) | |
begin | |
schema = JSON.parse(match[1]) | |
rescue | |
end | |
if schema && schema["blocks"] | |
accepts_app_block = schema["blocks"].any? { |b| b["type"] == "@app" } | |
end | |
accepts_app_block ? file : nil | |
end.compact | |
if sections_with_app_block.size | |
:os2 | |
else | |
:vintage | |
end | |
end | |
vintage_count = 0 | |
app_blocks_count = 0 | |
store_theme_id_nil_count = 0 | |
store_theme_ids = [] | |
app_blocks_themes = [] | |
vintage_themes = [] | |
fake_accounts = FakeConnectedAccount.all | |
fake_accounts.each do |f| | |
session = ShopifyAPI::Session.new(domain: f.provider_account_uid, token: f.auth_token, api_version:SmileConfig.shopify_app.api_version) | |
ShopifyAPI::Base.activate_session(session) | |
begin | |
theme = ShopifyAPI::Theme.all.select{|t| t.role == "main"}.first | |
rescue | |
theme = nil | |
end | |
unless theme.nil? | |
if theme.theme_store_id.nil? | |
store_theme_id_nil_count += 1 | |
else | |
store_theme_ids.append(theme.theme_store_id) | |
end | |
if check_theme_version(theme) == :vintage | |
vintage_count += 1 | |
vintage_themes.append(theme.theme_store_id) | |
else | |
app_blocks_count += 1 | |
app_blocks_themes.append(theme.theme_store_id) | |
end | |
end | |
p "merchants using vintage themes: #{vintage_count}" | |
p "merchants using themes compatible with app blocks: #{app_blocks_count}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment