Skip to content
Open
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
12 changes: 0 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go/go.mod
cache-dependency-path: go/go.sum

- name: Build Go Library
run: bundle exec rake go:build

- name: Compile Native Extension
run: bundle exec rake compile

- name: Run tests
run: bundle exec rake test

Expand Down
37 changes: 29 additions & 8 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ Style/StringLiteralsInInterpolation:
Style/Documentation:
Enabled: false

Style/GlobalVars:
Exclude:
- ext/lipgloss/extconf.rb

Metrics/MethodLength:
Max: 20

Expand All @@ -24,6 +20,9 @@ Metrics/ClassLength:
Exclude:
- test/**

Metrics/ModuleLength:
Max: 100

Metrics/BlockLength:
Max: 50

Expand All @@ -33,13 +32,35 @@ Metrics/CyclomaticComplexity:
Metrics/ParameterLists:
Max: 10

Naming/MethodParameterName:
AllowedNames:
- n
- r
- g
- b
- t
- x
- y
- z
- l
- u
- v
- h
- c
- w
- c1
- c2
- r1
- g1
- b1
- r2
- g2
- b2
- cc

Layout/LineLength:
Enabled: false

Security/Eval:
Exclude:
- Rakefile

Layout/LeadingCommentSpace:
AllowRBSInlineAnnotation: true

Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
lipgloss (0.2.2)
unicode-display_width (~> 3.0)

GEM
remote: https://rubygems.org/
Expand Down
153 changes: 2 additions & 151 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,156 +9,7 @@ begin
require "rubocop/rake_task"
RuboCop::RakeTask.new
rescue LoadError
# rubocop not available in cross-compilation environment
end

begin
require "rake/extensiontask"

PLATFORMS = [
"aarch64-linux-gnu",
"aarch64-linux-musl",
"arm-linux-gnu",
"arm-linux-musl",
"arm64-darwin",
"x86-linux-gnu",
"x86-linux-musl",
"x86_64-darwin",
"x86_64-linux-gnu",
"x86_64-linux-musl"
].freeze

GO_PLATFORMS = {
"aarch64-linux-gnu" => { goos: "linux", goarch: "arm64", cc: "aarch64-linux-gnu-gcc" },
"aarch64-linux-musl" => { goos: "linux", goarch: "arm64", cc: "aarch64-linux-musl-gcc" },
"arm-linux-gnu" => { goos: "linux", goarch: "arm", cc: "arm-linux-gnueabihf-gcc" },
"arm-linux-musl" => { goos: "linux", goarch: "arm", cc: "arm-linux-musleabihf-gcc" },
"arm64-darwin" => { goos: "darwin", goarch: "arm64", cc: "o64-clang" },
"x86-linux-gnu" => { goos: "linux", goarch: "386", cc: "i686-linux-gnu-gcc" },
"x86-linux-musl" => { goos: "linux", goarch: "386", cc: "i686-unknown-linux-musl-gcc" },
"x86_64-darwin" => { goos: "darwin", goarch: "amd64", cc: "o64-clang" },
"x86_64-linux-gnu" => { goos: "linux", goarch: "amd64", cc: "x86_64-linux-gnu-gcc" },
"x86_64-linux-musl" => { goos: "linux", goarch: "amd64", cc: "gcc" }
}.freeze

def go_version
go_mod = File.read("go/go.mod")
go_mod[/^go (\d+\.\d+\.\d+)/, 1] || go_mod[/^go (\d+\.\d+)/, 1]
end

def detect_go_platform
cpu = RbConfig::CONFIG["host_cpu"]
os = RbConfig::CONFIG["host_os"]

arch = case cpu
when /aarch64|arm64/ then "arm64"
when /x86_64|amd64/ then "amd64"
else cpu
end

goos = case os
when /darwin/ then "darwin"
else "linux"
end

"#{goos}_#{arch}"
end

namespace :go do
desc "Build Go archive for current platform"
task :build do
platform = detect_go_platform
output_dir = "go/build/#{platform}"
FileUtils.mkdir_p(output_dir)
sh "cd go && CGO_ENABLED=1 go build -buildmode=c-archive -o build/#{platform}/liblipgloss.a ."
end

desc "Build Go archives for all platforms"
task :build_all do
GO_PLATFORMS.each_value do |env|
output_dir = "go/build/#{env[:goos]}_#{env[:goarch]}"
FileUtils.mkdir_p(output_dir)
sh "cd go && CGO_ENABLED=1 GOOS=#{env[:goos]} GOARCH=#{env[:goarch]} go build -buildmode=c-archive -o build/#{env[:goos]}_#{env[:goarch]}/liblipgloss.a ."
end
end

desc "Clean Go build artifacts"
task :clean do
FileUtils.rm_rf("go/build")
end

desc "Format Go source files"
task :fmt do
sh "gofmt -s -w go/"
end
end

Rake::ExtensionTask.new do |ext|
ext.name = "lipgloss"
ext.ext_dir = "ext/lipgloss"
ext.lib_dir = "lib/lipgloss"
ext.source_pattern = "*.c"
ext.gem_spec = Gem::Specification.load("lipgloss.gemspec")
ext.cross_compile = true
ext.cross_platform = PLATFORMS
end

namespace "gem" do
task "prepare" do
require "rake_compiler_dock"

sh "bundle config set cache_all true"

gemspec_path = File.expand_path("./lipgloss.gemspec", __dir__)
spec = eval(File.read(gemspec_path), binding, gemspec_path)

RakeCompilerDock.set_ruby_cc_version(spec.required_ruby_version.as_list)
rescue LoadError
abort "rake_compiler_dock is required for this task"
end

PLATFORMS.each do |platform|
desc "Build the native gem for #{platform}"
task platform => "prepare" do
require "rake_compiler_dock"

env = GO_PLATFORMS[platform]

build_script = <<~BASH
curl -sSL https://go.dev/dl/go#{go_version}.linux-amd64.tar.gz -o /tmp/go.tar.gz && \
sudo tar -C /usr/local -xzf /tmp/go.tar.gz && \
rm /tmp/go.tar.gz && \
export PATH=$PATH:/usr/local/go/bin && \
cd go && \
mkdir -p build/#{env[:goos]}_#{env[:goarch]} && \
CGO_ENABLED=1 CC=#{env[:cc]} GOOS=#{env[:goos]} GOARCH=#{env[:goarch]} go build -buildmode=c-archive -o build/#{env[:goos]}_#{env[:goarch]}/liblipgloss.a . && \
cd .. && \
rm -f .ruby-version && \
rm -rf vendor/bundle && \
bundle install && \
rake native:#{platform} gem RUBY_CC_VERSION='#{ENV.fetch("RUBY_CC_VERSION", nil)}'
BASH

RakeCompilerDock.sh(build_script, platform: platform)
end
end
end
rescue LoadError => e
desc "Compile task not available (rake-compiler not installed)"
task :compile do
puts e
abort <<~MESSAGE

rake-compiler is required for this task.

Are you running `rake` using `bundle exec rake`?

Otherwise:
* try to run bundle install
* add it to your Gemfile
* or install it with: gem install rake-compiler
MESSAGE
end
# rubocop not available
end

task :rbs_inline do
Expand All @@ -179,4 +30,4 @@ task :rbs_inline do
end
end

task default: [:test, :rubocop, :compile]
task default: [:test, :rubocop]
13 changes: 13 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ target :lib do
signature "sig"

check "lib"

# The pure Ruby implementation uses metaprogramming patterns
# (define_method, allocate, instance_variable_set) that steep
# cannot fully type-check. Downgrade these to non-failing levels.
configure_code_diagnostics do |hash|
hash[Steep::Diagnostic::Ruby::NoMethod] = :information
hash[Steep::Diagnostic::Ruby::UnknownConstant] = :information
hash[Steep::Diagnostic::Ruby::UnannotatedEmptyCollection] = :information
hash[Steep::Diagnostic::Ruby::UndeclaredMethodDefinition] = :information
hash[Steep::Diagnostic::Ruby::MethodBodyTypeMismatch] = :information
hash[Steep::Diagnostic::Ruby::UnexpectedPositionalArgument] = :information
hash[Steep::Diagnostic::Ruby::ArgumentTypeMismatch] = :information
end
end
Loading