steps:
- task: NodeTool@0
  inputs:
    versionSpec: "10.15.1"

- task: geeklearningio.gl-vsts-tasks-yarn.yarn-installer-task.YarnInstaller@2
  inputs:
    versionSpec: "1.10.1"

- task: DownloadPipelineArtifact@0
  displayName: 'Download Pipeline Artifact'
  inputs:
    artifactName: snap-$(VSCODE_ARCH)
    targetPath: .build/linux/snap-tarball

- script: |
    set -e

    # Get snapcraft version
    snapcraft --version

    # Make sure we get latest packages
    sudo apt-get update
    sudo apt-get upgrade -y

    # Define variables
    REPO="$(pwd)"
    ARCH="$(VSCODE_ARCH)"
    SNAP_ROOT="$REPO/.build/linux/snap/$ARCH"

    # Install build dependencies
    (cd build && yarn)

    # Unpack snap tarball artifact, in order to preserve file perms
    SNAP_TARBALL_PATH="$REPO/.build/linux/snap-tarball/snap-$ARCH.tar.gz"
    (cd .build/linux && tar -xzf $SNAP_TARBALL_PATH)

    # Create snap package
    BUILD_VERSION="$(date +%s)"
    SNAP_FILENAME="code-$VSCODE_QUALITY-$BUILD_VERSION.snap"
    PACKAGEJSON="$(ls $SNAP_ROOT/code*/usr/share/code*/resources/app/package.json)"
    VERSION=$(node -p "require(\"$PACKAGEJSON\").version")
    SNAP_PATH="$SNAP_ROOT/$SNAP_FILENAME"
    (cd $SNAP_ROOT/code-* && sudo snapcraft snap --output "$SNAP_PATH")

    # Publish snap package
    AZURE_DOCUMENTDB_MASTERKEY="$(AZURE_DOCUMENTDB_MASTERKEY)" \
    AZURE_STORAGE_ACCESS_KEY_2="$(AZURE_STORAGE_ACCESS_KEY_2)" \
    MOONCAKE_STORAGE_ACCESS_KEY="$(MOONCAKE_STORAGE_ACCESS_KEY)" \
    node build/azure-pipelines/common/publish.js "$VSCODE_QUALITY" "linux-snap-$ARCH" package "$SNAP_FILENAME" "$VERSION" true "$SNAP_PATH"