-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cache): Add persistent documentation cache #14
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
Merged
+253
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import Foundation | ||
|
|
||
| #if DEBUG | ||
| protocol DocumentationCache { | ||
| var currentDiskUsage: Int { get } | ||
|
|
||
| func removeAllCachedResponses() | ||
| } | ||
|
|
||
| extension URLCache: DocumentationCache {} | ||
| #else | ||
| typealias DocumentationCache = URLCache | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import ArgumentParser | ||
| import Logging | ||
| @preconcurrency import SentrySwift | ||
|
|
||
| struct CacheCleanCommand: ParsableCommand { | ||
| private static let logger = Logger( | ||
| label: "com.techprimate.apple-docs.cache-clean" | ||
| ) | ||
|
|
||
| static let configuration = CommandConfiguration( | ||
| commandName: "clean", | ||
| abstract: "Clear cached Apple documentation." | ||
| ) | ||
|
|
||
| mutating func run() throws { | ||
| if SentrySDK.isEnabled { | ||
| let context = SentryCommandContext.cacheClean | ||
| let transaction = SentrySDK.startTransaction( | ||
| name: context.transactionName, | ||
| operation: "console.command", | ||
| bindToScope: true | ||
| ) | ||
| for (key, value) in context.attributes { | ||
| transaction.setData(value: value, key: key) | ||
| } | ||
| SentrySDK.configureScope { scope in | ||
| scope.setContext(value: context.attributes, key: "cli") | ||
| } | ||
| let breadcrumb = Breadcrumb( | ||
| level: .info, | ||
| category: SentryConfiguration.breadcrumbCategory | ||
| ) | ||
| breadcrumb.type = "user" | ||
| breadcrumb.message = "CLI command invoked" | ||
| for (key, value) in context.attributes { | ||
| breadcrumb.setData(value: value, key: key) | ||
| } | ||
| SentrySDK.addBreadcrumb(breadcrumb) | ||
| Self.logger.info( | ||
| "CLI command started", | ||
| metadata: context.logMetadata | ||
| ) | ||
| } | ||
|
|
||
| let result = CacheCleanCommandRunner( | ||
| cache: Dependencies.documentationCache | ||
| ).run() | ||
| print(result.output) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Foundation | ||
|
|
||
| struct CacheCleanCommandRunner { | ||
| struct Result { | ||
| let clearedByteCount: Int? | ||
| let output: String | ||
| } | ||
|
|
||
| private let cache: DocumentationCache? | ||
|
|
||
| init(cache: DocumentationCache?) { | ||
| self.cache = cache | ||
| } | ||
|
|
||
| func run() -> Result { | ||
| guard let cache else { | ||
| return Result( | ||
| clearedByteCount: nil, | ||
| output: "Apple documentation cache is unavailable." | ||
| ) | ||
| } | ||
|
|
||
| let clearedByteCount = cache.currentDiskUsage | ||
| cache.removeAllCachedResponses() | ||
| let formattedByteCount = ByteCountFormatter.string( | ||
| fromByteCount: Int64(clearedByteCount), | ||
| countStyle: .file | ||
| ) | ||
| return Result( | ||
| clearedByteCount: clearedByteCount, | ||
| output: "Cleared \(formattedByteCount) of cached Apple documentation." | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import ArgumentParser | ||
|
|
||
| struct CacheCommand: ParsableCommand { | ||
| static let configuration = CommandConfiguration( | ||
| commandName: "cache", | ||
| abstract: "Manage cached Apple documentation.", | ||
| subcommands: [CacheCleanCommand.self] | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Tests/CLITests/cmd/cache/CacheCleanCommandRunnerTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import Testing | ||
|
|
||
| @testable import CLI | ||
|
|
||
| @Suite("Cache clean command runner") | ||
| struct CacheCleanCommandRunnerTests { | ||
| @Test("clears cached responses and reports their disk usage") | ||
| func clearsCache() { | ||
| // -- Arrange -- | ||
| let cache = TestDocumentationCache(currentDiskUsage: 5_000_000) | ||
| let runner = CacheCleanCommandRunner(cache: cache) | ||
|
|
||
| // -- Act -- | ||
| let result = runner.run() | ||
|
|
||
| // -- Assert -- | ||
| #expect(cache.isEmpty) | ||
| #expect(result.clearedByteCount == 5_000_000) | ||
| #expect(result.output == "Cleared 5 MB of cached Apple documentation.") | ||
| } | ||
|
|
||
| @Test("reports when no cache is available") | ||
| func reportsUnavailableCache() { | ||
| // -- Arrange -- | ||
| let runner = CacheCleanCommandRunner(cache: nil) | ||
|
|
||
| // -- Act -- | ||
| let result = runner.run() | ||
|
|
||
| // -- Assert -- | ||
| #expect(result.clearedByteCount == nil) | ||
| #expect(result.output == "Apple documentation cache is unavailable.") | ||
| } | ||
| } | ||
|
|
||
| private final class TestDocumentationCache: DocumentationCache { | ||
| private(set) var currentDiskUsage: Int | ||
|
|
||
| var isEmpty: Bool { | ||
| currentDiskUsage == 0 | ||
| } | ||
|
|
||
| init(currentDiskUsage: Int) { | ||
| self.currentDiskUsage = currentDiskUsage | ||
| } | ||
|
|
||
| func removeAllCachedResponses() { | ||
| currentDiskUsage = 0 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import Testing | ||
|
|
||
| @testable import CLI | ||
|
|
||
| @Suite("Cache clean command") | ||
| struct CacheCleanCommandTests { | ||
| @Test("registers the cache clean command hierarchy") | ||
| func parsesCacheCleanCommand() throws { | ||
| // -- Arrange -- | ||
| let arguments = ["cache", "clean"] | ||
|
|
||
| // -- Act -- | ||
| let command = try CLI.parseAsRoot(arguments) | ||
|
|
||
| // -- Assert -- | ||
| #expect(command is CacheCleanCommand) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import CLI | ||
|
|
||
| @Suite("Dependencies") | ||
| struct DependenciesTests { | ||
| @Test("uses a dedicated large documentation cache") | ||
| func usesDedicatedDocumentationCache() throws { | ||
| // -- Arrange -- | ||
| let session = Dependencies.httpDataTransport | ||
|
|
||
| // -- Act -- | ||
| let cache = try #require(session.configuration.urlCache) | ||
| let documentationCache = try #require(Dependencies.documentationCache) | ||
|
|
||
| // -- Assert -- | ||
| #expect(session !== URLSession.shared) | ||
| #expect(cache === documentationCache) | ||
| #expect(cache.diskCapacity == 1_000_000_000) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.