name: release
on:
  workflow_dispatch:
    inputs:
      version:
        description: 'version number'
        required: true

env:
  RELEASE_FILE: RELEASE.txt

jobs:
  check:
    name: check (${{ matrix.name }})
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
        - name: "default"
          features: ""
    steps:
    - uses: actions/checkout@v1
    - name: install nightly
      run: rustup default nightly
    - name: install tools
      run: sudo apt-get install build-essential
    - name: check formatting
      run: |
        rustup component add rustfmt
        cargo fmt --all -- --check
    - name: execute tests
      run: cargo test ${{ matrix.features }}
    - name: build program
      run: cargo build ${{ matrix.features }}
  
  tag:
    if: github.ref == 'refs/heads/master'
    needs: check
    name: tag and release
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: create release
      id: create_release
      uses: actions/create-release@latest
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.event.inputs.version }}
        release_name: ${{ github.event.inputs.version }}
        body: |
          Release ${{ github.event.inputs.version }}.
        draft: false
        prerelease: false
    - run: printf ${{ steps.create_release.outputs.upload_url }} > ${{ env.RELEASE_FILE }}
    - name: upload release data
      uses: actions/upload-artifact@v1.0.0
      with:
        name: RELEASE
        path: ${{ env.RELEASE_FILE }}

  publish-cratesio:
    needs: tag
    name: crates.io
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: get version
      id: get_version
      run: echo ::set-output name=VERSION::$(git tag --points-at HEAD --sort -version:refname | head -1)
    - name: install tools
      run: sudo apt-get install build-essential
    - name: publish
      id: publish
      run: |
        chmod +x ./do.sh && 
        bash ./do.sh update-version ${{ steps.get_version.outputs.VERSION }} && 
        cargo login ${{ secrets.CRATES_IO_TOKEN }} && 
        cargo publish --allow-dirty

  publish:
    needs: tag
    name: publish for ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
        - os: macos-latest
          target: x86_64-apple-darwin
          install: printf ok
        - os: ubuntu-latest
          target: x86_64-unknown-linux-gnu
          install: sudo apt-get install build-essential
    steps:
    - uses: actions/checkout@v1
    - name: get version
      id: get_version
      run: echo ::set-output name=VERSION::$(git tag --points-at HEAD --sort -version:refname | head -1)
    - name: download release id
      uses: actions/download-artifact@v1.0.0
      with:
        name: RELEASE
    - name: get release data
      id: get_release_data
      run: echo ::set-output name=upload_url::$(cat RELEASE/${{ env.RELEASE_FILE }})
    - name: install tools
      run: |
        ${{ matrix.install }} && 
        rustup target install ${{ matrix.target }}
    - name: build-${{ matrix.target }}
      run: |
        chmod +x ./do.sh && 
        bash ./do.sh update-version ${{ steps.get_version.outputs.VERSION }} && 
        cargo build --release --target ${{ matrix.target }}
    - name: zip
      run: cd ./target/${{ matrix.target }}/release && tar -zcvf ${{ matrix.target }}.tar.gz senile
    - name: upload asset
      uses: svenstaro/upload-release-action@v1-release
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: ./target/${{ matrix.target }}/release/${{ matrix.target }}.tar.gz
        asset_name: ${{ matrix.target }}.tar.gz
        tag: ${{ steps.get_version.outputs.VERSION }}
        overwrite: true
