<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>GeneratePreferences.rb</title>
<style type="text/css">
.enscript-comment { font-style: italic; color: rgb(178,34,34); }
.enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
.enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
.enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
.enscript-reference { font-weight: bold; color: rgb(95,158,160); }
.enscript-string { font-weight: bold; color: rgb(188,143,143); }
.enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
.enscript-type { font-weight: bold; color: rgb(34,139,34); }
.enscript-highlight { text-decoration: underline; color: 0; }
</style>
</head>
<body id="top">
<h1 style="margin:8px;" id="f1">GeneratePreferences.rb&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="GeneratePreferences.rb">plain text</a>]</span></h1>
<hr/>
<div></div>
<pre>
#!/usr/bin/env ruby
#
# Copyright (c) 2017 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.

require &quot;fileutils&quot;
require 'erb'
require 'optparse'
require 'yaml'

options = {
  :input =&gt; nil,
  :outputDirectory =&gt; nil
}
optparse = OptionParser.new do |opts|
    opts.banner = &quot;Usage: #{File.basename($0)} --input file&quot;

    opts.separator &quot;&quot;

    opts.on(&quot;--input input&quot;, &quot;file to generate settings from&quot;) { |input| options[:input] = input }
    opts.on(&quot;--outputDir output&quot;, &quot;directory to generate file in&quot;) { |output| options[:outputDirectory] = output }
end

optparse.parse!

if !options[:input]
  puts optparse
  exit -1
end

if !options[:outputDirectory]
  options[:outputDirectory] = Dir.getwd
end

FileUtils.mkdir_p(options[:outputDirectory])

parsedPreferences = begin
  YAML.load_file(options[:input])
rescue ArgumentError =&gt; e
  puts &quot;Could not parse input file: #{e.message}&quot;
  exit(-1)
end

class Preference
  attr_accessor :name
  attr_accessor :type
  attr_accessor :defaultValue
  attr_accessor :humanReadableName
  attr_accessor :humanReadableDescription
  attr_accessor :category
  attr_accessor :webcoreBinding
  attr_accessor :condition
  attr_accessor :hidden

  def initialize(name, opts)
    @name = name
    @type = opts[&quot;type&quot;]
    @defaultValue = opts[&quot;defaultValue&quot;]
    @humanReadableName = '&quot;' + (opts[&quot;humanReadableName&quot;] || &quot;&quot;) + '&quot;'
    @humanReadableDescription = '&quot;' + (opts[&quot;humanReadableDescription&quot;] || &quot;&quot;) + '&quot;'
    @category = opts[&quot;category&quot;]
    @getter = opts[&quot;getter&quot;]
    @webcoreBinding = opts[&quot;webcoreBinding&quot;]
    @webcoreName = opts[&quot;webcoreName&quot;]
    @condition = opts[&quot;condition&quot;]
    @hidden = opts[&quot;hidden&quot;] || false
  end

  def nameLower
    if @getter
      @getter
    elsif @name.start_with?(&quot;VP&quot;)
      @name[0..1].downcase + @name[<a href="mailto:2..@name.leng">2..@name.leng</a>th]
    elsif @name.start_with?(&quot;CSS&quot;, &quot;XSS&quot;, &quot;FTP&quot;, &quot;DOM&quot;, &quot;DNS&quot;, &quot;PDF&quot;, &quot;ICE&quot;)
      @name[0..2].downcase + @name[<a href="mailto:3..@name.leng">3..@name.leng</a>th]
    elsif @name.start_with?(&quot;HTTP&quot;)
      @name[0..3].downcase + @name[<a href="mailto:4..@name.leng">4..@name.leng</a>th]
    else
      @name[0].downcase + @name[<a href="mailto:1..@name.leng">1..@name.leng</a>th]
    end
  end

  def webcoreNameUpper
    if @webcoreName
      @webcoreName[0].upcase + @webcoreName[<a href="mailto:1..@webcoreName.leng">1..@webcoreName.leng</a>th]
    else
      @name
    end
  end

  def typeUpper
    if @type == &quot;uint32_t&quot;
      &quot;UInt32&quot;
    else
      @type.capitalize
    end
  end

end

class Conditional
  attr_accessor :condition
  attr_accessor :preferences

  def initialize(condition, settings)
    @condition = condition
    @preferences = preferences
  end
end

class Preferences
  attr_accessor :preferences

  def initialize(hash, outputDirectory)
    @outputDirectory = outputDirectory

    @preferences = []
    hash.each do |name, options|
      @preferences &lt;&lt; Preference.new(name, options)
    end
    @preferences.sort! { |x, y| x.name &lt;=&gt; y.name }

    @preferencesNotDebug = @preferences.select { |p| !p.category }
    @preferencesDebug = @preferences.select { |p| p.category == &quot;debug&quot; }
    @experimentalFeatures = @preferences.select { |p| p.category == &quot;experimental&quot; }.sort! { |x, y| x.humanReadableName &lt;=&gt; y.humanReadableName }
    @internalDebugFeatures = @preferences.select { |p| p.category == &quot;internal&quot; }.sort! { |x, y| x.humanReadableName &lt;=&gt; y.humanReadableName }

    @preferencesBoundToSetting = @preferences.select { |p| !p.webcoreBinding }
    @preferencesBoundToDeprecatedGlobalSettings = @preferences.select { |p| p.webcoreBinding == &quot;DeprecatedGlobalSettings&quot; }
    @preferencesBoundToRuntimeEnabledFeatures = @preferences.select { |p| p.webcoreBinding == &quot;RuntimeEnabledFeatures&quot; }

    @warning = &quot;THIS FILE WAS AUTOMATICALLY GENERATED, DO NOT EDIT.&quot;
  end

  def renderTemplate(template)
    templateFile = File.join(File.dirname(__FILE__), &quot;PreferencesTemplates&quot;, template + &quot;.erb&quot;)

    output = ERB.new(File.read(templateFile), 0, &quot;-&quot;).result(binding)
    File.open(File.join(@outputDirectory, template), &quot;w+&quot;) do |f|
      f.write(output)
    end
  end
end

preferences = Preferences.new(parsedPreferences, options[:outputDirectory])
preferences.renderTemplate(&quot;WebPreferencesDefinitions.h&quot;)
preferences.renderTemplate(&quot;WebPageUpdatePreferences.cpp&quot;)
preferences.renderTemplate(&quot;WebPreferencesKeys.h&quot;)
preferences.renderTemplate(&quot;WebPreferencesKeys.cpp&quot;)
preferences.renderTemplate(&quot;WebPreferencesStoreDefaultsMap.cpp&quot;)
preferences.renderTemplate(&quot;WebPreferencesInternalDebugFeatures.cpp&quot;)
preferences.renderTemplate(&quot;WebPreferencesExperimentalFeatures.cpp&quot;)
</pre>
<hr />
</body></html>