Skip to content

Commit

Permalink
swift 5, swift package manager 5
Browse files Browse the repository at this point in the history
missing comment lint rule
  • Loading branch information
g-Off committed Jun 12, 2019
1 parent 5c8cb03 commit fe55e69
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/*.xcodeproj
.swiftpm
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM swift:4.2.1
COPY Package.swift ./Package.swift
COPY Sources ./Sources
COPY Tests ./Tests
RUN swift test --configuration debug
44 changes: 0 additions & 44 deletions Makefile

This file was deleted.

1 change: 0 additions & 1 deletion Overrides.xcconfig

This file was deleted.

12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@
"repositoryURL": "https://github.com/scottrhoyt/SwiftyTextTable.git",
"state": {
"branch": null,
"revision": "7b8661865f0d9590a4b7c146237fecd99f3d8406",
"version": "0.8.2"
"revision": "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version": "0.9.0"
}
},
{
"package": "XcodeProject",
"repositoryURL": "https://github.com/g-Off/XcodeProject.git",
"state": {
"branch": null,
"revision": "a1e12a8659684039258b79761c65abb9ec98fc94",
"version": "0.4.0"
"revision": "f5095a860de4cd1f0e635957bed7c4b80392dac8",
"version": "0.5.0"
}
},
{
"package": "Yams",
"repositoryURL": "https://github.com/jpsim/Yams.git",
"state": {
"branch": null,
"revision": "26ab35f50ea891e8edefcc9d975db2f6b67e1d68",
"version": "1.0.1"
"revision": "b08dba4bcea978bf1ad37703a384097d3efce5af",
"version": "1.0.2"
}
}
]
Expand Down
17 changes: 10 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// swift-tools-version:4.2
// swift-tools-version:5.0
import PackageDescription

let package = Package(
name: "stringray",
platforms: [
.macOS(.v10_14)
],
products: [
.executable(
name: "stringray",
Expand All @@ -16,8 +19,8 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/jpsim/Yams.git", from: "1.0.1"),
.package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.5.0"),
.package(url: "https://github.com/g-Off/XcodeProject.git", from: "0.4.0"),
.package(url: "https://github.com/g-Off/CommandRegistry.git", .branch("master")),
.package(url: "https://github.com/g-Off/XcodeProject.git", from: "0.5.0-alpha.3"),
.package(url: "https://github.com/g-Off/CommandRegistry.git", from: "0.1.0"),
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.3.0")
],
targets: [
Expand All @@ -26,15 +29,15 @@ let package = Package(
dependencies: [
"CommandRegistry",
"RayGun",
"SwiftyTextTable"
"SwiftyTextTable",
"XcodeProject",
"Utility",
"Yams",
]
),
.target(
name: "RayGun",
dependencies: [
"Utility",
"Yams",
"XcodeProject"
]
),
.testTarget(
Expand Down
11 changes: 0 additions & 11 deletions Sources/RayGun/Exports.swift

This file was deleted.

9 changes: 2 additions & 7 deletions Sources/RayGun/Lint Rules/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import Yams

public struct Linter {
public struct Config: Decodable {
Expand All @@ -29,11 +28,6 @@ public struct Linter {
self.rules = [:]
}

public init(url: Foundation.URL) throws {
let string = try String(contentsOf: url, encoding: .utf8)
self = try YAMLDecoder().decode(Config.self, from: string, userInfo: [:])
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.included = try container.decodeIfPresent([String].self, forKey: .included) ?? []
Expand All @@ -47,7 +41,8 @@ public struct Linter {
public static let allRules: [LintRule] = [
MissingLocalizationLintRule(),
OrphanedLocalizationLintRule(),
MissingPlaceholderLintRule()
MissingPlaceholderLintRule(),
MissingCommentLintRule()
]

public struct Error: LocalizedError {
Expand Down
25 changes: 25 additions & 0 deletions Sources/RayGun/Lint Rules/MissingCommentLintRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// MissingCommentLintRule.swift
// RayGun
//
// Created by Geoffrey Foster on 2019-06-02.
//

import Foundation

struct MissingCommentLintRule: LintRule {
let info: RuleInfo = RuleInfo(identifier: "missing_comment", name: "Missing Comment", description: "", severity: .error)

func scan(table: StringsTable, url: Foundation.URL, config: Linter.Config.Rule?) throws -> [LintRuleViolation] {
var violations: [LintRuleViolation] = []
let file = Foundation.URL(fileURLWithPath: "\(table.base.identifier).lproj/\(table.name).strings", relativeTo: url)
for entry in table.baseEntries where entry.comment == nil {
let line = entry.location?.line
let location = LintRuleViolation.Location(file: file, line: line)
let reason = "Mismatched placeholders \(entry.key)"
let violation = LintRuleViolation(locale: table.base, location: location, severity: config?.severity ?? info.severity, reason: reason)
violations.append(violation)
}
return violations
}
}
4 changes: 2 additions & 2 deletions Sources/RayGun/Strings Table/StringsTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct StringsTable: Codable {
return dictEntries[base] ?? [:]
}

public init(name: String, base: Locale, entries: EntriesType, dictEntries: DictEntriesType) {
public init(name: String, base: Locale, entries: EntriesType = [:], dictEntries: DictEntriesType = [:]) {
self.name = name
self.base = base
self.entries = entries
Expand Down Expand Up @@ -125,7 +125,7 @@ public struct StringsTable: Codable {
}

private mutating func replace(entry: Entry, with otherEntry: Entry, locale: Locale) {
guard let index = entries[locale]?.index(of: entry) else { return }
guard let index = entries[locale]?.firstIndex(of: entry) else { return }
entries[locale]?[index] = otherEntry
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/RayGun/Utilities/OrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extension OrderedSet: SetAlgebra {

@discardableResult
public mutating func update(with newMember: Element) -> Element? {
if contains(newMember), let index = orderedStorage.index(of: newMember) {
if contains(newMember), let index = orderedStorage.firstIndex(of: newMember) {
orderedStorage[index] = newMember
}
let result = storage.update(with: newMember)
Expand All @@ -70,7 +70,7 @@ extension OrderedSet: SetAlgebra {

@discardableResult
public mutating func remove(_ member: Element) -> Element? {
guard let index = orderedStorage.index(of: member) else { return nil }
guard let index = orderedStorage.firstIndex(of: member) else { return nil }
orderedStorage.remove(at: index)
storage.remove(member)
return member
Expand Down
18 changes: 10 additions & 8 deletions Sources/stringray/Commands/CopyCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//

import Foundation
import RayGun
import CommandRegistry
import Basic
import Utility
import RayGun

struct CopyCommand: Command {
private struct Arguments {
Expand All @@ -17,17 +19,17 @@ struct CopyCommand: Command {
}
let command: String = "copy"
let overview: String = "Copy keys matching the given pattern from one strings table to another."

private let binder: ArgumentBinder<Arguments>

init(parser: ArgumentParser) {
binder = ArgumentBinder<Arguments>()
let subparser = parser.add(subparser: command, overview: overview)

let inputFile = subparser.add(positional: "inputFile", kind: PathArgument.self, optional: false, usage: "", completion: .filename)
let outputFile = subparser.add(positional: "outputFile", kind: PathArgument.self, optional: false, usage: "", completion: .filename)
let prefix = subparser.add(option: "--prefix", shortName: "-p", kind: [String].self, strategy: .oneByOne, usage: "", completion: nil)

binder.bind(positional: inputFile) { (arguments, inputFile) in
arguments.inputFile = URL(fileURLWithPath: inputFile.path.asString)
}
Expand All @@ -40,18 +42,18 @@ struct CopyCommand: Command {
}
}
}

func run(with arguments: ArgumentParser.Result) throws {
var commandArgs = Arguments()
try binder.fill(parseResult: arguments, into: &commandArgs)
try copy(from: commandArgs.inputFile, to: commandArgs.outputFile, matching: commandArgs.matching)
}

private func copy(from: Foundation.URL, to: Foundation.URL, matching: [Match]) throws {
let loader = StringsTableLoader()
let fromTable = try loader.load(url: from)
var toTable = try loader.load(url: to)

let filteredTable = fromTable.withKeys(matching: matching)
toTable.addEntries(from: filteredTable)
try loader.write(to: to.resourceDirectory, table: toTable)
Expand Down
6 changes: 5 additions & 1 deletion Sources/stringray/Commands/LintCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
//

import Foundation
import RayGun
import CommandRegistry
import Basic
import Utility
import RayGun
import XcodeProject
import SwiftyTextTable

struct LintCommand: Command {
private struct Arguments {
Expand Down
2 changes: 2 additions & 0 deletions Sources/stringray/Commands/MoveCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import Foundation
import CommandRegistry
import Basic
import Utility
import RayGun

struct MoveCommand: Command {
Expand Down
4 changes: 3 additions & 1 deletion Sources/stringray/Commands/RenameCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//

import Foundation
import RayGun
import CommandRegistry
import Basic
import Utility
import RayGun

struct RenameCommand: Command {
private struct Arguments {
Expand Down
12 changes: 7 additions & 5 deletions Sources/stringray/Commands/SortCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,34 @@
import Foundation
import RayGun
import CommandRegistry
import Basic
import Utility

struct SortCommand: Command {
private struct Arguments {
var inputFile: Foundation.URL!
}
let command: String = "sort"
let overview: String = "Sorts the given strings table alphabetically by key."

private let binder: ArgumentBinder<Arguments>

init(parser: ArgumentParser) {
binder = ArgumentBinder<Arguments>()
let subparser = parser.add(subparser: command, overview: overview)
let inputFile = subparser.add(positional: "inputFile", kind: PathArgument.self, optional: false, usage: "", completion: .filename)

binder.bind(positional: inputFile) { (arguments, inputFile) in
arguments.inputFile = URL(fileURLWithPath: inputFile.path.asString)
}
}

func run(with arguments: ArgumentParser.Result) throws {
var commandArgs = Arguments()
try binder.fill(parseResult: arguments, into: &commandArgs)
try sort(url: commandArgs.inputFile)
}

private func sort(url: Foundation.URL) throws {
let loader = StringsTableLoader()
var table = try loader.load(url: url)
Expand Down
2 changes: 1 addition & 1 deletion Sources/stringray/Version.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Utility

extension Version {
static var current: Version = "0.3.0"
static var current: Version = "0.3.1"
}
9 changes: 9 additions & 0 deletions Sources/stringray/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
import Foundation
import Utility
import CommandRegistry
import Yams
import RayGun

extension Linter.Config {
public init(url: Foundation.URL) throws {
let string = try String(contentsOf: url, encoding: .utf8)
self = try YAMLDecoder().decode(Linter.Config.self, from: string, userInfo: [:])
}
}

var registry = Registry(usage: "<command> <options>", overview: "", version: Version.current)
registry.register(command: MoveCommand.self)
Expand Down
Loading

0 comments on commit fe55e69

Please sign in to comment.