Skip to content

Commit 1adb5fa

Browse files
authored
Merge pull request #17 from RobQuistNL/cat-12
Upgrade a test project, and implement local directory repositories
2 parents f1373e1 + 94e52af commit 1adb5fa

File tree

16 files changed

+210
-43
lines changed

16 files changed

+210
-43
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ after_success:
1717

1818
cache:
1919
directories:
20-
- $HOME/.composer/cache/files
20+
- $HOME/.composer/cache/files

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ The windows installer adds `catalyst` to the path by default. You can run `catal
8181

8282
If you haven't got catalyst in your path (linux or OSX), you can execute the `index.php` file manually.
8383

84+
### Install requirements
85+
First initialize your project with Catalyst by writing a `catalyst.json` file, or using `catalyst init`.
86+
87+
Write requirements into the file, or use `catalyst require <pacakgename>`.
88+
89+
Run `catalyst install` to install all dependencies.
90+
91+
### Local directories as packages
92+
If you don't want to share packages, you can add a directory repository in your `catalyst.json` file.
93+
Make sure that the project folders inside of the given folder contain a `catalyst.json` file!
94+
Since there is no version information available through this setup, the system assumes "1.0.0". So if you want to
95+
require the package `../private-projects/My Tools/My Tools.yyp` you'll have create a `catalyst.json` with
96+
`"name": "private/my-tools"` and then run `catalyst require private/my-tools` in your main project, using a constraint that
97+
satisfies `1.0.0`; for example `*` or `>=1.0`. You can mix multiple repositories.
98+
99+
```json
100+
{
101+
"name": "dukesoft/other-project",
102+
"description": "Other project",
103+
"license": "MIT",
104+
"homepage": "https://github.com/robquistnl/other",
105+
"yyp": "OtherProject.yyp",
106+
"repositories": {
107+
"../private-projects": "directory"
108+
},
109+
"require": {
110+
"private/my-tools": "*",
111+
"dukesoft/dscpu-mercy": ">=1.2"
112+
}
113+
}
114+
```
115+
84116
`catalyst help` will display all commands and information
85117

86118
### Arguments

src/Command/RequireCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Catalyst\Command;
44

55
use Catalyst\Entity\CatalystEntity;
6+
use Catalyst\Exception\PackageNotFoundException;
67
use Catalyst\Service\CatalystService;
78
use Catalyst\Service\PackageService;
89
use Catalyst\Service\StorageService;
@@ -70,7 +71,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7071
return 1;
7172
}
7273

73-
$this->packageService->packageExists($package, $version);
74+
if (!$this->packageService->packageExists($package, $version)) {
75+
throw new PackageNotFoundException($package, $version);
76+
}
7477

7578
$catalyst->addRequire($package, $version);
7679

src/Model/Repository.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
22
namespace Catalyst\Model;
33

4+
use Assert\Assert;
5+
use Assert\Assertion;
46
use Catalyst\Entity\CatalystEntity;
57
use Catalyst\Exception\PackageNotFoundException;
68
use Catalyst\Exception\PackageNotSatisfiableException;
79
use Catalyst\Service\GithubService;
10+
use Catalyst\Service\StorageService;
811
use Composer\Semver\Semver;
912

1013
class Repository implements \JsonSerializable {
@@ -27,6 +30,7 @@ class Repository implements \JsonSerializable {
2730

2831
public function __construct(string $type, string $uri)
2932
{
33+
Assertion::inArray($type, $this->getRepositoryTypes());
3034
$this->type = $type;
3135
$this->uri = $uri;
3236
}
@@ -63,7 +67,7 @@ public function packageExists(string $packageName, string $version): bool
6367
return true;
6468
}
6569

66-
throw new PackageNotSatisfiableException($packageName, $version);
70+
return false;
6771
}
6872

6973
public function findPackage(string $packageName, string $version): CatalystEntity
@@ -72,7 +76,7 @@ public function findPackage(string $packageName, string $version): CatalystEntit
7276
if (count($satisfieableVersions)) {
7377
switch ($this->type) {
7478
case self::REPO_DIRECTORY:
75-
return CatalystEntity::createFromPath($this->availablePackages[$packageName][$satisfieableVersions[0]]);
79+
return CatalystEntity::createFromPath($this->availablePackages[$packageName]['source'], true);
7680
break;
7781
case self::REPO_VCS: //@todo replace zipball downloading with git clone
7882
//@see https://github.com/GameMakerHub/Catalyst/issues/11
@@ -97,15 +101,12 @@ public function findPackageDependencies(string $packageName, string $version): a
97101
$satisfieableVersions = $this->getSatisfiableVersions($packageName, $version);
98102
if (count($satisfieableVersions)) {
99103
switch ($this->type) {
100-
case self::REPO_DIRECTORY:
101-
$catalystFile = CatalystEntity::createFromPath($this->availablePackages[$packageName][$satisfieableVersions[0]]);
102-
return $catalystFile->require();
103-
break;
104104
case self::REPO_VCS:
105105
$githubService = new GithubService();
106106
return $githubService->getDependenciesFor($packageName, $satisfieableVersions[0]);
107107
break;
108108
case self::REPO_CATALYST:
109+
case self::REPO_DIRECTORY:
109110
return $this->availablePackages[$packageName]['versions'][$version];
110111
break;
111112
}
@@ -135,9 +136,10 @@ private function scanPackagesIfNotScanned():void
135136

136137
private function scanPackagesForDirectory():void
137138
{
138-
throw new \Exception('Directory repositories are not yet supported.');
139139
$packagePaths = [];
140-
foreach (glob($this->uri . '/*/*', GLOB_ONLYDIR) as $projectPath) {
140+
141+
$realLocation = StorageService::pathToAbsolute($this->uri);
142+
foreach (glob($realLocation . '/*', GLOB_ONLYDIR) as $projectPath) {
141143
if (file_exists($projectPath . '/catalyst.json')) {
142144
$packagePaths[] = $projectPath;
143145
}
@@ -146,12 +148,18 @@ private function scanPackagesForDirectory():void
146148
foreach ($packagePaths as $packagePath) {
147149
try {
148150
$jsonData = json_decode(file_get_contents($packagePath . '/catalyst.json'));
149-
if ($jsonData->name && $jsonData->version) {
151+
if ($jsonData->name) {
150152
if (!array_key_exists($jsonData->name, $this->availablePackages)) {
151153
$this->availablePackages[$jsonData->name] = [];
152154
}
153-
154-
$this->availablePackages[$jsonData->name][$jsonData->version] = $packagePath;
155+
$this->availablePackages[$jsonData->name]['source'] = $packagePath;
156+
$versions = [];
157+
if (isset($jsonData->require)) {
158+
$versions = (array) $jsonData->require;
159+
}
160+
$this->availablePackages[$jsonData->name]['versions'] = [
161+
'1.0.0' => $versions //Default version, 1.0.0 ?
162+
];
155163
}
156164
} catch (\Exception $e) {
157165
// ignore
@@ -197,4 +205,13 @@ private function scanCatalystForPackages(): void
197205
];
198206
}
199207
}
208+
209+
private function getRepositoryTypes(): array
210+
{
211+
return [
212+
self::REPO_CATALYST,
213+
self::REPO_DIRECTORY,
214+
self::REPO_VCS,
215+
];
216+
}
200217
}

src/Service/InstallService.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function setOutput(OutputInterface $output)
3030
public function install(CatalystEntity &$project)
3131
{
3232
$this->project = $project;
33-
$this->writeLine('Resolving dependencies and building installation list...');
33+
$this->write('Resolving dependencies and building installation list...');
3434
$packagesToInstall = $this->packageService->solveDependencies($project);
3535
$this->writeLine('Done!');
3636

@@ -144,4 +144,11 @@ private function writeLine($string)
144144
$this->output->writeln($string);
145145
}
146146
}
147+
148+
private function write($string)
149+
{
150+
if (null !== $this->output) {
151+
$this->output->write($string);
152+
}
153+
}
147154
}

src/Service/PackageService.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Catalyst\Entity\CatalystEntity;
66
use Catalyst\Exception\PackageNotFoundException;
7+
use Catalyst\Exception\UnresolveableDependenciesException;
78
use Catalyst\Model\Repository;
89
use Composer\Semver\Semver;
9-
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
1010

1111
class PackageService
1212
{
@@ -16,9 +16,6 @@ class PackageService
1616
public function __construct()
1717
{
1818
// Add default repositories
19-
//new Repository(Repository::REPO_DIRECTORY, 'C:\Users\PC\Documents\GameMakerStudio2\Catalyst\tests')
20-
//new Repository(Repository::REPO_catalyst, 'https://raw.githubusercontent.com/GameMakerHub/packages/master/packages.json')
21-
//new Repository(Repository::REPO_VCS, 'git@github.com:DukeSoft/extended-functions.git')
2219
$this->addRepository(new Repository(Repository::REPO_CATALYST, 'http://repo.gamemakerhub.net'));
2320
}
2421

@@ -53,7 +50,7 @@ public function packageExists(string $package, string $version): bool
5350
}
5451
}
5552

56-
throw new PackageNotFoundException($package, $version);
53+
return false;
5754
}
5855

5956
public function getPackageDependencies(string $package, string $version): array
@@ -73,24 +70,33 @@ public function getSatisfiableVersions(string $package, string $version): array
7370
$versions = [];
7471

7572
foreach ($this->repositories as $repository) {
76-
$versions += $repository->getSatisfiableVersions($package, $version);
73+
try {
74+
$versions += $repository->getSatisfiableVersions($package, $version);
75+
} catch (PackageNotFoundException $e) {
76+
// Ignore
77+
}
7778
}
7879

7980
return $versions;
8081
}
8182

83+
private function addRepositoriesFromCatalyst(CatalystEntity $project)
84+
{
85+
foreach ($project->repositories() as $location => $type) {
86+
$this->addRepository(new Repository($type, $location));
87+
}
88+
}
89+
8290
public function solveDependencies(CatalystEntity $project, $finalPackages = [])
8391
{
8492
$requirements = $project->require();
85-
foreach ($project->repositories() as $repository) {
86-
$this->addRepository($repository);
87-
}
93+
$this->addRepositoriesFromCatalyst($project);
8894

8995
// First find all available versions of all required packages
9096
foreach ($requirements as $package => $version) {
9197
$finalPackages[$package] = $this->getSatisfiableVersions($package, $version);
9298
if (count($finalPackages[$package]) == 0) {
93-
throw new UnsatisfiedDependencyException(
99+
throw new UnresolveableDependenciesException(
94100
sprintf('No version for constraint "%s" for package "%s" can be found', $version, $package)
95101
);
96102
}
@@ -102,7 +108,7 @@ public function solveDependencies(CatalystEntity $project, $finalPackages = [])
102108
$addedNewPackage = false;
103109
foreach ($finalPackages as $package => $versions) {
104110
if (count($versions) == 0) {
105-
throw new UnsatisfiedDependencyException(
111+
throw new UnresolveableDependenciesException(
106112
$package . ' cant be satisfied, due to a dependency constraint'
107113
);
108114
}
@@ -113,7 +119,7 @@ public function solveDependencies(CatalystEntity $project, $finalPackages = [])
113119
//Apply constraint on current list
114120
$finalPackages[$depPackage] = Semver::satisfiedBy($finalPackages[$depPackage], $depVersionConstraint);
115121
} else {
116-
//Add new pacakge to list
122+
//Add new package to list
117123
$finalPackages[$depPackage] = $this->getSatisfiableVersions($depPackage, $depVersionConstraint);
118124
$addedNewPackage = true;
119125
}
@@ -125,7 +131,7 @@ public function solveDependencies(CatalystEntity $project, $finalPackages = [])
125131
$result = [];
126132
foreach ($finalPackages as $package => $versions) {
127133
if (count($versions) == 0) {
128-
throw new UnsatisfiedDependencyException(
134+
throw new UnresolveableDependenciesException(
129135
$package . ' cant be satisfied, due to a dependency constraint'
130136
);
131137
}

src/Service/StorageService.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ public function getFromWriteStorage($filename)
161161
throw new \Exception('File not written in storage: ' . $filename);
162162
}
163163

164+
public static function pathToAbsolute($pathOrFile)
165+
{
166+
$inst = self::getInstance();
167+
return $inst->getAbsoluteFilename($inst->makeRealFilename($pathOrFile));
168+
}
169+
164170
private function makeRealFilename($filename)
165171
{
166172
if (!$this->pathIsAbsolute($filename)) {
@@ -263,6 +269,11 @@ public function getAbsoluteFilename($filename) {
263269
}
264270
}
265271

266-
return join('/', $path);
272+
if (strcasecmp(substr(PHP_OS, 0, 3), 'WIN') === 0) {
273+
// Make windows style directories if we're on windows
274+
return join('/', $path);
275+
}
276+
277+
return '/' . join('/', $path);
267278
}
268279
}

tests/projects/GMLProject/catalyst.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"require": {
88
"gamemakerhub/extended-functions": "*",
99
"dukesoft/dscpu-mercy": ">=1.2"
10+
},
11+
"repositories": {
12+
"../projects": "directory"
1013
}
1114
}

tests/projects/OtherProject/OtherProject.yyp

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"license": "MIT",
55
"homepage": "https://github.com/robquistnl/other",
66
"yyp": "OtherProject.yyp",
7-
"version": "1.2.1"
7+
"require": {
8+
"dukesoft/dscpu-mercy": ">=1.2"
9+
}
810
}

0 commit comments

Comments
 (0)