Skip to content

Commit

Permalink
Merge pull request #1089 from realm/jf-spm-integration
Browse files Browse the repository at this point in the history
SwiftPM integration
  • Loading branch information
johnfairh authored Sep 12, 2019
2 parents b28a02d + 8994b59 commit 7b377b7
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 39 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
* Update bundled jQuery to 3.4.1 (all themes).
[Paul Idstein](https://github.com/idstein)

* Support Xcode 11 Swift projects that pass a response file to the Swift
compiler.
[John Fairhurst](https://github.com/johnfairh)
[#1087](https://github.com/realm/jazzy/issues/1087)

* Generate Swift docs from a Swift Package Manager package without
requiring an Xcode project file. Add `--swift-build-tool` to choose
the build method if both `.xcodeproj` and `Package.swift` files are
present. Add `--build-tool-flags` as a preferred alias for
`--xcodebuild-flags`.
[John Fairhurst](https://github.com/johnfairh)
[#487](https://github.com/realm/jazzy/issues/487)

##### Bug Fixes

* Preserve non-latin characters in guide filenames and heading IDs.
Expand All @@ -24,6 +37,10 @@
[John Fairhurst](https://github.com/johnfairh)
[#945](https://github.com/realm/jazzy/issues/945)

* Fix crash on files with misplaced documentation comments.
[John Fairhurst](https://github.com/johnfairh)
[#1083](https://github.com/realm/jazzy/issues/1083)

## 0.10.0

##### Breaking
Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Both Swift and Objective-C projects are supported.

*Objective-C support was recently added, so please report any issues you find.*
*SwiftPM support was recently added, so please report any issues you find.*

Instead of parsing your source files, `jazzy` hooks into [Clang][clang] and
[SourceKit][sourcekit] to use the [AST][ast] representation of your code and
Expand All @@ -23,9 +23,8 @@ unacceptable behavior to [[email protected]](mailto:[email protected]).

## Requirements

* A version of [Xcode][xcode] capable of building the project you wish to
document. It must be installed in a location indexed by Spotlight for the
`--swift-version` configuration option to succeed.
* Development tools that can build the project you wish to document. Jazzy supports
both [Xcode][xcode] and [Swift Package Manager][spm] projects.

## Installation

Expand All @@ -41,8 +40,9 @@ common problems.
Run `jazzy` from your command line. Run `jazzy -h` for a list of additional options.

If your Swift module is the first thing to build, and it builds fine when running
`xcodebuild` without any arguments from the root of your project, then just running
`jazzy` (without any arguments) from the root of your project should succeed too!
`xcodebuild` or `swift build` without any arguments from the root of your project, then
just running `jazzy` (without any arguments) from the root of your project should
succeed too!

You can set options for your project’s documentation in a configuration file,
`.jazzy.yaml` by default. For a detailed explanation and an exhaustive list of
Expand Down Expand Up @@ -83,13 +83,22 @@ jazzy \
--github_url https://github.com/realm/realm-cocoa \
--github-file-prefix https://github.com/realm/realm-cocoa/tree/v0.96.2 \
--module-version 0.96.2 \
--xcodebuild-arguments -scheme,RealmSwift \
--build-tool-arguments -scheme,RealmSwift \
--module RealmSwift \
--root-url https://realm.io/docs/swift/0.96.2/api/ \
--output docs/swift_output \
--theme docs/themes
```

This is how docs are generated for a project that uses the Swift Package Manager:

```shell
jazzy \
--module DeckOfPlayingCards \
--swift-build-tool spm \
--build-tool-arguments -Xswiftc,-swift-version,-Xswiftc,5
```

### Objective-C

To generate documentation for Objective-C headers, you must pass the following
Expand All @@ -116,7 +125,7 @@ jazzy \
--github_url https://github.com/realm/realm-cocoa \
--github-file-prefix https://github.com/realm/realm-cocoa/tree/v2.2.0 \
--module-version 2.2.0 \
--xcodebuild-arguments --objc,Realm/Realm.h,--,-x,objective-c,-isysroot,$(xcrun --show-sdk-path),-I,$(pwd) \
--build-tool-arguments --objc,Realm/Realm.h,--,-x,objective-c,-isysroot,$(xcrun --show-sdk-path),-I,$(pwd) \
--module Realm \
--root-url https://realm.io/docs/objc/2.2.0/api/ \
--output docs/objc_output \
Expand Down Expand Up @@ -326,3 +335,4 @@ read [our blog](https://realm.io/news) or say hi on twitter
[SourceKitten]: https://github.com/jpsim/SourceKitten "SourceKitten"
[bundler]: https://rubygems.org/gems/bundler
[mustache]: https://mustache.github.io "Mustache"
[spm]: https://swift.org/package-manager/ "Swift Package Manager"
2 changes: 1 addition & 1 deletion SourceKitten
Submodule SourceKitten updated 38 files
+62 −0 CHANGELOG.md
+1 −1 Gemfile
+8 −8 Gemfile.lock
+1 −1 README.md
+31 −0 Source/SourceKittenFramework/CursorInfo+Parsing.swift
+113 −0 Source/SourceKittenFramework/Exec.swift
+2 −2 Source/SourceKittenFramework/File.swift
+1 −1 Source/SourceKittenFramework/Info.plist
+60 −12 Source/SourceKittenFramework/Module.swift
+14 −3 Source/SourceKittenFramework/Request.swift
+68 −48 Source/SourceKittenFramework/SourceKitObject.swift
+17 −0 Source/SourceKittenFramework/String+SourceKitten.swift
+0 −11 Source/SourceKittenFramework/SwiftDocKey.swift
+3 −9 Source/SourceKittenFramework/UID.swift
+23 −0 Source/SourceKittenFramework/UIDRepresentable.swift
+1 −1 Source/SourceKittenFramework/Version.swift
+5 −70 Source/SourceKittenFramework/Xcode.swift
+0 −4 Source/SourceKittenFramework/XcodeBuildSetting.swift
+56 −115 Source/SourceKittenFramework/library_wrapper.swift
+1 −0 Source/SourceKittenFramework/library_wrapper_CXString.swift
+1 −0 Source/SourceKittenFramework/library_wrapper_Documentation.swift
+1 −0 Source/SourceKittenFramework/library_wrapper_Index.swift
+1 −0 Source/SourceKittenFramework/library_wrapper_sourcekitd.swift
+26 −15 Source/sourcekitten/DocCommand.swift
+1 −1 Source/sourcekitten/Info.plist
+1 −1 SourceKittenFramework.podspec
+1 −0 Tests/LinuxMain.swift
+53 −0 Tests/SourceKittenFrameworkTests/CursorInfoParsingTests.swift
+37 −0 Tests/SourceKittenFrameworkTests/CursorInfoUSRTests.swift
+13 −0 Tests/SourceKittenFrameworkTests/Fixtures/[email protected]
+13 −0 Tests/SourceKittenFrameworkTests/Fixtures/[email protected]
+4 −0 Tests/SourceKittenFrameworkTests/Fixtures/ExternalRef1.swift
+3 −0 Tests/SourceKittenFrameworkTests/Fixtures/ExternalRef2.swift
+15 −42 Tests/SourceKittenFrameworkTests/ModuleTests.swift
+2 −35 Tests/SourceKittenFrameworkTests/SourceKitTests.swift
+12 −2 Tests/SourceKittenFrameworkTests/StringTests.swift
+13 −38 Tests/SourceKittenFrameworkTests/SwiftDocsTests.swift
+20 −0 sourcekitten.xcodeproj/project.pbxproj
Binary file modified bin/sourcekitten
Binary file not shown.
33 changes: 30 additions & 3 deletions lib/jazzy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def self.config_attr(name, **opts)
@all_config_attrs << Attribute.new(name, **opts)
end

def self.alias_config_attr(name, forward, **opts)
alias_method name.to_s, forward.to_s
alias_method "#{name}=", "#{forward}="
alias_method "#{name}_configured", "#{forward}_configured"
alias_method "#{name}_configured=", "#{forward}_configured="
@all_config_attrs << Attribute.new(name, **opts)
end

class << self
attr_reader :all_config_attrs
end
Expand Down Expand Up @@ -146,11 +154,16 @@ def expand_path(path)
'Default: .jazzy.yaml in source directory or ancestor'],
parse: ->(cf) { expand_path(cf) }

config_attr :xcodebuild_arguments,
command_line: ['-x', '--xcodebuild-arguments arg1,arg2,…argN', Array],
description: 'Arguments to forward to xcodebuild',
config_attr :build_tool_arguments,
command_line: ['-b', '--build-tool-arguments arg1,arg2,…argN', Array],
description: 'Arguments to forward to xcodebuild, swift build, or ' \
'sourcekitten.',
default: []

alias_config_attr :xcodebuild_arguments, :build_tool_arguments,
command_line: ['-x', '--xcodebuild-arguments arg1,arg2,…argN', Array],
description: 'Back-compatibility alias for build_tool_arguments.'

config_attr :sourcekitten_sourcefile,
command_line: ['-s', '--sourcekitten-sourcefile FILEPATH'],
description: 'File generated from sourcekitten output to parse',
Expand Down Expand Up @@ -192,6 +205,20 @@ def expand_path(path)
end
end

SWIFT_BUILD_TOOLS = %w[spm xcodebuild].freeze

config_attr :swift_build_tool,
command_line: "--swift-build-tool #{SWIFT_BUILD_TOOLS.join(' | ')}",
description: 'Control whether Jazzy uses Swift Package Manager or '\
'xcodebuild to build the module to be documented. By '\
'default it uses xcodebuild if there is a .xcodeproj '\
'file in the source directory.',
parse: ->(tool) do
return tool.to_sym if SWIFT_BUILD_TOOLS.include?(tool)
raise "Unsupported swift_build_tool #{tool}, "\
"supported values: #{SWIFT_BUILD_TOOLS.join(', ')}"
end

# ──────── Metadata ────────

config_attr :author_name,
Expand Down
20 changes: 6 additions & 14 deletions lib/jazzy/doc_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,13 @@ def self.doc_structure_for_docs(docs)
def self.build(options)
if options.sourcekitten_sourcefile
stdout = options.sourcekitten_sourcefile.read
elsif options.podspec_configured
pod_documenter = PodspecDocumenter.new(options.podspec)
stdout = pod_documenter.sourcekitten_output(options)
else
if options.podspec_configured
pod_documenter = PodspecDocumenter.new(options.podspec)
stdout = pod_documenter.sourcekitten_output(options)
else
stdout = Dir.chdir(options.source_directory) do
arguments = SourceKitten.arguments_from_options(options)
SourceKitten.run_sourcekitten(arguments)
end
end
unless $?.success?
warn 'Please pass in xcodebuild arguments using -x'
warn 'If build arguments are correct, please file an issue on ' \
'https://github.com/realm/jazzy/issues'
exit $?.exitstatus || 1
stdout = Dir.chdir(options.source_directory) do
arguments = SourceKitten.arguments_from_options(options)
SourceKitten.run_sourcekitten(arguments)
end
end

Expand Down
31 changes: 20 additions & 11 deletions lib/jazzy/sourcekitten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ def self.make_doc_urls(docs)
warn 'A compile error prevented ' + doc.fully_qualified_name +
' from receiving a unique USR. Documentation may be ' \
'incomplete. Please check for compile errors by running ' \
'`xcodebuild ' \
"#{Config.instance.xcodebuild_arguments.shelljoin}`."
'`xcodebuild` or `swift build` with arguments ' \
"`#{Config.instance.build_tool_arguments.shelljoin}`."
end
id = doc.usr
unless id
Expand Down Expand Up @@ -182,22 +182,31 @@ def self.rec_path(path)
end.select { |x| x }.flatten(1)
end

def self.use_spm?(options)
options.swift_build_tool == :spm ||
(!options.swift_build_tool_configured &&
Dir['*.xcodeproj'].empty?)
end

# Builds SourceKitten arguments based on Jazzy options
def self.arguments_from_options(options)
arguments = ['doc']
arguments += if options.objc_mode
objc_arguments_from_options(options)
elsif !options.module_name.empty?
['--module-name', options.module_name, '--']
else
['--']
end
arguments + options.xcodebuild_arguments
if options.objc_mode
arguments += objc_arguments_from_options(options)
else
arguments += ['--spm'] if use_spm?(options)
if options.module_name_configured
arguments += ['--module-name', options.module_name]
end
arguments += ['--']
end

arguments + options.build_tool_arguments
end

def self.objc_arguments_from_options(options)
arguments = []
if options.xcodebuild_arguments.empty?
if options.build_tool_arguments.empty?
arguments += ['--objc', options.umbrella_header.to_s, '--', '-x',
'objective-c', '-isysroot',
`xcrun --show-sdk-path --sdk #{options.sdk}`.chomp,
Expand Down
3 changes: 2 additions & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def configure_cocoapods
end

describe 'Creates docs for Swift project with a variety of contents' do
behaves_like cli_spec 'misc_jazzy_features'
behaves_like cli_spec 'misc_jazzy_features',
'-b -Xswiftc,-swift-version,-Xswiftc,4.2'
end
end if !spec_subset || spec_subset == 'swift'

Expand Down

0 comments on commit 7b377b7

Please sign in to comment.