6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

swift-doccでライブラリのドキュメントをGitHub Pagesにデプロイするまで

Last updated at Posted at 2023-10-23

今回は、自分が開発したライブラリのドキュメントを、swift-doccを使用してGitHub Pagesにデプロイするまでの過程をご紹介したいと思います。

最近、私は「swiftui-simplex-architecture」という新しいSwiftライブラリをリリースしました。これは、TCAのようなアーキテクチャのライブラリで、SwiftUIを使用したアプリ開発をよりシンプルで効率的に行うことを目指したものとなっています。

より詳しいAPIのドキュメントを公開したかったので、Appleが提供しているswift-doccを使用してドキュメントを生成し、GitHub Pagesを通じて公開することにしました。

実装

実装はめちゃくちゃ簡単でした。このようなステップで簡単に公開が可能です.

  1. GitHub Pagesの設定
  2. swift-docc-pluginを使用してローカルでドキュメントを生成
  3. GitHubへpush

GitHub Pagesの設定

まずはリポジトリのSettingsをクリックします。

スクリーンショット 2023-10-23 20.10.13.png

次にPagesをクリックします。

設定をこのようにします。

スクリーンショット 2023-10-23 20.18.47.png

このようにすることで、mainブランチにpushされたら自動でdocsディレクトリにある内容がdeployされます。

swift-docc-plugin

まずは、ドキュメントを生成したいライブラリのPackage.swiftにswift-docc-pluginを追加します。

let package = Package(
    name: "swiftui-simplex-architecture",
    platforms: [
        ...
    ],
    products: [
        ...
    ],
    dependencies: [
        ...
        .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
    ],
    ...
)

そして、コマンドを実行します。再度実行しやすいように、Makefileにこのように記述します。

.PHONY: docc
docc:
	swift package --allow-writing-to-directory ./docs \
		generate-documentation --target SimplexArchitecture \
		--disable-indexing \
		--transform-for-static-hosting \
		--hosting-base-path swiftui-simplex-architecture \
		--output-path ./docs

--targetにはドキュメントを生成したいライブラリのターゲットを、
--hosting-base-pathにはリポジトリ名を、
--allow-writing-to-directory--output-pathには、docsディレクトリへのパスを指定します。

$ make docc

で実行し、差分をmainにpushします。
すると、

https://{ユーザー名}.github.io/{リポジトリ名}/documentation/{ターゲット名}/

こちらのURLにドキュメントがデプロイされます!!

今回私はswiftui-simplex-architectureリポジトリのターゲットがSimlexArchitectureをデプロイしたので、

https://ryu0118.github.io/swiftui-simplex-architecture/documentation/simplexarchitecture/

こちらのURLにデプロイされました🎉

DocCのローカルでのプレビュー

ローカルでプレビューすることも可能です。

.PHONY: docc-preview
docc-preview:
	swift package --disable-sandbox preview-documentation --target {ターゲット名}

これを実行すると、
スクリーンショット 2023-10-23 20.27.41.png
このようにローカルサーバーが起動し、
http://localhost:8080/documentation/simplexarchitecture
にアクセスすると、プレビューが見れます。

さいごに

swift-doccでドキュメントを生成してGithub Pagesにデプロイするのめんどくさいだろうな...と考えていましたが、
めちゃくちゃ簡単にできてよかったです。
まだドキュメントが貧弱なので、TCA並に強化していきたいです!

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?