forked from aviflombaum/shadcn-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
105 lines (80 loc) · 3.49 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require "bundler/gem_tasks"
require 'fileutils'
if ENV["RAILS_ENV"] != "production"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task default: :spec
end
require_relative "config/application"
Rails.application.load_tasks
# Rake task
HELPER_TEMPLATE = <<~TEMPLATE
module Components::<%= component_name %>Helper
end
TEMPLATE
STIMULUS_TEMPLATE = <<~TEMPLATE
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
}
TEMPLATE
def documentation_erb(name)
content = <<~TEMPLATE
<%= render "layouts/documentation/component_header",
title: "#{name.capitalize}",
description: "" %>
<% content_for :preview, flush: true do %>
<div class="w-full flex justify-center">
<%= render_code_preview('#{name}') %>
</div>
<% end %>
<% content_for :code, flush: true do %>
<div class="w-full flex justify-center">
<%= code_partial("#{name}/preview", :erb) %>
</div>
<% end %>
<%= render_preview %>
<h2 class="font-heading scroll-m-20 border-b pb-2 text-2xl font-semibold tracking-tight first:mt-0" id="installation">Installation</h2>
<%= code_sample(language: "sh") do %>
rails generate shadcn-ui #{name}
<% end %>
<h2 class="font-heading mt-12 scroll-m-20 border-b pb-2 mb-2 text-2xl font-semibold tracking-tight first:mt-0" id="usage">Usage</h2>
<%= code_partial("#{name}/usage", :erb) %>
<%= render_usage("#{name}") %>
TEMPLATE
end
namespace :component do
desc "Generate a new component"
task :generate => :environment do
name = ENV['name']
abort("No component name provided. Use `rake component:generate name=component_name`") unless name.present?
puts "Do you want to include a Stimulus controller? (yes/no)"
response = STDIN.gets.chomp.downcase
component_view_path = Rails.root.join("app", "views", "components", "ui", "_#{name}.html.erb")
helper_path = Rails.root.join("app", "helpers", "components", "#{name}_helper.rb")
controller_path = Rails.root.join("app", "javascript", "controllers", "ui", "#{name}_controller.js")
documentation_path = Rails.root.join("app", "views", "examples", "components", "#{name}.html.erb")
preview_path = Rails.root.join("app", "views", "examples", "components", name, "code", "_preview.html.erb")
preview_directory_path = Rails.root.join("app", "views", "examples", "components", name, "code")
preview_file_path = "#{preview_directory_path}/_preview.erb"
sample_usage_path = "#{preview_directory_path}/_usage.erb"
usage_instructions_path = Rails.root.join("app", "views", "examples", "components", name, "_usage.html.erb")
camelized_name = name.camelize
helper_template = ERB.new(HELPER_TEMPLATE)
helper_content = helper_template.result_with_hash(component_name: camelized_name)
stimulus_template = ERB.new(STIMULUS_TEMPLATE)
stimulus_content = stimulus_template.result
File.new(component_view_path, "w")
File.open(helper_path, "w") { |file| file.write(helper_content) }
File.open(documentation_path, "w") { |file| file.write(documentation_erb(name)) }
FileUtils.mkdir_p(preview_directory_path)
File.new(preview_file_path, "w")
File.new(sample_usage_path, "w")
File.new(usage_instructions_path, "w")
if ["yes", "y"].include?(response.downcase)
File.open(controller_path, "w") { |file| file.write(stimulus_content) }
end
puts "Component #{name} was created successfully!"
end
end