name: Deployment

on: 
  workflow_dispatch:
   inputs:
      VersionNumber:
        description: 'The New Version Number Ex: "1.2.3"'
        required: true
        type: string
        default: "0.0.0"

      IsPreRelease:
        description: 'Is Prelease'
        required: true
        type: boolean
        default: true
      
      UploadToVSMarketPlace:
        description: 'Upload To VS MarketPlace'
        required: true
        type: boolean
        default: false

jobs:
  
  build-Windows-32bit:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v3

      - name: Add Premake to PATH
        uses: abel0b/setup-premake@v2.4
        with:
         version: "5.0.0-beta2"      

      - name: Add MSBuild to PATH
        uses: microsoft/setup-msbuild@v1.0.2

      - name: Get zydis
        run: premake5 installzydis

      - name: Version Setup
        run: | 
         echo ${{ inputs.VersionNumber }} > ./version.txt 
         premake5 updateverion

      - name: Run Premake Install Dependencies
        run: premake5 installdependencies

      - name: Run Premake
        run: premake5 vs2022

      - name: Build 32 bit
        run: premake5 build_published_32bit
     
      - name: Archive Output
        uses: actions/upload-artifact@v4
        with:
          name: Output_Windows32
          path: |
           Output/UCodeLangCL
           Output/UCodeLanguageSever
           Output/UCodeLang
           !Output/int
          retention-days: 1
 
  build-Windows-64bit:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v3

      - name: Add Premake to PATH
        uses: abel0b/setup-premake@v2.4
        with:
         version: "5.0.0-beta2"      

      - name: Add MSBuild to PATH
        uses: microsoft/setup-msbuild@v1.0.2

      - name: Get zydis
        run: premake5 installzydis

      - name: Version Setup
        run: | 
         echo ${{ inputs.VersionNumber }} > ./version.txt 
         premake5 updateverion

      - name: Run Premake Install Dependencies
        run: premake5 installdependencies

      - name: Run Premake Install inno
        run: premake5 installinno

      - name: Run Premake
        run: premake5 vs2022

      - name: Build 64 bit
        run: premake5 build_published
    
      - name: Build installer
        run: premake5 buildinstaller
   
      - uses: actions/upload-artifact@v4
        with:
          name: Output_Windows
          path: |
           Output/UCodeLangCL
           Output/UCodeLanguageSever
           Output/UCodeLang
           Output/UCodeLangSetup.exe
           !Output/int
          retention-days: 1


  build-Linux-64bit:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v3

      - name: Add Premake to PATH
        uses: abel0b/setup-premake@v2.4
        with:
         version: "5.0.0-beta2"      

      - name: Setup mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: 'latest'

      - name: Version Setup
        run: | 
         echo ${{ inputs.VersionNumber }} > ./version.txt 
         premake5 updateverion 

      - name: Run Premake Install Dependencies
        run: premake5 installdependencies
      
      - name: Get zydis
        run: premake5 installzydis

      - name: Run Premake
        run: premake5 gmake2

      - name: Build 64 bit
        run: premake5 build_published

      - name: Make packedfiles for unix
        run: |
         premake5 build_docs
         premake5 zipforunix 

      - name: Archive Output
        uses: actions/upload-artifact@v4
        with:
          name: Output_Linux
          path: |
           Output/UCodeLangCL
           Output/UCodeLanguageSever
           Output/UCodeLang
           Output/ULangPacked.tar.gz 
           !Output/int
          retention-days: 1
 
  build-Linux-32bit:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v3

      - name: Add Premake to PATH
        uses: abel0b/setup-premake@v2.4
        with:
         version: "5.0.0-beta2"      

      - name: Setup mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: 'latest'

      - name: Add gcc multilib to PATH
        run: sudo apt install gcc-multilib g++-multilib
    
      - name: Version Setup
        run: | 
         echo ${{ inputs.VersionNumber }} > ./version.txt 
         premake5 updateverion 

      - name: Get zydis
        run: premake5 installzydis

      - name: Run Premake
        run: premake5 gmake2

      - name: Build 32 bit
        run: premake5 build_published_32bit

      - name: Make packedfiles for unix
        run: |
         premake5 build_docs
         premake5 zipforunix 

      - name: Archive Output
        uses: actions/upload-artifact@v4
        with:
          name: Output_Linux32
          path: |
           Output/UCodeLangCL
           Output/UCodeLanguageSever
           Output/UCodeLang
           Output/ULangPacked.tar.gz 
           !Output/int
          retention-days: 1


  build-MacOS:
    runs-on: macos-latest
    
    steps:
      - uses: actions/checkout@v3
    
      - name: Add Premake to PATH
        uses: abel0b/setup-premake@v2.4
        with:
         version: "5.0.0-beta2"      

      - name: Setup mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: 'latest'

      - name: Get zydis
        run: premake5 installzydis

      - name: Run Premake Install Dependencies
        run: premake5 installdependencies

      - name: Version Setup
        run: | 
         echo ${{ inputs.VersionNumber }} > ./version.txt 
         premake5 updateverion 

      - name: Run Premake
        run: premake5 gmake2

      - name: Build 
        run: premake5 build_published
     
      - name: Make packedfiles for unix
        run: |
         premake5 build_docs
         premake5 zipforunix
     
      - name: Archive Output
        uses: actions/upload-artifact@v4
        with:
          name: Output_MacOS
          path: |
           Output/UCodeLangCL
           Output/UCodeLanguageSever
           Output/UCodeLang
           Output/ULangPacked.tar.gz
           !Output/int 
          retention-days: 1

  wasm-build-32:
   runs-on: ubuntu-latest
   steps:
    - uses: actions/checkout@v3

    - name: Add Premake to PATH
      uses: abel0b/setup-premake@v2.4
      with:
        version: "5.0.0-beta2"      

    - name: Add Emsdk to PATH
      uses: mymindstorm/setup-emsdk@v13

    - name: Get zydis
      run: premake5 installzydis

    - name: Run Premake
      run: premake5 gmake2
    
    - name: Build Wasm
      run: premake5 web_build_published
 
  android-build:
   runs-on: ubuntu-latest

   steps:
    - uses: actions/checkout@v3

    - name: Add Ndk to PATH
      uses: nttld/setup-ndk@v1
      with:
        ndk-version: r26d

    - name: Add Premake to PATH
      uses: abel0b/setup-premake@v2.4
      with:
        version: "5.0.0-beta2"

    - name: Get premake-androidndk
      run: git clone https://github.com/Triang3l/premake-androidndk .premake/premake-androidndk

    - name: Get zydis
      run: premake5 installzydis

    - name: Add require to Premake 
      run: echo "require \"premake-androidndk/androidndk\"" > temp && cat premake5.lua >> temp && mv temp premake5.lua

    - name: Run Premake 
      run: premake5 androidndk

    - name: Build
      run: premake5 build_android

  ios-build:
   runs-on: "macos-latest"
   steps:
    - uses: actions/checkout@v3

    - name: Add Premake to PATH
      uses: abel0b/setup-premake@v2.4
      with:
        version: "5.0.0-beta2"      

    - uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: '14.3.1'

    - name: Get zydis
      run: premake5 installzydis

    - name: Run Premake 
      run: premake5 xcode4

    - name: Fix IOS xcode file 
      run: premake5 fixios

    - name: Build
      run: xcodebuild -project UCodeLang/UCodeLang.xcodeproj -configuration published -sdk iphoneos build

  build-Extensions:
    if: ${{ inputs.UploadToVSMarketPlace == true}}
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: set working path
        run: cd ./UCodeLanguageSever/EditorSpecific/VSCode/ucode-language


      - name: Install the dependencies
        run: npm i
        working-directory: ./UCodeLanguageSever/EditorSpecific/VSCode/ucode-language

      - name: Install vsce
        run: npm i -g vsce
        working-directory: ./UCodeLanguageSever/EditorSpecific/VSCode/ucode-language

      - name: PublishPreRelease
        if: ${{ inputs.IsPreRelease == true}}
        run: vsce publish --pre-release -p ${{ secrets.VSCE_TOKEN }}
        working-directory: ./UCodeLanguageSever/EditorSpecific/VSCode/ucode-language

      - name: Publish
        if: ${{ inputs.IsPreRelease == false}}
        run: vsce publish -p ${{ secrets.VSCE_TOKEN }}
        working-directory: ./UCodeLanguageSever/EditorSpecific/VSCode/ucode-language

  deployment-GitHub:
    needs: [build-Windows-64bit,build-Windows-32bit,build-Linux-32bit,build-Linux-64bit,wasm-build-32,build-MacOS,ios-build,android-build]
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v3

      - name: Add Premake to PATH
        uses: abel0b/setup-premake@v2.4
        with:
         version: "5.0.0-beta2"      

      - name: Version Setup
        run: | 
         echo ${{ inputs.VersionNumber }} > ./version.txt 
         premake5 updateverion 

      - name: Download all workflow run artifacts
        uses: actions/download-artifact@v4


      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          release_name: Release ${{ inputs.VersionNumber }}
          body_path: ./changelog.md
          draft: false
          prerelease: ${{ inputs.IsPreRelease }}
          tag_name: Release-${{ inputs.VersionNumber }}
      
      - name: Upload UCodeLangCAPI
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./UCodeLang/UCodeLang/UCodeLangCAPI.h
          asset_name: UCodeLangCAPI.h
          asset_content_type: text/plain
      
      - name: Upload Unix Setup
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./install.sh
          asset_name: install.sh
          asset_content_type: text/plain  
      
      - name: Upload ULangPackedMacOS 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_MacOS/ULangPacked.tar.gz
          asset_name: ULangPackedMacOS.tar.gz
          asset_content_type: application/gzip  
      
      - name: Upload ULangPackedLinux 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux/ULangPacked.tar.gz
          asset_name: ULangPackedLinux.tar.gz
          asset_content_type: application/gzip  
      
      - name: Upload Windows Setup
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Windows/UCodeLangSetup.exe
          asset_name: UCodeLangSetup.exe
          asset_content_type: application/octet-stream
        
      - name: Upload UCodeLangLibWindows64
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Windows/UCodeLang/Win64/Published/UCodeLang.lib
          asset_name: UCodeLang-win64.lib
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangCLWindows64
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Windows/UCodeLangCL/Win64/Published/uclang.exe
          asset_name: uclang-win64.exe
          asset_content_type: application/octet-stream 
          
      - name: Upload UCodeLangSeverWindows64
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Windows/UCodeLanguageSever/Win64/Published/uclanglsp.exe
          asset_name: uclanglsp-win64.exe
          asset_content_type: application/octet-stream 
 
      - name: Upload UCodeLangLibWindows32
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} 
          asset_path: ./Output_Windows32/UCodeLang/Win32/Published/UCodeLang.lib
          asset_name: UCodeLang-win32.lib
          asset_content_type: application/octet-stream 
      
      - name: Upload UCodeLangCLWindows32
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Windows32/UCodeLangCL/Win32/Published/uclang.exe
          asset_name: uclang-win32.exe
          asset_content_type: application/octet-stream 
      
      - name: Upload UCodeLangSeverWindows32
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Windows32/UCodeLanguageSever/Win32/Published/uclanglsp.exe
          asset_name: uclanglsp-win32.exe
          asset_content_type: application/octet-stream 
      
      - name: Upload UCodeLangLibLinux64
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux/UCodeLang/linux64/Published/libUCodeLang.a
          asset_name: libUCodeLang-linux64.a
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangCLlinux64
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux/UCodeLangCL/linux64/Published/uclang
          asset_name: uclang-linux64
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangSeverlinux64
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux/UCodeLanguageSever/linux64/Published/uclanglsp
          asset_name: uclanglsp-linux64
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangLibLinux32
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux32/UCodeLang/linux32/Published/libUCodeLang.a
          asset_name: libUCodeLang-linux32.a
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangCLlinux32
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux32/UCodeLangCL/linux32/Published/uclang
          asset_name: uclang-linux32
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangSeverlinux32
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_Linux32/UCodeLanguageSever/linux32/Published/uclanglsp
          asset_name: uclanglsp-linux32
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangLibMacOS
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_MacOS/UCodeLang/MacOS/Published/libUCodeLang.a
          asset_name: libUCodeLang-macos.a
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangCLMacOS
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_MacOS/UCodeLangCL/MacOS/Published/uclang
          asset_name: uclang-macos
          asset_content_type: application/octet-stream 

      - name: Upload UCodeLangSeverMacOS
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./Output_MacOS/UCodeLanguageSever/MacOS/Published/uclanglsp
          asset_name: uclanglsp-macos
          asset_content_type: application/octet-stream 
 
      - name: Upload version Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} 
          asset_path: version.txt
          asset_name: version.txt
          asset_content_type: application/octet-stream
 
