Sha256: 30c729756fa5a7bddc5d509c4a4494e7e205f6fe5c9b40c016d50a22d3e49c1d

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

#!/usr/bin/env ruby

require "rubygems"
require "ecology"
require "multi_json"

if ARGV.size == 0
  $stderr.puts <<USAGE
Usage:  with_ecology [-simple] <ecology-name>.ecology[.erb] <binary> [<args>]

Use "-simple" to exclude ECOLOGY_property_LENGTH and _TYPE fields.
USAGE
  exit -1
end

@exclude_fields = false

ARGS = ["-simple"]

arg = ARGV.shift

while ARGS.include?(arg)
  if arg == "-simple"
    @exclude_fields = true
    arg = ARGV.shift
  end
end

arg = ARGV.shift if arg == "--"

Ecology.read(arg)

def export_prefix_key_value(prefix, key, value)
  env_var_name = "#{prefix}#{key}"

  type = case value
  when Fixnum
    "int"
  when String
    "string"
  when Float
    "float"
  when TrueClass
    "bool"
  when FalseClass
    "bool"
  when NilClass
    "null"
  else
    "unknown"
  end

  ENV[env_var_name] = value.to_s

  unless @exclude_fields
    ENV[env_var_name + "_TYPE"] = type
    ENV[env_var_name + "_LENGTH"] = value.to_s.length.to_s
  end
end

def export_properties(data, prefix)
  data.each do |key, value|
    if value.is_a?(Hash)
      export_properties(value, "#{prefix}#{key}_")
    elsif value.is_a?(Array)
      export_prefix_key_value prefix, key, MultiJSON.encode(value)
    else
      export_prefix_key_value prefix, key, value
    end
  end
end

export_properties(Ecology.data, "ECOLOGY_")

exec ARGV.join(" ")

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecology-0.0.18 bin/with_ecology