How to create a project from github template with custom package name?

Issue

When I use template from https://github.com/JetBrains/intellij-platform-plugin-template#sample-code it creates as
/my-account-name/new-project-name (renamed automatically).

My task: create multi module template for my company (for reusing).
I created template with

package com.companyname.templatename

And when I tried to use that template it was created as com.companyname.templatename, but i want com.companyname.newprojectname

Seems, that for auto package renaming template files should use

package com.myaccountname.templatename

But its not good for me, cause I wanna use company-name as vendor.

Do you know any ways to auto renaming a template’s package?

Solution

I thought that their auto-renaming works cause they used the correct vendors name, but in their code I found .yml file with solution

name: Template Cleanup
on:
  push:
    branches:
      - main

jobs:

  # Run cleaning process only if workflow is triggered by the template repository.
  template-cleanup:
    name: Template Cleanup
    runs-on: ubuntu-latest
    permissions:
      contents: write
    if: github.event.repository.name != 'android-template'
    steps:

      # Check out current repository
      - name: Fetch Sources
        uses: actions/[email protected]

      # Cleanup project
      - name: Cleanup
        run: |
          export LC_CTYPE=C
          export LANG=C

          # Prepare variables
          NAME="${GITHUB_REPOSITORY##*/}"
          SAFE_NAME=$(echo $NAME | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]')
          GROUP="com.companyname.$SAFE_NAME"

          # Replace placeholders in the template-cleanup files
          sed -i "s/%NAME%/$NAME/g" .github/template-cleanup/*
          sed -i "s/%REPOSITORY%/${GITHUB_REPOSITORY/\//\\/}/g" .github/template-cleanup/*
          sed -i "s/%GROUP%/$GROUP/g" .github/template-cleanup/*

          # Replace template package name in project files with $GROUP
          find app/src -type f -exec sed -i "s/com.companyname.template/$GROUP/g" {} +
          find app/src -type f -exec sed -i "s/Template/$NAME/g" {} +

          # Move content
          mkdir -p app/src/main/java/${GROUP//.//}
          mkdir -p app/src/test/java/${GROUP//.//}
          cp -R .github/template-cleanup/* .
          cp -R app/src/main/java/com/companyname/template/* app/src/main/java/${GROUP//.//}/
          cp -R app/src/test/java/com/companyname/template/* app/src/test/java/${GROUP//.//}/

          # Cleanup
          rm -rf \
            .github/ISSUE_TEMPLATE \
            .github/readme \
            .github/template-cleanup \
            .github/workflows/template-cleanup.yml \
            .idea/icon.png \
            app/src/main/java/com/companyname/template \
            app/src/test/java/com/companyname/template \

      # Commit modified files
      - name: Commit files
        run: |
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          git add .
          git commit -m "Template cleanup"

      # Push changes
      - name: Push changes
        uses: ad-m/[email protected]
        with:
          branch: main
          github_token: ${{ secrets.GITHUB_TOKEN }}
  1. put that file to .github/workflows

enter image description here

  1. to template-cleanup put files which should overwrite the default. For example my gradle propeties has group and name fields.
    enter image description here

It will be rewritten by this part of code

 GROUP="com.companyname.$SAFE_NAME"

and moved to root by this

cp -R app/src/main/java/com/companyname/template/* app/src/main/java/${GROUP//.//}/

Answered By – Vera Iureva

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published