-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Migrate
generate_docs
command to new tool
- Loading branch information
1 parent
30a1384
commit ad61fe2
Showing
8 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
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
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
89 changes: 89 additions & 0 deletions
89
dev/lib/product_taxonomy/commands/generate_docs_command.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProductTaxonomy | ||
class GenerateDocsCommand < Command | ||
UNSTABLE = "unstable" | ||
|
||
def initialize(options) | ||
super | ||
|
||
@version = options[:version] || UNSTABLE | ||
end | ||
|
||
def execute | ||
logger.info("Version: #{@version}") | ||
|
||
load_taxonomy | ||
generate_data_files | ||
# generate_release_folder unless params[:version] == UNSTABLE | ||
end | ||
|
||
private | ||
|
||
def generate_data_files | ||
data_target = File.expand_path("../docs/_data/#{@version}", DATA_PATH) | ||
|
||
logger.info("Generating sibling groups...") | ||
sibling_groups_yaml = YAML.dump(Serializers::CategoryDocsSiblingsSerializer.serialize_all, line_width: -1) | ||
File.write("#{data_target}/sibling_groups.yml", sibling_groups_yaml + "\n") | ||
|
||
logger.info("Generating category search index...") | ||
search_index_json = JSON.fast_generate(Serializers::CategoryDocsSearchSerializer.serialize_all) | ||
File.write("#{data_target}/search_index.json", search_index_json + "\n") | ||
|
||
# logger.info("Generating attributes...") | ||
# attributes_yml = YAML.dump(AttributeDocsAllSerializer.serialize_all, line_width: -1) | ||
# File.write("#{data_target}/attributes.yml", attributes_yml + "\n") | ||
|
||
# logger.info("Generating mappings...") | ||
# mappings_json = JSON.parse(File.read("dist/en/integrations/all_mappings.json")).fetch("mappings") | ||
# mappings_data = reverse_shopify_mapping_rules(mappings_json) | ||
# mappings_yml = YAML.dump(mappings_data, line_width: -1) | ||
# File.write("#{data_target}/mappings.yml", mappings_yml + "\n") | ||
|
||
# logger.info("Generating attributes with categories...") | ||
# reversed_attributes_yml = YAML.dump(AttributeDocsReversedSerializer.serialize_all, line_width: -1) | ||
# File.write("#{data_target}/reversed_attributes.yml", reversed_attributes_yml + "\n") | ||
|
||
# logger.info("Generating attribute with categories search index...") | ||
# attribute_search_index_json = JSON.fast_generate(AttributeDocsSearchSerializer.serialize_all) | ||
# File.write("#{data_target}/attribute_search_index.json", attribute_search_index_json + "\n") | ||
end | ||
|
||
def generate_release_folder | ||
spinner("Generating release folder") do |sp| | ||
sys.write_file("docs/_releases/#{params[:version]}/index.html") do |file| | ||
content = sys.read_file("docs/_releases/_index_template.html") | ||
content.gsub!("TITLE", params[:version].upcase) | ||
content.gsub!("TARGET", params[:version]) | ||
content.gsub!("GH_URL", "https://github.com/Shopify/product-taxonomy/releases/tag/v#{params[:version]}") | ||
file.write(content) | ||
end | ||
sys.write_file("docs/_releases/#{params[:version]}/attributes.html") do |file| | ||
content = sys.read_file("docs/_releases/_attributes_template.html") | ||
content.gsub!("TITLE", params[:version].upcase) | ||
content.gsub!("TARGET", params[:version]) | ||
content.gsub!("GH_URL", "https://github.com/Shopify/product-taxonomy/releases/tag/v#{params[:version]}") | ||
file.write(content) | ||
end | ||
sp.update_title("Generated release folder") | ||
end | ||
end | ||
|
||
def reverse_shopify_mapping_rules(mappings) | ||
mappings.each do |mapping| | ||
next unless mapping["output_taxonomy"].include?("shopify") | ||
|
||
mapping["input_taxonomy"], mapping["output_taxonomy"] = mapping["output_taxonomy"], mapping["input_taxonomy"] | ||
mapping["rules"] = mapping["rules"].flat_map do |rule| | ||
rule["output"]["category"].map do |output_category| | ||
{ | ||
"input" => { "category" => output_category }, | ||
"output" => { "category" => [rule["input"]["category"]] }, | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
dev/lib/product_taxonomy/models/serializers/attribute_docs_all_serializer.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProductTaxonomy | ||
module Serializers | ||
class AttributeDocsAllSerializer | ||
class << self | ||
def serialize_all | ||
end | ||
end | ||
|
||
def initialize(attribute) | ||
@attribute = attribute | ||
end | ||
|
||
def serialize | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
dev/lib/product_taxonomy/models/serializers/attribute_docs_reversed_serializer.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProductTaxonomy | ||
module Serializers | ||
class AttributeDocsReversedSerializer | ||
class << self | ||
def serialize_all | ||
end | ||
end | ||
|
||
def initialize(attribute) | ||
@attribute = attribute | ||
end | ||
|
||
def serialize | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
dev/lib/product_taxonomy/models/serializers/attribute_docs_search_serializer.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProductTaxonomy | ||
module Serializers | ||
class AttributeDocsSearchSerializer | ||
class << self | ||
def serialize_all | ||
end | ||
end | ||
|
||
def initialize(attribute) | ||
@attribute = attribute | ||
end | ||
|
||
def serialize | ||
end | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
dev/lib/product_taxonomy/models/serializers/category_docs_search_serializer.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProductTaxonomy | ||
module Serializers | ||
class CategoryDocsSearchSerializer | ||
class << self | ||
def serialize_all | ||
Category.verticals.flat_map(&:descendants_and_self).flat_map do |category| | ||
new(category).serialize | ||
end | ||
end | ||
end | ||
|
||
def initialize(category) | ||
@category = category | ||
end | ||
|
||
def serialize | ||
{ | ||
"searchIdentifier" => @category.id, | ||
"title" => @category.full_name, | ||
"url" => "?categoryId=#{CGI.escapeURIComponent(@category.id)}", | ||
"category" => { | ||
"id" => @category.id, | ||
"name" => @category.name, | ||
"fully_qualified_type" => @category.full_name, | ||
"depth" => @category.level, | ||
}, | ||
} | ||
end | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
dev/lib/product_taxonomy/models/serializers/category_docs_siblings_serializer.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module ProductTaxonomy | ||
module Serializers | ||
class CategoryDocsSiblingsSerializer | ||
class << self | ||
def serialize_all | ||
Category.verticals.flat_map(&:descendants_and_self).each_with_object({}) do |category, groups| | ||
parent_id = category.parent&.gid.presence || "root" | ||
sibling = new(category).serialize | ||
|
||
groups[category.level] ||= {} | ||
groups[category.level][parent_id] ||= [] | ||
groups[category.level][parent_id] << sibling | ||
end | ||
end | ||
end | ||
|
||
def initialize(category) | ||
@category = category | ||
end | ||
|
||
def serialize | ||
{ | ||
"id" => @category.gid, | ||
"name" => @category.name, | ||
"fully_qualified_type" => @category.full_name, | ||
"depth" => @category.level, | ||
"parent_id" => @category.parent&.gid.presence || "root", | ||
"node_type" => @category.level.zero? ? "root" : "leaf", | ||
"ancestor_ids" => @category.ancestors.map { _1.gid }.join(","), | ||
"attribute_handles" => @category.attributes.map { _1.handle }.join(","), | ||
} | ||
end | ||
end | ||
end | ||
end |