Skip to content

Commit

Permalink
chore: ARM ruby build (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Sep 23, 2024
1 parent 0286e61 commit f8310a7
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 41 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/publish-ruby.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Build Ruby
on:
push:
tags:
- "*"
workflow_dispatch:

jobs:
Expand All @@ -14,11 +11,11 @@ jobs:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
output: libyggdrasilffi.so
name: libyggdrasilffi.so
name: libyggdrasilffi_x86_64.so
- os: windows-latest
target: x86_64-pc-windows-gnu
output: yggdrasilffi.dll
name: yggdrasilffi.dll
name: libyggdrasilffi_x86_64.dll
- os: macos-13
target: x86_64-apple-darwin
output: libyggdrasilffi.dylib
Expand Down Expand Up @@ -51,7 +48,7 @@ jobs:
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.output }}
name: ${{ matrix.name }}
path: target/${{ matrix.target }}/release/${{ matrix.name }}

build_single_binary_gems:
Expand All @@ -60,9 +57,9 @@ jobs:
matrix:
include:
- os: ubuntu-latest
binary: libyggdrasilffi.so
binary: libyggdrasilffi_x86_64.so
- os: windows-latest
binary: yggdrasilffi.dll
binary: libyggdrasilffi_x86_64.dll
- os: macos-13
binary: libyggdrasilffi_x86_64.dylib
platform: x86_64-darwin
Expand Down Expand Up @@ -105,6 +102,8 @@ jobs:
ruby: jruby-9.3
- java: 17
ruby: jruby-9.4
- java: 21
ruby: jruby-9.4

needs: build-binaries
steps:
Expand Down Expand Up @@ -134,4 +133,4 @@ jobs:
gem push *.gem
working-directory: ${{ github.workspace }}
env:
GEM_HOST_API_KEY: ${{ secrets.GEMS_PUBLISH_KEY }}
GEM_HOST_API_KEY: ${{ secrets.GEMS_PUBLISH_KEY }}
2 changes: 1 addition & 1 deletion .github/workflows/sarif-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
uses: actions/checkout@v3
with:
repository: Unleash/client-specification
ref: v5.1.0
ref: v5.1.7
path: client-specification
- name: Run tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/go-engine/unleash_engine.h
/php-engine/unleash_engine.h
/php-engine/libyggdrasilffi.so
/ruby-engine/lib/libyggdrasilffi.*
/ruby-engine/lib/libyggdrasilffi*.*

# Devenv
.devenv*
Expand Down
46 changes: 44 additions & 2 deletions ruby-engine/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
#!/bin/bash
set -e

cargo build --release
rm -f lib/libyggdrasilffi.so
cp ../target/release/libyggdrasilffi.so lib/

## Start copy the correct binary to the correct name
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

case "$OS" in
darwin)
EXT="dylib"
;;
linux)
EXT="so"
;;
msys*|mingw*|cygwin*)
EXT="dll"
;;
*)
echo "Unsupported platform: $OS"
exit 1
;;
esac

case "$ARCH" in
x86_64)
ARCH_SUFFIX="x86_64"
;;
arm*|aarch64)
ARCH_SUFFIX="arm"
;;
*)
echo "Unsupported architecture: $ARCH"
exit 1
;;
esac

LIB_NAME="libyggdrasilffi_${ARCH_SUFFIX}.${EXT}"

rm -f "lib/$LIB_NAME"
cp "../target/release/libyggdrasilffi.${EXT}" "lib/$LIB_NAME"
## End copy the correct binary to the correct name

# Build the gem
gem build yggdrasil-engine.gemspec
25 changes: 20 additions & 5 deletions ruby-engine/lib/yggdrasil_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,38 @@
OK_RESPONSE = 'Ok'.freeze

def platform_specific_lib
case RbConfig::CONFIG['host_os']
os = RbConfig::CONFIG['host_os']
cpu = RbConfig::CONFIG['host_cpu']

extension = case os
when /darwin|mac os/
'libyggdrasilffi.dylib'
'dylib'
when /linux/
'libyggdrasilffi.so'
'so'
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
'libyggdrasilffi.dll'
'dll'
else
raise "unsupported platform #{RbConfig::CONFIG['host_os']}"
raise "unsupported platform #{os}"
end

arch_suffix = case cpu
when /x86_64/
'x86_64'
when /arm|aarch64/
'arm64'
else
raise "unsupported architecture #{cpu}"
end

"libyggdrasilffi_#{arch_suffix}.#{extension}"
end

def to_variant(raw_variant)
payload = raw_variant[:payload] && raw_variant[:payload].transform_keys(&:to_s)
{
name: raw_variant[:name],
enabled: raw_variant[:enabled],
feature_enabled: raw_variant[:featureEnabled],
payload: payload,
}
end
Expand Down
16 changes: 14 additions & 2 deletions ruby-engine/spec/yggdrasil_engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
index_file_path = '../client-specification/specifications/index.json'
test_suites = JSON.parse(File.read(index_file_path))

def test_suite_variant(base_variant)
payload = base_variant[:payload] && base_variant[:payload].transform_keys(&:to_s)
{
name: base_variant[:name],
enabled: base_variant[:enabled],
feature_enabled: base_variant[:feature_enabled],
payload: payload,
}
end

RSpec.describe YggdrasilEngine do
let(:yggdrasil_engine) { YggdrasilEngine.new }

Expand Down Expand Up @@ -119,18 +129,20 @@
describe "Variant Test '#{test[:description]}'" do
let(:context) { test[:context] }
let(:toggle_name) { test[:toggleName] }
let(:expected_result) { to_variant(test[:expectedResult]) }
let(:expected_result) { test_suite_variant(test[:expectedResult]) }

it 'returns correct result for `get_variant` method' do
result = yggdrasil_engine.get_variant(toggle_name, context) || {
:name => 'disabled',
:payload => nil,
:enabled => false
:enabled => false,
:feature_enabled => false
}

expect(result[:name]).to eq(expected_result[:name])
expect(result[:payload]).to eq(expected_result[:payload])
expect(result[:enabled]).to eq(expected_result[:enabled])
expect(result[:feature_enabled]).to eq(expected_result[:feature_enabled])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion ruby-engine/yggdrasil-engine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Gem::Specification.new do |s|
target_platform = -> { ENV['YGG_BUILD_PLATFORM'] || Gem::Platform::CURRENT }

s.name = 'yggdrasil-engine'
s.version = '0.0.5.beta.11'
s.version = '0.0.5.beta.19'
s.date = '2023-06-28'
s.summary = 'Unleash engine for evaluating feature toggles'
s.description = '...'
Expand Down
25 changes: 19 additions & 6 deletions unleash-yggdrasil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,12 @@ impl EngineState {
context: &Context,
external_values: &Option<HashMap<String, bool>>,
) -> Option<VariantDef> {
self.get_toggle(name).and_then(|toggle| {
self.get_toggle(name).map(|toggle| {
if self.enabled(toggle, context, external_values) {
self.check_variant_by_toggle(toggle, context)
.unwrap_or_default()
} else {
None
VariantDef::default()
}
})
}
Expand Down Expand Up @@ -650,6 +651,7 @@ impl VariantDef {
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExtendedVariantDef {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -673,11 +675,11 @@ mod test {
use serde::Deserialize;
use std::{collections::HashMap, fs};
use test_case::test_case;
use unleash_types::client_features::{ClientFeatures, FeatureDependency, Override};
use unleash_types::client_features::{ClientFeatures, FeatureDependency, Override, Payload};

use crate::{
check_for_variant_override, get_seed, CompiledToggle, CompiledVariant, Context,
EngineState, ExtendedVariantDef, VariantDef,
EngineState, VariantDef,
};

const SPEC_FOLDER: &str = "../client-specification/specifications";
Expand Down Expand Up @@ -705,7 +707,15 @@ mod test {
pub(crate) description: String,
pub(crate) context: Context,
pub(crate) toggle_name: String,
pub(crate) expected_result: ExtendedVariantDef,
pub(crate) expected_result: TestCaseVariantDef,
}

#[derive(Deserialize, Debug)]
pub struct TestCaseVariantDef {
pub name: String,
pub payload: Option<Payload>,
pub enabled: bool,
pub feature_enabled: bool,
}

fn load_spec(spec_name: &str) -> TestSuite {
Expand Down Expand Up @@ -763,7 +773,10 @@ mod test {
);
let expected = test_case.expected_result;
let actual = engine.get_variant(&test_case.toggle_name, &test_case.context, &None);
assert_eq!(expected, actual);
assert_eq!(expected.enabled, actual.enabled);
assert_eq!(expected.feature_enabled, actual.feature_enabled);
assert_eq!(expected.name, actual.name);
assert_eq!(expected.payload, actual.payload);
}
}
}
Expand Down
Loading

0 comments on commit f8310a7

Please sign in to comment.