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

Methods included from Ruby2D::DSL

clear, close, get, off, on, set, show, update

Methods included from Ruby2D

assets, assets=

Instance Method Details

#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_versionObject



14
15
16
17
18
19
20
21
22
# File 'ext/ruby2d/extconf.rb', line 14

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

#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 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

#launch_nativeObject

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_webObject

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_libObject

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


7
8
9
10
11
12
# File 'ext/ruby2d/extconf.rb', line 7

def print_errors
  puts "
#{"== #{"Ruby 2D Installation Errors".error} =======================================\n"}
  #{$errors.join("\n  ")}\n
#{"======================================================================="}"
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