Skip to content
This repository has been archived by the owner on Jun 6, 2022. It is now read-only.

Commit

Permalink
Fix composer lookup validation (#3)
Browse files Browse the repository at this point in the history
* Ensure develop branches/tags can be installed (with branch/tag validation). i.e. 2.4-develop
  • Loading branch information
Tyler Mills committed May 8, 2020
1 parent 67ae282 commit 1517097
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 52 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# mage2me

Magento 2 Downloader for Commerce & Open Source
mage2me is a download helper for Mageno 2 Commerce and Open Source. The tool allows you to not only specify the Magento edition but also any tag or branch version available in the Magento composer repository. If that's not enough, it will also validate your Magento and Github access tokens to ensure you're on the right path.

Go ahead and grab your tokens before getting started:

- Magento: https://marketplace.magento.com/customer/accessKeys/
- Github: https://github.com/settings/tokens

Download mage2me
----------------
Expand All @@ -17,7 +22,7 @@ Download & Install Magento
Some of the options shown below are indeed optional. Checkout `./mage2me --help` for more info.

```bash
./mage2me output-directory \
./mage2me download output-directory \
--mage-edition "Open Source" \
--mage-version "2.3.5" \
--github-token $GITHUB_TOKEN \
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Magento 2 Commerce/Open Source Downloader",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0|~7.2.0|~7.3.0",
"ext-json": "*",
"symfony/console": "^5.0",
"illuminate/validation": "^7.9",
"symfony/http-client": "^5.0",
Expand Down
177 changes: 157 additions & 20 deletions src/Builder/MagentoBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
class MagentoBuilder
{
/**
* @param \Closure $processOutput Handle client output
* @param string $edition
* @param string $version
* @param string $outDir
* @param string $magentoToken
* @param string $magentoPrivateToken
* @param string $githubToken
* @param \Closure $processOutput Handle client output
* @param string $edition
* @param string $version
* @param string $outDir
* @param string $magentoToken
* @param string $magentoPrivateToken
* @param string $githubToken
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
Expand All @@ -39,19 +39,61 @@ public static function deploy(
$magentoToken = "",
$magentoPrivateToken = "",
$githubToken = ""
) {
$composerAuth = sprintf(
)
{
$composerAuth = self::makeComposerAuth($magentoToken, $magentoPrivateToken, $githubToken);

self::prepareDirectoriesForInstall($outDir);
self::composerCreateProject($edition, $version, $composerAuth, $processOutput);
self::moveToOutDirectory($outDir);
self::saveComposerAuth($outDir, $composerAuth);
self::putSampleNginx($outDir, $version);
}

/**
* @param $magentoToken
* @param $magentoPrivateToken
* @param $githubToken
* @return string
*/
private static function makeComposerAuth($magentoToken, $magentoPrivateToken, $githubToken)
{
return sprintf(
'{"http-basic": {"repo.magento.com": {"username": "%s","password": "%s"}}, "github-oauth": {"github.com": "%s"}}',
$magentoToken,
$magentoPrivateToken,
$githubToken
);
}

/**
* @param $outDir
* @return void
*/
private static function prepareDirectoriesForInstall($outDir)
{
$filesystem = new Filesystem();
$filesystem->ensureDirectoryExists(self::temporaryPath());
$filesystem->ensureDirectoryExists($outDir);
$filesystem->cleanDirectory(self::temporaryPath());
}

/**
* @return string
*/
private static function temporaryPath()
{
return sys_get_temp_dir() . '/magento';
}

/**
* @param $edition
* @param $version
* @param $composerAuth
* @param \Closure $processOutput
*/
private static function composerCreateProject($edition, $version, $composerAuth, \Closure $processOutput)
{
$cmdInstall = 'composer create-project -n --no-install --repository=https://repo.magento.com/ ' . $edition . ' . ' . $version;
$createProject = Process::fromShellCommandline(
$cmdInstall,
Expand All @@ -70,33 +112,128 @@ function ($type, $buffer) use ($processOutput) {
if ($createProject->getExitCode() !== 0) {
throw new ProcessFailedException($createProject);
}

$filesystem->copyDirectory(self::temporaryPath(), $outDir);
self::putSampleNginx($version, $filesystem, $outDir);
$filesystem->put($outDir . '/auth.json', $composerAuth);
}

/**
* @return string
* @param $outDir
* @return void
*/
protected static function temporaryPath()
private static function moveToOutDirectory($outDir)
{
return sys_get_temp_dir() . '/magento';
$filesystem = new Filesystem();
$filesystem->copyDirectory(self::temporaryPath(), $outDir);
}

/**
* @param $version
* @param Filesystem $filesystem
* This is appropriate for ensuring Magento's NGINX is in place before a composer install has been executed
* @param $outDir
* @param $mageVersion
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected static function putSampleNginx($version, Filesystem $filesystem, $outDir)
private static function putSampleNginx($outDir, $mageVersion)
{
$filesystem = new Filesystem();
$mageRepoClient = HttpClient::createForBaseUri('https://raw.githubusercontent.com/magento/magento2/');
$sampleNginx = $mageRepoClient->request('GET', $version . '/nginx.conf.sample')->getContent(true);
$installedVersion = @self::getComposerData($filesystem, $outDir)['version'];
if(empty($installedVersion)){
$installedVersion = $mageVersion;
}
$sampleNginx = $mageRepoClient->request('GET', $installedVersion . '/nginx.conf.sample')->getContent(true);
$filesystem->put($outDir . '/nginx.conf.sample', $sampleNginx, true);
}

/**
* @param $outDir
* @param $composerAuth
* @return void
*/
private static function saveComposerAuth($outDir, $composerAuth)
{
(new Filesystem())->put($outDir . '/auth.json', $composerAuth);
}

/**
* @param $processOutput
* @param string $version
* @param string $outDir
* @param string $githubUrl
* @param string $magentoToken
* @param string $magentoPrivateToken
* @param string $githubToken
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public static function deployFromGitHub(
$processOutput,
$version = '2.3',
$outDir = 'new-magento',
$githubUrl = 'https://github.com/magento/magento2',
$magentoToken = "",
$magentoPrivateToken = "",
$githubToken = ""
)
{
$composerAuth = self::makeComposerAuth($magentoToken, $magentoPrivateToken, $githubToken);
self::prepareDirectoriesForInstall($outDir);
self::gitClone($version, self::authenticatedRepoUrl($githubToken, $githubUrl), $processOutput);
self::moveToOutDirectory($outDir);
self::saveComposerAuth($outDir, $composerAuth);
}

/**
* @param $branchOrTag
* @param $githubRepo
* @param \Closure $processOutput
*/
private static function gitClone($branchOrTag, $githubRepo, \Closure $processOutput)
{
$cmdGitClone = 'git clone --depth 1 --single-branch --branch ' . $branchOrTag . ' ' . $githubRepo . ' . ';

$gitClone = Process::fromShellCommandline(
$cmdGitClone,
self::temporaryPath()
);

$gitClone->start(
function ($type, $buffer) use ($processOutput) {
$processOutput($type, $buffer);
}
);
$gitClone->wait();
if ($gitClone->getExitCode() !== 0) {
throw new ProcessFailedException($gitClone);
}
}

/**
* @param $githubToken
* @param $githubUrl
* @return string|string[]
*/
private static function authenticatedRepoUrl($githubToken, $githubUrl)
{
$cloneUrl = rtrim(rtrim($githubUrl, '/'), '.git') . '.git';
return str_replace(
'https://',
'https://' . $githubToken . '@',
$cloneUrl
);
}

/**
* @param Filesystem $filesystem
* @param $outDir
* @return mixed
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
private static function getComposerData(Filesystem $filesystem, $outDir)
{
return json_decode($filesystem->get($outDir . DIRECTORY_SEPARATOR . 'composer.json'), true);
}
}
5 changes: 4 additions & 1 deletion src/Config/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public static function load()
return [
'user' => [
'auth_url' => 'https://api.github.com/user'
]
],
'magento'=> [
'Open Source' => 'https://github.com/magento/magento2/'
],
];
}
}
Loading

0 comments on commit 1517097

Please sign in to comment.