namespace :bullet_train do namespace :themes do namespace :tester do desc "Fork the \"Tester\" theme into your local repository." task :eject, [:destination] => :environment do |task, args| theme_base_path = `bundle show --paths bullet_train-themes-tester`.chomp puts "Ejecting from Tester theme in `#{theme_base_path}`." puts "Ejecting Tailwind configuration into `./tailwind.#{args[:destination]}.config.js`." `cp #{theme_base_path}/tailwind.tester.config.js #{Rails.root}/tailwind.#{args[:destination]}.config.js` puts "Ejecting Tailwind mailer configuration into `./tailwind.mailer.#{args[:destination]}.config.js`." `cp #{theme_base_path}/tailwind.mailer.tester.config.js #{Rails.root}/tailwind.mailer.#{args[:destination]}.config.js` `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/tester/#{args[:destination]}/g" #{Rails.root}/tailwind.mailer.#{args[:destination]}.config.js` puts "Ejecting stylesheets into `./app/assets/stylesheets/#{args[:destination]}`." `mkdir #{Rails.root}/app/assets/stylesheets` `cp -R #{theme_base_path}/app/assets/stylesheets/tester #{Rails.root}/app/assets/stylesheets/#{args[:destination]}` `cp -R #{theme_base_path}/app/assets/stylesheets/tester.tailwind.css #{Rails.root}/app/assets/stylesheets/#{args[:destination]}.tailwind.css` `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/tester/#{args[:destination]}/g" #{Rails.root}/app/assets/stylesheets/#{args[:destination]}.tailwind.css` puts "Ejecting JavaScript into `./app/javascript/application.#{args[:destination]}.js`." `cp #{theme_base_path}/app/javascript/application.tester.js #{Rails.root}/app/javascript/application.#{args[:destination]}.js` puts "Ejecting all theme partials into `./app/views/themes/#{args[:destination]}`." `mkdir #{Rails.root}/app/views/themes` `cp -R #{theme_base_path}/app/views/themes/tester #{Rails.root}/app/views/themes/#{args[:destination]}` `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/tester/#{args[:destination]}/g" #{Rails.root}/app/views/themes/#{args[:destination]}/layouts/_head.html.erb` puts "Cutting local `Procfile.dev` over from `tester` to `#{args[:destination]}`." `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/tester/#{args[:destination]}/g" #{Rails.root}/Procfile.dev` puts "Cutting local `package.json` over from `tester` to `#{args[:destination]}`." `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/tester/#{args[:destination]}/g" #{Rails.root}/package.json` # Stub out the class that represents this theme and establishes its inheritance structure. target_path = "#{Rails.root}/app/lib/bullet_train/themes/#{args[:destination]}.rb" puts "Stubbing out a class that represents this theme in `.#{target_path}`." `mkdir -p #{Rails.root}/app/lib/bullet_train/themes` `cp #{theme_base_path}/lib/bullet_train/themes/tester.rb #{target_path}` `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/module Tester/module #{args[:destination].titlecase}/g" #{target_path}` `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/TailwindCss/Tester/g" #{target_path}` `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/tester/#{args[:destination]}/g" #{target_path}` ["require", "TODO", "mattr_accessor"].each do |thing_to_remove| `grep -v #{thing_to_remove} #{target_path} > #{target_path}.tmp` `mv #{target_path}.tmp #{target_path}` end `standardrb --fix #{target_path}` puts "Cutting local project over from `tester` to `#{args[:destination]}` in `app/helpers/application_helper.rb`." `sed -i #{'""' if `echo $OSTYPE`.include?("darwin")} "s/:tester/:#{args[:destination]}/g" #{Rails.root}/app/helpers/application_helper.rb` puts "You must restart `bin/dev` at this point, because of the changes to `Procfile.dev` and `package.json`." end desc "Install this theme to your main application." task :install do |rake_task| # Grab the theme name from the rake task, bullet_train:theme:tester:install theme_name = rake_task.name.split(":")[2] # Grabs the current theme from # def current_theme # :theme_name # end current_theme_regexp = /(^ :)(.*)/ current_theme = nil new_lines = [] [ "./app/helpers/application_helper.rb", "./Procfile.dev", "./package.json" ].each do |file| File.open(file, "r") do |f| new_lines = f.readlines new_lines = new_lines.map do |line| # Make sure we get the current theme before trying to replace it in any of the files. # We grab it from the first file in the array above. current_theme = line.scan(current_theme_regexp).flatten.last if line.match?(current_theme_regexp) line.gsub!(/#{current_theme}/, theme_name) unless current_theme.nil? line end end File.open(file, "w") do |f| f.puts new_lines.join end end end end end end