-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port_ruby_to_crystal' Initial commit. Date:
2022-11-02 18:53:05'
- Loading branch information
0 parents
commit 36c0ded
Showing
8 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,29 @@ | ||
a Regex script for port ruby code to crystal, reduce friction. | ||
|
||
Include only the easiest, most obvious, and safest parts. | ||
|
||
only change the most easy part with minimum effort. | ||
|
||
with minimal effort and cost. | ||
|
||
**Note:** Run this code at your own risk. | ||
|
||
# How to use it | ||
|
||
Entrying the projet folder, you should have renamed the extension .rb file | ||
to .cr for those in-place files. | ||
|
||
Before this step, it's a good idea to use rubocop to make some style changes. | ||
e.g. change all single-quoted strings to double-quoted strings. | ||
|
||
```sh | ||
$: ./port_ruby_to_crystal | ||
``` | ||
|
||
# Run test | ||
|
||
```sh | ||
╰─ $ ./test.sh | ||
Running ruby test.rb successful. | ||
Running crystal run test.cr successful. | ||
``` |
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,31 @@ | ||
#!/bin/env ruby | ||
|
||
def replace(code) | ||
code.gsub!(/\bFile\.exist\?(\s*)/, 'File.exists?\1') | ||
code.gsub!(/\bFile\.readlines\b/, 'File.read_lines') | ||
code.gsub!(/\bYAML\.load\b/, 'YAML.parse') | ||
code.gsub!(/require (?:'|")fileutils(?:'|")(\s*)/, 'require "file_utils"\1') | ||
code.gsub!(/\.respond_to\?(\s*)/, '.responds_to?\1') | ||
code.gsub!(/\brequire_relative\b/, 'require') | ||
code.gsub!(/\.include\?(\s*)/, '.includes?\1') | ||
code.gsub!(/\.end_with\?(\s*)/, '.ends_with?\1') | ||
code.gsub!(/\.start_with\?(\s*)/, '.starts_with?\1') | ||
code.gsub!(/(\w+\[.*?\]) \|\|/, '\1? ||') | ||
code.gsub!(/\battr_accessor\b/, 'property') | ||
code.gsub!(/\battr_reader\b/, 'getter') | ||
code.gsub!(/\battr_writer\b/, 'setter') | ||
code.gsub!("__dir__", '__DIR__') | ||
code.gsub!(/(\()?(&):(\w+)(\))?/, ' \2.\3') | ||
end | ||
|
||
if ARGV[0] == '--test' | ||
code = File.read('test/test.rb') | ||
replace(code) | ||
File.write('test/test.cr', code) | ||
else | ||
Dir.glob("**/*.cr").each do |cr_file| | ||
code = File.read(cr_file) | ||
replace(code) | ||
File.write(cr_file, code) | ||
end | ||
end |
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,8 @@ | ||
#!/usr/bin/env sh | ||
|
||
set -e | ||
|
||
ruby test/test.rb && echo "Running ruby test.rb successful." | ||
chmod +x ./port_ruby_to_crystal | ||
./port_ruby_to_crystal --test | ||
crystal run test/test.cr && echo "Running crystal run test.cr successful." |
Empty file.
Empty file.
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,27 @@ | ||
File.exists?("ruby") | ||
"hello".responds_to? :size | ||
require "./empty" | ||
|
||
arr = {"world" => 100} | ||
arr["hello"]? || "world" | ||
|
||
["hello", "world"].includes? "hello" | ||
|
||
class Test | ||
property :test1 | ||
getter :test2 | ||
setter :test3 | ||
end | ||
|
||
__DIR__ | ||
|
||
["a", "b"].map &.upcase | ||
["a", "b"].map &.upcase | ||
|
||
"hello".ends_with? "lo" | ||
"hello".starts_with? "he" | ||
|
||
File.read_lines("./test/test.rb") | ||
|
||
require "file_utils" | ||
require "file_utils" |
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,27 @@ | ||
File.exist?("ruby") | ||
"hello".respond_to? :size | ||
require_relative "./empty" | ||
|
||
arr = {"world" => 100} | ||
arr["hello"] || "world" | ||
|
||
["hello", "world"].include? "hello" | ||
|
||
class Test | ||
attr_accessor :test1 | ||
attr_reader :test2 | ||
attr_writer :test3 | ||
end | ||
|
||
__dir__ | ||
|
||
["a", "b"].map(&:upcase) | ||
["a", "b"].map &:upcase | ||
|
||
"hello".end_with? "lo" | ||
"hello".start_with? "he" | ||
|
||
File.readlines("./test/test.rb") | ||
|
||
require 'fileutils' | ||
require "fileutils" |