Deploy scripts across all projects using Powershell (Windows Only)

poliver

Member
WARNING!!! ALTHOUGH I HAVE FULLY TESTED THIS METHOD, USE IT AT YOUR OWN RISK. THERE IS NO GUARANTEE THAT THE GMS2 PROJECT FILE FORMAT MIGHT CHANGE IN THE FUTURE THUS BREAKING THIS ETC.
ALWAYS MAKE BACKUPS AND USE SOURCE CONTROL


Hi guys,

I've been looking for an easy streamlined way to make sure all my custom functions are always available across IDE/all projects without any need of copy pasting or importing/exporting packages so I've come up with this method

1. Create a folder anywhere on your computer and call it GMLIB
(The name can be anything else but then you'd need to make sure that the change is reflected across all the following)

2. In this folder create 3 files

GMLIB.gml, GMLIB.yy and UPDATE.ps1

Sk6XNKX4lZ.jpg

GMLIB.gml file is where you'd be writting all your functions that you'll be able to access from across any project.


3. Paste this code as is into GMLIB.yy file without changing anything

Code:
{
  "isDnD": false,
  "isCompatibility": false,
  "parent": {
    "name": "CURRENT_PROJECT",
    "path": "CURRENT_PROJECT.yyp",
  },
  "resourceVersion": "1.0",
  "name": "GMLIB",
  "tags": [],
  "resourceType": "GMScript",
}
4. Paste this into the UPDATE.ps1 file

GML:
#ASSET PATHS
$current_folder = $PSScriptRoot
$gml_file       = "$current_folder\GMLIB.gml"
$yy_file        = "$current_folder\GMLIB.yy"

#PROJECTS FOLDER
$projects_folder = "C:\Users\kewne\Documents\GameMakerStudio2"

#GET PROJECTS
$projects = Get-ChildItem -Path $projects_folder

foreach ($project in $projects) {

    if (Test-Path -Path "$projects_folder\$project\scripts") {
        #if scripts folder in project exists, create or overwrite the GMLIB files
        Copy-Item $current_folder -Destination "$projects_folder\$project\scripts\" -force
        Copy-Item $gml_file       -Destination "$projects_folder\$project\scripts\GMLIB\" -force
        Copy-Item $yy_file        -Destination "$projects_folder\$project\scripts\GMLIB\" -force
        #place the GMLIB script into root level
        (Get-Content "$projects_folder\$project\scripts\GMLIB\GMLIB.yy").replace('CURRENT_PROJECT', $project) | Set-Content "$projects_folder\$project\scripts\GMLIB\GMLIB.yy"
    }
    else {
        #if scripts folder in project doesn't exist, create the folder and copy the GMLIB files
        New-Item -Path "$projects_folder\$project\" -Name scripts -ItemType Directory
        Copy-Item $current_folder -Destination "$projects_folder\$project\scripts\"
        Copy-Item $gml_file       -Destination "$projects_folder\$project\scripts\GMLIB\"
        Copy-Item $yy_file        -Destination "$projects_folder\$project\scripts\GMLIB\"
        #place the GMLIB script into root level
        (Get-Content "$projects_folder\$project\scripts\GMLIB\GMLIB.yy").replace('CURRENT_PROJECT', $project) | Set-Content "$projects_folder\$project\scripts\GMLIB\GMLIB.yy"
    }

    #add reference to GMLIB script to the project file
    $yyp_file = "$projects_folder\$project\$project.yyp"
    $resource = '{"id":{"name":"GMLIB","path":"scripts/GMLIB/GMLIB.yy",},"order":0,},'

    $entry = Select-String -Path $yyp_file -Pattern $resource
    if ($entry -eq $null) {
        #adding the resource

        $NewContent = Get-Content -Path $yyp_file |

        ForEach-Object {
            $_
            if($_ -match ('^' + [regex]::Escape('  "resources": ['))) {
                "    $resource"
            }
        }
        $NewContent | Out-File -FilePath $yyp_file -Encoding Default -Force
    }
}

Change the path in the line
Code:
$projects_folder = "C:\Users\kewne\Documents\GameMakerStudio2"
to the path to your own GMS2 projects folder

5. Work on your code in the GML file and whenever you're ready to add/update the code to all your projects right click on the ps1 file and click Run in PowerShell

DkeDSjzacK.jpg

The PowerShell will flash for a split second and the code will be added to/updated in all your projects instantly.


What the code does is:

- Loops through all your project folders according to the projects folder path you setup
- Checks whether there's a "script" folder in the project. If there's none it creates one.
- Inside of the script folder it creates a GMLIB folder
- Copies the GMLIB.gml file and GMLIB.yy file into the GMLIB folder (or overwrites if they already exist)
- Modifies the GMLIB.yy file in the project folder to place the asset into root level of the current project
- Checks whether there's an entry for a new script asset in the project.yyp file if no it adds it


Happy coding!
 
Last edited:
Top