Skip to content

Commit

Permalink
app/resources: implement composer resource
Browse files Browse the repository at this point in the history
  • Loading branch information
alexAubin committed Mar 9, 2025
1 parent e916353 commit 4a1d5f4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/tests/test_app_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,40 @@ def test_resource_go():


def test_resource_composer():
raise NotImplementedError()

os.system("echo '[integration]' >> /etc/yunohost/apps/testapp/manifest.toml")
os.system("echo 'helpers_version = \"2.1\"' >> /etc/yunohost/apps/testapp/manifest.toml")

r = AppResourceClassesByType["system_user"]
r({}, "testapp").provision_or_update()

r = AppResourceClassesByType["install_dir"]
r({}, "testapp").provision_or_update()
install_dir = app_setting("testapp", "install_dir")

r = AppResourceClassesByType["apt"]
manager = AppResourceManager("testapp", current={}, wanted={"name": "Test App", "integration": {"helpers_version": "2.1"}})
conf = {"packages": "php8.2-fpm"}
r(conf, "testapp", manager).provision_or_update()

r = AppResourceClassesByType["composer"]
assert not app_setting("testapp", "composer_version")
conf = {
"version": "2.8.3",
}

r(conf, "testapp").provision_or_update()
assert app_setting("testapp", "composer_version")
assert os.path.exists(install_dir + "/composer.phar")

r(conf, "testapp")._run_script(
"test_composer_exec",
f"cd {install_dir}; ynh_composer_exec require symfony/polyfill-mbstring 1.31.0"
)

assert os.path.exists(install_dir + "/.composer")
assert os.path.exists(install_dir + "/vendor/symfony/polyfill-mbstring")

r(conf, "testapp").deprovision()
assert not app_setting("testapp", "composer_version")
assert not os.path.exists(install_dir + "/composer.phar")
29 changes: 29 additions & 0 deletions src/utils/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,5 +1932,34 @@ class ComposerAppResource(AppResource):
"version": None,
}

@property
def composer_url(self):
return f"https://getcomposer.org/download/{self.version}/composer.phar"

def provision_or_update(self, context: Dict = {}):

install_dir = self.get_setting("install_dir")
if not install_dir:
raise YunohostError("This app has no install_dir defined ? Packagers: please make sure to have the install_dir resource before composer")

if not self.get_setting("php_version"):
raise YunohostError("This app has no php_version defined ? Packagers: please make sure to install php dependencies using apt before composer")

import requests
composer_r = requests.get(self.composer_url, timeout=30)
assert composer_r.status_code == 200, "Uhoh, failed to download {self.composer_url} ? Return code: {composer_r.status_code}"

with open(f"{install_dir}/composer.phar", "wb") as f:
f.write(composer_r.content)

self.set_setting("composer_version", self.version)

def deprovision(self, context: Dict = {}):
install_dir = self.get_setting("install_dir")

self.delete_setting("composer_version")
if os.path.exists(f"{install_dir}/composer.phar"):
os.remove(f"{install_dir}/composer.phar")


AppResourceClassesByType = {c.type: c for c in AppResource.__subclasses__()}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ynhtest_composer_install() {
pushd $install_dir
ynh_composer_install
# FIXME: should test installing as non-root with ynh_exec_as_app to validate PATH propagation ?
# Install a random simple package to validate npm is in the path and working
# Install a random simple package to validate composer is working
ynh_composer_exec require symfony/polyfill-mbstring 1.31.0
popd
}

0 comments on commit 4a1d5f4

Please sign in to comment.