Skip to content

Commit

Permalink
Merge pull request #73 from will-lumley/tech/swiftlint
Browse files Browse the repository at this point in the history
[TECH] Added SwiftLint Support
  • Loading branch information
will-lumley authored Apr 24, 2024
2 parents d9d4398 + 02b8961 commit 49289d4
Show file tree
Hide file tree
Showing 28 changed files with 295 additions and 216 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/BuildTests-iOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
jobs:
build:
name: Build and Test (iOS)
runs-on: macos-latest
runs-on: macos-14
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -22,10 +22,10 @@ jobs:
- name: Setup Swift
uses: fwal/setup-swift@v1
with:
swift-version: '5.8.0'
swift-version: '5.9.0'

- name: Install xcpretty
run: gem install xcpretty

- name: Test
run: xcodebuild test -scheme FaviconFinder -destination 'platform=iOS Simulator,name=iPhone 14,OS=latest'
run: xcodebuild test -scheme FaviconFinder -destination 'platform=iOS Simulator,name=iPhone 14,OS=latest' -skipPackagePluginValidation
2 changes: 1 addition & 1 deletion .github/workflows/BuildTests-macOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
jobs:
build:
name: Build and Test (macOS)
runs-on: macos-latest
runs-on: macos-14
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: SwiftLint

on:
pull_request:
paths:
- '.github/workflows/swiftlint.yml'
- '.swiftlint.yml'
- 'Sources/**/*.swift'

jobs:
SwiftLint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: GitHub Action for SwiftLint
uses: norio-nomura/action-swiftlint@3.2.1

- name: GitHub Action for SwiftLint with --strict
uses: norio-nomura/action-swiftlint@3.2.1
with:
args: --strict

- name: GitHub Action for SwiftLint (Only files changed in the PR)
uses: norio-nomura/action-swiftlint@3.2.1
env:
DIFF_BASE: ${{ github.base_ref }}

- name: GitHub Action for SwiftLint (Different working directory)
uses: norio-nomura/action-swiftlint@3.2.1
env:
WORKING_DIRECTORY: Source
18 changes: 4 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Xcode
build/
.build/
*.pbxuser
!default.pbxuser
*.mode1v3
Expand All @@ -19,22 +20,11 @@ DerivedData
*.hmap
*.ipa

# SPM
Package.resolved

# Bundler
.bundle

# Add this line if you want to avoid checking in source code from Carthage dependencies.
Carthage/Checkouts
Carthage/Build

# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Note: if you ignore the Pods directory, make sure to uncomment
# `pod install` in .travis.yml
#
# Pods/
.build/

# VSCode
.vscode/
32 changes: 32 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# swiftlint_version: 0.54.0

disabled_rules:
inclusive_language

nesting:
type_level:
warning: 4

file_length:
warning: 500

function_body_length:
warning: 100

opt_in_rules:
- first_where
- modifier_order
- sorted_imports

file_length:
warning: 750

identifier_name:
min_length:
warning: 1

excluded:
- .build
- iOSFaviconFinderExample
- macOSFaviconFinderExample
- LinuxFaviconFinderExample
14 changes: 0 additions & 14 deletions Package.resolved

This file was deleted.

35 changes: 26 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -17,35 +17,52 @@ let targetDependencies: [Target.Dependency] = [
.product(name: "AsyncHTTPClient", package: "async-http-client")
]

let plugins: [Target.PluginUsage] = [

]

#else
let dependencies: [PackageDescription.Package.Dependency] = [
// SwiftSoup is used to parse the HTML tree
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.7")
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.7"),
.package(url: "https://github.com/realm/SwiftLint.git", branch: "main")
]

let targetDependencies: [Target.Dependency] = [
"SwiftSoup",
"SwiftSoup"
]

let plugins: [Target.PluginUsage] = [
.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")
]

#endif

let package = Package(
name: "FaviconFinder",
platforms: [.iOS(.v15), .macOS(.v12), .watchOS(.v8), .tvOS(.v15)],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "FaviconFinder",
targets: ["FaviconFinder"]),
targets: [
"FaviconFinder"
]
)
],
dependencies: dependencies,
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "FaviconFinder",
dependencies: targetDependencies
dependencies: targetDependencies,
plugins: plugins
),

.testTarget(name: "FaviconFinderTests", dependencies: ["FaviconFinder"]),
.testTarget(
name: "FaviconFinderTests",
dependencies: [
"FaviconFinder"
],
plugins: plugins
)
]
)
2 changes: 1 addition & 1 deletion Sources/FaviconFinder/FaviconFinder+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public extension FaviconFinder {

public init(
preferredSource: FaviconSourceType = .html,
preferences: [FaviconSourceType : String] = [:],
preferences: [FaviconSourceType: String] = [:],
checkForMetaRefreshRedirect: Bool = false,
prefetchedHTML: Document? = nil
) {
Expand Down
12 changes: 6 additions & 6 deletions Sources/FaviconFinder/FaviconFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

import Foundation

public class FaviconFinder {
public final class FaviconFinder {

// MARK: - Properties

/// The URL of the site we're trying to extract the Favicon from
private let url: URL

/// Our configuration object
private let configuration: FaviconFinder.Configuration

// MARK: - Lifecycle

public init(url: URL, configuration: FaviconFinder.Configuration = .defaultConfiguration) {
self.url = url
self.configuration = configuration
Expand Down
6 changes: 3 additions & 3 deletions Sources/FaviconFinder/FaviconFinderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ protocol FaviconFinderProtocol {
var configuration: FaviconFinder.Configuration { get set }

/// The preferred type of Favicon. This is dependant on type.
/// For example, in`ICOFaviconFinder` the `preferredType` is a filename, for `WebApplicationManifestFaviconFinder` the
/// `preferredType` is the desired key in the JSON file, etc.
/// For example, in`ICOFaviconFinder` the `preferredType` is a filename,
/// for `WebApplicationManifestFaviconFinder` the `preferredType`
/// is the desired key in the JSON file, etc.
var preferredType: String { get }

init(url: URL, configuration: FaviconFinder.Configuration)

func find() async throws -> [FaviconURL]

}

18 changes: 9 additions & 9 deletions Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftSoup
class HTMLFaviconFinder: FaviconFinderProtocol {

// MARK: - Types

struct Reference {
let rel: String
let href: String
Expand All @@ -20,23 +20,23 @@ class HTMLFaviconFinder: FaviconFinderProtocol {
let baseURL: URL
let format: FaviconFormatType
}

// MARK: - Properties

var url: URL
var configuration: FaviconFinder.Configuration

var preferredType: String {
self.configuration.preferences[.html] ?? FaviconFormatType.appleTouchIcon.rawValue
}

// MARK: - FaviconFinder

required init(url: URL, configuration: FaviconFinder.Configuration) {
self.url = url
self.configuration = configuration
}

func find() async throws -> [FaviconURL] {
let html: Document

Expand Down Expand Up @@ -74,7 +74,7 @@ class HTMLFaviconFinder: FaviconFinderProtocol {

return faviconURLs
}

}

// MARK: - Private
Expand All @@ -98,7 +98,7 @@ private extension HTMLFaviconFinder {
let sizeTag = try link.attr("sizes")

// If this link's "rel" is something other than an accepted image format type, dismiss it
guard (FaviconFormatType(rawValue: rel) != nil) else {
guard FaviconFormatType(rawValue: rel) != nil else {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/FaviconFinder/Finders/ICOFaviconFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ICOFaviconFinder: FaviconFinderProtocol {

var url: URL
var configuration: FaviconFinder.Configuration

var preferredType: String {
self.configuration.preferences[.ico] ?? "favicon.ico"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WebApplicationManifestFaviconFinder: FaviconFinderProtocol {

var url: URL
var configuration: FaviconFinder.Configuration

var preferredType: String {
self.configuration.preferences[.webApplicationManifestFile] ?? "manifest"
}
Expand Down Expand Up @@ -125,16 +125,20 @@ private extension WebApplicationManifestFaviconFinder {
/// - Parameter manifestFileReference: The now-native data from our HTML head that contains the manifest file data
/// - Returns: A dictionary containing the key/value data contained in the manifest file
///
func downloadManifestFile(with reference: ManifestFileReference) async throws -> Dictionary<String, Any> {
func downloadManifestFile(
with reference: ManifestFileReference
) async throws -> [String: Any] {
let response = try await FaviconURLSession.dataTask(with: reference.baseURL)
do {
guard let manifestData = try JSONSerialization.jsonObject(with: response.data, options: .allowFragments) as? [String: Any] else {
guard let manifestData = try JSONSerialization.jsonObject(
with: response.data,
options: .allowFragments
) as? [String: Any] else {
throw FaviconError.failedToDownloadWebApplicationManifestFile
}

return manifestData
}
catch {
} catch {
throw FaviconError.failedToParseWebApplicationManifestFile
}
}
Expand Down
11 changes: 5 additions & 6 deletions Sources/FaviconFinder/Toolbox/Extensions/String+HREF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ extension String {
if Regex.testForHttpsOrHttp(input: self) == false {
let baseRef = {() -> URL in
// Try and get the base URL from a HTML tag if we can
if let baseRef = try? head.getElementsByTag("base").attr("href"), let baseRefUrl = URL(string: baseRef, relativeTo: url) {
if let baseRef = try? head.getElementsByTag("base").attr("href"),
let baseRefUrl = URL(string: baseRef, relativeTo: url) {
return baseRefUrl
}

// We couldn't get the base URL from a HTML tag, so we'll use the base URL that we have on hand
else {
return url
}
}

return URL(string: self, relativeTo: baseRef())
}

// Our href is a proper URL, nevermind
else {
} else {
// Our href is a proper URL, nevermind
return URL(string: self)
}
}
Expand Down
Loading

0 comments on commit 49289d4

Please sign in to comment.