mfpub 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bash
  2. #
  3. # mfpub
  4. #
  5. # Use Jekyll to generate Marlin Documentation, which is then
  6. # git-pushed to Github to publish it to the live site.
  7. # This publishes the current branch, and doesn't force
  8. # changes to be pushed to the 'master' branch. Be sure to
  9. # push any permanent changes to 'master'.
  10. #
  11. [[ $# < 2 && $1 != "-h" && $1 != "--help" ]] || { echo "Usage: `basename $0` [branch]" 1>&2 ; exit 1; }
  12. MFINFO=$(mfinfo "$@") || exit 1
  13. IFS=' ' read -a INFO <<< "$MFINFO"
  14. ORG=${INFO[0]}
  15. FORK=${INFO[1]}
  16. REPO=${INFO[2]}
  17. TARG=${INFO[3]}
  18. BRANCH=${INFO[4]}
  19. CURR=${INFO[5]}
  20. if [[ $ORG != "MarlinFirmware" || $REPO != "MarlinDocumentation" ]]; then
  21. echo "Wrong repository."
  22. exit
  23. fi
  24. if [[ $BRANCH == "gh-pages" ]]; then
  25. echo "Can't build from 'gh-pages.' Only the Jekyll branches (based on 'master')."
  26. exit
  27. fi
  28. # Check out the named branch (or stay in current)
  29. if [[ $BRANCH != $CURR ]]; then
  30. echo "Stashing any changes to files..."
  31. [[ $(git stash) != "No local "* ]] && HAS_STASH=1
  32. git checkout $BRANCH
  33. fi
  34. COMMIT=$( git log --format="%H" -n 1 )
  35. # Clean out changes and other junk in the branch
  36. git clean -d -f
  37. opensite() {
  38. URL="$1"
  39. OPEN=$(echo $(which gnome-open xdg-open open) | awk '{ print $1 }')
  40. if [ -z "$OPEN" ]; then
  41. echo "Can't find a tool to open the URL:"
  42. echo $URL
  43. else
  44. echo "Opening the site in the browser..."
  45. "$OPEN" "$URL"
  46. fi
  47. }
  48. # Push 'master' to the fork and make a proper PR...
  49. if [[ $BRANCH == $TARG ]]; then
  50. # Don't lose upstream changes!
  51. git fetch upstream
  52. # Rebase onto latest master
  53. if git rebase upstream/$TARG; then
  54. # Allow working directly with the main fork
  55. echo
  56. echo -n "Pushing to origin/$TARG... "
  57. git push -f origin
  58. echo
  59. echo -n "Pushing to upstream/$TARG... "
  60. git push -f upstream
  61. else
  62. echo "Merge conflicts? Stopping here."
  63. exit
  64. fi
  65. else
  66. if [ -z "$(git branch -vv | grep ^\* | grep \\\[origin)" ]; then
  67. firstpush
  68. else
  69. echo
  70. echo -n "Pushing to origin/$BRANCH... "
  71. git push -f origin
  72. fi
  73. opensite "https://github.com/$ORG/$REPO/compare/$TARG...$FORK:$BRANCH?expand=1"
  74. fi
  75. # Uncomment to compress the final html files
  76. # mv ./_plugins/jekyll-press.rb-disabled ./_plugins/jekyll-press.rb
  77. # bundle install
  78. echo
  79. echo "Generating MarlinDocumentation..."
  80. rm -rf build
  81. # build the site statically and proof it
  82. bundle exec jekyll build --profile --trace --no-watch
  83. bundle exec htmlproofer ./build --only-4xx --allow-hash-href --check-favicon --check-html --url-swap ".*marlinfw.org/:/"
  84. # Sync the built site into a temporary folder
  85. TMPFOLDER=$( mktemp -d )
  86. rsync -av build/ ${TMPFOLDER}/
  87. # Clean out changes and other junk in the branch
  88. git reset --hard
  89. git clean -d -f
  90. # Copy built-site into the gh-pages branch
  91. git checkout gh-pages || { echo "Something went wrong!"; exit 1; }
  92. rsync -av ${TMPFOLDER}/ ./
  93. # Commit and push the new live site directly
  94. git add --all
  95. git commit --message "Built from ${COMMIT}"
  96. git push -f origin
  97. git push -f upstream | {
  98. while IFS= read -r line
  99. do
  100. [[ $line =~ "gh-pages -> gh-pages" ]] && opensite "http://marlinfw.org/"
  101. echo "$line"
  102. done
  103. }
  104. # remove the temporary folder
  105. rm -rf ${TMPFOLDER}
  106. # Go back to the branch we started from
  107. [[ $BRANCH != $CURR ]] && git checkout $BRANCH && [[ $HAS_STASH == 1 ]] && git stash pop