Skip to content

Commit ceffcda

Browse files
marcorothkarreiro
andauthored
Setup Ruby C-extension (#3)
* Initialize Ruby C-extension * Setup shared library and add GitHub action * Try to make the shared library name OS dependent * Build all in ruby workflow * Add Gemfile * Add some Ruby tests * First take on Ruby extension * `ERBX::Lexer.lex()` -> `ERBX.lex()` * Use `token_type_string` over `token_type_to_string` * Call `buffer_init` before passing the buffers into `erbx_lex_to_buffer` * Adjust rake tasks * Update tests / CI --------- Co-Authored-By: Guilherme Carreiro <karreiro@gmail.com>
1 parent 3bf2c0b commit ceffcda

32 files changed

+557
-35
lines changed

.github/workflows/ruby.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Ruby Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
12+
- name: Install dependencies
13+
run: sudo apt-get install check
14+
15+
- name: make
16+
run: make
17+
18+
- name: Set up Ruby
19+
uses: ruby/setup-ruby@v1
20+
with:
21+
bundler-cache: true
22+
23+
- name: Setup extension
24+
working-directory: ./ext/erbx
25+
run: bundle install
26+
27+
- name: Build extension
28+
working-directory: ./ext/erbx
29+
run: bundle exec rake
30+
31+
- name: Test extension
32+
run: bundle exec ruby ext/erbx/test.rb

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
/erbx
77
/run_erbx_tests
88

9+
# ERBx Ruby extension
10+
/ext/erbx/extconf.h
11+
/ext/erbx/erbx.bundle
12+
/ext/erbx/Makefile
13+
/lib/erbx/erbx.bundle
14+
915
# Prerequisites
1016
*.d
1117

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gemspec

Gemfile.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
PATH
2+
remote: .
3+
specs:
4+
erbx (0.0.1)
5+
ffi
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
ffi (1.17.0)
11+
ffi (1.17.0-x86_64-darwin)
12+
maxitest (5.5.0)
13+
minitest (>= 5.14.0, < 5.24.0)
14+
minitest (5.23.1)
15+
rake (13.2.1)
16+
rake-compiler (1.2.7)
17+
rake
18+
19+
PLATFORMS
20+
ruby
21+
x86_64-darwin-23
22+
23+
DEPENDENCIES
24+
erbx!
25+
maxitest
26+
rake (~> 13.2)
27+
rake-compiler (~> 1.2)
28+
29+
BUNDLED WITH
30+
2.5.11

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ test_sources = $(wildcard test/*.c)
88
test_objects = $(test_sources:.c=.o)
99
non_main_objects = $(filter-out src/main.o, $(objects))
1010

11+
soext ?= $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')
12+
lib_name = lib$(exec).$(soext)
13+
ruby_extension = ext/erbx/$(lib_name)
14+
1115
os := $(shell uname -s)
1216

1317
flags = -g -Wall -fPIC
@@ -23,11 +27,15 @@ ifeq ($(os),Darwin)
2327
test_ldflags = -L$(brew_prefix)/lib -lcheck -lm
2428
endif
2529

26-
all: $(exec) test
30+
all: $(exec) $(lib_name) test
2731

2832
$(exec): $(objects)
2933
gcc $(objects) $(flags) -o $(exec)
3034

35+
$(lib_name): $(objects)
36+
gcc -shared $(objects) $(flags) -o $(lib_name)
37+
# cp $(lib_name) $(ruby_extension)
38+
3139
%.o: %.c include/%.h
3240
gcc -c $(flags) $< -o $@
3341

@@ -38,5 +46,5 @@ test: $(test_objects) $(non_main_objects)
3846
gcc $(test_objects) $(non_main_objects) $(test_cflags) $(test_ldflags) -o $(test_exec)
3947

4048
clean:
41-
rm -f $(exec) $(test_exec)
49+
rm -f $(exec) $(test_exec) $(lib_name) $(ruby_extension)
4250
rm -f src/*.o test/*.o

Rakefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "bundler/gem_tasks"
2+
require "rake/extensiontask"
3+
require "rake/testtask"
4+
5+
Rake::ExtensionTask.new do |ext|
6+
ext.name = "erbx"
7+
ext.source_pattern = "*.{c,h}"
8+
ext.ext_dir = "ext/erbx"
9+
ext.lib_dir = "lib/erbx"
10+
ext.gem_spec = Gem::Specification.load("erbx.gemspec")
11+
end
12+
13+
Rake::TestTask.new(:test) do |t|
14+
t.libs << "test"
15+
t.libs << "lib"
16+
t.test_files = FileList["ext/erbx/test/**/*_test.rb"]
17+
end
18+
19+
Rake::Task[:compile].enhance do
20+
IO.popen("make") do |output|
21+
output.each_line do |line|
22+
puts "#{line}"
23+
end
24+
end
25+
26+
if $?.exitstatus != 0
27+
raise "src/* could not be compiled #{$?.exitstatus}"
28+
end
29+
end
30+
31+
Rake::Task[:clean].enhance do
32+
IO.popen("make clean") do |output|
33+
output.each_line do |line|
34+
puts "#{line}"
35+
end
36+
end
37+
end
38+
39+
task default: [:compile, :test]

erbx.gemspec

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "erbx"
5+
spec.version = "0.0.1"
6+
spec.authors = ["Marco Roth"]
7+
spec.email = ["marco.roth@intergga.ch"]
8+
9+
spec.summary = "HTML-aware ERB parser"
10+
spec.homepage = "https://github.com/marcoroth/erbx"
11+
spec.license = "MIT"
12+
13+
spec.required_ruby_version = ">= 3.0.0"
14+
15+
spec.require_paths = ["lib"]
16+
spec.files = [
17+
"ext/erbx/extension.c",
18+
"lib/erbx.rb",
19+
# "lib/erbx/version.rb"
20+
]
21+
22+
spec.extensions = ["ext/erbx/extconf.rb"]
23+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
24+
spec.metadata["source_code_uri"] = "https://github.com/marcoroth/erbx"
25+
spec.metadata["changelog_uri"] = "https://github.com/marcoroth/erbx/releases"
26+
27+
spec.add_dependency "ffi"
28+
29+
spec.add_development_dependency "rake", "~> 13.2"
30+
spec.add_development_dependency "rake-compiler", "~> 1.2"
31+
spec.add_development_dependency "maxitest"
32+
end

examples/ruby/Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "erbx", path: "../.."

examples/ruby/Gemfile.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PATH
2+
remote: ../..
3+
specs:
4+
erbx (0.0.1)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
10+
PLATFORMS
11+
ruby
12+
x86_64-darwin-23
13+
14+
DEPENDENCIES
15+
erbx!
16+
17+
BUNDLED WITH
18+
2.5.10

examples/ruby/test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require "erbx"
2+
3+
puts ERBX.lex("<html><html>")

0 commit comments

Comments
 (0)