【问题标题】:Configure Composer dependency to use path for source and git for dist配置 Composer 依赖项以使用路径作为源,使用 git 作为 dist
【发布时间】:2019-06-22 02:57:43
【问题描述】:

我有一个项目使用我的另一个 GitHub 项目作为依赖项。对于此依赖项,我的 Composer 配置有两个要求:

  • 当我在本地工作时,我需要 composer install --prefer-sourcesymlink to my local copy of the dependency 以便我可以在依赖项中运行 git 命令。
  • 当我的构建过程运行时,我需要 composer install --prefer-dist 直接从 GitHub 存储库安装,无需符号链接。

之前我在composer.json 内部使用了以下内容来实现第一个要求:

  "require": {
    "narthur/natlib": "@dev"
  },
  "repositories": [
    {
      "type": "path",
      "url": "../natlib",
      "options": {
        "symlink": true
      }
    }
  ]

经过一番努力,我想出了这个,它设法满足了第二个要求:

  "require": {
    "narthur/natlib": "@dev"
  },
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "narthur/natlib",
        "version": "2019.06.20",
        "dist": {
          "type": "zip",
          "url": "https://github.com/narthur/natlib/archive/master.zip"
        },
        "source": {
          "type": "path",
          "url": "../natlib",
          "reference": "master",
          "options": {
            "symlink": true
          }
        }
      }
    }
  ]

这在 CI 中效果很好。但是当我删除我的本地vendor/ 目录和composer.lock 文件并运行composer install --prefer-source 时,我得到以下异常:

[逻辑异常]
下载器“Composer\Downloader\PathDownloader”是dist类型的下载器,不能用于下载包narthur/natlib-2019.06.20.0的源代码

我尝试将它们分成两个存储库条目,如下所示:

{
  "type": "path",
  "url": "../natlib",
  "options": {
    "symlink": true
  }
},
{
  "type": "package",
  "package": {
    "name": "narthur/natlib",
    "version": "2019.06.20",
    "dist": {
      "type": "zip",
      "url": "https://github.com/narthur/natlib/archive/master.zip"
    }
  }
}

这在本地工作,对依赖项进行符号链接,但在 CI 中失败,because Composer stops at the first matching repository entry

这里的顺序很重要。在查找包时,Composer 会从第一个到最后一个存储库进行查找,然后选择第一个匹配项。

如何配置 Composer 以按照我想要的方式运行,以便在本地 --prefer-source 标志将符号链接依赖项,而在 CI 中 --prefer-dist 标志将拉入存储库?

我找到的信息:

【问题讨论】:

  • 您可以通过为您的 github-repository 指定发布 url 来解决 dist 存储库的上述限制(必须指定存档的位置),例如https://github.com/narthur/natlib/releases/latest。这有帮助吗?
  • @dbrumann 是的!这帮助我修复了其中的 CI 部分。我已经更新了我的问题以显示我现在所拥有的。不幸的是,当我使用 --prefer-source 时,它仍然没有在本地对依赖项进行符号链接。

标签: git composer-php


【解决方案1】:

我意识到我的要求可能是不必要的。由于我的依赖项托管在 GitHub 上,因此我可以以相同的方式在本地和远程拉取它,并在其供应商文件夹中更改依赖项,然后从那里推回 GitHub。

这是我现在使用的:

  "require": {
    "narthur/natlib": "@dev"
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/narthur/natlib"
    }
  ]

我在进行这种转变时遇到的一个问题是,我最初尝试使用包类型的存储库,而不是简单的 vcs 类型的存储库。这不起作用,因为it caused Composer to ignore the dependency's composer.json file, preventing it from being autoloaded。切换到上面简单的 vcs 入口后,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2020-09-28
    相关资源
    最近更新 更多