class Nova::Project

A Nova project, containing the galaxy and configuration settings for that project.

Constants

DEFAULT_PATHS

The default paths to load from.

Attributes

directory[R]

The directory the project is based in.

@return [Directory]

load_paths[R]

The load paths for this project.

@return [Array<String>]

options[R]

The options that were loaded from the config for this project.

@return [Hash]

Public Class Methods

new(dir, load_config = true) click to toggle source

Initializes the project. Loads the configuration file by default.

@param dir [String] the path to the directory for the project. @param load_config [Boolean] whether or not to load the

configuration file.
# File lib/nova/project.rb, line 42
def initialize(dir, load_config = true)
  @directory = Dir.new(dir)
  @load_paths = DEFAULT_PATHS.dup
  @options   = {}

  if load_config
    load_config!
  end
end
valid?(dir) click to toggle source

Whether or not the given directory is deemable as a Nova project.

@param dir [String] the directory to test. @return [Boolean]

# File lib/nova/project.rb, line 17
def self.valid?(dir)
  Dir.new(dir).each.include?("nova.yml")
end

Public Instance Methods

load_config!() click to toggle source

Loads the configuration file.

@return [Hash] the data.

# File lib/nova/project.rb, line 56
def load_config!
  return unless options.empty?

  data = ::YAML.load_file(File.open("#{directory.path}/nova.yml", "r"))
  load_paths.push(*data.fetch("load_paths", []))

  load_paths.map! do |path|
    File.absolute_path(path, directory.path)
  end

  @options = data
end
require_files() click to toggle source

Requires all of the star files that is in the project.

@return [void]

# File lib/nova/project.rb, line 72
def require_files
  @load_paths.each do |path|
    Dir["#{path}/**/*"].each do |f|
      require f
    end
  end
end
run_servers(do_fork = true, which = []) click to toggle source

Runs the servers defined in the options.

@note If do_fork is false, only the first server in the config

file will actually be created.

@param do_fork [Boolean] whether or not to actually fork the

process when creating servers.

@param which [Array<String>] which servers to run. Defaults to

all of them.

@return [void]

# File lib/nova/project.rb, line 89
def run_servers(do_fork = true, which = [])
  each_server(which) do |server, name|
    puts name

    if File.exists?(server[:files][:pid])
      Nova.logger.warn {
        "PID file #{server[:files][:pid]} already exists. " +
        "Ignoring server definition."
      }
      next
    end

    if do_fork
      process_id = fork
    end

    if process_id
      File.open(server[:files][:pid], "w") { |f| f.write process_id }
      Process.detach(process_id)
    else
      return build_server(server, do_fork)
    end
  end
end
shoot(which = []) click to toggle source

Takes down running servers.

@param which [Array<String>] which servers to take down.

Defaults to all of them.

@return [void]

# File lib/nova/project.rb, line 119
def shoot(which = [])
  each_server do |server, name|
    if File.exists?(server[:files][:pid])
      pid = File.open(server[:files][:pid], "r") { |f| f.read }.to_i

      print "Sending INT to #{pid}... "

      Process.kill :INT, pid rescue Errno::ESRCH

      File.delete(server[:files][:pid]) rescue Errno::ENOENT

      puts "OK!"
    end
  end
end

Private Instance Methods

build_server(server, redirect = true) click to toggle source

Creates a server, with the given server options.

@param server [Hash<Symbol, Object>] the server information to

base the server instance off of.

@param redirect [Boolean] whether or not to redirect the

{Nova.logger} and the stdin, stdout, and stderr for the
server.

@return [void]

# File lib/nova/project.rb, line 175
def build_server(server, redirect = true)
  if redirect
    Nova.logger = Logger.new(server[:files][:log], 10, 1_024_000)
    $stdin = $stdout = $stderr = File.open("/dev/null", "a")
  end

  begin
    s = Nova::Starbound::Server.new(server)
    s.read_client_file server[:files][:client]

    trap :INT do
      s.shutdown
      File.delete(server[:files][:pid]) rescue Errno::ENOENT
    end

    s.listen
  rescue => e
    Nova.logger.fatal { "#{e}: #{e.message} => #{e.backtrace[0]}" }
    File.delete(server[:files][:pid]) rescue Errno::ENOENT
    exit
  end
end
each_server(only = []) { |srv, srv_name, i| ... } click to toggle source

Loops over all of the defined servers, yielding the server definition and the index for that server.

@yieldparam server [Hash<Symbol, Object>] the server data @yieldparam server_name [String] the name of the server. @yieldparam index [Numeric] the index of the server in the

definition file.

@return [void]

# File lib/nova/project.rb, line 145
def each_server(only = [])
  server_list = [options["servers"], options["server"]].flatten.compact

  server_list.each_with_index do |srv, i|
    srv_name = srv.fetch(:name, "server#{i}")
    srv[:files] ||= {}

    files = {}
    {:log => :log, :pid => :pid, :client => :rb}.each do |f, n|
      files[f] = File.absolute_path(
        srv[:files].fetch(f, "./#{srv_name}.#{n}"),
        directory.path
      )
    end
    srv[:files] = files

    next unless only.empty? or only.include?(srv_name)

    yield srv, srv_name, i
  end
end