Skip to content

Commit

Permalink
Merge pull request #12 from transifex/stelios/feature/cds-v2
Browse files Browse the repository at this point in the history
Update CDSHandler logic to v2
  • Loading branch information
stelabouras authored Jul 29, 2021
2 parents e1a1cd1 + 5850feb commit ccedafb
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ devel ]
branches: [ master ]
pull_request:
branches: [ devel ]
branches: [ master ]

jobs:
build:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@
- Changes `--tags` to `--append-tags` for push command.
- Allows content to pulled with specific tags in pull command.
- Formats found strings on push command correctly.

## Transifex Command Line Tool 1.0.0

*July 29, 2021*

- Updates Transifex Swift library to 1.0.0.
- Adds initial value to `withTagsOnly` argument so that is not required.
- Displays custom message when max retries have been exhausted during the push operation.
- Uses animated cursor from CLISpinner library while waiting for a response from CDS when
verbose flag is not provided.
22 changes: 20 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
{
"object": {
"pins": [
{
"package": "CLISpinner",
"repositoryURL": "https://github.com/kiliankoe/CLISpinner",
"state": {
"branch": null,
"revision": "0572232b92ddfd80cbab4ced6973d1c210022968",
"version": "0.4.0"
}
},
{
"package": "Rainbow",
"repositoryURL": "https://github.com/onevcat/Rainbow",
"state": {
"branch": null,
"revision": "626c3d4b6b55354b4af3aa309f998fae9b31a3d9",
"version": "3.2.0"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
Expand All @@ -15,8 +33,8 @@
"repositoryURL": "https://github.com/transifex/transifex-swift",
"state": {
"branch": null,
"revision": "47d47d4eee281da15721285b86f75539d4061fff",
"version": "0.5.0"
"revision": "1a79564a8c51ef748c9f4a27fb3e3e10d6dbe9d0",
"version": "1.0.0"
}
}
]
Expand Down
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ let package = Package(
dependencies: [
.package(name: "transifex",
url: "https://github.com/transifex/transifex-swift",
from: "0.5.0"),
from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser",
from: "0.3.0"),
.package(url: "https://github.com/kiliankoe/CLISpinner",
from: "0.4.0")
],
targets: [
.target(
Expand All @@ -27,6 +29,7 @@ let package = Package(
package: "transifex"),
.product(name: "ArgumentParser",
package: "swift-argument-parser"),
"CLISpinner"
]
),
.target(
Expand Down
60 changes: 54 additions & 6 deletions Sources/TXCli/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Transifex
import TXCliLib
import ArgumentParser
import Foundation
import CLISpinner

/// All possible error codes that might trigger a failure during the execution of a TXCli command.
enum CommandError : Error {
Expand Down Expand Up @@ -41,7 +42,7 @@ that can be bundled with the iOS application.
The tool can be also used to force CDS cache invalidation so that the next pull
command will fetch fresh translations from CDS.
""",
version: "0.1.0",
version: "1.0.0",
subcommands: [Push.self, Pull.self, Invalidate.self])
}

Expand Down Expand Up @@ -209,23 +210,61 @@ the CDS server.
[high]Pushing[end] [num]\(translations.count)[end] [high]source strings to CDS ([end][prompt]Purge: \(purge ? "Yes" : "No")[end][high])...[end]
""")

let spinner = Spinner(pattern: .dots, text: "Pushing")
if !options.verbose {
spinner.start()
}

// Block until the push logic completes using a semaphore.
let semaphore = DispatchSemaphore(value: 0)
var pushResult = false
var pushErrors: [Error] = []

TXNative.pushTranslations(translations,
purge: purge) { (result) in
purge: purge) { (result, errors) in
pushResult = result
pushErrors = errors
semaphore.signal()
}

semaphore.wait()

if !options.verbose {
spinner.stopAndClear()
}

if !pushResult {
logHandler.error("Error while pushing source strings to CDS")
throw CommandError.cdsPushFailure
if containsMaxRetriesReachedError(pushErrors) {
logHandler.info("[prompt]Strings are queued for processing[end]")
}
else {
logHandler.error("Error while pushing source strings to CDS")
throw CommandError.cdsPushFailure
}
}
else {
logHandler.info("""
[success]✓[end] [num]\(translations.count)[end][success] source strings pushed successfully[end]
""")
}
}

/// Reports whether the passed array of errors contains a max retries reached error or not.
///
/// - Parameter errors: Passed array of errors as returned by the pushTranslations() method
/// - Returns: true if the array contains a max retries reached error, false otherwise
func containsMaxRetriesReachedError(_ errors: [Error]) -> Bool {
guard errors.count > 0 else {
return false
}

for error in errors {
if case TXCDSError.maxRetriesReached = error {
return true
}
}

logHandler.info("[success]✓[end] [num]\(translations.count)[end][success] source strings pushed successfully[end]")
return false
}
}

Expand Down Expand Up @@ -263,7 +302,7 @@ will try to create it (alongside any intermediate folders).
@Option(name: .long, parsing: .upToNextOption, help: """
If set, only the strings that have all of the given tags will be downloaded.
""")
private var withTagsOnly: [String]
private var withTagsOnly: [String] = []

func run() throws {
let logHandler = CliLogHandler()
Expand All @@ -288,6 +327,11 @@ If set, only the strings that have all of the given tags will be downloaded.

logHandler.info("[high]Fetching translations from CDS...[end]")

let spinner = Spinner(pattern: .dots, text: "Fetching")
if !options.verbose {
spinner.start()
}

// Block until the pull logic completes using a semaphore.
let semaphore = DispatchSemaphore(value: 0)
var appTranslations: [String: TXLocaleStrings] = [:]
Expand All @@ -301,6 +345,10 @@ If set, only the strings that have all of the given tags will be downloaded.

semaphore.wait()

if !options.verbose {
spinner.stopAndClear()
}

guard appErrors.count == 0 else {
logHandler.error("Errors while fetching translations from CDS: \(appErrors)")
throw CommandError.cdsPullFailure
Expand Down
3 changes: 3 additions & 0 deletions Sources/TXCliLib/CliLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class CliLogHandler: TXLogHandler {
/// Extension responsible for printing the debug description of a TXSourceString to the console with proper
/// styling.
extension TXSourceString {
/// Stylize the debug description of the TXSourceString for the CLI needs.
///
/// We are aware of the 'method in category overrides method from class' warning(s) produced here.
public override var debugDescription: String {
var description = "\n"

Expand Down

0 comments on commit ccedafb

Please sign in to comment.