Skip to content

Instantly share code, notes, and snippets.

@manuelpuyol
Created July 22, 2021 17:59
Show Gist options
  • Save manuelpuyol/d88671afce8c1773889278066ec2e136 to your computer and use it in GitHub Desktop.
Save manuelpuyol/d88671afce8c1773889278066ec2e136 to your computer and use it in GitHub Desktop.
class ComponentStatusMigratorGenerator < Thor::Group
include Thor::Actions
# Define arguments and options
argument :name
class_option :status, default: "alpha", desc: "Status of the component. One of alpha, beta or stable"
def self.source_root
File.dirname(__FILE__)
end
def find_file
raise unless File.exist?("app/components/primer/#{name.underscore}.rb")
puts "File found!"
end
def move_controller
raise unless File.exist?(controller_path)
copy_file(controller_path, controller_path_with_status)
remove_file(controller_path)
end
def move_template
raise unless File.exist?(template_path)
copy_file(template_path, template_path_with_status)
remove_file(template_path)
end
def move_test
raise unless File.exist?(test_path)
copy_file(test_path, test_path_with_status)
remove_file(test_path)
end
def move_story
raise unless File.exist?(story_path)
copy_file(story_path, story_path_with_status)
remove_file(story_path)
end
def add_module
insert_into_file(controller_path_with_status, " module #{status.capitalize}\n", after: "module Primer\n")
append_to_file(controller_path_with_status, "end\n")
end
def remove_suffix
gsub_file(controller_path_with_status, "class #{name}", name_without_suffix
end
def rename_test_class
# gsub like above
end
def add_require_to_story
insert_into_file(story_path_with_stats, "require \"primer/#{status}/#{name_without_suffix.underscore}\"\n", after: "# frozen_string_literal: true\n")
end
def update_all_calls
# either use `run` https://rubydoc.info/github/wycats/thor/master/Thor/Actions#run-instance_method
# or use `run_ruby_script` https://rubydoc.info/github/wycats/thor/master/Thor/Actions#run_ruby_script-instance_method
# to run a regex in all dirs
end
def rename_nav_entry
# another gsub_file
end
private
def controller_path
"app/components/primer/#{name.underscore}.rb"
end
def controller_path_with_status
"app/components/primer/#{status}/#{name_without_suffix.underscore}.rb"
end
def status
options[:status]
end
def name_without_suffix
name.gsub("Component", "")
end
end
@manuelpuyol
Copy link
Author

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