class Gamefic::Sdk::Tasks::Web
Tasks
for running and building web apps.
Public Instance Methods
Source
# File lib/gamefic-sdk/tasks/web.rb, line 30 def autoload require_relative File.join(absolute_path, 'main') if defined?(Gamefic::Autoload) require 'gamefic-autoload' puts 'Generating autoload.rb file for Opal' autoload_path = File.join(absolute_path, 'autoload.rb') autoload_file = <<~FILE # frozen_string_literal: true # This file is automatically generated by the Gamefic SDK. Opal uses it to build # the project in accordance with its Gamefic::Autoload configuration. #{Gamefic::Autoload.encode_all.join("\n")} FILE File.write(autoload_path, autoload_file) else puts 'Skipping autoload.rb (Gamefic::Autoload is not enabled)' end end
Generate the autoload.rb file for Opal builds.
Source
# File lib/gamefic-sdk/tasks/web.rb, line 62 def build check_for_web_build autoload Dir.chdir File.join(absolute_path, 'web') do system 'npm run build' end rescue Errno::ENOENT => e warn "#{e.class}: #{e.message}" warn 'Web app building requires Node (https://nodejs.org).' end
Build a distributable web app using NPM.
Source
# File lib/gamefic-sdk/tasks/web.rb, line 15 def generate(version = nil) version ||= '@latest' puts "Node version #{check_for_npm} detected. Preparing the web app..." web_path = File.join(absolute_path, 'web') FileUtils.mkdir_p web_path Dir.chdir web_path do system 'npx', '-y', "react-gamefic#{version}", '--name', File.basename(absolute_path), '--class', 'GAMEFIC_PLOT_CLASS', '--path', '../lib' end autoload puts 'The web app is ready.', 'Run `rake web:run` to start the app in dev mode.' end
Generate a web app using NPM.
Source
# File lib/gamefic-sdk/tasks/web.rb, line 52 def run check_for_web_build autoload Dir.chdir File.join(absolute_path, 'web') do system 'npm start' end end
Run the web app in a server.
Private Instance Methods
Source
# File lib/gamefic-sdk/tasks/web.rb, line 94 def check_for_npm `npm -v`.strip rescue Errno::ENOENT => e warn "#{e.class}: #{e.message}" warn 'Web app generation requires Node (https://nodejs.org).' raise end
Source
# File lib/gamefic-sdk/tasks/web.rb, line 86 def check_for_web_build return if web_build_exists? puts 'This project does not appear to be configured for web builds.' puts 'Try running `rake web:generate` first.' if Rake::Task.task_defined?('web:generate') raise LoadError, 'package.json not found' end
Check if a web build exists.
@raise [LoadError] if a web build is not available. @return [void]
Source
# File lib/gamefic-sdk/tasks/web.rb, line 78 def web_build_exists? File.exist?(File.join(absolute_path, 'web', 'package.json')) end
True if a web build has been generated for the current project.
@return [Boolean]