Skip to content

Instantly share code, notes, and snippets.

@felixcantet
Last active July 26, 2023 08:43
Show Gist options
  • Save felixcantet/79d778d50ea21989cf6edde0ba379c34 to your computer and use it in GitHub Desktop.
Save felixcantet/79d778d50ea21989cf6edde0ba379c34 to your computer and use it in GitHub Desktop.
Package Creator
<# The MIT License (MIT)
Copyright (c) $COPYRIGHT_YEAR $COPYRIGHT_HOLDERS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#>
# Function to get user input
function Get-UserInput {
param([string]$prompt)
Read-Host $prompt
}
# Function to generate the Unity package structure
function Generate-UnityPackage {
# Get user input for package details
$domain = Get-UserInput "Enter domain (e.g., com): "
$company = Get-UserInput "Enter company name (e.g., MyCompany): "
$packageName = Get-UserInput "Enter package name (e.g., my-package): "
$packageDisplayName = Get-UserInput "Enter package Display name (e.g., My Package): "
$packageDescription = Get-UserInput "Enter Package Description : "
$packageLicense = Get-UserInput "What is the License of the package? (e.g., MIT) : "
$targetUnityVersion = Get-UserInput "Targeted Unity version (e.g., 2021.3): "
$author = Get-UserInput "Name of the author/maintainer of the package: "
$rootNamespace = Get-UserInput "Name of the root Namespace (e.g. Author.MyPackage): "
$repositoryName = $packageDisplayName.replace(' ', '')
$githubUserName = & gh api user -q ".login"
Write-Host $githubUserName
# Create the folder structure
$packageRoot = "$domain.$company.$packageName"
$packageFolder = Join-Path $PSScriptRoot $packageRoot
# Check if the package folder already exists
if (Test-Path -Path $packageFolder) {
Write-Host "Package folder '$packageFolder' already exists. Aborting package creation."
return
}
# Create the package folder and navigate to it
New-Item -ItemType Directory -Path $packageFolder | Out-Null
Set-Location -Path $packageFolder
# Create subfolders and asmdef files
$documentationFolder = Join-Path $packageFolder "Documentation~"
$editorFolder = Join-Path $packageFolder "Editor"
$runtimeFolder = Join-Path $packageFolder "Runtime"
$samplesFolder = Join-Path $packageFolder "Samples~"
$testsFolder = Join-Path $packageFolder "Tests"
$testsEditorFolder = Join-Path $testsFolder "Editor"
$testsRuntimeFolder = Join-Path $testsFolder "Runtime"
$manualFolder = Join-Path $documentationFolder "manual"
$apiFolder = Join-Path $documentationFolder "api"
$resourcesFolder = Join-Path $documentationFolder "resources"
$folders = @($documentationFolder, $editorFolder, $runtimeFolder, $samplesFolder, $testsFolder, $testsEditorFolder, $testsRuntimeFolder, $manualFolder, $resourcesFolder, $apiFolder)
foreach ($folder in $folders) {
if (-Not (Test-Path -Path $folder)) {
New-Item -ItemType Directory -Path $folder | Out-Null
}
}
# Create asmdef files
$runtimeAsmdefContent = @"
{
"name": "$($rootNamespace).Runtime",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
"@
$editorAsmdefContent = @"
{
"name": "$($rootNamespace).Editor",
"references": [
"$($rootNamespace).Runtime"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
"@
$testsRuntimeAsmdefContent = @"
{
"name": "$($rootNamespace).Tests.Runtime",
"references": [
"TestRunner"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": ["INCLUDE_TESTS"],
"versionDefines": [],
"noEngineReferences": false
}
"@
$testsEditorAsmdefContent = @"
{
"name": "$($rootNamespace).Tests.Editor",
"references": [
"$($rootNamespace).Tests.Runtime",
"TestRunner.Editor"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": ["INCLUDE_TESTS"],
"versionDefines": [],
"noEngineReferences": false
}
"@
$runtimeAsmdefPath = Join-Path $runtimeFolder "$($rootNamespace).Runtime.asmdef"
$editorAsmdefPath = Join-Path $editorFolder "$($rootNamespace).Editor.asmdef"
$testsRuntimeAsmdefPath = Join-Path $testsRuntimeFolder "$($rootNamespace).Tests.Runtime.asmdef"
$testsEditorAsmdefPath = Join-Path $testsEditorFolder "$($rootNamespace).Tests.Editor.asmdef"
$runtimeAsmdefContent | Out-File -FilePath $runtimeAsmdefPath
$editorAsmdefContent | Out-File -FilePath $editorAsmdefPath
$testsRuntimeAsmdefContent | Out-File -FilePath $testsRuntimeAsmdefPath
$testsEditorAsmdefContent | Out-File -FilePath $testsEditorAsmdefPath
# Generate package.json and README.md files
$packageJsonContent = @"
{
"name": "$($domain).$($company).$($packageName)",
"displayName" : "$($packageDisplayName)",
"version": "0.0.1",
"description": "$($packageDescription)",
"unity": "$($targetUnityVersion)",
"author": {
"name": $($author)",
}"
"keywords": [],
"dependencies": {},
"license": "$($packageLicense)",
}
"@
$readmeContent = @"
# $($domain).$($company).$($packageName)
Your package description here.
## Installation
To install this package, follow the instructions below:
1. Open Unity.
2. Go to Window > Package Manager.
3. Click on the "+" button at the top left of the window.
4. Choose "Add package from disk...".
5. Select the package.json file in the root of this repository.
## License
This package is licensed under the [MIT License](LICENSE).
"@
$packageJsonPath = Join-Path $packageFolder "package.json"
$readmePath = Join-Path $packageFolder "README.md"
$packageJsonContent | Out-File -FilePath $packageJsonPath
$readmeContent | Out-File -FilePath $readmePath
# Generate docfx.json
$docfxJsonContent = @"
{
"metadata": [
{
"src": [
{
"src": "..",
"files": [
"Runtime/**/*.cs",
"Editor/**/*.cs"
],
"exclude": [
"Test/*"
]
}
],
"globalNamespaceId": "Global",
"filter": "filterConfig.yml",
"dest": "api"
}
],
"build": {
"globalMetadata": {
"_appTitle": "$($packageDisplayName) Documentation",
"_appFooter": "$($packageDisplayName) Documentation",
"_enableSearch": true
},
"content": [
{
"files": [
"api/**.yml",
"api/index.md"
]
},
{
"files": [
"toc.yml",
"index.md"
]
},
{
"src": "api",
"files": [
"*.yml"
],
"dest": "api"
},
{
"src": "manual",
"files": [
"toc.yml",
"*.md"
],
"dest": "manual"
}
],
"overwrite": [
{
"src": "..",
"files": [
"api/**/*.md"
]
}
],
"resource": [
{
"files": [
"resources/**/*"
]
}
],
"sitemap": {
"baseUrl": "https://$($githubUserName).github.io/$($repositoryName)",
"changefreq": "weekly",
"fileOptions": {
"api/*": {
"changefreq": "daily"
}
}
},
"xref": [
"https://normanderwan.github.io/UnityXrefMaps/xrefmap.yml"
],
"xrefService": [
"https://xref.docs.microsoft.com/query?uid={uid}"
],
"dest": "../_site"
}
}
"@
$docfxJsonPath = Join-Path $documentationFolder "docfx.json"
$filterConfigContent = @"
apiRules:
- include:
uidRegex: ^$($rootNamespace)
type: Namespace
- include:
uidRegex: ^Global
type: Namespace
- exclude:
uidRegex: .*
type: Namespace
"@
$filterConfigPath = Join-Path $documentationFolder "filterConfig.yml"
$rootTocContent = @"
- name : Manual
href manual/
- name : Scripting API
href : api/
"@
$rootTocPath = Join-Path $documentationFolder "toc.yml"
$docfxJsonContent | Out-File -FilePath $docfxJsonPath
$filterConfigContent | Out-File -FilePath $filterConfigPath
$rootTocContent | Out-File -FilePath $rootTocPath
# Generate GitHub action workflow
$workflowPath = Join-Path $packageFolder ".github/workflows"
New-Item -ItemType Directory -Path $workflowPath | Out-Null
$workflowContent = @"
name: Documentation website
on:
push:
branches:
- master
jobs:
# Build the documentation
build:
runs-on: windows-latest # Required by DocFX
steps:
- name: Checkout
uses: actions/checkout@v2
# with:
# submodules: true
- name: Install DocFX
run: choco install -y docfx
- name: Use README.md as index.md
run: cp README.md Documentation~/index.md
- name: Build
run: docfx Documentation~/docfx.json
# Upload the generated documentation
- name: Upload site artifact
uses: actions/upload-artifact@v1
with:
name: _site
path: _site # Must equals the 'build.dest' value on your docfx.json
# Deploy the generated documentation to the gh-pages branch
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# with:
# submodules: true
# Download the generated documentation
- name: Download site artifact
uses: actions/download-artifact@v1
with:
name: _site
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: `${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
publish_dir: _site
"@
$workflowFilePath = Join-Path $workflowPath "build-deploy-docs.yml"
$workflowContent | Out-File -FilePath $workflowFilePath
Write-Host "Unity Package files and GitHub action workflow created successfully."
# Prompt user to create GitHub repository
$createGitHubRepo = Read-Host "Do you want to create a GitHub repository for the package? (Y/N)"
if ($createGitHubRepo -eq "Y" -or $createGitHubRepo -eq "y") {
Initialize-GitRepo
# Create GitHub repository
& gh repo create $repositoryName --public --remote origin --source .
# Push to the master branch
& git push -u origin master
Write-Host "GitHub repository created and initialized successfully."
}
}
function Initialize-GitRepo {
git init
git add .
git commit -m "Initial commit"
}
# Call the Generate-UnityPackage function to start the package creation process
Generate-UnityPackage
@felixcantet
Copy link
Author

Require Git and Github CLI install on the local computer.
Disclaimer : This has been essentialy generated by ChatGPT with few hands modification. There could be some issues.
License : MIT
This is a PowerShell Script that generate a blank Unity Package with correct package layout, asmdef and package.json.
it optionnaly create a github repository and setup a github action to automatically generate the documentation of the project using DocFX

@felixcantet
Copy link
Author

The documentation is generated at each push on the master branch.
To make it work make sure the actions settings of the repository are the following :
Workflow permissions :

  • Read and Write Permission
  • Allow Github Actions to create and approve Pull Request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment