Skip to content

Instantly share code, notes, and snippets.

@ngruychev
Last active September 1, 2021 08:13
Show Gist options
  • Save ngruychev/fda1bba39c1e9bec7628984bfd05373c to your computer and use it in GitHub Desktop.
Save ngruychev/fda1bba39c1e9bec7628984bfd05373c to your computer and use it in GitHub Desktop.
LÖVE/lua scripts for locally-downloaded dependencies
{
"github": [
"user/repo",
"foobar/baz:branchname"
],
"luarocks": [
"foo",
"bar:1.2.0"
]
}
#!/usr/bin/env pwsh
# dependencies: luarocks git lua5.1 pwsh
# linux/mac user? just install powershell lmao, I am _not_ bothering with json in shell scripts
# not gonna use jq either
# `require 'lua_modules'` in your project main file
# this article was used as a reference for this atrocity:
# https://leafo.net/guides/customizing-the-luarocks-tree.html
# please, please add lua_modules to .gitignore
$DebugPreference = 'Continue'
# some variables
$SCRIPT_ROOT = $PSScriptRoot
$LUA_VERSION = "5.1"
$LUA_MODULES_FOLDER = "$SCRIPT_ROOT/lua_modules"
$GIT_CLONE_FOLDER = "$LUA_MODULES_FOLDER/github"
$ROOT_DEPS_FILE = "$SCRIPT_ROOT/deps.json"
$DEPS_PATH_CODE = @"
local path = ''
local cpath = ''
-- github
path =
'lua_modules/github/?/?.lua;'
.. 'lua_modules/github/?.lua;'
.. 'lua_modules/github/?/init.lua;'
.. 'lua_modules/github/?/?/init.lua;'
.. 'lua_modules/github/?/?/?.lua;'
.. 'lua_modules/github/?/src/?.lua;'
.. 'lua_modules/github/?/src/init.lua;'
.. 'lua_modules/github/?/lib/?.lua;'
.. 'lua_modules/github/?/lib/init.lua;'
.. path
cpath =
'lua_modules/github.com/?/?.so;'
.. cpath
-- luarocks
local version = '5.1'
path =
'lua_modules/share/lua/' .. version .. '/?.lua;'
.. 'lua_modules/share/lua/' .. version .. '/?/init.lua;'
.. path
cpath =
'lua_modules/lib/lua/' .. version .. '/?.so;'
.. cpath
if love then
love.filesystem.setRequirePath(path .. love.filesystem.getRequirePath())
love.filesystem.setCRequirePath(cpath .. love.filesystem.getCRequirePath())
else
package.path = path .. package.path
package.cpath = cpath .. package.cpath
end
"@
function Prepare-LuaModulesFolder {
if (!(Test-Path -Path $LUA_MODULES_FOLDER -PathType Container)) {
New-Item -ItemType Directory -Path $LUA_MODULES_FOLDER > $null
}
if (!(Test-Path -Path "$LUA_MODULES_FOLDER/init.lua" -PathType Leaf)) {
New-Item -ItemType File -Path "$LUA_MODULES_FOLDER/init.lua" > $null
$DEPS_PATH_CODE | Out-File -Encoding utf8 -Path "$LUA_MODULES_FOLDER/init.lua"
}
}
function Install-Deps {
param (
[ValidateNotNullOrEmpty()]
[string]$DepsFile
)
try {
$deps = (Get-Content -Path $DepsFile | ConvertFrom-Json)
} catch {
Write-Error -Message "Could not parse $DepsFile"
return
}
foreach ($dep in $deps.luarocks) {
$local:version = $dep.Split(':')[1]
$local:name = $dep.Split(':')[0]
Write-Debug -Message "Installing luarocks dependency '$dep'"
luarocks --lua-version $LUA_VERSION show --tree $LUA_MODULES_FOLDER $name ($version ? $null : $version) 2>&1 > $null
if ($?) {
Write-Debug -Message "Skipping already downloaded '$dep'"
continue
}
luarocks --lua-version $LUA_VERSION install --tree $LUA_MODULES_FOLDER $name ($version ? $null : $version)
}
foreach ($dep in $deps.github) {
$local:name = $dep.Split(':')[0]
$local:branch = $dep.Split(':')[1]
$local:dest = "$GIT_CLONE_FOLDER/$($name.Split('/') | Select-Object -Last 1)"
Write-Debug -Message "Installing github dependency '$dep'"
if (Test-Path -Path $dest) {
Write-Debug -Message "Skipping already downloaded '$dep'"
continue
}
git clone --depth=1 ($branch ? @('--single-branch', '--branch', $branch) : $null) "[email protected]:$name.git" $dest
Remove-Item -Recurse -Force -Path "$dest/.git" # very danger, much!
if (Test-Path -Path "$dest/deps.json" -PathType Leaf) {
Write-Debug -Message "Recursing into '$dest/deps.json'"
Install-Deps -DepsFile "$dest/deps.json"
}
}
}
if (Test-Path -Path $ROOT_DEPS_FILE -PathType Leaf) {
Prepare-LuaModulesFolder
Install-Deps -DepsFile $ROOT_DEPS_FILE
} else {
Write-Warning -Message "No deps.json file"
}
{
"Lua.runtime.version": "LuaJIT",
"Lua.workspace.library": [
"${3rd}/love2d/library",
"lua_modules"
],
"Lua.workspace.checkThirdParty": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment