向蓝图添加存储库和源代码组件 - Amazon CodeCatalyst

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

向蓝图添加存储库和源代码组件

Amazon 使用存储库 CodeCatalyst 来存储代码。存储库以名称作为输入。大多数组件都存储在存储库中,例如源代码文件、工作流程和其他组件,例如托管开发环境 (MDE)。源存储库组件还导出用于管理文件和静态资产的组件。存储库有名称限制。有关更多信息,请参阅 使用源存储库存储代码并协作处理代码 CodeCatalyst

const repository = new SourceRepository(this, { title: 'my-new-repository-title', });

导入 Amazon CodeCatalyst 蓝图存储库和源代码组件

在您的blueprint.ts文件中,添加以下内容:

import {...} from '@caws-blueprint-component/caws-source-repositories'

添加文件

您可以使用SourceFile构造将文本文件写入存储库。该操作是最常见的用例之一,它需要存储库、文件路径和文本内容。如果存储库中不存在文件路径,则该组件将创建所有必需的文件夹。

new SourceFile(repository, `path/to/my/file/in/repo/file.txt`, 'my file contents');
注意

如果您将两个文件写入同一个存储库中的相同位置,则最新的实现会覆盖前一个实现方案。您可以使用该功能对生成的代码进行分层,这对于扩展自定义蓝图可能已生成的代码特别有用。

添加通用文件

您可以向存储库中写入任意位。您可以从缓冲区读取并使用该File构造。

new File(repository, `path/to/my/file/in/repo/file.img`, new Buffer(...)); new File(repository, `path/to/my/file/in/repo/new-img.img`, new StaticAsset('path/to/image.png').content());

正在复制文件

您可以通过复制并粘贴起始代码,然后在该基础上生成更多代码来开始使用生成的代码。将代码放在static-assets目录中,然后使用StaticAsset构造来定位该代码。在这种情况下,路径始终从static-assets目录的根目录开始。

const starterCode = new StaticAsset('path/to/file/file.txt') const starterCodeText = new StaticAsset('path/to/file/file.txt').toString() const starterCodeRawContent = new StaticAsset('path/to/image/hello.png').content() const starterCodePath = new StaticAsset('path/to/image/hello.png').path() // starterCodePath is equal to 'path/to/image/hello.png'

的子类StaticAsset是。SubstitutionAsset子类的功能完全相同,但你可以改为对文件进行胡须替换。它可用于执行 copy-and-replace 样式生成。

静态资产替换使用 mustache 模板引擎来渲染植入生成的源存储库的静态文件。Mustache 模板规则是在渲染期间应用的,这意味着默认情况下,所有值都是 HTML 编码的。要呈现未转义的 HTML,请使用三胡子语法。{{{name}}}有关更多信息,请参阅胡子模板规则

注意

对无法用文本解释的文件运行替换可能会产生错误。

const starterCodeText = new SubstitionAsset('path/to/file/file.txt').subsitite({ 'my_variable': 'subbed value1', 'another_variable': 'subbed value2' })

定位多个文件

静态资源支持通过静态函数进行全局定位,StaticAsset并调用其子类findAll(...),该函数返回预加载了路径、内容等的静态资源列表。您可以将列表与File结构链接起来,以便在static-assets目录中复制和粘贴内容。

new File(repository, `path/to/my/file/in/repo/file.img`, new Buffer(...)); new File(repository, `path/to/my/file/in/repo/new-img.img`, new StaticAsset('path/to/image.png').content());

创建新存储库并添加文件

您可以使用存储库组件在生成的项目中创建新的存储库。然后,您可以将文件或工作流程添加到创建的存储库中。

import { SourceRepository } from '@amazon-codecatalyst/codecatalyst-source-repositories'; ... const repository = new SourceRepository(this, { title: 'myRepo' });

以下示例说明如何向现有存储库中添加文件和工作流程:

import { SourceFile } from '@amazon-codecatalyst/codecatalyst-source-repositories'; import { Workflow } from '@amazon-codecatalyst/codecatalyst-workflows'; ... new SourceFile(repository, 'README.md', 'This is the content of my readme'); new Workflow(this, repository, {/**...workflowDefinition...**/});

将这两段代码组合在myRepo一起生成一个名为的存储库,其根目录为源文件README.md和 CodeCatalyst工作流程。