Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,35 @@ permissions:

jobs:
test:

runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos]
ruby-version: ['3.0', '3.4']

steps:
- uses: actions/checkout@v4

- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically

- name: Run tests
run: bundle exec rake
run: bundle exec rspec

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.4'
bundler-cache: true

- name: Run RuboCop
run: bundle exec rubocop -f github
18 changes: 14 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ AllCops:
Exclude:
- 'tmp/**/*'
- 'examples/*'
TargetRubyVersion: 2.6
TargetRubyVersion: 3.0
NewCops: enable

Layout/LineLength:
Enabled: false
Max: 120

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: false
Expand Down Expand Up @@ -119,15 +121,15 @@ Style/SlicingWithRange:
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- 'fast.gemspec'
- '*.gemspec'

Lint/InterpolationCheck:
Exclude:
- 'spec/**/*'

Metrics/MethodLength:
CountComments: false # count full line comments?
Max: 12
Max: 20

Metrics/ModuleLength:
Enabled: false
Expand All @@ -139,10 +141,11 @@ RSpec/NestedGroups:
Max: 4

RSpec/ExampleLength:
Max: 20
Max: 15

RSpec/MultipleExpectations:
Enabled: false
Max: 5

RSpec/DescribedClass:
Enabled: false
Expand All @@ -158,3 +161,10 @@ Style/HashTransformKeys:

Style/HashTransformValues:
Enabled: true

Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes

Style/Documentation:
Enabled: false
45 changes: 39 additions & 6 deletions Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ end
# Use to walkthrough the docs files with fast examples
# fast .intro
Fast.shortcut :intro do
ARGV << File.join(File.dirname(__FILE__), 'docs', 'walkthrough.md')
docs_dir = File.expand_path('../docs', __FILE__)
walkthrough_file = File.join(docs_dir, 'walkthrough.md')

unless File.exist?(walkthrough_file)
puts "Error: Could not find walkthrough.md in #{docs_dir}"
return
end

ARGV << walkthrough_file
Fast.shortcuts[:walk].run
end

Expand All @@ -63,28 +70,54 @@ private
def require_or_install_tty_md
require 'tty-markdown'
rescue LoadError
puts 'Installing tty-markdown gem to better engage you :)'
Gem.install('tty-markdown')
puts 'Installing tty-markdown gem via bundler to better engage you :)'
system('bundle install')
puts 'Done! Now, back to our topic \o/'
system('clear')
retry
begin
require 'tty-markdown'
rescue LoadError
puts "Failed to load tty-markdown even after installation."
puts "Please try running: bundle install"
exit 1
end
end

# Interactive command line walkthrough
# fast .walk docs/walkthrough.md
Fast.shortcut :walk do
require_or_install_tty_md
file = ARGV.last
execute = ->(line) { system(line) }

unless File.exist?(file)
puts "Error: Could not find file #{file}"
return
end

execute = ->(line) {
# Replace gem which commands with local file paths
if line =~ /gem which fast/
puts "Using local Fast installation"
return
end
# For other commands, use the shell
system(line)
}

walk = ->(line) { line.each_char { |c| sleep(0.02) and print(c) } }

File.readlines(file).each do |line|
case line
when /^fast /
walk[line]
execute[line]
when /^\$ /
walk[line]
execute[line[2..]]
if line =~ /gem which fast/
puts "Using local Fast installation at #{File.expand_path('..', __FILE__)}"
else
execute[line[2..]]
end
when /^!{3}\s/
# Skip warnings that are only for web tutorials
else
Expand Down
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in fast.gemspec
gemspec

group :development do
gem 'tty-markdown'
gem 'rubocop', require: false
gem 'rubocop-rspec', require: false
end
1 change: 1 addition & 0 deletions fast.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'coderay'
spec.add_dependency 'parallel'
spec.add_dependency 'parser'
spec.add_dependency 'racc'
spec.add_dependency 'pg_query'

spec.add_development_dependency 'bundler'
Expand Down
35 changes: 28 additions & 7 deletions lib/fast/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def highlight(node, show_sexp: false, colorize: true, sql: false)
if node.respond_to?(:loc) && !show_sexp
wrap_source_range(node).source
else
node
output = node.to_s
output = output.gsub(/\b-\b/, '_') if sql
output
end
return output unless colorize

Expand Down Expand Up @@ -192,20 +194,39 @@ def run!
end

if @similar
ast = Fast.public_send( @sql ? :parse_sql : :ast, @pattern)
ast = Fast.public_send(@sql ? :parse_sql : :ast, @pattern)
@pattern = Fast.expression_from(ast)
debug "Search similar to #{@pattern}"
elsif @from_code
ast = Fast.public_send( @sql ? :parse_sql : :ast, @pattern)
@pattern = ast.to_sexp
if @sql
@pattern.gsub!(/\b-\b/,'_')
# For SQL, we parse the pattern as SQL code first
sql_ast = Fast.parse_sql(@pattern)
if @files.empty?
# If no files provided, just show the AST
puts Fast.highlight(sql_ast, show_sexp: true, colorize: @colorize, sql: @sql)
return
else
# For file searches, create a more flexible pattern that matches the statement type
@pattern = "(#{sql_ast.type.to_s.gsub('-', '_')} ...)"
end
else
# For Ruby, keep existing behavior
ast = Fast.ast(@pattern)
if ast.nil?
# If we can't parse the code directly, try to make it parseable
ast = Fast.ast("#{@pattern}; end")
if ast.nil?
puts "Error: Could not parse Ruby code: #{@pattern}"
return
end
end
@pattern = ast.type.to_s
debug "Search from code to #{@pattern}"
end
debug "Search from code to #{@pattern}"
end

if @files.empty?
ast ||= Fast.public_send( @sql ? :parse_sql : :ast, @pattern)
ast ||= Fast.public_send(@sql ? :parse_sql : :ast, @pattern)
puts Fast.highlight(ast, show_sexp: @show_sexp, colorize: @colorize, sql: @sql)
else
search
Expand Down
Loading
Loading