釋出一個自己的composer擴充套件[實戰系列]

豎橫山發表於2023-04-08

第一步新建資料夾

mkdir httptool 
cd httptool

初始化composer

composer init


Welcome to the Composer config generator  


# 互動引導初始化
This command will guide you through creating your composer.json config.

# 填寫包名,最好填寫github名/包名
Package name (<vendor>/<name>) [afishpapa/httptool]: afishpapa/httptool

# 包描述
Description []: a simple httptool

# 作者
Author [laoxie <116265858@qq.com>, n to skip]: 
laoxie <116265858@qq.com>



# 最低穩定性, 是指你可以接受的最低穩定性級別
# dev級別最低,表示你的專案可以安裝全部穩定性級別的擴充套件
# stable級別最高,表示只能安裝stable的擴充套件
# 級別從低到高排序:dev,alpha,beta,RC,stable,^,~
Minimum Stability []: stable

# 包型別,,專案,還是composer外掛,metapackage是一組依賴包的集合,安裝這種包就等於安裝一組包
Package Type (e.g. library, project, metapackage, composer-plugin) []: project

# 開源協議
License []: MIT

# 定義依賴,這裡沒有
Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no

# 定義開發依賴,這裡沒有
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

# 新增PSR-4 對映 預設 src/
Add PSR-4 autoload mapping? Maps namespace "Afishpapa\Httptool" to the entered relative path. [src/, n to skip]: src/

# 至此結束,最終在根目錄生成以下檔案: 
PS D:\workspace\composertest\httptool> ls
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/4/7     22:10                .idea
d-----          2023/4/7     22:02                src
d-----          2023/4/7     22:02                vendor
-a----          2023/4/7     22:02            388 composer.json

# 其中composer.json
{
    "name": "afishpapa/httptool",
    "description": "simple httptool",
    "type": "project",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Afishpapa\\Httptool\\": "src/"
        }
    },
    "authors": [
        {
            "name": "laoxie",
            "email": "116265858@qq.com"
        }
    ],
    "minimum-stability": "stable",
    "require": {}
}

Do you confirm generation [yes]? yes
Generating autoload files
Generated autoload files
PSR-4 autoloading configured. Use "namespace Afishpapa\Httptool;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';

新增內容

PS D:\workspace\composertest\httptool> ls      
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/4/7     22:25                .idea
d-----          2023/4/7     22:26                example  //demo程式碼,介紹你這個包怎麼呼叫
d-----          2023/4/7     22:25                src       //核心科技
d-----          2023/4/7     22:26                test     //測試單例
d-----          2023/4/7     22:02                vendor   
-a----          2023/4/7     22:13            378 composer.json
-a----          2023/4/7     22:13                README.md  //讀我

github新建一個空倉庫,上傳剛建的擴充套件包

釋出一個自己的composer擴充套件[實戰系列]

echo "# httptool" >> README.md
git init
git add README.md
git add .
git commit -m "first commit"
git branch -M main
git remote add origin
# 這裡填寫你自己的
git@github.com:afishpapa/httptool.git
git push -u origin main

註冊packagist.org賬號

可以透過oauth2.0使用GITHUB賬號登入packagist.org
點選submit,輸入github的連結

釋出一個自己的composer擴充套件[實戰系列]

check透過之後就可以了,網站會去自動去下載程式碼,
如果以後包有改動需要再來這裡點選update
釋出一個自己的composer擴充套件[實戰系列]

composer create-project試試看看

PS  composer create-project afishpapa/httptool
Creating a "afishpapa/httptool" project at "./httptool"

In CreateProjectCommand.php line 424:

  Could not find package afishpapa/httptool with stability stable.  

這個時候還不行,因為github還沒有釋出穩定版本

github release

先去打個tag

PS D:\workspace\composertest\httptool> git tag 1.0.0

PS D:\workspace\composertest\httptool> git push --tags
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:afishpapa/httptool.git
 * [new tag]         1.0.0 -> 1.0.0

釋出一個自己的composer擴充套件[實戰系列]

釋出一個自己的composer擴充套件[實戰系列]

釋出一個自己的composer擴充套件[實戰系列]

打好包的

composer create-project 再試試看

PS > composer create-project afishpapa/httptool test1
Creating a "afishpapa/httptool" project at "./test1"
Info from https://repo.packagist.org: #StandWithUkraine
Installing afishpapa/httptool (1.0.0)
  - Downloading afishpapa/httptool (1.0.0)
  - Installing afishpapa/httptool (1.0.0): Extracting archive
Created project in D:\workspace\composertest\httptool\test1
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
No installed packages - skipping audit.

得,nice

本作品採用《CC 協議》,轉載必須註明作者和本文連結
遇強則強,太強另說

相關文章