Class: Gamefic::Sdk::Tasks::Web

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/gamefic-sdk/tasks/web.rb

Overview

Tasks for running and building web apps.

Instance Attribute Summary

Attributes included from Common

#directory

Instance Method Summary collapse

Methods included from Common

#absolute_path, #initialize, #plot_class, #string_to_constant

Instance Method Details

#autoloadObject

Generate the autoload.rb file for Opal builds.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#buildObject

Build a distributable web app using NPM.



62
63
64
65
66
67
68
69
70
71
# 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

#generate(version = nil) ⇒ Object

Generate a web app using NPM.



15
16
17
18
19
20
21
22
23
24
25
26
# 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

#runObject

Run the web app in a server.



52
53
54
55
56
57
58
# 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