Sha256: f9dd851960c39e11e15a75adeb0c6f47622c164f5a7638d5ed7f5875715a71eb

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

require "thor"

module Expressir
  class Cli < Thor
    desc "format PATH", "pretty print EXPRESS schema located at PATH"
    def format(path)
      repository = Expressir::Express::Parser.from_file(path)
      repository.schemas.each do |schema|
        puts "\n(* Expressir formatted schema: #{schema.id} *)\n"
        puts schema.to_s(no_remarks: true)
      end
    end

    no_commands do
      def _validate_schema(path)
        repository = Expressir::Express::Parser.from_file(path)
        repository.schemas.inject([]) do |acc, schema|
          acc << schema.id unless schema.version&.value
          acc
        end
      rescue StandardError
        nil
      end

      def _print_validation_errors(type, array)
        return if array.empty?

        puts "#{'*' * 20} RESULTS: #{type.to_s.upcase.gsub('_', ' ')} #{'*' * 20}"
        array.each do |msg|
          puts msg
        end
      end
    end

    desc "validate *PATH", "validate EXPRESS schema located at PATH"
    def validate(*paths) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
      no_version = no_valid = []

      paths.each do |path|
        x = Pathname.new(path).realpath.relative_path_from(Dir.pwd)
        puts "Validating #{x}"
        ret = _validate_schema(path)

        if ret.nil?
          no_valid << "Failed to parse: #{x}"
          next
        end

        ret.each do |schema_id|
          no_version << "Missing version string: schema `#{schema_id}` | #{x}"
        end
      end

      _print_validation_errors(:failed_to_parse, no_valid)
      _print_validation_errors(:missing_version_string, no_version)

      exit 1 unless [no_valid, no_version].all?(&:empty?)

      puts "Validation passed for all EXPRESS schemas."
    end

    desc "version", "Expressir Version"
    def version
      say(Expressir::VERSION)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
expressir-1.4.2 lib/expressir/cli.rb
expressir-1.4.2-x86_64-linux-musl lib/expressir/cli.rb
expressir-1.4.2-x86_64-linux-gnu lib/expressir/cli.rb
expressir-1.4.2-x86_64-darwin lib/expressir/cli.rb
expressir-1.4.2-x64-mingw32 lib/expressir/cli.rb
expressir-1.4.2-x64-mingw-ucrt lib/expressir/cli.rb
expressir-1.4.2-arm64-darwin lib/expressir/cli.rb
expressir-1.4.2-aarch64-linux-musl lib/expressir/cli.rb
expressir-1.4.2-aarch64-linux-gnu lib/expressir/cli.rb