Skip to content

Commit

Permalink
Merge pull request #165 from noir-cr/hahwul-dev
Browse files Browse the repository at this point in the history
Add ruby_hanami detector/analyzer (#162)
  • Loading branch information
hahwul authored Nov 19, 2023
2 parents ad5ec85 + 31caf8d commit 4b5fade
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
| Python | FastAPI ||||||
| Ruby | Rails ||||| X |
| Ruby | Sinatra ||||| X |
| Ruby | Hanami ||| | | X |
| Php | ||||| X |
| Java | Jsp |||| X | X |
| Java | Armeria ||| X | X | X |
Expand Down
31 changes: 31 additions & 0 deletions spec/functional_test/fixtures/ruby_hanami/Gemfile
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
9 changes: 9 additions & 0 deletions spec/functional_test/fixtures/ruby_hanami/app/action.rb
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 spec/functional_test/fixtures/ruby_hanami/config/routes.rb
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.
15 changes: 15 additions & 0 deletions spec/functional_test/testers/ruby_hanami_spec.cr
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
1 change: 1 addition & 0 deletions src/analyzer/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def initialize_analyzers(logger : NoirLogger)
analyzers = {} of String => Proc(Hash(Symbol, String), Array(Endpoint))
analyzers["ruby_rails"] = ->analyzer_rails(Hash(Symbol, String))
analyzers["ruby_sinatra"] = ->analyzer_sinatra(Hash(Symbol, String))
analyzers["ruby_hanami"] = ->analyzer_hanami(Hash(Symbol, String))
analyzers["java_spring"] = ->analyzer_spring(Hash(Symbol, String))
analyzers["kotlin_spring"] = ->analyzer_spring(Hash(Symbol, String))
analyzers["java_armeria"] = ->analyzer_armeria(Hash(Symbol, String))
Expand Down
73 changes: 73 additions & 0 deletions src/analyzer/analyzers/analyzer_hanami.cr
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
2 changes: 1 addition & 1 deletion src/detector/detector.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def detect_techs(base_path : String, options : Hash(Symbol, String), logger : No
defind_detectors([
DetectorCrystalKemal, DetectorGoEcho, DetectorJavaJsp, DetectorJavaSpring, DetectorJsExpress,
DetectorPhpPure, DetectorPythonDjango, DetectorPythonFlask, DetectorPythonFastAPI,
DetectorRubyRails, DetectorRubySinatra, DetectorOas2, DetectorOas3, DetectorRAML,
DetectorRubyRails, DetectorRubySinatra, DetectorRubyHanami, DetectorOas2, DetectorOas3, DetectorRAML,
DetectorGoGin, DetectorKotlinSpring, DetectorJavaArmeria, DetectorCSharpAspNetMvc,
DetectorRustAxum,
])
Expand Down
15 changes: 15 additions & 0 deletions src/detector/detectors/ruby_hanami.cr
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
5 changes: 5 additions & 0 deletions src/techs/techs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ module NoirTechs
:framework => "Sinatra",
:similar => ["sinatra", "ruby-sinatra", "ruby_sinatra"],
},
:ruby_hanami => {
:language => "Ruby",
:framework => "Hanami",
:similar => ["hanami", "ruby-hanami", "ruby_hanami"],
},
:rust_axum => {
:language => "Rust",
:framework => "Axum",
Expand Down

0 comments on commit 4b5fade

Please sign in to comment.