name: hugo
issues: https://github.com/gohugoio/hugo/issues
source-code: https://github.com/gohugoio/hugo.git
website: https://gohugo.io/
summary: Fast and Flexible Static Site Generator
description: |
  Hugo is a static HTML and CSS website generator written in Go. It is
  optimized for speed, easy use and configurability. Hugo takes a directory
  with content and templates and renders them into a full HTML website.
license: "Apache-2.0"
base: core20
confinement: strict
adopt-info: hugo

package-repositories:
  - type: apt
    components: [main]
    suites: [focal]
    key-id: 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280
    url: https://deb.nodesource.com/node_16.x

plugs:
  etc-gitconfig:
    interface: system-files
    read:
      - /etc/gitconfig
  gitconfig:
    interface: personal-files
    read:
      - $HOME/.gitconfig
      - $HOME/.config/git       # Allows $HOME/.config/git/config and more
      - $HOME/.gitconfig.local  # See #10337

environment:
  HOME: $SNAP_REAL_HOME
  GIT_EXEC_PATH: $SNAP/usr/lib/git-core
  GOCACHE: $SNAP_USER_DATA/.cache/go-build
  npm_config_cache: $SNAP_USER_DATA/.npm
  npm_config_init_module: $SNAP_USER_DATA/.npm-init.js
  npm_config_userconfig: $SNAP_USER_DATA/.npmrc
  pandoc_datadir: $SNAP/usr/share/pandoc
  PYTHONHOME: /usr:$SNAP/usr
  RUBYLIB: $SNAP/usr/lib/ruby/vendor_ruby/2.7.0:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/ruby/vendor_ruby/2.7.0:$SNAP/usr/lib/ruby/vendor_ruby:$SNAP/usr/lib/ruby/2.7.0:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/ruby/2.7.0

apps:
  hugo:
    command: bin/hugo
    completer: hugo-completion
    plugs:
      - home
      - network-bind
      - removable-media
      - etc-gitconfig
      - gitconfig

parts:
  git:
    plugin: nil
    stage-packages:
      - git
    prime:
      - usr/bin/git
      - usr/lib

  go:
    plugin: nil
    stage-snaps:
      - go/1.19/stable
    prime:
      - bin/go
      - pkg/tool
      - -pkg/tool/*

  hugo:
    plugin: nil
    source: .
    after:
      - git
      - go
    override-pull: |
      snapcraftctl pull
      snapcraftctl set-version "$(git describe --tags --always --match 'v[0-9]*' | sed 's/^v//; s/-/+git/; s/-g/./')"
      if grep -q 'Suffix:\s*""' common/hugo/version_current.go; then
        snapcraftctl set-grade "stable"
      else
        snapcraftctl set-grade "devel"
      fi
    override-build: |
      echo "\nStarting override-build:"
      set -ex

      export GOPATH=$(realpath ../go)
      export PATH=$GOPATH/bin:$PATH

      echo " * Patch securityConfig.go to allow"
      echo "   - GIT_EXEC_PATH and LD_LIBRARY_PATH to be passed to git"
      echo "   - npm_config_{cache,init_module,userconfig} to be passed to npx"
      echo "   - pandoc_datadir to be passed to pandoc"
      echo "   - PYTHONHOME and SNAP to be passed to rst2html"
      echo "   - RUBYLIB to be passed to asciidoctor"
      sed -i '/OsEnv: NewWhitelist/s/)\$/|GIT_EXEC_PATH|LD_LIBRARY_PATH|npm_config_(cache|init_module|userconfig)|pandoc_datadir|PYTHONHOME|RUBYLIB|SNAP&/' config/security/securityConfig.go
      git diff config/security/securityConfig.go

      echo " * SNAPCRAFT_IMAGE_INFO=${SNAPCRAFT_IMAGE_INFO=}"
      # Example: SNAPCRAFT_IMAGE_INFO='{"build_url": "https://launchpad.net/~gohugoio/+snap/hugo-extended-dev/+build/344022"}'
      export HUGO_BUILD_TAGS=""
      if echo $SNAPCRAFT_IMAGE_INFO | grep -q '/+snap/hugo-extended'; then
        HUGO_BUILD_TAGS="extended"
      fi

      echo " * Building hugo (HUGO_BUILD_TAGS=\"$HUGO_BUILD_TAGS\")..."
      go build -v -ldflags "-X github.com/gohugoio/hugo/common/hugo.vendorInfo=snap:$(git describe --tags --always --match 'v[0-9]*' | sed 's/^v//; s/-/+git/; s/-g/./')" -tags "$HUGO_BUILD_TAGS"
      ./hugo version
      ldd hugo || :

      echo " * Building shell completion..."
      ./hugo completion bash > hugo-completion

      echo " * Installing to ${SNAPCRAFT_PART_INSTALL}..."
      install -d $SNAPCRAFT_PART_INSTALL/bin
      cp -av hugo $SNAPCRAFT_PART_INSTALL/bin/
      mv -v hugo-completion $SNAPCRAFT_PART_INSTALL/

      echo " * Stripping binary..."
      ls -l $SNAPCRAFT_PART_INSTALL/bin/hugo
      strip --remove-section=.comment --remove-section=.note $SNAPCRAFT_PART_INSTALL/bin/hugo
      ls -l $SNAPCRAFT_PART_INSTALL/bin/hugo

  asciidoctor:
    plugin: nil
    stage-packages:
      - asciidoctor
    override-build: |
      set -ex
      snapcraftctl build
      sed -i '1s|#!/usr/bin/ruby|#!/usr/bin/env ruby|' $SNAPCRAFT_PART_INSTALL/usr/bin/asciidoctor

  dart-sass-embedded:
    plugin: nil
    build-packages:
      - curl
    override-build: |
      set -ex
      snapcraftctl build
      case "$SNAPCRAFT_TARGET_ARCH" in
        amd64)  arch=x64    ;;
        arm64)  arch=arm64  ;;
        armhf)  arch=arm    ;;
        i386)   arch=ia32   ;;
        *)      arch=""     ;;
      esac
      if [[ -n $arch ]]; then
        url=$(curl -s https://api.github.com/repos/sass/dart-sass-embedded/releases/latest | awk -F\" "/browser_download_url.*-linux-${arch}.tar.gz/{print \$(NF-1)}")
        curl -LO --retry-connrefused --retry 10 "$url"
        tar xf sass_embedded-*-linux-$arch.tar.gz
        install -d $SNAPCRAFT_PART_INSTALL/bin
        cp -av sass_embedded/* $SNAPCRAFT_PART_INSTALL/bin/
      fi

  node:
    plugin: nil
    stage-packages:
      - nodejs

  pandoc:
    plugin: nil
    stage-packages:
      - pandoc

  rst2html:
    plugin: nil
    stage-packages:
      - python3-docutils
    override-build: |
      set -ex
      snapcraftctl build
      sed -i "s|'/usr/share/docutils/'|os.path.expandvars('\$SNAP/usr/share/docutils/')|" $SNAPCRAFT_PART_INSTALL/usr/lib/python3/dist-packages/docutils/__init__.py
    organize:
      usr/share/docutils/scripts/python3: usr/bin
