跳至內容

shards 命令

Crystal 通常會附帶 Shards,這是它的相依性管理器。

它管理 Crystal 專案和函式庫的相依性,並在不同的電腦和系統上提供可重現的安裝。

安裝

Shards 通常會與 Crystal 本身一起發佈。或者,您的系統可能會提供獨立的 shards 套件。

若要從原始碼安裝,請下載或複製儲存庫並執行 make CRFLAGS=--release。編譯後的二進位檔位於 bin/shards 中,應將其新增至 PATH

使用方式

shards 需要在專案資料夾(工作目錄)中存在 shard.yml 檔案。此檔案描述專案並列出建置專案所需的相依性。執行 shards init 可以建立預設檔案。該檔案的內容在撰寫 Shard 指南中有說明,並且shard.yml 規格提供了該檔案格式的詳細描述。

執行 shards install 會解析並安裝指定的相依性。安裝的版本會寫入 shard.lock 檔案,以便在再次執行 shards install 時使用完全相同的相依性版本。

如果您的 shard 建置應用程式,則應將 shard.ymlshard.lock 都簽入版本控制,以提供可重現的相依性安裝。如果它僅是其他 shard 依賴的函式庫,則不應簽入 shard.lock,而僅簽入 shard.yml。建議將其新增至 .gitignorecrystal init 在初始化 lib 儲存庫時會自動執行此操作)。

Shards 命令

shards [<options>...] [<command>]

如果沒有給定任何命令,則預設會執行 install

若要查看特定命令的可用選項,請在命令後使用 --help

常用選項

  • --version:列印 shards 的版本。
  • -h, --help:列印使用方法概要。
  • --no-color:停用彩色輸出。
  • --production:以發佈模式執行。不會安裝開發相依性,且只會安裝鎖定的相依性。如果 shard.ymlshard.lock 中的相依性不同步,則命令將會失敗(由 installupdatechecklist 命令使用)
  • -q, --quiet:降低記錄詳細程度,僅列印警告和錯誤。
  • -v, --verbose:增加記錄詳細程度,列印所有偵錯陳述式。

shards build

shards build [<targets>] [<options>...]

bin 路徑中建置指定的目標。如果沒有指定任何目標,則會建置所有目標。此命令會確保已安裝所有相依性,因此不需要事先執行 shards install

命令後的所有選項都會委派給 crystal build

shards check

shards check

驗證是否已安裝所有相依性且滿足需求。

結束狀態

  • 0:滿足相依性。
  • 1:不滿足相依性。

shards init

shards init

初始化 shard 資料夾並建立 shard.yml

shards install

shards install

將相依性解析並安裝到 lib 資料夾中。如果尚未存在,則會從解析的相依性產生 shard.lock 檔案,鎖定版本號碼或 Git commit。

如果存在 shard.lock 檔案,則讀取並強制執行鎖定的版本和 commit。如果鎖定的版本與需求不符,則 install 命令可能會失敗,但如果新增了新的相依性,則可能會成功,只要它不會產生衝突,從而產生新的 shard.lock 檔案。

shards list

shards list

列出已安裝的相依性及其版本。

shards prune

shards prune

從 lib 資料夾移除未使用的相依性。

shards update

shards update

再次將所有相依性解析並更新到 lib 資料夾中,無論 shard.lock 檔案中的鎖定版本和 commit 為何。最終會產生新的 shard.lock 檔案。

shards version

shards version [<path>]

列印 shard 的版本。

修正相依性版本衝突

shard.override.yml 檔案允許覆寫相依性的來源和限制。可以使用環境變數 SHARDS_OVERRIDE 設定替代位置。

該檔案包含具有單一 dependencies 鍵的 YAML 文件。它具有與 shard.yml 相同的語意。相依性設定的優先順序高於 shard.yml 或任何相依性的 shard.yml 中的設定。

使用案例包括本機工作副本、強制使用特定的相依性版本(儘管存在不符的限制)、修正相依性、檢查與未發佈相依性版本的相容性。

範例檔案內容

dependencies:
  # Assuming we have a conflict with the version of the Redis shard
  # This will override any specified version and use the `master` branch instead
  redis:
    github: jgaskins/redis
    branch: master