summaryrefslogtreecommitdiffhomepage
path: root/bin/ruby2d
diff options
context:
space:
mode:
Diffstat (limited to 'bin/ruby2d')
-rwxr-xr-xbin/ruby2d240
1 files changed, 9 insertions, 231 deletions
diff --git a/bin/ruby2d b/bin/ruby2d
index 5ed6552..39a8325 100755
--- a/bin/ruby2d
+++ b/bin/ruby2d
@@ -1,247 +1,19 @@
#!/usr/bin/env ruby
+
require 'ruby2d/colorize'
require 'ruby2d/version'
-require 'fileutils'
-
-# The installed gem directory
-@gem_dir = "#{Gem::Specification.find_by_name('ruby2d').gem_dir}"
-
-# The Ruby 2D library files
-@lib_files = [
- 'renderable',
- 'exceptions',
- 'color',
- 'window',
- 'dsl',
- 'quad',
- 'line',
- 'circle',
- 'rectangle',
- 'square',
- 'triangle',
- 'image',
- 'sprite',
- 'font',
- 'text',
- 'sound',
- 'music'
-]
# Debugging command-line flag
@debug = false
-
-# Check if source file provided is good
-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
-
-
-# Assemble the Ruby 2D library in one `.rb` file
-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
-
- lib << "
-include Ruby2D
-extend Ruby2D::DSL\n"
-
- File.write('build/lib.rb', lib)
-end
-
-
-# Remove `require 'ruby2d'` from source file
-def strip_require(file)
- output = ''
- File.foreach(file) do |line|
- output << line unless line =~ /require ('|")ruby2d('|")/
- end
- return output
-end
-
-
-# Build a native version of the provided Ruby application
-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 a web-based version of the provided Ruby application
-def build_web(rb_file)
- puts "Warning: ".warn + "This feature is currently disabled while it's being upgraded."
-end
-
-
-# Build an iOS or tvOS app
-def build_apple(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 at `build/#{device}`"
-end
-
-
-# Clean up unneeded build files
-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/ios'
- FileUtils.rm_rf 'build/tvos'
- end
-end
-
-
-# Launch a native app
-def launch_native
- if !File.exist? 'build/app'
- puts "No native app built!"
- exit
- end
- `( cd build && ./app )`
-end
-
-
-# Launch a web app
-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
-
-
-# Launch an iOS or tvOS app in a simulator
-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 XR" &&
- 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
-
-
-# Check Command-line Arguments #################################################
+# Usage ########################################################################
usage = "Ruby 2D: Make cross-platform 2D applications in Ruby".bold + "\n
Usage: ruby2d <command> <options>
[-v|--version]
Summary of commands and options:
+ console Run script in an interactive console
build Build a Ruby source file
launch Launch a built Ruby 2D application
simulator Interact with iOS and tvOS simulators
@@ -297,8 +69,13 @@ Choose an option with the #{"simulator".bold} command:
--log <app> Stream log for the app only, given the app name
--log-errors Stream log containing only error messages\n\n"
+# Check Command-line Arguments #################################################
+
case ARGV[0]
+when 'console'
+ require 'ruby2d/cli/console'
when 'build'
+ require 'ruby2d/cli/build'
if ARGV.delete '--debug' then @debug = true end
case ARGV[1]
when '--native'
@@ -320,6 +97,7 @@ when 'build'
puts usage_build
end
when 'launch'
+ require 'ruby2d/cli/launch'
case ARGV[1]
when '--native'
launch_native