Sha256: b5f9327993c20fd9c356a2c5da8e2ebb3988753aab43ddc2f1b3fc0167a9fd0c

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

require 'polyglot'
require 'treetop'

require 'fig/grammar'
require 'fig/logging'
require 'fig/packageerror'
require 'fig/repository'
require 'fig/urlaccesserror'
require 'fig/userinputerror'

module Fig
  # Parses configuration files and deals with a few restrictions on them.
  class Parser
    def initialize(application_config)
      @parser = FigParser.new
      @application_config = application_config
    end

    def find_bad_urls(package, package_name, version_name)
      bad_urls = []
      package.walk_statements do |statement|
        statement.urls.each do |url|
          # collect all bad urls in bad_urls
          next if not Repository.is_url?(url)
          bad_urls << url if not @application_config.url_access_allowed?(url)
        end
      end

      raise URLAccessError.new(bad_urls, package_name, version_name) if not bad_urls.empty?
    end

    def find_multiple_command_statements(package)
      command_processed = false
      package.walk_statements do |statement|
        if statement.is_a?(Statement::Command)
          if command_processed == true
            raise UserInputError.new("Multiple command statements cannot be processed.")
          end
          command_processed = true
        elsif statement.is_a?(Statement::Configuration)
          command_processed = false
        end
      end
    end

    def parse_package(package_name, version_name, directory, input)
      input = input.gsub(/#.*$/, '')
      result = @parser.parse(" #{input} ")

      if result.nil?
        Logging.fatal "#{directory}: #{@parser.failure_reason}"
        raise PackageError.new("#{directory}: #{@parser.failure_reason}")
      end

      package = result.to_package(package_name, version_name, directory)

      find_bad_urls(package, package_name, version_name)
      find_multiple_command_statements(package)

      return package
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fig-0.1.57 lib/fig/parser.rb
fig-0.1.55 lib/fig/parser.rb
fig-0.1.54 lib/fig/parser.rb
fig-0.1.53 lib/fig/parser.rb
fig-0.1.52 lib/fig/parser.rb