Top Level Namespace
- Extended by:
- Ruby2D::DSL
- Includes:
- Ruby2D
Defined Under Namespace
Modules: Ruby2D Classes: String
Constant Summary collapse
- S2D_VERSION =
Simple 2D minimum version required
'1.1.0'
Constants included from Ruby2D
Ruby2D::Colour, Ruby2D::VERSION
Instance Method Summary collapse
-
#add_flags(type, flags) ⇒ Object
Add compiler and linker flags.
-
#build_ios_tvos(rb_file, device) ⇒ Object
Build an iOS or tvOS app.
-
#build_macos(rb_file) ⇒ Object
Build an app bundle for macOS.
-
#build_native(rb_file) ⇒ Object
Build a native version of the provided Ruby application.
-
#build_web(rb_file) ⇒ Object
Build a web-based version of the provided Ruby application.
-
#check_build_src_file(rb_file) ⇒ Object
Check if source file provided is good.
-
#check_s2d ⇒ Object
Check that Simple 2D is installed and meets minimum version requirements.
-
#check_sdl_linux ⇒ Object
Check SDL libraries on Linux.
-
#clean_up(cmd = nil) ⇒ Object
Clean up unneeded build files.
-
#launch_apple(device) ⇒ Object
Launch an iOS or tvOS app in a simulator.
-
#launch_native ⇒ Object
Launch a native app.
-
#launch_web ⇒ Object
Launch a web app.
-
#make_lib ⇒ Object
Assemble the Ruby 2D library in one ‘.rb` file.
-
#print_errors ⇒ Object
Print installation errors.
-
#set_rpi_flags ⇒ Object
Set Raspberry Pi flags.
-
#strip_require(file) ⇒ Object
Remove ‘require ’ruby2d’‘ from source file.
-
#use_usr_libs ⇒ Object
Use the Simple 2D, SDL, and other libraries installed by the user (not those bundled with the gem).
Methods included from Ruby2D::DSL
clear, close, get, off, on, set, show, update
Methods included from Ruby2D
Instance Method Details
#add_flags(type, flags) ⇒ Object
Add compiler and linker flags
56 57 58 59 60 61 62 63 |
# File 'ext/ruby2d/extconf.rb', line 56 def add_flags(type, flags) case type when :c $CFLAGS << " #{flags} " when :ld $LDFLAGS << " #{flags} " end end |
#build_ios_tvos(rb_file, device) ⇒ Object
Build an iOS or tvOS app
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ruby2d/cli/build.rb', line 162 def build_ios_tvos(rb_file, device) check_build_src_file(rb_file) # Check for Simple 2D framework, unless File.exist?('/usr/local/Frameworks/Simple2D/iOS/Simple2D.framework') && File.exist?('/usr/local/Frameworks/Simple2D/tvOS/Simple2D.framework') puts "#{'Error:'.error} Simple 2D iOS and tvOS frameworks not found. Install them and try again.\n" exit end # Check if MRuby exists; if not, quit if `which mruby`.empty? puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n" exit end # Add debugging information to produce backtrace if @debug then debug_flag = '-g' end # Assemble the Ruby 2D library in one `.rb` file and compile to bytecode make_lib `mrbc #{debug_flag} -Bruby2d_lib -obuild/lib.c build/lib.rb` # Read the provided Ruby source file, copy to build dir and compile to bytecode File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) } `mrbc #{debug_flag} -Bruby2d_app -obuild/src.c build/src.rb` # Copy over iOS project FileUtils.cp_r "#{@gem_dir}/assets/#{device}", "build" # Combine contents of C source files and bytecode into one file File.open("build/#{device}/main.c", 'w') do |f| f << "#define RUBY2D_IOS_TVOS 1" << "\n\n" f << "#define MRUBY 1" << "\n\n" f << File.read("build/lib.c") << "\n\n" f << File.read("build/src.c") << "\n\n" f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c") end # Build the Xcode project `simple2d build --#{device} build/#{device}/MyApp.xcodeproj` # Clean up clean_up unless @debug # Success! puts "App created: `build/#{device}`" end |
#build_macos(rb_file) ⇒ Object
Build an app bundle for macOS
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ruby2d/cli/build.rb', line 116 def build_macos(rb_file) # Build native app for macOS build_native(rb_file) # Property list source for the bundle info_plist = %( <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleExecutable</key> <string>app</string> <key>CFBundleIconFile</key> <string>app.icns</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleVersion</key> <string>1</string> <key>NSHighResolutionCapable</key> <string>True</string> </dict> </plist> ) # Create directories FileUtils.mkpath 'build/App.app/Contents/MacOS' FileUtils.mkpath 'build/App.app/Contents/Resources' # Create Info.plist and copy over assets File.open('build/App.app/Contents/Info.plist', 'w') { |f| f.write(info_plist) } FileUtils.cp 'build/app', 'build/App.app/Contents/MacOS/' # Consider using an icon: # FileUtils.cp "#{@gem_dir}/assets/app.icns", 'build/App.app/Contents/Resources' # Clean up FileUtils.rm_f 'build/app' unless @debug # Success! puts 'macOS app bundle created: `build/App.app`' end |
#build_native(rb_file) ⇒ Object
Build a native version of the provided Ruby application
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby2d/cli/build.rb', line 70 def build_native(rb_file) check_build_src_file(rb_file) # Check if MRuby exists; if not, quit if `which mruby`.empty? puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n" exit end # Add debugging information to produce backtrace if @debug then debug_flag = '-g' end # Assemble the Ruby 2D library in one `.rb` file and compile to bytecode make_lib `mrbc #{debug_flag} -Bruby2d_lib -obuild/lib.c build/lib.rb` # Read the provided Ruby source file, copy to build dir and compile to bytecode File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) } `mrbc #{debug_flag} -Bruby2d_app -obuild/src.c build/src.rb` # Combine contents of C source files and bytecode into one file open('build/app.c', 'w') do |f| f << "#define MRUBY 1" << "\n\n" f << File.read("build/lib.c") << "\n\n" f << File.read("build/src.c") << "\n\n" f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c") end # Compile to a native executable `cc build/app.c -lmruby \`simple2d --libs\` -o build/app` # Clean up clean_up unless @debug # Success! puts "Native app created at `build/app`" end |
#build_web(rb_file) ⇒ Object
Build a web-based version of the provided Ruby application
110 111 112 |
# File 'lib/ruby2d/cli/build.rb', line 110 def build_web(rb_file) puts "Warning: ".warn + "This feature is currently disabled while it's being upgraded." end |
#check_build_src_file(rb_file) ⇒ Object
Check if source file provided is good
33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby2d/cli/build.rb', line 33 def check_build_src_file(rb_file) if !rb_file puts "Please provide a Ruby file to build" exit elsif !File.exist? rb_file puts "Can't find file: #{rb_file}" exit end end |
#check_s2d ⇒ Object
Check that Simple 2D is installed and meets minimum version requirements
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'ext/ruby2d/extconf.rb', line 35 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".bold}" print_errors; exit # 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 |
#check_sdl_linux ⇒ Object
Check SDL libraries on Linux
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'ext/ruby2d/extconf.rb', line 67 def check_sdl_linux unless have_library('SDL2') && have_library('SDL2_image') && have_library('SDL2_mixer') && have_library('SDL2_ttf') $errors << "Couldn't find packages needed by Ruby 2D." # Fedora and CentOS if system('which yum') $errors << "Install the following packages using `yum` (or `dnf`) and try again:\n" << " SDL2-devel SDL2_image-devel SDL2_mixer-devel SDL2_ttf-devel".bold # Arch elsif system('which pacman') $errors << "Install the following packages using `pacman` and try again:\n" << " sdl2 sdl2_image sdl2_mixer sdl2_ttf".bold # openSUSE elsif system('which zypper') $errors << "Install the following packages using `zypper` and try again:\n" << " libSDL2-devel libSDL2_image-devel libSDL2_mixer-devel libSDL2_ttf-devel".bold # Ubuntu, Debian, and Mint # `apt` must be last because openSUSE has it aliased to `zypper` elsif system('which apt') $errors << "Install the following packages using `apt` and try again:\n" << " libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev".bold end $errors << "" << "See #{"ruby2d.com".bold} for additional help." print_errors; exit end end |
#clean_up(cmd = nil) ⇒ Object
Clean up unneeded build files
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/ruby2d/cli/build.rb', line 212 def clean_up(cmd = nil) FileUtils.rm( Dir.glob('build/{src,lib}.{rb,c,js}') + Dir.glob('build/app.c') ) if cmd == :all puts "cleaning up..." FileUtils.rm_f 'build/app' FileUtils.rm_f 'build/app.js' FileUtils.rm_f 'build/app.html' FileUtils.rm_rf 'build/App.app' FileUtils.rm_rf 'build/ios' FileUtils.rm_rf 'build/tvos' end end |
#launch_apple(device) ⇒ Object
Launch an iOS or tvOS app in a simulator
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby2d/cli/launch.rb', line 31 def launch_apple(device) case device when 'ios' if !File.exist? 'build/ios/build/Release-iphonesimulator/MyApp.app' puts "No iOS app built!" exit end puts `simple2d simulator --open "iPhone X" && simple2d simulator --install "build/ios/build/Release-iphonesimulator/MyApp.app" && simple2d simulator --launch "Ruby2D.MyApp"` when 'tvos' if !File.exist? 'build/tvos/build/Release-appletvsimulator/MyApp.app' puts "No tvOS app built!" exit end puts `simple2d simulator --open "Apple TV 4K" && simple2d simulator --install "build/tvos/build/Release-appletvsimulator/MyApp.app" && simple2d simulator --launch "Ruby2D.MyApp"` end end |
#launch_native ⇒ Object
Launch a native app
4 5 6 7 8 9 10 |
# File 'lib/ruby2d/cli/launch.rb', line 4 def launch_native if !File.exist? 'build/app' puts "No native app built!" exit end `( cd build && ./app )` end |
#launch_web ⇒ Object
Launch a web app
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby2d/cli/launch.rb', line 14 def launch_web if !File.exist? 'build/app.html' puts "No web app built!" exit end open_cmd = 'open' case RUBY_PLATFORM when /linux/ open_cmd = "xdg-#{open_cmd}" when /mingw/ open_cmd = "start" end system "#{open_cmd} build/app.html" end |
#make_lib ⇒ Object
Assemble the Ruby 2D library in one ‘.rb` file
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby2d/cli/build.rb', line 45 def make_lib FileUtils.mkdir_p 'build' lib_dir = "#{@gem_dir}/lib/ruby2d/" lib = '' @lib_files.each do |f| lib << File.read("#{lib_dir + f}.rb") + "\n\n" end File.write('build/lib.rb', lib) end |
#print_errors ⇒ Object
Print installation errors
26 27 28 29 30 31 |
# File 'ext/ruby2d/extconf.rb', line 26 def print_errors puts " #{"== #{"Ruby 2D Installation Errors".error} =======================================\n"} #{$errors.join("\n ")}\n #{"======================================================================"}" end |
#set_rpi_flags ⇒ Object
Set Raspberry Pi flags
101 102 103 104 105 106 |
# File 'ext/ruby2d/extconf.rb', line 101 def set_rpi_flags if $platform == :linux_rpi add_flags(:c, '-I/opt/vc/include') add_flags(:ld, '-L/opt/vc/lib -lbrcmGLESv2') end end |
#strip_require(file) ⇒ Object
Remove ‘require ’ruby2d’‘ from source file
60 61 62 63 64 65 66 |
# File 'lib/ruby2d/cli/build.rb', line 60 def strip_require(file) output = '' File.foreach(file) do |line| output << line unless line =~ /require ('|")ruby2d('|")/ end return output end |
#use_usr_libs ⇒ Object
Use the Simple 2D, SDL, and other libraries installed by the user (not those bundled with the gem)
110 111 112 113 114 115 116 117 |
# File 'ext/ruby2d/extconf.rb', line 110 def use_usr_libs check_s2d # Add flags set_rpi_flags add_flags(:c, '-I/usr/local/include') add_flags(:ld, `bash simple2d --libs`) end |