|
| 1 | +import path from "path"; |
| 2 | +import fs from "fs-extra"; |
| 3 | +import { UnityConfig, PlatformConfig } from "./configs/unityConfig.ts"; |
| 4 | + |
| 5 | +interface ProjectTemplates { |
| 6 | + templateName: string; |
| 7 | + templatePath: string; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * UnityTemplates class provides methods to access Unity templates |
| 12 | + * |
| 13 | + */ |
| 14 | +class UnityTemplates { |
| 15 | + /** |
| 16 | + * Platform-specific configuration for Unity Hub |
| 17 | + * @internal |
| 18 | + */ |
| 19 | + private static unityConfig: PlatformConfig = UnityConfig.getPlatformConfig(); |
| 20 | + |
| 21 | + /** |
| 22 | + * Get the path to the Unity project templates |
| 23 | + * @returns The path to the Unity project templates |
| 24 | + */ |
| 25 | + private static getProjectTemplatesPath(): string { |
| 26 | + return this.unityConfig.templates.projectTemplates; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Get the path to the Unity project templates for a specific version |
| 31 | + * @param version - The Unity version |
| 32 | + * @returns The path to the Unity project templates for the specified version |
| 33 | + */ |
| 34 | + private static getProjectTemplatesPathForVersion(version: string): string { |
| 35 | + return path.join(this.unityConfig.editor.base, version, this.getProjectTemplatesPath()); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the path to the Unity project templates for a specific version and template name |
| 40 | + * @param version - The Unity version |
| 41 | + * @param templateName - The name of the template |
| 42 | + * @returns The path to the Unity project templates for the specified version and template name |
| 43 | + */ |
| 44 | + public static getProjectTemplates(version: string): ProjectTemplates[] { |
| 45 | + const templatePath = this.getProjectTemplatesPathForVersion(version); |
| 46 | + |
| 47 | + if (!fs.existsSync(templatePath)) { |
| 48 | + throw new Error(`Template path does not exist: ${templatePath}`); |
| 49 | + } |
| 50 | + |
| 51 | + const templates = fs.readdirSync(templatePath).filter((file) => path.extname(file) === ".tgz"); |
| 52 | + return templates.map((template) => ({ |
| 53 | + templateName: path.basename(template, ".tgz"), |
| 54 | + templatePath: path.join(templatePath, template), |
| 55 | + })); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export default UnityTemplates; |
0 commit comments