Skip to content

Commit bdcf579

Browse files
authored
Merge pull request #2 from composer-version-manager/install-composer
Added the composer install functionality.
2 parents c89c5ad + d780ec5 commit bdcf579

File tree

5 files changed

+79
-35
lines changed

5 files changed

+79
-35
lines changed

cvm/cli.bkup.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

cvm/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from cvm.commands.use_command import UseCommand
1212
from cvm.commands.install_command import InstallCommand
1313
from cvm.commands.scan_command import ScanCommand
14+
from cvm.commands.list_command import ListCommand
1415

1516
from cvm.services.cache_service import CacheService
1617

@@ -22,7 +23,8 @@
2223
COMMANDS = {
2324
UseCommand.NAME: UseCommand,
2425
InstallCommand.NAME: InstallCommand,
25-
ScanCommand.NAME: ScanCommand
26+
ScanCommand.NAME: ScanCommand,
27+
ListCommand.NAME: ListCommand
2628
}
2729

2830

cvm/commands/list_command.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from argparse import Action, Namespace
2+
from cvm.helpers.cli import green
3+
4+
5+
from cvm.commands.command import Command
6+
from cvm.services.composer_service import ComposerService
7+
from cvm.services.github_service import GitHubService
8+
from cvm.services.cache_service import CacheService
9+
10+
11+
class ListCommand(Command):
12+
NAME = 'list'
13+
DESCRIPTION = 'Print a list of composer versions'
14+
15+
def exec(self, args: Namespace):
16+
github_service = GitHubService('composer', 'composer')
17+
composer_service = ComposerService(github_service)
18+
installed = [i.name for i in CacheService.CACHE_DIR.iterdir()]
19+
20+
for tag in composer_service.list_tags():
21+
if tag['name'] in installed:
22+
print(f"* {tag['name']} (installed)")
23+
else:
24+
print(f"* {tag['name']}")
25+
26+
27+
@staticmethod
28+
def define_signature(parser: Action):
29+
use_parser = parser.add_parser(ListCommand.NAME, help=ListCommand.DESCRIPTION)

cvm/services/cache_service.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import os
22
import logging
3+
import pathlib
34
import sys
45
from pathlib import Path
56

67

78
class CacheService:
8-
CACHE_DIR = os.path.expanduser('~') + '/.cvm/cache'
9+
CACHE_DIR = pathlib.Path.home() / '.cvm' / 'cache'
10+
SETUP_DIR = pathlib.Path.home() / '.cvm' / 'setup'
911

1012
@staticmethod
11-
def boot_cache():
12-
home_directory = os.path.expanduser('~')
13+
def boot_cache() -> None:
14+
home_directory = pathlib.Path.home()
1315

1416
if not os.access(home_directory, os.W_OK) or not os.access(home_directory, os.X_OK):
1517
logging.error('Permission to home directory is required.')
1618
sys.exit(1)
1719

18-
if not os.path.exists(CacheService.CACHE_DIR):
19-
Path(CacheService.CACHE_DIR).mkdir(parents=True, exist_ok=True)
20+
CacheService.CACHE_DIR.mkdir(parents=True, exist_ok=True)
21+
CacheService.SETUP_DIR.mkdir(parents=True, exist_ok=True)
22+
23+
@staticmethod
24+
def get_cache_folder(folder: str) -> pathlib.Path:
25+
fpath = CacheService.CACHE_DIR / folder
26+
fpath.mkdir(parents=True, exist_ok=True)
27+
return fpath

cvm/services/composer_service.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import sys
2-
import os
2+
import pathlib
3+
import requests
4+
import subprocess
35

46
from cvm.services.cache_service import CacheService
57
from cvm.services.github_service import GitHubService
68
import logging
79

810

911
class ComposerService:
12+
SETUP_URL = 'https://getcomposer.org/installer'
13+
SETUP_FILENAME = 'composer-setup.php'
14+
1015
def __init__(self, github_service: GitHubService):
1116
self.github_service = github_service
1217
self.tags = None
@@ -28,15 +33,40 @@ def tag_exists(self, desired_tag: str) -> bool:
2833

2934
@staticmethod
3035
def cached_version_exists(tag_name: str) -> bool:
31-
32-
return os.path.exists(CacheService.CACHE_DIR + "/{}".format(tag_name))
36+
'''
37+
Check if the local cache holds the requested version
38+
:param tag_name: The version to check
39+
:return: Bool
40+
'''
41+
42+
cache_path = CacheService.CACHE_DIR / tag_name
43+
return cache_path.exists()
44+
45+
def _get_setup_path(self) -> pathlib.Path:
46+
'''
47+
Download and return the path to the composer setup file.
48+
Will call sys.exit(1) if the composer setup file is unreachable
49+
'''
50+
setup_path = CacheService.SETUP_DIR / self.SETUP_FILENAME
51+
if not setup_path.exists():
52+
r = requests.get(self.SETUP_URL)
53+
if not r.ok:
54+
logging.error(f"Could not download the composer setup file. \n {r.content}")
55+
sys.exit(1)
56+
setup_path.write_bytes(r.content)
57+
return setup_path
3358

3459
def install_version(self, tag_name: str) -> bool:
3560
if not self.tag_exists(tag_name):
3661
logging.error("Tag {} does not exist.".format(tag_name))
3762
sys.exit(1)
3863

39-
# TODO: Download version into cache directory
64+
setup_path = self._get_setup_path()
65+
cache_path = CacheService.get_cache_folder(tag_name)
66+
67+
cmd = ['php', str(setup_path), f'--install-dir={str(cache_path)}', '--filename=composer', f'--version={tag_name}']
68+
result = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
69+
print(result.stdout.decode('utf-8'))
4070

4171
return True
4272

0 commit comments

Comments
 (0)