Skip to content

Commit

Permalink
Bundle dependencies with the gem
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktm committed Apr 7, 2019
1 parent 7d1a4de commit 385ab54
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 83 deletions.
9 changes: 1 addition & 8 deletions .travis.yml
@@ -1,12 +1,5 @@
os: osx
osx_image: xcode10.1
osx_image: xcode10.2
language: ruby
before_install:
- brew update
- brew tap simple2d/tap
- brew install simple2d mruby
script:
- rake
- ruby2d build --native test/ci-build.rb
- ./build/app
# - ruby2d build --web test/ci-build.rb
7 changes: 0 additions & 7 deletions Rakefile
Expand Up @@ -2,13 +2,6 @@ require 'rspec/core/rake_task'
require_relative 'lib/ruby2d/colorize'
require_relative 'lib/ruby2d/version'

# Simple 2D is required for these tasks
if `which simple2d`.empty?
puts "Simple 2D not found!".error
puts "Install before running Rake tasks."
exit
end

def get_args
ARGV.each { |a| task a.to_sym do ; end }
end
Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Expand Up @@ -2,8 +2,6 @@ install:
- set PATH=C:\ruby25-x64\bin;C:\msys64\usr\bin;C:\msys64\usr\local\bin;%PATH%
- set MSYSTEM=MINGW64
- bash --version
- wget https://raw.githubusercontent.com/simple2d/simple2d/master/bin/simple2d.sh
- bash simple2d.sh install -y
- git submodule update --init --recursive
build: off
before_test:
Expand Down
2 changes: 1 addition & 1 deletion assets
Submodule assets updated 184 files
109 changes: 59 additions & 50 deletions ext/ruby2d/extconf.rb
Expand Up @@ -2,78 +2,87 @@
require_relative '../../lib/ruby2d/colorize'

S2D_VERSION = '1.1.0' # Simple 2D minimum version required
$errors = []
$errors = [] # Array to capture errors

# Print installation errors
def print_errors
puts "
#{"== #{"Ruby 2D Installation Errors".error} =======================================\n"}
#{$errors.join("\n ")}\n
#{"======================================================================="}"
#{"======================================================================"}"
end

def check_s2d_version
unless Gem::Version.new(`bash simple2d --version`) >= Gem::Version.new(S2D_VERSION)
$errors << "Simple 2D needs to be updated for this version of Ruby 2D." <<
"Run the following, then try reinstalling this gem:\n" <<
" simple2d update".bold
print_errors
exit
end
end


# Install Simple 2D on supported platforms
case RUBY_PLATFORM

# macOS
when /darwin/
# Check that Simple 2D is installed and meets minimum version requirements
def check_s2d

# Simple 2D not installed
if `which simple2d`.empty?
$errors << "Ruby 2D uses a native library called Simple 2D, which was not found." <<
"To install, follow the instructions at #{"ruby2d.com/learn".bold}"
print_errors
exit

# Homebrew not installed
if `which brew`.empty?
$errors << "Ruby 2D uses a native library called Simple 2D, which was not found." <<
"On macOS, it can be installed using Homebrew.\n" <<
"First, install #{"Homebrew".bold}. See instructions at #{"http://brew.sh".bold}" <<
"Then, run the following:\n" <<
" brew tap simple2d/tap".bold <<
" brew install simple2d".bold
print_errors
exit

# Homebrew installed, instruct to install Simple 2D
else
$errors << "Ruby 2D uses a native library called Simple 2D, which was not found." <<
"Install it with Homebrew using:\n" <<
" brew tap simple2d/tap".bold <<
" brew install simple2d".bold
# Simple 2D installed, checking version
else
unless Gem::Version.new(`bash simple2d --version`) >= Gem::Version.new(S2D_VERSION)
$errors << "Simple 2D needs to be updated for this version of Ruby 2D." <<
"Run the following, then try reinstalling this gem:\n" <<
" simple2d update".bold
print_errors
exit
end
end
end

# Linux and Windows / MinGW
when /linux|mingw/
# Use the Simple 2D, SDL, and other libraries installed by the user (not those bundled with the gem)
def use_usr_libs
check_s2d

# Simple 2D not installed
if `which simple2d`.empty?
$errors << "Ruby 2D uses a native library called Simple 2D, which was not found." <<
"To install, follow the instructions at #{"ruby2d.com/learn".bold}"
print_errors
exit
# Add flags
$CFLAGS << ' -std=c11 -I/usr/local/include'
if `cat /etc/os-release` =~ /raspbian/ # Raspberry Pi
$CFLAGS << ' -I/opt/vc/include'
end
$LDFLAGS << ' ' << `bash simple2d --libs`
$LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they cause problems
end


check_s2d_version
# Build Ruby 2D native extention using libraries installed by user
# To use install flag: `gem install ruby2d -- libs`
if ARGV.include? 'libs'
use_usr_libs

# Add flags
$CFLAGS << ' -std=c11 -I/usr/local/include'
if `cat /etc/os-release` =~ /raspbian/ # Raspberry Pi
$CFLAGS << ' -I/opt/vc/include'
# Use libraries provided by the gem (default)
else
$CFLAGS << ' -std=c11 -I../../assets/include'
case RUBY_PLATFORM

# macOS
when /darwin/
# $LDFLAGS << " -L../../assets/macos/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -ljpeg -lpng16 -ltiff -lwebp -lmpg123 -logg -lflac -lvorbis -lvorbisfile -lfreetype -Wl,-framework,Cocoa -Wl,-framework,ForceFeedback"

ldir = "#{Dir.pwd}/../../assets/macos/lib"
$LDFLAGS << " #{ldir}/libsimple2d.a #{ldir}/libSDL2.a #{ldir}/libSDL2_image.a #{ldir}/libSDL2_mixer.a #{ldir}/libSDL2_ttf.a \
#{ldir}/libjpeg.a #{ldir}/libpng16.a #{ldir}/libtiff.a #{ldir}/libwebp.a \
#{ldir}/libmpg123.a #{ldir}/libogg.a #{ldir}/libflac.a #{ldir}/libvorbis.a #{ldir}/libvorbisfile.a \
#{ldir}/libfreetype.a -Wl,-framework,Cocoa -Wl,-framework,ForceFeedback"

# Linux
when /linux/
# TODO: Implement static compilation for Linux
use_usr_libs

# Windows / MinGW
when /mingw/
$LDFLAGS << " -L../../assets/mingw/lib -lsimple2d -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lmingw32 -lopengl32 -lglew32"

# If can't detect the platform, use libraries installed by the user
else
use_usr_libs
end
end
$LDFLAGS << ' ' << `bash simple2d --libs`
$LDFLAGS.gsub!(/\n/, ' ') # remove newlines in flags, they cause problems


# Create the Makefile
create_makefile('ruby2d/ruby2d')
8 changes: 1 addition & 7 deletions lib/ruby2d.rb
Expand Up @@ -21,13 +21,7 @@
require 'ruby2d/music'

if RUBY_PLATFORM =~ /mingw/
# When using the Windows CI AppVeyor
if ENV['APPVEYOR']
s2d_dll_path = 'C:\msys64\usr\local\bin'
# When in a standard MinGW shell
else
s2d_dll_path = '~/../../usr/local/bin'
end
s2d_dll_path = Gem::Specification.find_by_name('ruby2d').gem_dir + '/assets/mingw/bin'
RubyInstaller::Runtime.add_dll_directory(File.expand_path(s2d_dll_path))
end

Expand Down
39 changes: 32 additions & 7 deletions lib/ruby2d/font.rb
Expand Up @@ -17,14 +17,22 @@ def path(font_name)

# Get all fonts with full file paths
def all_paths
fonts = `find #{directory} -name *.ttf`.split("\n")
# MRuby does not have `Dir` defined
if RUBY_ENGINE == 'mruby'
fonts = `find #{directory} -name *.ttf`.split("\n")
# If MRI and/or non-Bash shell (like cmd.exe)
else
fonts = Dir["#{directory}/**/*.ttf"]
end

fonts = fonts.reject do |f|
f.downcase.include?('bold') ||
f.downcase.include?('italic') ||
f.downcase.include?('oblique') ||
f.downcase.include?('narrow') ||
f.downcase.include?('black')
end

fonts.sort_by { |f| f.downcase.chomp '.ttf' }
end

Expand All @@ -39,12 +47,29 @@ def default

# Get the fonts directory for the current platform
def directory
if `uname`.include? 'Darwin' # macOS
'/Library/Fonts'
elsif `uname`.include? 'Linux'
'/usr/share/fonts'
elsif `uname`.include? 'MINGW'
'C:/Windows/Fonts'
macos_font_path = '/Library/Fonts'
linux_font_path = '/usr/share/fonts'
windows_font_path = 'C:/Windows/Fonts'

# If MRI and/or non-Bash shell (like cmd.exe)
if Object.const_defined? :RUBY_PLATFORM
case RUBY_PLATFORM
when /darwin/ # macOS
macos_font_path
when /linux/
linux_font_path
when /mingw/
windows_font_path
end
# If MRuby
else
if `uname`.include? 'Darwin' # macOS
macos_font_path
elsif `uname`.include? 'Linux'
linux_font_path
elsif `uname`.include? 'MINGW'
windows_font_path
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/image_spec.rb
Expand Up @@ -3,6 +3,12 @@
RSpec.describe Ruby2D::Image do

describe "#new" do
it "creates images in various formats" do
Image.new('test/media/image.bmp')
Image.new('test/media/image.jpg')
Image.new('test/media/image.png')
end

it "raises exception if image file doesn't exist" do
expect { Image.new('bad_image.png') }.to raise_error(Ruby2D::Error)
end
Expand Down
7 changes: 7 additions & 0 deletions test/music_spec.rb
Expand Up @@ -14,6 +14,13 @@
end

unless ENV['CI']
it "creates music in various formats" do
Music.new('test/media/music.wav')
Music.new('test/media/music.mp3')
Music.new('test/media/music.ogg')
Music.new('test/media/music.flac')
end

it "creates music with options" do
mus = Music.new('test/media/music.mp3', loop: true)
expect(mus.path).to eq('test/media/music.mp3')
Expand Down
9 changes: 8 additions & 1 deletion test/sound_spec.rb
Expand Up @@ -8,7 +8,14 @@
end

unless ENV['CI'] # audio cannot be opened on CI; see `music_spec.rb`
it "creates sound" do
it "creates sound in various formats" do
Sound.new('test/media/music.wav')
Sound.new('test/media/music.mp3')
Sound.new('test/media/music.ogg')
Sound.new('test/media/music.flac')
end

it "creates sound and sets the media path" do
snd = Sound.new('test/media/sound.wav')
expect(snd.path).to eq('test/media/sound.wav')
end
Expand Down

0 comments on commit 385ab54

Please sign in to comment.