Skip to content

Commit

Permalink
Add Swift Package Manager and Linux support (#8)
Browse files Browse the repository at this point in the history
* create Swift package

* add Package.swift

* update gitignore

* update readme

* bump podspec version

* fix unit test for SPM's lack of resource support

* change Swift version to 4.2

* migrate away from arc4random_uniform to Int.random(in:) for Linux support, see Swift Evolution proposal SE-0202
  • Loading branch information
jonblatho authored and BenziAhamed committed Sep 8, 2019
1 parent 045e6d7 commit 2714eab
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Tracery.xcworkspace/xcuserdata/*
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
.build/
.swiftpm/

# CocoaPods
#
Expand Down
4 changes: 2 additions & 2 deletions Common/RuleCandidateSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private extension MutableCollection {
guard c > 1 else { return }

for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
let d: Int = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
let d = Int.random(in: 0..<unshuffledCount)
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
swapAt(firstUnshuffled, i)
Expand Down Expand Up @@ -82,7 +82,7 @@ class WeightedSelector : RuleCandidateSelector {
}

func pick(count: Int) -> Int {
let choice = Int(arc4random_uniform(sum))
let choice = Int.random(in: 0..<numericCast(sum))
let i = index(choice: choice)
// print("id: ", id, "weights: ", weights, "sum: ", sum, "choice: ", choice, "index: ", i)
return i
Expand Down
2 changes: 1 addition & 1 deletion Common/Tracery.Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func error(_ message: @autoclosure () -> String) {
}

func trace(_ message: @autoclosure () -> String) {
Tracery.log(level: .verbose, message: message)
Tracery.log(level: .verbose, message: message())
}


Expand Down
2 changes: 1 addition & 1 deletion CommonTesting/CandidateProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CandidateProvider: XCTestCase {
let candidates = ["jack","jill"]
func pick(count: Int) -> Int {
invokeCount += 1
return Int(arc4random_uniform(2))
return Int.random(in: 0..<2)
}
}

Expand Down
2 changes: 1 addition & 1 deletion CommonTesting/CustomProviders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WeightedCandidateSet : RuleCandidatesProvider, RuleCandidateSelector {
}

func pick(count: Int) -> Int {
var choice = Int(arc4random_uniform(sum))
var choice = Int.random(in: 0..<numericCast(sum))
var index = 0
for weight in weights {
choice = choice - weight
Expand Down
2 changes: 1 addition & 1 deletion CommonTesting/TextFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TextFormat: XCTestCase {

func testPlaintextFile() {

let fableFile = Bundle(for: type(of: self)).path(forResource: "fable", ofType: "txt")!
let fableFile = Bundle.main.executablePath! + "/CommonTesting/fable.txt"
let t = Tracery.init(path: fableFile)

for _ in 0..<10 {
Expand Down
30 changes: 30 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Tracery",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Tracery",
targets: ["Tracery"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Tracery",
dependencies: [],
path: "Common"),
.testTarget(
name: "TraceryTests",
dependencies: ["Tracery"],
path: "CommonTesting"),
]
)
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
- The project builds `iOS` and `macOS` framework targets, which can be linked to your projects

### Cocoapods
You want to add pod 'Tracery', '~> 0.0.1' similar to the following to your Podfile:
You want to add pod 'Tracery', '~> 0.0.2' similar to the following to your Podfile:

```
target 'MyApp' do
pod 'Tracery', '~> 0.0.1'
pod 'Tracery', '~> 0.0.2'
end
```
Then run a pod install inside your terminal, or from CocoaPods.app.
Expand All @@ -64,6 +64,18 @@ Alternatively to give it a test run, run the command:
[top](#contents)
****

### Swift Package Manager
Use Swift Package Manager support in Xcode 11 (File > Swift Packages > Add Package Dependency...) to add the Swift package to your targets. Or add Tracery to your Package.swift file with the following:

```swift
.package(url: "https://github.com/BenziAhamed/Tracery.git", from: "0.0.2")
```

Then, add Tracery as a dependency to your target(s):
```swift
.target(name: "App", dependencies: [..., "Tracery"])
```

## Basic usage


Expand Down
4 changes: 2 additions & 2 deletions Tracery.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Tracery"
s.version = "0.0.1"
s.version = "0.0.2"
s.summary = "Powerful extensible content generation library inspired by Tracery.io"

s.description = <<-DESC
Expand All @@ -20,4 +20,4 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/BenziAhamed/Tracery.git", :tag => "#{s.version}" }
s.source_files = "Common"

end
end
2 changes: 1 addition & 1 deletion Tracery/Tracery Tests/CandidateProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CandidateProvider: XCTestCase {
let candidates = ["jack","jill"]
func pick(count: Int) -> Int {
invokeCount += 1
return Int(arc4random_uniform(2))
return Int.random(in: 0..<2)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tracery/Tracery Tests/CustomProviders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class WeightedCandidateSet : RuleCandidatesProvider, RuleCandidateSelector {
}

func pick(count: Int) -> Int {
let choice = Int(arc4random_uniform(totalWeights) + 1) // since running weight start at 1
let choice = Int.random(in: 0...runningWeights)
for weight in runningWeights {
if choice <= weight.total {
return weight.target
Expand Down

0 comments on commit 2714eab

Please sign in to comment.