Skip to content

Commit

Permalink
added support for Android and Linux by using split and enumerated() f…
Browse files Browse the repository at this point in the history
…unc instead of old @objc enumerateLines func
  • Loading branch information
marekpridal committed Apr 23, 2018
1 parent 022377d commit 398abf7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ let package = Package(
.target(
name: "GherkinDoc",
dependencies: [],
sources: ["main.swift","Constants.swift","Generator.swift","HtmlMaker.swift","PhraseWithLineNumber.swift","SectionWithSteps.swift","StepWithComment.swift"]),
sources: ["main.swift","Constants.swift","Generator.swift","HtmlMaker.swift","PhraseWithLineNumber.swift","SectionWithSteps.swift","StepWithComment.swift","Platform.swift"]),
]
)
39 changes: 27 additions & 12 deletions Sources/GherkinDoc/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,25 @@ class Generator {
- returns:
Path to generated docs - Optional
*/
func generateDocs()throws -> URL {
func generateDocs(for platform:Platform)throws -> URL {

let contentOfFile = try String(contentsOf: path)

getParsedComments(for: contentOfFile).forEach { (value) in
comments.append(PhraseWithLineNumber(phrase: value.replacingOccurrences(of: "\n", with: ""), lineNumber: nil))
}

getParsedSteps(for: contentOfFile).forEach { (value) in
steps.append(PhraseWithLineNumber(phrase: value.replacingOccurrences(of: "\n", with: ""), lineNumber: nil))
}
switch platform {
case .Android:
getParsedStepsForAndroid(with: contentOfFile).forEach { (value) in
steps.append(PhraseWithLineNumber(phrase: value.replacingOccurrences(of: "\n", with: ""), lineNumber: nil))

}
case .iOS:
getParsedStepsForiOS(with: contentOfFile).forEach { (value) in
steps.append(PhraseWithLineNumber(phrase: value.replacingOccurrences(of: "\n", with: ""), lineNumber: nil))
}
}

getParsedSections(for: contentOfFile).forEach { (value) in
sections.append(PhraseWithLineNumber(phrase: value.replacingOccurrences(of: "\n", with: ""), lineNumber: nil))
Expand All @@ -59,7 +67,14 @@ class Generator {

stepsWithComments = getStepsWithComments(steps: steps, comments: comments, sections: sections)

stepsWithSections = getSections(stepsWithComments: stepsWithComments, sections: sections).map{ SectionWithSteps(name: $0.name,steps: $0.steps.map{ StepWithComment(step: $0.step.replacingOccurrences(of: "(.*)", with: "VALUE"), comment: $0.comment, stepLine: $0.stepLine, section: $0.section) }, line: $0.line) }
switch platform {
case .Android:
stepsWithSections = getSections(stepsWithComments: stepsWithComments, sections: sections).map{ SectionWithSteps(name: $0.name,steps: $0.steps.map{ StepWithComment(step: $0.step.replacingOccurrences(of: "\\\"([^\\\"]*)\\\"", with: "VALUE").replacingOccurrences(of: "\\\"(.+)\\\"", with: "VALUE").replacingOccurrences(of: "\"", with: "").replacingOccurrences(of: "^", with: "").replacingOccurrences(of: "$", with: ""), comment: $0.comment, stepLine: $0.stepLine, section: $0.section) }, line: $0.line) }
case .iOS:
stepsWithSections = getSections(stepsWithComments: stepsWithComments, sections: sections).map{ SectionWithSteps(name: $0.name,steps: $0.steps.map{ StepWithComment(step: $0.step.replacingOccurrences(of: "(.*)", with: "VALUE"), comment: $0.comment, stepLine: $0.stepLine, section: $0.section) }, line: $0.line) }
}


print(comments.count < steps.count ? "\n⚠️ Some steps are missing comments\n" : "")
print("Number of steps \(steps.count)\n")
print("Undocumented steps \(steps.count - comments.count)" + " " + (steps.count - comments.count == 0 ? "👍" : "👎"))
Expand All @@ -84,21 +99,21 @@ class Generator {
}

private func getLinesWithLineCount(contentOfFile:String, lines:[PhraseWithLineNumber]) -> [PhraseWithLineNumber] {
var lineNumber = 0
var result:[PhraseWithLineNumber] = lines

contentOfFile.enumerateLines { (line, _) in
lineNumber += 1
result = result.map{ line.contains($0.phrase) ? PhraseWithLineNumber(phrase: $0.phrase, lineNumber: lineNumber) : $0 }
contentOfFile.split(separator: "\n").enumerated().forEach { (offset,line) in
result = result.map{ line.contains($0.phrase) ? PhraseWithLineNumber(phrase: $0.phrase, lineNumber: offset) : $0 }
}

return result
}

private func getParsedSteps(for fileContent:String) -> [String] {
private func getParsedStepsForiOS(with fileContent:String) -> [String] {
return matches(for: "step(.*)", in: fileContent).map{ matches(for: "\".*\"", in: $0).first }.filter{ !($0?.isEmpty ?? true) }.compactMap{ $0?.replacingOccurrences(of: "\"", with: "") }
}

private func getParsedStepsForAndroid(with fileContent:String) -> [String] {
return matches(for: "(\"\\^(.*)\\$\")", in: fileContent)
}

private func getParsedComments(for fileContent:String) -> [String] {
return matches(for: "/\\*(.*)\\*/", in: fileContent).compactMap{ $0.replacingOccurrences(of: "/* ", with: "").replacingOccurrences(of: " */", with: "") }
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/GherkinDoc/Platform.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Platform.swift
// GherkinDoc
//
// Created by Marek Přidal on 23.04.18.
//

import Foundation

enum Platform: String {
case Android = "android"
case iOS = "ios"

static func getPlatformForFileExtension(_ pathExtension:String) -> Platform? {
switch pathExtension.lowercased()
{
case "java":
return .Android
case "swift":
return .iOS
default:
return nil
}
}
}
46 changes: 38 additions & 8 deletions Sources/GherkinDoc/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,59 @@
import Foundation

//MARK: Funcs
func makeDoc(with path: URL) {
func makeDoc(with path: URL, platform: Platform) {
let generator = Generator(pathToSteps: path)

do {
let filePath = try generator.generateDocs()
let filePath = try generator.generateDocs(for: platform)
print("File is located at path \(filePath.absoluteString)")
} catch let e {
print(e.localizedDescription)
}
}

//MARK: Main body
if CommandLine.argc == 2 {
let path = CommandLine.arguments[1]
makeDoc(with: URL(fileURLWithPath: path))
if CommandLine.argc == 3 {
let platformString = CommandLine.arguments[1]
let pathString = CommandLine.arguments[2]
if let platform = Platform.getPlatformForFileExtension(URL(fileURLWithPath: pathString).pathExtension) {
makeDoc(with: URL(fileURLWithPath: pathString), platform: platform)
} else {
guard let platform = Platform(rawValue: platformString.lowercased()) else {
print("⚠️ Unsupported platform, use android or ios identifier")
exit(1)
}
makeDoc(with: URL(fileURLWithPath: pathString), platform: platform)
}
exit(0)
} else if CommandLine.argc == 2 {
let pathString = CommandLine.arguments[1]
if let platform = Platform.getPlatformForFileExtension(URL(fileURLWithPath: pathString).pathExtension) {
makeDoc(with: URL(fileURLWithPath: pathString), platform: platform)
} else {
print("What's yout platform?")
guard let platformString = readLine(), let platform = Platform(rawValue: platformString.lowercased()) else {
print("⚠️ Unsupported platform, use android or ios identifier")
exit(1)
}
makeDoc(with: URL(fileURLWithPath: pathString), platform: platform)
}
exit(0)
}


print("What's the path of UITestStepDefinitions file?")
guard let path = readLine() else {
print("Cannot get path")
exit(1)
}

makeDoc(with: URL(fileURLWithPath: path))
if let platform = Platform.getPlatformForFileExtension(URL(fileURLWithPath: path).pathExtension) {
makeDoc(with: URL(fileURLWithPath: path), platform: platform)
} else {
print("What's yout platform?")
guard let platformString = readLine(), let platform = Platform(rawValue: platformString.lowercased()) else {
print("⚠️ Unsupported platform, use android or ios identifier")
exit(1)
}
makeDoc(with: URL(fileURLWithPath: path), platform: platform)
}

8 changes: 4 additions & 4 deletions make.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

swift build
chmod 700 .build/debug/GherkinDoc
cp .build/debug/GherkinDoc /usr/local/bin/GherkinDoc
echo "Run GherkinDoc anywhere by typing GherkinDoc"
swift build -c release
sudo chmod 700 .build/release/GherkinDoc
sudo cp .build/release/GherkinDoc /usr/local/bin/GherkinDoc
echo "Run generator anywhere by typing GherkinDoc"

0 comments on commit 398abf7

Please sign in to comment.