Gemfury的私人作曲家套餐

tech2022-09-18  105

Hopefully you’re familiar with Composer, the latest – and probably greatest – PHP package manager. If not, check out Alexander’s introduction, and I’ve no doubt you’ll be sold on it immediately. You’ll need a working knowledge of it to get the most out of this article.

希望您熟悉Composer ,这是最新的(也许也是最出色的)PHP软件包管理器。 如果没有,请查看Alexander的介绍 ,并且毫无疑问您将立即被出售。 您需要具有一定的实用知识才能充分利用本文。

Composer works effectively and seamlessly in conjunction with Packagist, a comprehensive repository of public packages.

Composer与Packagist (一个完整的公共软件包存储库)一起有效且无缝地协同工作 。

However, sooner or later the time will come when you’ve written your own package which, for whatever reason, cannot be open-sourced and shared freely via Packagist.

但是,您迟早会写出自己的程序包,无论出于何种原因,都无法通过Packagist自由地共享和共享该程序包。

There are a few options for hosting these private packages. You can configure your projects’ composer.json file by adding your packages’ repositories individually. Or, Satis allows you to generate your own static repositories. Alternatively, Toran Proxy allows you to create a self-hosted private version of Packagist, which once set up is much easier to manage than by specifying repositories in your composer.json.

有几种托管这些私有软件包的选项。 您可以通过单独添加软件包的存储库来配置项目的composer.json文件。 或者, Satis允许您生成自己的静态存储库。 另外, Toran Proxy允许您创建Packagist的自托管私有版本,该版本一旦设置,比在composer.json指定存储库更易于管理。

Gemfury is a PaaS alternative. Aside from the peace-of-mind that comes from a hosted solution – albeit one which comes at a price – one huge advantage is that it supports not just PHP Composer packages, but Ruby Gems, Node.js npm, Python PyPi, APT, Yum and Nu-Get. Great if you have a number of languages under your belt.

Gemfury是PaaS的替代品。 除了托管解决方案带来的省心功能(尽管这需要付出一定的代价)外,一个巨大的优势是它不仅支持PHP Composer软件包,而且还支持Ruby Gems,Node.js npm,Python PyPi,APT,百胜和Nu-Get。 如果您掌握多种语言,那就太好了。

Let’s dive in and look at how to use it.

让我们深入研究一下如何使用它。

设置您的账户 (Setting up your Account)

To begin with, you’ll need an account. There is a 14-day trial, a free account limited to one collaborator and a single hosted package, as well as a range of other plans starting at US $9 per month.

首先,您需要一个帐户。 有一个为期14天的试用版,一个帐户仅限于一个合作者的免费帐户和一个托管程序包,以及一系列其他计划 ,每月9美元起。

You can either register using your email address, or with your Github account. Head to the website to sign up.

您可以使用您的电子邮件地址或Github帐户进行注册。 前往网站进行注册。

创建第一个包 (Creating your First Package)

In order to demonstrate how to use Gemfury for private Composer packages, let’s go through the process of creating a package from scratch, which we’ll later submit to the service for use in other projects.

为了演示如何将Gemfury用于私有Composer软件包,让我们完成从头开始创建软件包的过程,稍后将其提交给服务以供其他项目使用。

The simplest way to create a new package is to use the following command:

创建新软件包的最简单方法是使用以下命令:

composer init

It will ask you a series of questions; here’s an example transcript of the process:

它会问您一系列问题; 这是该过程的示例记录:

Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [your-username/package]: your-username/coupon Description []: Generates a discount coupon code using a super-secret algorithm Author [Your Name <your-username@example.com>]: Minimum Stability []: dev License []: MIT Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? yes Search for a package []: faker Found 15 packages matching faker [0] fzaninotto/faker [1] fzaninotto/faker [2] bobthecow/faker [3] tomaj/faker [4] typo3/faker [5] yiisoft/yii2-faker [6] willdurand/faker-bundle [7] denheck/faker-context [8] coduo/tutu-faker-extension [9] davidbadura/faker-bundle [10] kphoen/faker-service-provider [11] vegas-cmf/faker [12] burriko/cake-faker [13] bit3/faker-cli [14] emanueleminotto/faker-service-provider Enter package # to add, or the complete package name if it is not listed []: 0 Enter the version constraint to require []: * Search for a package []: Would you like to define your dev dependencies (require-dev) interactively [yes]? no { "name": "your-username/coupon", "description": "Generates a coupon code", "require": { "fzaninotto/faker": "*" }, "license": "MIT", "authors": [ { "name": "Your Name", "email": "your-username@example.com" } ], "minimum-stability": "dev" } Do you confirm generation [yes]? yes

As you can see, we’re creating a simple package with one dependency, Faker.

如您所见,我们正在创建一个具有一个依赖项Faker的简单包。

Next, let’s add a line to the newly-created composer.json to tell it where to look for the package’s source code.

接下来,让我们在新创建的composer.json添加一行,以告诉它在哪里寻找软件包的源代码。

"autoload": { "psr-0": { "Acme\\": "src/" } },

Now let’s create the package itself. We’re going to create a class with a single purpose; to employ a complex, top-secret proprietary algorithm to generate a discount coupon code for an e-commerce platform.

现在让我们创建包本身。 我们将创建一个具有单一目的的类; 使用复杂,绝密的专有算法为电子商务平台生成折扣优惠券代码。

In your working directory, create the src and src/Acme directories, then the following file named Coupon.php:

在您的工作目录中,创建src和src/Acme目录,然后创建以下名为Coupon.php文件:

<?php namespace Acme; use Faker\Factory; class Coupon { public static function generate($percent) { $faker = Factory::create(); return sprintf('%s-%s-%d', strtoupper(date('M')), strtoupper($faker->word()), intval($percent)); } }

Now run composer install to load our only dependency and to configure the autoloader.

现在运行composer install来加载我们唯一的依赖项并配置自动加载器。

That’s our package built. Now to upload it to Gemfury.

那是我们的包裹。 现在将其上传到Gemfury。

To continue, you’ll need your API key. If you go to your Dashboard, you’ll find it under Settings.

要继续,您需要API密钥。 如果您转到信息中心,则可以在设置下找到它。

The simplest way to build and upload a package is simply to use Git, and let Gemfury take care of the rest.

构建和上传软件包的最简单方法就是使用Git,然后让Gemfury负责其余的工作。

Start by creating a .gitignore file with the following contents:

首先创建一个具有以下内容的.gitignore文件:

vendor/ composer.lock

Now initialize the repository:

现在初始化存储库:

git init

Add the files:

添加文件:

git add src git add composer.json

Now we’ll add a Git remote. You can find the relevant URL by selecting Get Started on the Gemfury dashboard, then selecting the “PHP Composer” tab. It will look a little like this:

现在,我们将添加一个Git遥控器。 通过选择Gemfury仪表板上的“ 入门 ”,然后选择“ PHP Composer”选项卡,可以找到相关的URL。 看起来会像这样:

https://your-username@git.fury.io/your-username/<package-name>.git

Be sure to substitute your-username with your Gemfury username – if you signed up using Github, it’ll be the same as your Github username – and add it as a remote:

确保将your-username替换your-username Gemfury用户名-如果您使用Github注册,则该名称将与您的Github用户名相同-并将其添加为远程用户:

git remote add fury https://your-username@git.fury.io/your-username/coupon.git

At this point, you have two options. The first is to use explicit versioning, where you specify the version in your composer.json file, like so:

此时,您有两个选择。 第一种是使用显式版本控制 ,您可以在composer.json文件中指定版本,如下所示:

{ "name": "your-username/coupon", "description": "Generates a coupon code", "version": "1.0.0", ...

It’s important that you use semantic versioning. Otherwise your package may not build properly; this can also result in some strangely worded error messages.

使用语义版本控制很重要。 否则,您的软件包可能无法正确构建; 这也会导致措辞奇怪的错误消息。

Alternatively, you can use Git tags. For example, create a new version using a tag as follows:

另外,您可以使用Git标签。 例如,使用标签创建一个新版本,如下所示:

git tag -a 1.0.0 -m "Version 1.0.0"

Whichever approach you take, the next step is to commit:

无论采用哪种方法,下一步都是要提交:

git commit -a -m "Initial commit"

Finally, run the following command:

最后,运行以下命令:

git push fury master --tags

This will push your code up to Gemfury, which will then automatically build it as a package for you.

这会将您的代码推送到Gemfury,后者将自动为您打包。

Now, if you go to your dashboard you should see your new repository listed. Next up, let’s look at how you might use it in a project.

现在,如果转到仪表板,应该会看到列出的新存储库。 接下来,让我们看一下如何在项目中使用它。

使用私有软件包 (Using a Private Package)

If you go back to your dashboard and select “Repos” on the left-hand side, you’ll find your private repo URL. This should remain private, so keep it safe. It’ll look a little like this:

如果返回仪表板并在左侧选择“回购”,则会找到您的私人回购URL。 这应该保持私密,因此请确保安全。 看起来会像这样:

https://php.fury.io/SECRET-CODE/your-username/

It’s the SECRET-CODE which makes it un-guessable, and therefore effectively private.

正是SECRET-CODE使其无法猜测,因此实际上是私有的。

Now add it your your project’s composer.json:

现在将其添加到您项目的composer.json :

"repositories": [{ "type": "composer", "url": "https://php.fury.io/SECRET-CODE/your-username/" }],

You only need add this one repository in order to use any of the private Composer packages you create with Gemfury. No need to add separate repositories each time you want to use a package.

您只需要添加该存储库即可使用您通过Gemfury创建的任何私有Composer软件包。 每次使用软件包时,无需添加单独的存储库。

Now you can require your private packages as if they were on Packagist. Here’s a complete example of a project’s composer.json:

现在,您可以require您的私人包裹,就像它们在Packagist上一样。 这是项目的composer.json的完整示例:

{ "name": "your-username/my-ecommerce-platform", "authors": [ { "name": "Your Name", "email": "your-username@example.com" } ], "repositories": [{ "type": "composer", "url": "https://php.fury.io/SECRET-CODE/your-username/" }], "require": { "your-username/coupon": "1.0.1" } }

其他方法 (Other Approaches)

Personally, I believe using Git and tags is the simplest and most effective way to manage your packages.

我个人认为,使用Git和标签是管理软件包的最简单,最有效的方法。

Alternatively, should you prefer, you can build it yourself by zipping up your package’s source code and uploading it via the Gemfury dashboard.

另外,如果您愿意,可以通过压缩包的源代码并通过Gemfury仪表板上载它来自己构建它。

命令行工具 (The Command Line Tool)

Gemfury also provides a command-line tool. To install it:

Gemfury还提供了命令行工具。 要安装它:

sudo gem install gemfury

To list your packages, you can use the following command:

要列出您的软件包,可以使用以下命令:

fury list

Too see the versions of a specific package:

看不到特定软件包的版本:

fury versions package-name

For more information about the CLI, visit the relevant section of the documentation.

有关CLI的更多信息,请访问文档的相关部分 。

摘要 (Summary)

In this article I’ve looked at Gemfury, one of a number of options for managing private repositories. As a PaaS solution, it comes without the additional burden of a self-hosted option such as Toran, and it’s simpler to use than Satis. It also has the great benefit of supporting packages across various languages, from PHP Composer packages to Ruby Gems and Node.js npm. Of course, being a PaaS solution, it does come at a cost – but why not try it out using the free trial or the free single-package plan, and see if it works for you.

在本文中,我介绍了Gemfury,它是管理私有存储库的众多选项之一。 作为PaaS解决方案,它没有Toran这样的自托管选项带来的额外负担,并且比Satis更易于使用。 从PHP Composer软件包到Ruby Gems和Node.js npm,支持多种语言的软件包也具有很大的好处。 当然,作为PaaS解决方案,确实需要付出一定的代价–但是为什么不使用免费试用版或免费的单包计划进行试用,看看它是否对您有用。

翻译自: https://www.sitepoint.com/private-composer-packages-gemfury/

相关资源:TT作曲家制谱
最新回复(0)