react_to_rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +151 -0
- data/Rakefile +10 -0
- data/examples/pricing.jsx +125 -0
- data/examples/pricing_component.html.erb +63 -0
- data/examples/pricing_component.rb +6 -0
- data/exe/react_to_rails +8 -0
- data/lib/react_to_rails/cli.rb +68 -0
- data/lib/react_to_rails/convert.rb +126 -0
- data/lib/react_to_rails/version.rb +5 -0
- data/lib/react_to_rails.rb +12 -0
- data/sig/react_to_rails.rbs +4 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 26bd74fbf51bf21c1df28eafd788693daea911a55489b1bf348b8fcdf1a3c8da
|
4
|
+
data.tar.gz: 408f21559f0fa31e3519a65b8851cac8d689ab161bc2c0ad13e95e06f28069db
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab2435b3ee8b82b7f0ad4904efffeef7fed9f3b9bfc3b9a8f121ad83c5d493e321532fe122befeb2a5d9ef7e9a613104912a1cccefd5460664cf35b771d30763
|
7
|
+
data.tar.gz: 25afc03e34d5ad5f438106b7b589c88a90c5511809a9b2217108a6e7779f60bdc821c7d1ec452d792f6fcc52433c5bdcbc38200fb8e9be378b30ec9aa7ec6bd4
|
data/.standard.yml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Andy Waite
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# React To Rails
|
2
|
+
|
3
|
+
This gem converts React components to Rails [View Components](https://viewcomponent.org) using AI.
|
4
|
+
|
5
|
+
## Background
|
6
|
+
|
7
|
+
I created this tool use with [Tailwind Plus](https://tailwindcss.com/plus/) (formerly Tailwind UI).
|
8
|
+
|
9
|
+
Tailwind Plus is a commercial product which provides high quality components for React and Vue.
|
10
|
+
|
11
|
+
There are also plain HTML versions but they are awkward to work with, e.g.:
|
12
|
+
|
13
|
+
- You need to add ERB `<%= %>` placeholders for any content you're passing in.
|
14
|
+
- The HTML is static so is missing any loops, conditionals, etc
|
15
|
+
- Icons are converted to verbose SVG markup
|
16
|
+
|
17
|
+
This tool generates a View Component based on the supplied React component which should be much more convenient for Rails.
|
18
|
+
|
19
|
+
For more details of the conversion process, you can look at the [prompt](./lib/react_to_rails/convert.rb).
|
20
|
+
|
21
|
+
# Demo
|
22
|
+
|
23
|
+
Here is an example of the free [Two tiers with emphasized tier](https://tailwindcss.com/plus/ui-blocks/marketing/sections/pricing) component.
|
24
|
+
|
25
|
+
Initial steps.
|
26
|
+
|
27
|
+
- Select React in the dropdown
|
28
|
+
- Switch to the Code view
|
29
|
+
- Click the Copy icon
|
30
|
+
- Paste that into a new file in your editor
|
31
|
+
- Save it to disk, e.g. `pricing.jsx`.
|
32
|
+
|
33
|
+
Now convert it by running:
|
34
|
+
|
35
|
+
`react_to_rails pricing.jsx PricingComponent`
|
36
|
+
|
37
|
+
The tool will output a report, and generate the view component files:
|
38
|
+
|
39
|
+
```
|
40
|
+
**Notes on the conversion:**
|
41
|
+
|
42
|
+
- The `@headlessui/react` library is not used, so no issues there.
|
43
|
+
- The `CheckIcon` from `@heroicons/react` is replaced with the `heroicon` helper.
|
44
|
+
- The `classNames` function is replaced with the `class_names` helper.
|
45
|
+
- The `tiers` array is converted to a Ruby array of hashes and should be passed in as a parameter to the component.
|
46
|
+
- `<a href>` is replaced with `link_to`.
|
47
|
+
- All Tailwind classes are preserved as-is.
|
48
|
+
- No JavaScript or interactivity is present, so no need for Stimulus or Turbo.
|
49
|
+
- No `<img>`, `<form>`, or `<button>` tags are present.
|
50
|
+
- The `style` attribute for the background div is converted to a Ruby hash in ERB.
|
51
|
+
- The ERB demo file shows how to pass the `tiers` array to the component.
|
52
|
+
- All logic and conditional classes are preserved using Ruby and ERB.
|
53
|
+
- No parts were impossible to convert.
|
54
|
+
|
55
|
+
### EXAMPLE USAGE ###
|
56
|
+
|
57
|
+
<%# app/views/demo/index.html.erb %>
|
58
|
+
<% tiers = [
|
59
|
+
{
|
60
|
+
name: 'Hobby',
|
61
|
+
id: 'tier-hobby',
|
62
|
+
href: '#',
|
63
|
+
price_monthly: '$29',
|
64
|
+
description: "The perfect plan if you're just getting started with our product.",
|
65
|
+
features: ['25 products', 'Up to 10,000 subscribers', 'Advanced analytics', '24-hour support response time'],
|
66
|
+
featured: false,
|
67
|
+
},
|
68
|
+
{
|
69
|
+
name: 'Enterprise',
|
70
|
+
id: 'tier-enterprise',
|
71
|
+
href: '#',
|
72
|
+
price_monthly: '$99',
|
73
|
+
description: 'Dedicated support and infrastructure for your company.',
|
74
|
+
features: [
|
75
|
+
'Unlimited products',
|
76
|
+
'Unlimited subscribers',
|
77
|
+
'Advanced analytics',
|
78
|
+
'Dedicated support representative',
|
79
|
+
'Marketing automations',
|
80
|
+
'Custom integrations',
|
81
|
+
],
|
82
|
+
featured: true,
|
83
|
+
}
|
84
|
+
] %>
|
85
|
+
|
86
|
+
<%= render(PricingComponent.new(tiers: tiers)) %>
|
87
|
+
```
|
88
|
+
|
89
|
+
And here is the generated code:
|
90
|
+
|
91
|
+
* [app/component/pricing_component.html.erb](./examples/pricing_component.html.erb).
|
92
|
+
* [app/components/pricing_component.rb](./examples/pricing_component.rb)
|
93
|
+
|
94
|
+
## Limitations
|
95
|
+
|
96
|
+
Components that make use of JavaScript will need further work, e.g to use Stimulus instead.
|
97
|
+
|
98
|
+
## Prerequisites
|
99
|
+
|
100
|
+
Your app must be set up with:
|
101
|
+
|
102
|
+
* [view_component](https://viewcomponent.org)
|
103
|
+
* [heroicons](https://github.com/jclusso/heroicons)
|
104
|
+
|
105
|
+
## Installation
|
106
|
+
|
107
|
+
Install the gem and add to the application's Gemfile by executing:
|
108
|
+
|
109
|
+
```bash
|
110
|
+
bundle add react_to_rails
|
111
|
+
```
|
112
|
+
|
113
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
114
|
+
|
115
|
+
```bash
|
116
|
+
gem install react_to_rails
|
117
|
+
```
|
118
|
+
|
119
|
+
## Setup
|
120
|
+
|
121
|
+
You will need to provide an OpenAI API key. If you need to create a key, you can do so in the OpenAI [settings](https://platform.openai.com/settings).
|
122
|
+
|
123
|
+
The gem looks for an `OPENAI_API_KEY` in the environment (or you can pass it directly with `OPEN_API_KEY=... react_to_erb`).
|
124
|
+
|
125
|
+
## Usage
|
126
|
+
|
127
|
+
Save the React JSX file, then use the command line tool to create a new view component with the given name, e.g.:
|
128
|
+
|
129
|
+
```bash
|
130
|
+
react_to_rails pricing.jsx PricingComponent
|
131
|
+
```
|
132
|
+
|
133
|
+
Options:
|
134
|
+
- `-h, --help` - Show help message
|
135
|
+
- `-v, --version` - Show version
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
## Development
|
140
|
+
|
141
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
142
|
+
|
143
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
144
|
+
|
145
|
+
## Contributing
|
146
|
+
|
147
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/andyw8/react_to_rails.
|
148
|
+
|
149
|
+
## License
|
150
|
+
|
151
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
import { CheckIcon } from '@heroicons/react/20/solid'
|
2
|
+
|
3
|
+
const tiers = [
|
4
|
+
{
|
5
|
+
name: 'Hobby',
|
6
|
+
id: 'tier-hobby',
|
7
|
+
href: '#',
|
8
|
+
priceMonthly: '$29',
|
9
|
+
description: "The perfect plan if you're just getting started with our product.",
|
10
|
+
features: ['25 products', 'Up to 10,000 subscribers', 'Advanced analytics', '24-hour support response time'],
|
11
|
+
featured: false,
|
12
|
+
},
|
13
|
+
{
|
14
|
+
name: 'Enterprise',
|
15
|
+
id: 'tier-enterprise',
|
16
|
+
href: '#',
|
17
|
+
priceMonthly: '$99',
|
18
|
+
description: 'Dedicated support and infrastructure for your company.',
|
19
|
+
features: [
|
20
|
+
'Unlimited products',
|
21
|
+
'Unlimited subscribers',
|
22
|
+
'Advanced analytics',
|
23
|
+
'Dedicated support representative',
|
24
|
+
'Marketing automations',
|
25
|
+
'Custom integrations',
|
26
|
+
],
|
27
|
+
featured: true,
|
28
|
+
},
|
29
|
+
]
|
30
|
+
|
31
|
+
function classNames(...classes) {
|
32
|
+
return classes.filter(Boolean).join(' ')
|
33
|
+
}
|
34
|
+
|
35
|
+
export default function Example() {
|
36
|
+
return (
|
37
|
+
<div className="relative isolate bg-white px-6 py-24 sm:py-32 lg:px-8">
|
38
|
+
<div aria-hidden="true" className="absolute inset-x-0 -top-3 -z-10 transform-gpu overflow-hidden px-36 blur-3xl">
|
39
|
+
<div
|
40
|
+
style={{
|
41
|
+
clipPath:
|
42
|
+
'polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)',
|
43
|
+
}}
|
44
|
+
className="mx-auto aspect-1155/678 w-288.75 bg-linear-to-tr from-[#ff80b5] to-[#9089fc] opacity-30"
|
45
|
+
/>
|
46
|
+
</div>
|
47
|
+
<div className="mx-auto max-w-4xl text-center">
|
48
|
+
<h2 className="text-base/7 font-semibold text-indigo-600">Pricing</h2>
|
49
|
+
<p className="mt-2 text-5xl font-semibold tracking-tight text-balance text-gray-900 sm:text-6xl">
|
50
|
+
Choose the right plan for you
|
51
|
+
</p>
|
52
|
+
</div>
|
53
|
+
<p className="mx-auto mt-6 max-w-2xl text-center text-lg font-medium text-pretty text-gray-600 sm:text-xl/8">
|
54
|
+
Choose an affordable plan that’s packed with the best features for engaging your audience, creating customer
|
55
|
+
loyalty, and driving sales.
|
56
|
+
</p>
|
57
|
+
<div className="mx-auto mt-16 grid max-w-lg grid-cols-1 items-center gap-y-6 sm:mt-20 sm:gap-y-0 lg:max-w-4xl lg:grid-cols-2">
|
58
|
+
{tiers.map((tier, tierIdx) => (
|
59
|
+
<div
|
60
|
+
key={tier.id}
|
61
|
+
className={classNames(
|
62
|
+
tier.featured ? 'relative bg-gray-900 shadow-2xl' : 'bg-white/60 sm:mx-8 lg:mx-0',
|
63
|
+
tier.featured
|
64
|
+
? ''
|
65
|
+
: tierIdx === 0
|
66
|
+
? 'rounded-t-3xl sm:rounded-b-none lg:rounded-tr-none lg:rounded-bl-3xl'
|
67
|
+
: 'sm:rounded-t-none lg:rounded-tr-3xl lg:rounded-bl-none',
|
68
|
+
'rounded-3xl p-8 ring-1 ring-gray-900/10 sm:p-10',
|
69
|
+
)}
|
70
|
+
>
|
71
|
+
<h3
|
72
|
+
id={tier.id}
|
73
|
+
className={classNames(tier.featured ? 'text-indigo-400' : 'text-indigo-600', 'text-base/7 font-semibold')}
|
74
|
+
>
|
75
|
+
{tier.name}
|
76
|
+
</h3>
|
77
|
+
<p className="mt-4 flex items-baseline gap-x-2">
|
78
|
+
<span
|
79
|
+
className={classNames(
|
80
|
+
tier.featured ? 'text-white' : 'text-gray-900',
|
81
|
+
'text-5xl font-semibold tracking-tight',
|
82
|
+
)}
|
83
|
+
>
|
84
|
+
{tier.priceMonthly}
|
85
|
+
</span>
|
86
|
+
<span className={classNames(tier.featured ? 'text-gray-400' : 'text-gray-500', 'text-base')}>/month</span>
|
87
|
+
</p>
|
88
|
+
<p className={classNames(tier.featured ? 'text-gray-300' : 'text-gray-600', 'mt-6 text-base/7')}>
|
89
|
+
{tier.description}
|
90
|
+
</p>
|
91
|
+
<ul
|
92
|
+
role="list"
|
93
|
+
className={classNames(
|
94
|
+
tier.featured ? 'text-gray-300' : 'text-gray-600',
|
95
|
+
'mt-8 space-y-3 text-sm/6 sm:mt-10',
|
96
|
+
)}
|
97
|
+
>
|
98
|
+
{tier.features.map((feature) => (
|
99
|
+
<li key={feature} className="flex gap-x-3">
|
100
|
+
<CheckIcon
|
101
|
+
aria-hidden="true"
|
102
|
+
className={classNames(tier.featured ? 'text-indigo-400' : 'text-indigo-600', 'h-6 w-5 flex-none')}
|
103
|
+
/>
|
104
|
+
{feature}
|
105
|
+
</li>
|
106
|
+
))}
|
107
|
+
</ul>
|
108
|
+
<a
|
109
|
+
href={tier.href}
|
110
|
+
aria-describedby={tier.id}
|
111
|
+
className={classNames(
|
112
|
+
tier.featured
|
113
|
+
? 'bg-indigo-500 text-white shadow-xs hover:bg-indigo-400 focus-visible:outline-indigo-500'
|
114
|
+
: 'text-indigo-600 ring-1 ring-indigo-200 ring-inset hover:ring-indigo-300 focus-visible:outline-indigo-600',
|
115
|
+
'mt-8 block rounded-md px-3.5 py-2.5 text-center text-sm font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 sm:mt-10',
|
116
|
+
)}
|
117
|
+
>
|
118
|
+
Get started today
|
119
|
+
</a>
|
120
|
+
</div>
|
121
|
+
))}
|
122
|
+
</div>
|
123
|
+
</div>
|
124
|
+
)
|
125
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
<%# app/components/pricing_component.html.erb %>
|
2
|
+
<div class="relative isolate bg-white px-6 py-24 sm:py-32 lg:px-8">
|
3
|
+
<div aria-hidden="true" class="absolute inset-x-0 -top-3 -z-10 transform-gpu overflow-hidden px-36 blur-3xl">
|
4
|
+
<div
|
5
|
+
style="clip-path: polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%);"
|
6
|
+
class="mx-auto aspect-1155/678 w-288.75 bg-linear-to-tr from-[#ff80b5] to-[#9089fc] opacity-30"
|
7
|
+
></div>
|
8
|
+
</div>
|
9
|
+
<div class="mx-auto max-w-4xl text-center">
|
10
|
+
<h2 class="text-base/7 font-semibold text-indigo-600">Pricing</h2>
|
11
|
+
<p class="mt-2 text-5xl font-semibold tracking-tight text-balance text-gray-900 sm:text-6xl">
|
12
|
+
Choose the right plan for you
|
13
|
+
</p>
|
14
|
+
</div>
|
15
|
+
<p class="mx-auto mt-6 max-w-2xl text-center text-lg font-medium text-pretty text-gray-600 sm:text-xl/8">
|
16
|
+
Choose an affordable plan that’s packed with the best features for engaging your audience, creating customer
|
17
|
+
loyalty, and driving sales.
|
18
|
+
</p>
|
19
|
+
<div class="mx-auto mt-16 grid max-w-lg grid-cols-1 items-center gap-y-6 sm:mt-20 sm:gap-y-0 lg:max-w-4xl lg:grid-cols-2">
|
20
|
+
<% @tiers.each_with_index do |tier, tier_idx| %>
|
21
|
+
<div
|
22
|
+
class="<%= class_names(
|
23
|
+
tier[:featured] ? 'relative bg-gray-900 shadow-2xl' : 'bg-white/60 sm:mx-8 lg:mx-0',
|
24
|
+
tier[:featured] ? '' : tier_idx == 0 ? 'rounded-t-3xl sm:rounded-b-none lg:rounded-tr-none lg:rounded-bl-3xl' : 'sm:rounded-t-none lg:rounded-tr-3xl lg:rounded-bl-none',
|
25
|
+
'rounded-3xl p-8 ring-1 ring-gray-900/10 sm:p-10'
|
26
|
+
) %>"
|
27
|
+
>
|
28
|
+
<h3
|
29
|
+
id="<%= tier[:id] %>"
|
30
|
+
class="<%= class_names(tier[:featured] ? 'text-indigo-400' : 'text-indigo-600', 'text-base/7 font-semibold') %>"
|
31
|
+
>
|
32
|
+
<%= tier[:name] %>
|
33
|
+
</h3>
|
34
|
+
<p class="mt-4 flex items-baseline gap-x-2">
|
35
|
+
<span
|
36
|
+
class="<%= class_names(tier[:featured] ? 'text-white' : 'text-gray-900', 'text-5xl font-semibold tracking-tight') %>"
|
37
|
+
>
|
38
|
+
<%= tier[:price_monthly] %>
|
39
|
+
</span>
|
40
|
+
<span class="<%= class_names(tier[:featured] ? 'text-gray-400' : 'text-gray-500', 'text-base') %>">/month</span>
|
41
|
+
</p>
|
42
|
+
<p class="<%= class_names(tier[:featured] ? 'text-gray-300' : 'text-gray-600', 'mt-6 text-base/7') %>">
|
43
|
+
<%= tier[:description] %>
|
44
|
+
</p>
|
45
|
+
<ul
|
46
|
+
role="list"
|
47
|
+
class="<%= class_names(tier[:featured] ? 'text-gray-300' : 'text-gray-600', 'mt-8 space-y-3 text-sm/6 sm:mt-10') %>"
|
48
|
+
>
|
49
|
+
<% tier[:features].each do |feature| %>
|
50
|
+
<li class="flex gap-x-3">
|
51
|
+
<%= heroicon "check", options: { class: class_names(tier[:featured] ? 'text-indigo-400' : 'text-indigo-600', 'h-6 w-5 flex-none') } %>
|
52
|
+
<%= feature %>
|
53
|
+
</li>
|
54
|
+
<% end %>
|
55
|
+
</ul>
|
56
|
+
<%= link_to "Get started today", tier[:href], aria: { describedby: tier[:id] }, class: class_names(
|
57
|
+
tier[:featured] ? 'bg-indigo-500 text-white shadow-xs hover:bg-indigo-400 focus-visible:outline-indigo-500' : 'text-indigo-600 ring-1 ring-indigo-200 ring-inset hover:ring-indigo-300 focus-visible:outline-indigo-600',
|
58
|
+
'mt-8 block rounded-md px-3.5 py-2.5 text-center text-sm font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 sm:mt-10'
|
59
|
+
) %>
|
60
|
+
</div>
|
61
|
+
<% end %>
|
62
|
+
</div>
|
63
|
+
</div>
|
data/exe/react_to_rails
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
|
5
|
+
module ReactToRails
|
6
|
+
class CLI
|
7
|
+
def initialize
|
8
|
+
@options = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def run(args)
|
12
|
+
remaining_args = parse_options(args)
|
13
|
+
|
14
|
+
# Get path from positional argument
|
15
|
+
path = remaining_args.first
|
16
|
+
name = remaining_args[1] || "ExampleComponent"
|
17
|
+
|
18
|
+
if path.nil? || path.empty?
|
19
|
+
puts "Error: path argument is required"
|
20
|
+
puts
|
21
|
+
puts parser.help
|
22
|
+
exit 1
|
23
|
+
end
|
24
|
+
|
25
|
+
client = OpenAI::Client.new
|
26
|
+
ReactToRails::Convert.for_path(path, client: client, name: name)
|
27
|
+
rescue OptionParser::InvalidOption => e
|
28
|
+
puts "Error: #{e.message}"
|
29
|
+
puts
|
30
|
+
puts parser.help
|
31
|
+
exit 1
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def parse_options(args)
|
37
|
+
parser.parse!(args)
|
38
|
+
args # Return remaining arguments after parsing options
|
39
|
+
end
|
40
|
+
|
41
|
+
def parser
|
42
|
+
@parser ||= OptionParser.new do |opts|
|
43
|
+
opts.banner = "Usage: react_to_rails [options] <path> [name]"
|
44
|
+
opts.separator ""
|
45
|
+
opts.separator "Arguments:"
|
46
|
+
opts.separator " path Path to React components (required)"
|
47
|
+
opts.separator " name Component name (optional, defaults to ExampleComponent)"
|
48
|
+
opts.separator ""
|
49
|
+
opts.separator "Options:"
|
50
|
+
|
51
|
+
opts.on("-h", "--help", "Show this help message") do
|
52
|
+
puts opts
|
53
|
+
exit
|
54
|
+
end
|
55
|
+
|
56
|
+
opts.on("-v", "--version", "Show version") do
|
57
|
+
puts ReactToRails::VERSION
|
58
|
+
exit
|
59
|
+
end
|
60
|
+
|
61
|
+
opts.separator ""
|
62
|
+
opts.separator "Examples:"
|
63
|
+
opts.separator " react_to_rails pricing.jsx"
|
64
|
+
opts.separator " react_to_rails pricing.jsx PricingComponent"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "openai"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module ReactToRails
|
7
|
+
class StructuredResponse < OpenAI::BaseModel
|
8
|
+
required :summary, String, doc: "A summary of what was done"
|
9
|
+
required :erb_template, String
|
10
|
+
required :view_component_ruby_code, String
|
11
|
+
required :demo_erb_code, String
|
12
|
+
end
|
13
|
+
|
14
|
+
class Convert
|
15
|
+
def initialize(react_content, client:, name:)
|
16
|
+
@react_content = react_content
|
17
|
+
@client = client
|
18
|
+
@name = name
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.for_path(path, client:, name:)
|
22
|
+
react_content = File.read(path)
|
23
|
+
new(react_content, client: client, name: name).call
|
24
|
+
end
|
25
|
+
|
26
|
+
def call
|
27
|
+
puts "### PROMPT ###"
|
28
|
+
puts
|
29
|
+
puts prompt
|
30
|
+
puts
|
31
|
+
puts "Waiting for OpenAI to respond..."
|
32
|
+
|
33
|
+
response = client.responses.create(
|
34
|
+
model: :"gpt-4.1",
|
35
|
+
temperature: 0,
|
36
|
+
input: [
|
37
|
+
{role: "user", content: prompt}
|
38
|
+
],
|
39
|
+
text: StructuredResponse
|
40
|
+
)
|
41
|
+
|
42
|
+
puts "### RESPONSE ###"
|
43
|
+
|
44
|
+
response = JSON.parse(response.output.first.content.first.to_json).fetch("parsed")
|
45
|
+
|
46
|
+
@summary = response.fetch("summary")
|
47
|
+
@view_component_ruby_code = response.fetch("view_component_ruby_code")
|
48
|
+
@erb_template = response.fetch("erb_template")
|
49
|
+
@demo_erb_code = response.fetch("demo_erb_code")
|
50
|
+
|
51
|
+
component_file_name = @name.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
52
|
+
File.write("app/components/#{component_file_name}.rb", @view_component_ruby_code)
|
53
|
+
File.write("app/components/#{component_file_name}.html.erb", @erb_template)
|
54
|
+
puts @summary
|
55
|
+
puts
|
56
|
+
puts "### EXAMPLE USAGE ###"
|
57
|
+
puts
|
58
|
+
puts @demo_erb_code
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def prompt
|
64
|
+
<<~EOS
|
65
|
+
You are a professional Ruby on Rails developer.
|
66
|
+
|
67
|
+
Convert this React JSX component into a View Component for use with Ruby on Rails.
|
68
|
+
|
69
|
+
The component should be named: #{@name}
|
70
|
+
|
71
|
+
The view component consists of two files.
|
72
|
+
|
73
|
+
The first is a Ruby file, like this:
|
74
|
+
|
75
|
+
```
|
76
|
+
# app/components/message_component.rb
|
77
|
+
class MessageComponent < ViewComponent::Base
|
78
|
+
def initialize(name:)
|
79
|
+
@name = name
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
The other is an ERB template, like this:
|
85
|
+
|
86
|
+
```
|
87
|
+
<%# app/components/message_component.html.erb %>
|
88
|
+
<h1>Hello, <%= @name %>!<h1>
|
89
|
+
```
|
90
|
+
|
91
|
+
Also provide the source for an ERB demo file showing one example use of the component, e.g.:
|
92
|
+
|
93
|
+
```
|
94
|
+
<%# app/views/demo/index.html.erb %>
|
95
|
+
|
96
|
+
<% name = "World" %>
|
97
|
+
|
98
|
+
<%= render(MessageComponent.new(name: name)) %>
|
99
|
+
```
|
100
|
+
|
101
|
+
Notes:
|
102
|
+
|
103
|
+
* If the code contains components from `@headlessui/react' then explain that is not supported and don't attempt to convert.
|
104
|
+
* Don't attempt to verify Tailwind class names, assume they are valid.
|
105
|
+
* Keep single-line ternary conditions as single-line ternary.
|
106
|
+
* The React code may contain an array of example objects before the markup. Preserve this, but convert it to an array of Ruby hashes in the ERB demo file. And change camelCase keys to snake_case.
|
107
|
+
* Replace `<img>` tags with Rails `image_tag` helper calls.
|
108
|
+
* Replace `<a href>` tags with Rails `link_to` helper calls. Preserve all CSS utility classes as-is, and assume Tailwind is available.
|
109
|
+
* Replace `<form>` tags with Rails `form_tag` helper calls.
|
110
|
+
* Replace `<button>` tags with Rails `buton_tag` helper calls.
|
111
|
+
* Replace `onClick` events with theoretical Stimulus calls.
|
112
|
+
* Assume a heroicons helper is available, e.g.: <%= heroicon "magnifying-glass", options: { class: "text-primary-500" } %>
|
113
|
+
* Asssume a `class_names` helper is available which works just like React's `classNames` helper.
|
114
|
+
* If the component relies on JavaScript, suggest to use Stimulus or Turbo instead. Also provide https://github.com/excid3/tailwindcss-stimulus-components as a reference.
|
115
|
+
* If the code constains instructions such as "This example requires updating your template" then report those details.
|
116
|
+
* Let me know if any parts were not possible to accurately convert.
|
117
|
+
|
118
|
+
```
|
119
|
+
#{react_content}
|
120
|
+
```
|
121
|
+
EOS
|
122
|
+
end
|
123
|
+
|
124
|
+
attr_reader :react_content, :client, :name, :summary, :view_component_ruby_code, :demo_erb_code, :erb_template
|
125
|
+
end
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: react_to_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andy Waite
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: openai
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0.6'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0.6'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: zeitwerk
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.7'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.7'
|
40
|
+
email:
|
41
|
+
- 13400+andyw8@users.noreply.github.com
|
42
|
+
executables:
|
43
|
+
- react_to_rails
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".standard.yml"
|
48
|
+
- LICENSE.txt
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- examples/pricing.jsx
|
52
|
+
- examples/pricing_component.html.erb
|
53
|
+
- examples/pricing_component.rb
|
54
|
+
- exe/react_to_rails
|
55
|
+
- lib/react_to_rails.rb
|
56
|
+
- lib/react_to_rails/cli.rb
|
57
|
+
- lib/react_to_rails/convert.rb
|
58
|
+
- lib/react_to_rails/version.rb
|
59
|
+
- sig/react_to_rails.rbs
|
60
|
+
homepage: https://github.com/andyw8/react_to_rails
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata:
|
64
|
+
allowed_push_host: https://rubygems.org
|
65
|
+
homepage_uri: https://github.com/andyw8/react_to_rails
|
66
|
+
source_code_uri: https://github.com/andyw8/react_to_rails
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 3.1.0
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubygems_version: 3.6.9
|
82
|
+
specification_version: 4
|
83
|
+
summary: Converts React components to Rails using AI
|
84
|
+
test_files: []
|