aboutsummaryrefslogtreecommitdiff
path: root/buildroot/share/git/mfpub
blob: 6a912e5515b0d64b5545dfc4fdd3502722762c75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
#
# mfpub
#
# Use Jekyll to generate Marlin Documentation, which is then
# git-pushed to Github to publish it to the live site.
# This publishes the current branch, and doesn't force
# changes to be pushed to the 'master' branch. Be sure to
# push any permanent changes to 'master'.
#

[[ $# < 2 && $1 != "-h" && $1 != "--help" ]] || { echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1; }

MFINFO=$(mfinfo "$@") || exit 1
IFS=' ' read -a INFO <<< "$MFINFO"
ORG=${INFO[0]}
FORK=${INFO[1]}
REPO=${INFO[2]}
TARG=${INFO[3]}
BRANCH=${INFO[4]}
CURR=${INFO[5]}

if [[ $ORG != "MarlinFirmware" || $REPO != "MarlinDocumentation" ]]; then
  echo "Wrong repository."
  exit
fi

if [[ $BRANCH == "gh-pages" ]]; then
  echo "Can't build from 'gh-pages.' Only the Jekyll branches (based on 'master')."
  exit
fi

# Check out the named branch (or stay in current)

if [[ $BRANCH != $CURR ]]; then
  echo "Stashing any changes to files..."
  [[ $(git stash) != "No local "* ]] && HAS_STASH=1
  git checkout $BRANCH
fi

COMMIT=$( git log --format="%H" -n 1 )

# Clean out changes and other junk in the branch
git clean -d -f

opensite() {
  URL="$1"
  OPEN=$(echo $(which gnome-open xdg-open open) | awk '{ print $1 }')
  if [ -z "$OPEN" ]; then
    echo "Can't find a tool to open the URL:"
    echo $URL
  else
    echo "Opening the site in the browser..."
    "$OPEN" "$URL"
  fi
}

# Push 'master' to the fork and make a proper PR...
if [[ $BRANCH == $TARG ]]; then

  # Don't lose upstream changes!
  git fetch upstream

  # Rebase onto latest master
  if git rebase upstream/$TARG; then

    # Allow working directly with the main fork
    echo
    echo -n "Pushing to origin/$TARG... "
    git push origin HEAD:$TARG

    echo
    echo -n "Pushing to upstream/$TARG... "
    git push upstream HEAD:$TARG

  else

    echo "Merge conflicts? Stopping here."
    exit

  fi

else

  if [ -z "$(git branch -vv | grep ^\* | grep \\\[origin)" ]; then
    firstpush
  else
    echo
    echo -n "Pushing to origin/$BRANCH... "
    git push -f origin
  fi

  opensite "https://github.com/$ORG/$REPO/compare/$TARG...$FORK:$BRANCH?expand=1"

fi

# Uncomment to compress the final html files
# mv ./_plugins/jekyll-press.rb-disabled ./_plugins/jekyll-press.rb
# bundle install

echo
echo "Generating MarlinDocumentation..."

rm -rf build

# build the site statically and proof it
bundle exec jekyll build --profile --trace --no-watch
bundle exec htmlproofer ./build --only-4xx --allow-hash-href --check-favicon --check-html --url-swap ".*marlinfw.org/:/"

# Sync the built site into a temporary folder
TMPFOLDER=$( mktemp -d )
rsync -av build/ ${TMPFOLDER}/

# Clean out changes and other junk in the branch
git reset --hard
git clean -d -f

# Copy built-site into the gh-pages branch
git checkout gh-pages || { echo "Something went wrong!"; exit 1; }
rsync -av ${TMPFOLDER}/ ./

# Commit and push the new live site directly
git add --all
git commit --message "Built from ${COMMIT}"
git push -f origin
git push -f upstream | {
  while IFS= read -r line
  do
    [[ $line =~ "gh-pages -> gh-pages" ]] && opensite "https://marlinfw.org/"
    echo "$line"
  done
}

# remove the temporary folder
rm -rf ${TMPFOLDER}

# Go back to the branch we started from
git checkout $CURR && [[ $HAS_STASH == 1 ]] && git stash pop