-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Dinomite. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
import path = require('path'); | ||
import fs from 'fs'; | ||
|
||
export class UnityPackageManagerTools { | ||
|
||
private static readonly packagesFolder = 'Packages'; | ||
private static readonly packageManifestFile = 'manifest.json'; | ||
|
||
/** | ||
* Adds a package to a project's package manifest. | ||
* | ||
* @param projectPath The path to the project on disc. | ||
* @param packageName The package identifer. | ||
* @param versionOrGitUrl The package version to install (can also be a git URL in case the package is installed via git). | ||
*/ | ||
public static addPackageToProject(projectPath: string, packageName: string, versionOrGitUrl: string): Error | null | undefined { | ||
try { | ||
const packageManifestFilePath = path.join(projectPath, UnityPackageManagerTools.packagesFolder, UnityPackageManagerTools.packageManifestFile); | ||
const packageManifestFileContent = fs.readFileSync(packageManifestFilePath, { encoding: 'utf8' }); | ||
let jsonData = JSON.parse(packageManifestFileContent); | ||
|
||
if (!jsonData.dependencies.hasOwnProperty(packageName)) { | ||
jsonData.dependencies[packageName] = versionOrGitUrl; | ||
const updatedData = JSON.stringify(jsonData); | ||
fs.writeFileSync(packageManifestFilePath, updatedData, 'utf-8'); | ||
} | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
return e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Removes a package from a project's package manifest. | ||
* | ||
* @param projectPath The path to the project on disc. | ||
* @param packageName The package identifer. | ||
*/ | ||
public static removePackageFromProject(projectPath: string, packageName: string): Error | null | undefined { | ||
try { | ||
const packageManifestFilePath = path.join(projectPath, UnityPackageManagerTools.packagesFolder, UnityPackageManagerTools.packageManifestFile); | ||
const packageManifestFileContent = fs.readFileSync(packageManifestFilePath, { encoding: 'utf8' }); | ||
let jsonData = JSON.parse(packageManifestFileContent); | ||
|
||
if (jsonData.dependencies.hasOwnProperty(packageName)) { | ||
delete jsonData.dependencies[packageName]; | ||
const updatedData = JSON.stringify(jsonData); | ||
fs.writeFileSync(packageManifestFilePath, updatedData, 'utf-8'); | ||
} | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
return e; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"dependencies": { | ||
"com.unity.ai.navigation": "2.0.4", | ||
"com.unity.collab-proxy": "2.5.1", | ||
"com.unity.ide.rider": "3.0.31", | ||
"com.unity.ide.visualstudio": "2.0.22", | ||
"com.unity.inputsystem": "1.11.0", | ||
"com.unity.multiplayer.center": "1.0.0", | ||
"com.unity.render-pipelines.universal": "17.0.3", | ||
"com.unity.test-framework": "1.4.5", | ||
"com.unity.timeline": "1.8.7", | ||
"com.unity.ugui": "2.0.0", | ||
"com.unity.visualscripting": "1.9.4", | ||
"com.unity.modules.accessibility": "1.0.0", | ||
"com.unity.modules.ai": "1.0.0", | ||
"com.unity.modules.androidjni": "1.0.0", | ||
"com.unity.modules.animation": "1.0.0", | ||
"com.unity.modules.assetbundle": "1.0.0", | ||
"com.unity.modules.audio": "1.0.0", | ||
"com.unity.modules.cloth": "1.0.0", | ||
"com.unity.modules.director": "1.0.0", | ||
"com.unity.modules.imageconversion": "1.0.0", | ||
"com.unity.modules.imgui": "1.0.0", | ||
"com.unity.modules.jsonserialize": "1.0.0", | ||
"com.unity.modules.particlesystem": "1.0.0", | ||
"com.unity.modules.physics": "1.0.0", | ||
"com.unity.modules.physics2d": "1.0.0", | ||
"com.unity.modules.screencapture": "1.0.0", | ||
"com.unity.modules.terrain": "1.0.0", | ||
"com.unity.modules.terrainphysics": "1.0.0", | ||
"com.unity.modules.tilemap": "1.0.0", | ||
"com.unity.modules.ui": "1.0.0", | ||
"com.unity.modules.uielements": "1.0.0", | ||
"com.unity.modules.umbra": "1.0.0", | ||
"com.unity.modules.unityanalytics": "1.0.0", | ||
"com.unity.modules.unitywebrequest": "1.0.0", | ||
"com.unity.modules.unitywebrequestassetbundle": "1.0.0", | ||
"com.unity.modules.unitywebrequestaudio": "1.0.0", | ||
"com.unity.modules.unitywebrequesttexture": "1.0.0", | ||
"com.unity.modules.unitywebrequestwww": "1.0.0", | ||
"com.unity.modules.vehicles": "1.0.0", | ||
"com.unity.modules.video": "1.0.0", | ||
"com.unity.modules.vr": "1.0.0", | ||
"com.unity.modules.wind": "1.0.0", | ||
"com.unity.modules.xr": "1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Dinomite. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
import 'mocha'; | ||
import { expect } from 'chai'; | ||
import { UnityPackageManagerTools } from '../dist'; | ||
|
||
describe('UnityPackageManagerTools', | ||
() => { | ||
it('Invalid file path should return error', () => { | ||
const error = UnityPackageManagerTools.addPackageToProject('XYZ:\\This\\Path\\Does\\Not\\Exist', 'games.dinomite.azurepipelines', 'https://github.com/Dinomite-Studios/unity-azure-pipelines-tasks-build-scripts.git'); | ||
expect(error); | ||
}); | ||
|
||
it('games.dinomite.azurepipelines package was added to manifest', () => { | ||
const error = UnityPackageManagerTools.addPackageToProject(__dirname, 'games.dinomite.azurepipelines', 'https://github.com/Dinomite-Studios/unity-azure-pipelines-tasks-build-scripts.git'); | ||
expect(!error); | ||
}); | ||
|
||
it('games.dinomite.azurepipelines package was removed from manifest', () => { | ||
const error = UnityPackageManagerTools.removePackageFromProject(__dirname, 'games.dinomite.azurepipelines'); | ||
expect(!error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters