Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace commandant with swift argument parser #38

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 6 additions & 33 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,21 @@
"object": {
"pins": [
{
"package": "Commandant",
"repositoryURL": "https://github.com/Carthage/Commandant.git",
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser.git",
"state": {
"branch": null,
"revision": "07cad52573bad19d95844035bf0b25acddf6b0f6",
"version": "0.15.0"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "7c61d8e7e830dd37f7161ce2b894be178532163c",
"version": "7.3.0"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "b060679e70d13c3c7dcd124201b5b1b34ce6f340",
"version": "1.3.1"
}
},
{
"package": "Result",
"repositoryURL": "https://github.com/antitypical/Result.git",
"state": {
"branch": null,
"revision": "8fc088dcf72802801efeecba76ea8fb041fb773d",
"version": "4.0.0"
"revision": "41982a3656a71c768319979febd796c6fd111d5c",
"version": "1.5.0"
}
},
{
"package": "Yams",
"repositoryURL": "https://github.com/jpsim/Yams.git",
"state": {
"branch": null,
"revision": "26ab35f50ea891e8edefcc9d975db2f6b67e1d68",
"version": "1.0.1"
"revision": "3036ba9d69cf1fd04d433527bc339dc0dc75433d",
"version": "5.1.3"
}
}
]
Expand Down
15 changes: 6 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -10,28 +10,25 @@ let package = Package(
.library(name: "StringsLintFramework", targets: ["StringsLintFramework"])
],
dependencies: [
.package(url: "https://github.com/Carthage/Commandant.git", from: "0.15.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.1"),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.6"),
],
targets: [
.target(
.executableTarget(
name: "stringslint",
dependencies: [
"Commandant",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"StringsLintFramework",
]),
.target(
name: "StringsLintFramework",
dependencies: [
"Yams",
.product(name: "Yams", package: "Yams"),
]),
.testTarget(
name: "StringsLintFrameworkTests",
dependencies: [
"StringsLintFramework"
],
exclude: [
"Resources",
]
)
]
Expand Down
85 changes: 85 additions & 0 deletions Sources/stringslint/Commands/Lint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Lint.swift
// StringsLint
//
// Created by Alessandro Calzavara on 30/09/24.
//

import ArgumentParser
import StringsLintFramework

@main
extension StringsLint {
struct Lint: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Print lint warnings and errors (default command)")

@Option(help: "The path to the file or directory to lint.")
var path: String?
@Argument(help: "List of paths to the files or directories to lint.")
var paths = [String]()
@Option(help: "The path to StringsLint's configuration file.")
var config: String = Configuration.fileName

func run() throws {

let allPaths: [String]
if let path {
allPaths = [path]
} else if !paths.isEmpty {
allPaths = paths
} else {
allPaths = [""] // Lint files in current working directory if no paths were specified.
}
let options = LintOptions(paths: allPaths, configurationFile: config)

// Setup
let configuration = Configuration(options: options)
let files = configuration.lintableFiles(options: options)

// Process all files
let result = linterFrom(configuration: configuration).lintFiles(files)
.map { $0.filter { $0.severity != .none } }
.map { violations -> [Violation] in

// Report violations
let reporter = reporterFrom(identifier: XcodeReporter.identifier)
reporter.report(violations: violations)

return violations
}
.map { violations -> Result<[Violation], StringsLintError> in

let numberOfSeriousViolations = violations.filter({ $0.severity == .error }).count

printStatus(violations: violations, files: files, serious: numberOfSeriousViolations)

if numberOfSeriousViolations > 0 {
return .failure(StringsLintError.usageError(description: "A serious violation has been found"))
}
return .success(violations)
}

switch result {
case .success:
return
case .failure(let error):
throw error
}
}

private func printStatus(violations: [Violation], files: [String], serious: Int) {
let pluralSuffix = { (collection: [Any]) -> String in
return collection.count != 1 ? "s" : ""
}
queuedPrintError(
"Done linting! Found \(violations.count) violation\(pluralSuffix(violations)), " +
"\(serious) serious in \(files.count) file\(pluralSuffix(files))."
)
}
}
}

struct LintOptions {
let paths: [String]
let configurationFile: String
}
80 changes: 0 additions & 80 deletions Sources/stringslint/Commands/LintCommand.swift

This file was deleted.

23 changes: 23 additions & 0 deletions Sources/stringslint/Commands/StringsLint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// StringsLint.swift
// StringsLint
//
// Created by Alessandro Calzavara on 30/09/24.
//

import ArgumentParser
import Foundation
import StringsLintFramework

struct StringsLint: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "stringslint",
abstract: "A tool to enforce Swift style and conventions.",
version: Version.value,
subcommands: [
Lint.self,
Version.self
],
defaultSubcommand: Lint.self
)
}
21 changes: 21 additions & 0 deletions Sources/stringslint/Commands/Version.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Version.swift
// StringsLint
//
// Created by Alessandro Calzavara on 30/09/24.
//

import ArgumentParser
import StringsLintFramework

extension StringsLint {
struct Version: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Display the current version of StringsLint")

static var value: String { StringsLintFramework.Version.current.value }

mutating func run() throws {
print(Self.value)
}
}
}
20 changes: 0 additions & 20 deletions Sources/stringslint/Commands/VersionCommand.swift

This file was deleted.

24 changes: 0 additions & 24 deletions Sources/stringslint/Helpers/CommonOptions.swift

This file was deleted.

6 changes: 2 additions & 4 deletions Sources/stringslint/Helpers/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import Foundation
import Dispatch
import Commandant
import Result
import StringsLintFramework

struct Linter {
Expand All @@ -23,8 +21,8 @@ struct Linter {
self.lintableExtentions = supportedFilesExtentions()
}

func lintFiles(_ files: [String]) -> Result<[Violation], CommandantError<()>> {
func lintFiles(_ files: [String]) -> Result<[Violation], StringsLintError> {

var violations = [Violation]()

let apply = { (file: File) in
Expand Down
19 changes: 19 additions & 0 deletions Sources/stringslint/Helpers/StringsLintError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// StringsLintError.swift
// StringsLint
//
// Created by Alessandro Calzavara on 30/09/24.
//

import Foundation

enum StringsLintError: LocalizedError {
case usageError(description: String)

var errorDescription: String? {
switch self {
case .usageError(let description):
return description
}
}
}
Loading