zawadi/gitignore-writer

用于在.gitignore文件中添加和删除条目的库

1.1.0 2022-01-19 09:56 UTC

This package is auto-updated.

Last update: 2024-08-28 12:26:19 UTC


README

此库可用于以编程方式添加和删除.gitignore文件中的条目。尽管它可以在将前缀#视为注释行的任何类型的文件上工作。

条目管理基于部分名称进行,这样您可以分别管理多个部分。它还用于在调整时查找部分。

用法

$gitIgnoreWriter = new \Zawadi\GitignoreWriter\GitignoreWriter(
    './path/.gitignore', # the path of your .gitignore file 
    'name-of-my-section' # the name of the section you want to edit
);
# add items
$gitIgnoreWriter->updateSection(['/robots.txt', '/admin']);

# remove items
$gitIgnoreWriter->updateSection([], ['/robots.txt', '/admin']);

# add and remove items at the same time
$gitIgnoreWriter->updateSection(['/robots.txt', '/admin'], ['to.remove.txt']);

# replace entire section with new items; all existing items will be removed
$gitIgnoreWriter->replaceSection(['/robots.txt', '/admin']);

# remove entire section is the same as replacing it with nothing
$gitIgnoreWriter->replaceSection();

# get list of current entries in a section
$entries = $gitIgnoreWriter->getEntries();

.gitignore文件中的输出将如下所示

###> name-of-mysection >###
/admin
/robots.txt
###< name-of-mysection <###

保留条目周围的注释,因为它们用于在需要再次更新时找到部分。

常见问题解答

  • 当文件不存在时,它将被创建。
  • 当部分不存在时,它将被追加到文件的末尾。
  • 当删除部分或从部分中删除最后一个条目时,整个部分将从文件中删除。
  • 当更新部分但不会导致任何更改时,文件将不会被修改。
  • 部分内部将删除重复条目。
  • 忽略部分外部的条目,并且不会对其进行修改。
  • 条目将按字母顺序排序。
  • 条目将原样添加。

换行符

不要在条目中使用换行符,它们在写入文件时不会被考虑,但在读取文件时会被考虑。