Sha256: b8398b0dda2bfacfc105aae4f5970af55217b135ab661983e8f2dd8347865b15

Contents?: true

Size: 1.87 KB

Versions: 35

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true
require "json"
require "pathname"
require "uri"

module Licensed
  module Sources
    class Swift < Source
      def enabled?
        return unless Licensed::Shell.tool_available?("swift") && swift_package?
        File.exist?(package_resolved_file_path)
      end

      def enumerate_dependencies
        pins.map { |pin|
          name = pin["package"]
          version = pin.dig("state", "version")
          path = dependency_path_for_url(pin["repositoryURL"])
          error = "Unable to determine project path from #{url}" unless path

          Dependency.new(
            name: name,
            path: path,
            version: version,
            errors: Array(error),
            metadata: {
              "type"      => Swift.type,
              "homepage"  => homepage_for_url(pin["repositoryURL"])
            }
          )
        }
      end

      private

      def pins
        return @pins if defined?(@pins)

        @pins = begin
          json = JSON.parse(File.read(package_resolved_file_path))
          json.dig("object", "pins")
        rescue => e
          message = "Licensed was unable to read the Package.resolved file. Error: #{e.message}"
          raise Licensed::Sources::Source::Error, message
        end
      end

      def dependency_path_for_url(url)
        last_path_component = URI(url).path.split("/").last.sub(/\.git$/, "")
        File.join(config.pwd, ".build", "checkouts", last_path_component)
      rescue URI::InvalidURIError
      end

      def homepage_for_url(url)
        return unless %w{http https}.include?(URI(url).scheme)
        url.sub(/\.git$/, "")
      rescue URI::InvalidURIError
      end

      def package_resolved_file_path
        File.join(config.pwd, "Package.resolved")
      end

      def swift_package?
        Licensed::Shell.success?("swift", "package", "describe")
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
licensed-5.0.0 lib/licensed/sources/swift.rb
licensed-4.5.0 lib/licensed/sources/swift.rb
licensed-4.4.0 lib/licensed/sources/swift.rb
licensed-4.3.1 lib/licensed/sources/swift.rb
licensed-4.3.0 lib/licensed/sources/swift.rb
licensed-4.2.0 lib/licensed/sources/swift.rb
licensed-4.1.0 lib/licensed/sources/swift.rb
licensed-4.0.4 lib/licensed/sources/swift.rb
licensed-4.0.3 lib/licensed/sources/swift.rb
licensed-4.0.2 lib/licensed/sources/swift.rb
licensed-4.0.1 lib/licensed/sources/swift.rb
licensed-4.0.0 lib/licensed/sources/swift.rb
licensed-3.9.1 lib/licensed/sources/swift.rb
licensed-3.9.0 lib/licensed/sources/swift.rb
licensed-3.8.0 lib/licensed/sources/swift.rb
licensed-3.7.5 lib/licensed/sources/swift.rb
licensed-3.7.4 lib/licensed/sources/swift.rb
licensed-3.7.3 lib/licensed/sources/swift.rb
licensed-3.7.2 lib/licensed/sources/swift.rb
licensed-3.7.1 lib/licensed/sources/swift.rb