-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from noir-cr/hahwul-dev
Add ruby_hanami detector/analyzer (#162)
- Loading branch information
Showing
11 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
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
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 @@ | ||
source "https://rubygems.org" | ||
ruby "3.2.2" | ||
|
||
gem "hanami", "~> 2.0" | ||
gem "hanami-router", "~> 2.0" | ||
gem "hanami-controller", "~> 2.0" | ||
gem "hanami-validations", "~> 2.0" | ||
|
||
gem "dry-types", "~> 1.0", ">= 1.6.1" | ||
gem "puma" | ||
gem "rake" | ||
|
||
group :development, :test do | ||
gem "dotenv" | ||
end | ||
|
||
group :cli, :development do | ||
gem "hanami-reloader" | ||
end | ||
|
||
group :cli, :development, :test do | ||
gem "hanami-rspec" | ||
end | ||
|
||
group :development do | ||
gem "guard-puma", "~> 0.8" | ||
end | ||
|
||
group :test do | ||
gem "rack-test" | ||
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,9 @@ | ||
# auto_register: false | ||
# frozen_string_literal: true | ||
|
||
require "hanami/action" | ||
|
||
module Testapp | ||
class Action < Hanami::Action | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
spec/functional_test/fixtures/ruby_hanami/config/routes.rb
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,12 @@ | ||
module Testapp | ||
class Routes < Hanami::Routes | ||
root { "Hello from Hanami" } | ||
|
||
get "/books", to: "books.index" | ||
get "/books/:id", to: "books.show" | ||
get "/books/new", to: "books.new" | ||
post "/books", to: "books.create" | ||
patch "/books/:id", to: "books.update" | ||
delete "/books/:id", to: "books.destroy" | ||
end | ||
end |
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,15 @@ | ||
require "../func_spec.cr" | ||
|
||
extected_endpoints = [ | ||
Endpoint.new("/books", "GET"), | ||
Endpoint.new("/books/:id", "GET"), | ||
Endpoint.new("/books/new", "GET"), | ||
Endpoint.new("/books", "POST"), | ||
Endpoint.new("/books/:id", "PATCH"), | ||
Endpoint.new("/books/:id", "DELETE"), | ||
] | ||
|
||
FunctionalTester.new("fixtures/ruby_hanami/", { | ||
:techs => 1, | ||
:endpoints => 6, | ||
}, extected_endpoints).test_all |
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
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,73 @@ | ||
require "../../models/analyzer" | ||
|
||
class AnalyzerHanami < Analyzer | ||
def analyze | ||
# Config Analysis | ||
if File.exists?("#{@base_path}/config/routes.rb") | ||
File.open("#{@base_path}/config/routes.rb", "r", encoding: "utf-8", invalid: :skip) do |file| | ||
last_endpoint = Endpoint.new("", "") | ||
file.each_line do |line| | ||
endpoint = line_to_endpoint(line) | ||
if endpoint.method != "" | ||
@result << endpoint | ||
last_endpoint = endpoint | ||
_ = last_endpoint | ||
end | ||
end | ||
end | ||
end | ||
|
||
@result | ||
end | ||
|
||
def line_to_endpoint(content : String) : Endpoint | ||
content.scan(/get\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "GET") | ||
end | ||
end | ||
|
||
content.scan(/post\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "POST") | ||
end | ||
end | ||
|
||
content.scan(/put\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "PUT") | ||
end | ||
end | ||
|
||
content.scan(/delete\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "DELETE") | ||
end | ||
end | ||
|
||
content.scan(/patch\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "PATCH") | ||
end | ||
end | ||
|
||
content.scan(/head\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "HEAD") | ||
end | ||
end | ||
|
||
content.scan(/options\s+['"](.+?)['"]/) do |match| | ||
if match.size > 1 | ||
return Endpoint.new("#{@url}#{match[1]}", "OPTIONS") | ||
end | ||
end | ||
|
||
Endpoint.new("", "") | ||
end | ||
end | ||
|
||
def analyzer_hanami(options : Hash(Symbol, String)) | ||
instance = AnalyzerHanami.new(options) | ||
instance.analyze | ||
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
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,15 @@ | ||
require "../../models/detector" | ||
|
||
class DetectorRubyHanami < Detector | ||
def detect(filename : String, file_contents : String) : Bool | ||
check = file_contents.includes?("gem 'hanami'") | ||
check = check || file_contents.includes?("gem \"hanami\"") | ||
check = check && filename.includes?("Gemfile") | ||
|
||
check | ||
end | ||
|
||
def set_name | ||
@name = "ruby_hanami" | ||
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