Skip to content

Commit 78ee7aa

Browse files
committed
Adding mdbook support
1 parent 8583740 commit 78ee7aa

File tree

4 files changed

+315
-0
lines changed

4 files changed

+315
-0
lines changed

.env

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MDBOOK_VERSION=0.4.6
2+
MDBOOK_LINKCHECK_VERSION=0.7.2
3+
MDBOOK_TOC_VERSION=0.6.1
4+
5+
GIT_DEPLOY_DIR=book
6+
GIT_DEPLOY_BRANCH=gh-pages
7+
GIT_DEPLOY_REPO=https://github.com/simonsan/std-dev-guide

.github/workflows/gh-pages.yml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: github pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- mdbook
8+
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-20.04
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Read .env
16+
id: mdbook-version
17+
run: |
18+
. ./.env
19+
echo "::set-output name=MDBOOK_VERSION::${MDBOOK_VERSION}"
20+
echo "::set-output name=MDBOOK_LINKCHECK_VERSION::${MDBOOK_LINKCHECK_VERSION}"
21+
echo "::set-output name=MDBOOK_TOC_VERSION::${MDBOOK_TOC_VERSION}"
22+
23+
- name: Cache binaries
24+
id: mdbook-cache
25+
uses: actions/cache@v2
26+
with:
27+
path: |
28+
~/.cargo/bin
29+
key: ${{ runner.os }}-${{ steps.mdbook-version.outputs.MDBOOK_VERSION }}--${{ steps.mdbook-version.outputs.DBOOK_LINKCHECK_VERSION }}--${{ steps.mdbook-version.outputs.MDBOOK_TOC_VERSION }}
30+
31+
- name: Install latest stable Rust toolchain
32+
if: steps.mdbook-cache.outputs.cache-hit != 'true'
33+
uses: actions-rs/toolchain@v1
34+
with:
35+
toolchain: stable
36+
override: true
37+
38+
- name: Install Dependencies
39+
if: steps.mdbook-cache.outputs.cache-hit != 'true'
40+
run: |
41+
cargo install mdbook --version ${{ steps.mdbook-version.outputs.MDBOOK_VERSION }}
42+
cargo install mdbook-linkcheck --version ${{ steps.mdbook-version.outputs.MDBOOK_LINKCHECK_VERSION }}
43+
cargo install mdbook-toc --version ${{ steps.mdbook-version.outputs.MDBOOK_TOC_VERSION }}
44+
45+
- name: Build book
46+
run: mdbook build
47+
48+
- name: Deploy to gh-pages
49+
run: |
50+
git config --global user.email "Runner@GH Actions Deployment"
51+
git config --global user.name "GH Actions Runner"
52+
chmod u+x ci/ghpages-deploy.sh
53+
./ci/ghpages-deploy.sh
54+
55+
# git branch gh-pages
56+
# sed -i 's/book/#book/' .gitignore
57+
# rm book/.nojekyll
58+
# git add book/
59+
# git commit -am "MDBook Deployment"
60+
# git subtree split -P book -b gh-pages
61+
# git checkout gh-pages
62+
# git push -f https://github.com/simonsan/std-dev-guide gh-pages:gh-pages

book.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[book]
2+
title = "Standard library developers Guide"
3+
authors = ["the rust-lang authors"]
4+
description = "Guide for standard library developers"
5+
language = "en"
6+
multilingual = false
7+
src = "./src"
8+
9+
[build]
10+
create-missing = false
11+
12+
[rust]
13+
edition = "2018"
14+
15+
[output.html]
16+
default-theme = "rust"
17+
git-repository-url = "https://github.com/rust-lang/std-dev-guide"
18+
git-repository-icon = "fa-github"
19+
20+
# [output.linkcheck] # enable the "mdbook-linkcheck" renderer, disabled due to gh-actions

ci/ghpages-deploy.sh

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#!/usr/bin/env bash
2+
3+
# This file was taken from https://github.com/X1011/git-directory-deploy/blob/e37ac94cda4bfc5773c0f01d89d8c875a21ab4f9/deploy.sh,
4+
# Copyright 2013-2021 Daniel Smith
5+
# All rights reserved.
6+
# It's licensed under the terms of the 3-clause BSD license.
7+
8+
set -o errexit #abort if any command fails
9+
me=$(basename "$0")
10+
11+
help_message="\
12+
Usage: $me [-c FILE] [<options>]
13+
Deploy generated files to a git branch.
14+
15+
Options:
16+
17+
-h, --help Show this help information.
18+
-v, --verbose Increase verbosity. Useful for debugging.
19+
-e, --allow-empty Allow deployment of an empty directory.
20+
-m, --message MESSAGE Specify the message used when committing on the
21+
deploy branch.
22+
-n, --no-hash Don't append the source commit's hash to the deploy
23+
commit's message.
24+
-c, --config-file PATH Override default & environment variables' values
25+
with those in set in the file at 'PATH'. Must be the
26+
first option specified.
27+
28+
Variables:
29+
30+
GIT_DEPLOY_DIR Folder path containing the files to deploy.
31+
GIT_DEPLOY_BRANCH Commit deployable files to this branch.
32+
GIT_DEPLOY_REPO Push the deploy branch to this repository.
33+
34+
These variables have default values defined in the script. The defaults can be
35+
overridden by environment variables. Any environment variables are overridden
36+
by values set in a '.env' file (if it exists), and in turn by those set in a
37+
file specified by the '--config-file' option."
38+
39+
parse_args() {
40+
# Set args from a local environment file.
41+
if [ -e ".env" ]; then
42+
source .env
43+
fi
44+
45+
# Set args from file specified on the command-line.
46+
if [[ $1 = "-c" || $1 = "--config-file" ]]; then
47+
source "$2"
48+
shift 2
49+
fi
50+
51+
# Parse arg flags
52+
# If something is exposed as an environment variable, set/overwrite it
53+
# here. Otherwise, set/overwrite the internal variable instead.
54+
while : ; do
55+
if [[ $1 = "-h" || $1 = "--help" ]]; then
56+
echo "$help_message"
57+
return 0
58+
elif [[ $1 = "-v" || $1 = "--verbose" ]]; then
59+
verbose=true
60+
shift
61+
elif [[ $1 = "-e" || $1 = "--allow-empty" ]]; then
62+
allow_empty=true
63+
shift
64+
elif [[ ( $1 = "-m" || $1 = "--message" ) && -n $2 ]]; then
65+
commit_message=$2
66+
shift 2
67+
elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then
68+
GIT_DEPLOY_APPEND_HASH=false
69+
shift
70+
else
71+
break
72+
fi
73+
done
74+
75+
# Set internal option vars from the environment and arg flags. All internal
76+
# vars should be declared here, with sane defaults if applicable.
77+
78+
# Source directory & target branch.
79+
deploy_directory=${GIT_DEPLOY_DIR:-dist}
80+
deploy_branch=${GIT_DEPLOY_BRANCH:-gh-pages}
81+
82+
#if no user identity is already set in the current git environment, use this:
83+
default_username=${GIT_DEPLOY_USERNAME:-deploy.sh}
84+
default_email=${GIT_DEPLOY_EMAIL:-}
85+
86+
#repository to deploy to. must be readable and writable.
87+
repo=${GIT_DEPLOY_REPO:-origin}
88+
89+
#append commit hash to the end of message by default
90+
append_hash=${GIT_DEPLOY_APPEND_HASH:-true}
91+
}
92+
93+
main() {
94+
parse_args "$@"
95+
96+
enable_expanded_output
97+
98+
if ! git diff --exit-code --quiet --cached; then
99+
echo Aborting due to uncommitted changes in the index >&2
100+
return 1
101+
fi
102+
103+
commit_title=`git log -n 1 --format="%s" HEAD`
104+
commit_hash=` git log -n 1 --format="%H" HEAD`
105+
106+
#default commit message uses last title if a custom one is not supplied
107+
if [[ -z $commit_message ]]; then
108+
commit_message="publish: $commit_title"
109+
fi
110+
111+
#append hash to commit message unless no hash flag was found
112+
if [ $append_hash = true ]; then
113+
commit_message="$commit_message"$'\n\n'"generated from commit $commit_hash"
114+
fi
115+
116+
previous_branch=`git rev-parse --abbrev-ref HEAD`
117+
118+
if [ ! -d "$deploy_directory" ]; then
119+
echo "Deploy directory '$deploy_directory' does not exist. Aborting." >&2
120+
return 1
121+
fi
122+
123+
# must use short form of flag in ls for compatibility with OS X and BSD
124+
if [[ -z `ls -A "$deploy_directory" 2> /dev/null` && -z $allow_empty ]]; then
125+
echo "Deploy directory '$deploy_directory' is empty. Aborting. If you're sure you want to deploy an empty tree, use the --allow-empty / -e flag." >&2
126+
return 1
127+
fi
128+
129+
if git ls-remote --exit-code $repo "refs/heads/$deploy_branch" ; then
130+
# deploy_branch exists in $repo; make sure we have the latest version
131+
132+
disable_expanded_output
133+
git fetch --force $repo $deploy_branch:$deploy_branch
134+
enable_expanded_output
135+
fi
136+
137+
# check if deploy_branch exists locally
138+
if git show-ref --verify --quiet "refs/heads/$deploy_branch"
139+
then incremental_deploy
140+
else initial_deploy
141+
fi
142+
143+
restore_head
144+
}
145+
146+
initial_deploy() {
147+
git --work-tree "$deploy_directory" checkout --orphan $deploy_branch
148+
git --work-tree "$deploy_directory" add --all
149+
commit+push
150+
}
151+
152+
incremental_deploy() {
153+
#make deploy_branch the current branch
154+
git symbolic-ref HEAD refs/heads/$deploy_branch
155+
#put the previously committed contents of deploy_branch into the index
156+
git --work-tree "$deploy_directory" reset --mixed --quiet
157+
git --work-tree "$deploy_directory" add --all
158+
159+
set +o errexit
160+
diff=$(git --work-tree "$deploy_directory" diff --exit-code --quiet HEAD --)$?
161+
set -o errexit
162+
case $diff in
163+
0) echo No changes to files in $deploy_directory. Skipping commit.;;
164+
1) commit+push;;
165+
*)
166+
echo git diff exited with code $diff. Aborting. Staying on branch $deploy_branch so you can debug. To switch back to master, use: git symbolic-ref HEAD refs/heads/master && git reset --mixed >&2
167+
return $diff
168+
;;
169+
esac
170+
}
171+
172+
commit+push() {
173+
set_user_id
174+
git --work-tree "$deploy_directory" commit -m "$commit_message"
175+
176+
disable_expanded_output
177+
#--quiet is important here to avoid outputting the repo URL, which may contain a secret token
178+
git push --quiet $repo $deploy_branch
179+
enable_expanded_output
180+
}
181+
182+
#echo expanded commands as they are executed (for debugging)
183+
enable_expanded_output() {
184+
if [ $verbose ]; then
185+
set -o xtrace
186+
set +o verbose
187+
fi
188+
}
189+
190+
#this is used to avoid outputting the repo URL, which may contain a secret token
191+
disable_expanded_output() {
192+
if [ $verbose ]; then
193+
set +o xtrace
194+
set -o verbose
195+
fi
196+
}
197+
198+
set_user_id() {
199+
if [[ -z `git config user.name` ]]; then
200+
git config user.name "$default_username"
201+
fi
202+
if [[ -z `git config user.email` ]]; then
203+
git config user.email "$default_email"
204+
fi
205+
}
206+
207+
restore_head() {
208+
if [[ $previous_branch = "HEAD" ]]; then
209+
#we weren't on any branch before, so just set HEAD back to the commit it was on
210+
git update-ref --no-deref HEAD $commit_hash $deploy_branch
211+
else
212+
git symbolic-ref HEAD refs/heads/$previous_branch
213+
fi
214+
215+
git reset --mixed
216+
}
217+
218+
filter() {
219+
sed -e "s|$repo|\$repo|g"
220+
}
221+
222+
sanitize() {
223+
"$@" 2> >(filter 1>&2) | filter
224+
}
225+
226+
[[ $1 = --source-only ]] || main "$@"

0 commit comments

Comments
 (0)