Skip to content

Commit

Permalink
add way to generate docs for an SPM module
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed May 28, 2016
1 parent 7f2f54b commit 5fed121
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Source/SourceKittenFramework/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Yaml

/// Represents source module to be documented.
public struct Module {
Expand All @@ -33,6 +34,30 @@ public struct Module {
}
}

public init?(spmName: String) {
let yamlPath = ".build/debug.yaml"
guard let yamlContents = try? String(contentsOfFile: yamlPath),
yamlCommands = Yaml.load(yamlContents).value?.dictionary?["commands"]?.dictionary?.values else {
return nil
}
guard let moduleCommand = yamlCommands.filter({ command in
command.dictionary?["module-name"]?.string == spmName
}).first?.dictionary else {
return nil
}
func stringArray(key: Yaml) -> [String]? {
return moduleCommand[key]?.array?.flatMap { $0.string }
}
guard let imports = stringArray("import-paths"),
otherArguments = stringArray("other-args"),
sources = stringArray("sources") else {
return nil
}
name = spmName
compilerArguments = sources + otherArguments + ["-I"] + imports
sourceFiles = sources
}

/**
Failable initializer to create a Module by the arguments necessary pass in to `xcodebuild` to build it.
Optionally pass in a `moduleName` and `path`.
Expand Down
18 changes: 13 additions & 5 deletions Source/sourcekitten/DocCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ struct DocCommand: CommandType {
let function = "Print Swift docs as JSON or Objective-C docs as XML"

struct Options: OptionsType {
let spmModule: String
let singleFile: Bool
let moduleName: String
let objc: Bool
let arguments: [String]

static func create(singleFile: Bool) -> (moduleName: String) -> (objc: Bool) -> (arguments: [String]) -> Options {
return { moduleName in { objc in { arguments in
self.init(singleFile: singleFile, moduleName: moduleName, objc: objc, arguments: arguments)
}}}
static func create(spmModule: String) -> (singleFile: Bool) -> (moduleName: String) -> (objc: Bool) -> (arguments: [String]) -> Options {
return { singleFile in { moduleName in { objc in { arguments in
self.init(spmModule: spmModule, singleFile: singleFile, moduleName: moduleName, objc: objc, arguments: arguments)
}}}}
}

static func evaluate(m: CommandMode) -> Result<Options, CommandantError<SourceKittenError>> {
return create
<*> m <| Option(key: "spm-module", defaultValue: "", usage: "document a Swift Package Manager module")
<*> m <| Option(key: "single-file", defaultValue: false, usage: "only document one file")
<*> m <| Option(key: "module-name", defaultValue: "", usage: "name of module to document (can't be used with `--single-file` or `--objc`)")
<*> m <| Option(key: "objc", defaultValue: false, usage: "document Objective-C headers")
Expand All @@ -38,7 +40,13 @@ struct DocCommand: CommandType {

func run(options: Options) -> Result<(), SourceKittenError> {
let args = options.arguments
if options.objc {
if !options.spmModule.isEmpty {
if let docs = Module(spmName: options.spmModule)?.docs {
print(docs)
return .Success()
}
return .Failure(.DocFailed)
} else if options.objc {
return runObjC(options, args: args)
} else if options.singleFile {
return runSwiftSingleFile(args)
Expand Down

0 comments on commit 5fed121

Please sign in to comment.