Skip to content

Commit

Permalink
Fix an issue with decoding Violation without date
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Sep 13, 2021
1 parent 584f603 commit 0bbe6cf
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ let package = Package(
.target(
name: "Core",
dependencies: [
.product(name: "BetterCodable", package: "BetterCodable"),
.product(name: "Rainbow", package: "Rainbow"),
]
),
Expand Down
62 changes: 38 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,48 @@ This will create the Swift script file `anylint.yml` with something like the fol

```yaml
FileContents: []
# - id: Readme
# hint: 'Each project should have a README.md file, explaining how to use or contribute to the project.'
# regex: '^README\.md$'
# violateIfNoMatchesFound: true
# matchingExamples: ['README.md']
# nonMatchingExamples: ['README.markdown', 'Readme.md', 'ReadMe.md']
# TODO: replace below sample checks with your custom checks and remove empty array specifier `[]` from above
# - id: ReadmeTypoLicense
# hint: 'ReadmeTypoLicense: Misspelled word `license`.'
# regex: '([\s#]L|l)isence([\s\.,:;])'
# matchingExamples: [' lisence:', "## Lisence\n"]
# nonMatchingExamples: [' license:', "## License\n"]
# includeFilters: ['^README\.md$']
# autoCorrectReplacement: '$1icense$2'
# autoCorrectExamples:
# - { before: ' lisence:', after: ' license:' }
# - { before: "## Lisence\n", after: "## License\n" }

FilePaths: []
# - id: 'ReadmePath'
# hint: 'The README file should be named exactly `README.md`.'
# regex: '^(.*/)?([Rr][Ee][Aa][Dd][Mm][Ee]\.markdown|readme\.md|Readme\.md|ReadMe\.md)$'
# matchingExamples: ['README.markdown', 'readme.md', 'ReadMe.md']
# nonMatchingExamples: ['README.md', 'CHANGELOG.md', 'CONTRIBUTING.md', 'api/help.md']
# autoCorrectReplacement: '$1README.md'
# autoCorrectExamples:
# - { before: 'api/readme.md', after: 'api/README.md' }
# - { before: 'ReadMe.md', after: 'README.md' }
# - { before: 'README.markdown', after: 'README.md' }
# TODO: replace below sample checks with your custom checks and remove empty array specifier `[]` from above
# - id: Readme
# hint: 'Each project should have a README.md file, explaining how to use or contribute to the project.'
# regex: '^README\.md$'
# violateIfNoMatchesFound: true
# matchingExamples: ['README.md']
# nonMatchingExamples: ['README.markdown', 'Readme.md', 'ReadMe.md']
#
# - id: ReadmePath
# hint: 'The README file should be named exactly `README.md`.'
# regex: '^(.*/)?([Rr][Ee][Aa][Dd][Mm][Ee]\.markdown|readme\.md|Readme\.md|ReadMe\.md)$'
# matchingExamples: ['README.markdown', 'readme.md', 'ReadMe.md']
# nonMatchingExamples: ['README.md', 'CHANGELOG.md', 'CONTRIBUTING.md', 'api/help.md']
# autoCorrectReplacement: '$1README.md'
# autoCorrectExamples:
# - { before: 'api/readme.md', after: 'api/README.md' }
# - { before: 'ReadMe.md', after: 'README.md' }
# - { before: 'README.markdown', after: 'README.md' }

CustomScripts: []
# - id: LintConfig
# hint: 'Lint the AnyLint config file to conform to YAML best practices.'
# command: |-
# if which yamllint > /dev/null; then
# yamllint anylint.yml
# else
# echo '{ "warning": { "YamlLint: Not installed, see instructions at https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint": [{}] } }'
# fi
# TODO: replace below sample check with your custom checks and remove empty array specifier `[]` from above
# - id: LintConfig
# hint: 'Lint the AnyLint config file to conform to YAML best practices.'
# command: |-
# if which yamllint > /dev/null; then
# yamllint anylint.yml
# else
# echo '{ "warning": { "YamlLint@warning: Not installed, see instructions at https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint": [{}] } }'
# fi

```

Expand Down
1 change: 1 addition & 0 deletions Sources/Checkers/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public enum Lint {
public static func runCustomScript(check: Check, command: String) throws -> LintResults {
let tempScriptFileUrl = URL(fileURLWithPath: "_\(check.id).tempscript")
try command.write(to: tempScriptFileUrl, atomically: true, encoding: .utf8)
try shellOut(to: "chmod", arguments: ["+x", tempScriptFileUrl.path])

do {
let output = try shellOut(to: "/bin/bash", arguments: [tempScriptFileUrl.path])
Expand Down
1 change: 1 addition & 0 deletions Sources/Configuration/Core+DefaultCodableStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extension Severity {
}

extension Regex {
/// Use to set the default value of `Regex` instances to `.*` in rules when users don't provide an explicit value.
public enum DefaultToMatchAllArray: DefaultCodableStrategy {
public static var defaultValue: [Regex] { [try! Regex(".*")] }
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Configuration/Templates/Blank.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ CustomScripts: []
# if which yamllint > /dev/null; then
# yamllint anylint.yml
# else
# echo '{ "warning": { "YamlLint: Not installed, see instructions at https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint": [{}] } }'
# echo '{ "warning": { "YamlLint@warning: Not installed, see instructions at https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint": [{}] } }'
# fi
2 changes: 1 addition & 1 deletion Sources/Configuration/Templates/OpenSource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ CustomScripts:
if which yamllint > /dev/null; then
yamllint anylint.yml
else
echo '{ "warning": { "YamlLint: Not installed, see instructions at https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint": [{}] } }'
echo '{ "warning": { "YamlLint@warning: Not installed, see instructions at https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint": [{}] } }'
fi
9 changes: 9 additions & 0 deletions Sources/Core/Extensions/Date+DefaultCodableStrategy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import BetterCodable

extension Date {
/// Use to set the default value of `Date` instances to `Date.now` in rules when users don't provide an explicit value.
public enum DefaultToNow: DefaultCodableStrategy {
public static var defaultValue: Date { Date() }
}
}
4 changes: 3 additions & 1 deletion Sources/Core/Violation.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation
import BetterCodable

/// A violation found in a check.
public struct Violation: Codable, Equatable {
/// The exact time this violation was discovered. Needed for sorting purposes.
public let discoverDate: Date
@DefaultCodable<Date.DefaultToNow>
public var discoverDate: Date

/// The matched string that violates the check.
public let matchedString: String?
Expand Down

0 comments on commit 0bbe6cf

Please sign in to comment.