将 .goosehints 文件与 Goose 结合使用

介绍

根据 goose 官方网站的介绍,Goose 🦢 是一个开发者代理,它通过直接在终端或 IDE 中自动执行一系列编码任务来增强你的软件开发能力。在你的指导下,它可以智能地评估你的项目需求,生成所需的代码或修改,并自行实施这些更改。Goose 可以通过外部 API 与多种工具进行交互,例如 Jira、GitHub、Slack、基础设施和数据管道等——如果你的任务使用 shell 命令或可以通过 Python 脚本执行,Goose 也可以为你完成!就像半自动驾驶一样,Goose 会处理繁重的工作,让你专注于其他优先事项。只需将它设置为一项任务,稍后返回即可发现它已完成,从而以更少的手动工作提高你的工作效率。在这篇文章中,我们将探讨什么是 `.goosehints` 文件。

了解更多

什么是 .goosehints 文件以及为什么要使用它们

理解 .goosehints

“提示”文件旨在提供有关您项目的更多背景信息。背景信息可以是特定于用户的,也可以是项目级别的,在这种情况下,您可以将其提交给 git。

`.goosehints` 文件是一个**纯文本配置文件**,它为 Goose 提供有关您项目的补充信息。它充当**指南**,帮助 Goose 更好地了解项目的结构、目的,甚至您使用的特定编码约定。通过为 Goose 提供额外的背景信息,您可以让它做出**更明智的决策**、定制建议并生成**更准确的代码**或**配置**。

  • 项目特定信息:关键模块、文件夹结构或领域知识。
  • 开发约定:自定义编码风格、命名约定或框架。
  • 用户偏好:代码格式或工具使用的个人偏好。
  • 为什么使用.goosehints?

    这就是为什么 .goosehints 文件会改变游戏规则:

  • 增强上下文:当 Goose 了解项目的细节时,它会表现最佳。.goosehints 文件提供了 Goose 做出明智决策所需的额外细节。
  • 团队间一致性:通过将 .goosehints 文件提交到项目存储库,您可以确保所有团队成员都能从相同的指导中受益。Goose 将始终遵守项目标准,从而减少差异。
  • 用户特定定制:如果您更喜欢个人偏好而不是全球标准,您可以创建用户特定的 .goosehints 文件来为您定制 Goose 的行为。
  • 节省时间:有了 .goosehints 文件,Goose 就无需重复提问或做出假设。这可以节省时间并确保更快地完成任务。
  • 在你的项目中设置 .goosehints

  • 创建文件
  • # At your project root
    touch .goosehints
  • 选择正确的位置 项目范围的提示:放在根目录中 用户特定的提示:放在你的主目录中,作为 ~/.goosehints 目录特定的提示:放在相关的子目录中
  • 基础配置模板

    从这个基本模板开始并根据您的需要进行自定义:

    project:
      name: "Your Project Name"
      repository: "https://github.com/username/repo"
      type: "web-

    在 .goosehints 中使用 Jinja 模板

    Goose 🦢 本身就很强大,但当你将它与动态且适应性强的配置相结合时,它的真正潜力就会显现出来。实现这一点的一种方法是将 Jinja 模板合并到你的 `.goosehints` 文件中。使用 Jinja,你可以使你的提示动态化、可重复使用并适应各种环境或用户偏好。

    什么是 Jinja 模板?

    Jinja 是一个 **基于 Python 的模板引擎**,允许您在文本文件中使用占位符和逻辑(例如循环、条件)。它广泛用于 Flask 等 Web 框架和 Ansible 等工具,非常适合创建动态 `.goosehints` 文件。

    了解更多

    通过将 Jinja 嵌入到你的 `.goosehints` 中,你可以:

  • 根据环境变量或用户输入动态调整提示。
  • 在多个项目之间重复使用模板。
  • 添加逻辑以使 .goosehints 文件更加模块化和灵活。
  • 为什么在 .goosehints 中使用 Jinja?

    以下是在 `.goosehints` 文件中使用 Jinja 模板的一些好处:

  • 动态配置:无需手动编辑文件即可调整不同环境(例如,暂存、生产)的提示。
  • 可重用性:通过动态注入特定于项目的详细信息,在多个项目中使用相同的 .goosehints 模板。
  • 定制:允许开发人员传入自己的变量以获得更加个性化的提示。
  • 减少冗余:使用循环和宏来简化提示中的重复结构。
  • 在 .goosehints 中设置 Jinja

  • 在 Goose 中启用 Jinja 渲染 要在 .goosehints 中使用 Jinja,您需要确保 Goose 已配置为处理 Jinja 模板。虽然 Goose 通常原生支持模板,但请检查文档以验证其是否已启用或使用适当的标志:
  • goose --hints-file .goosehints --enable-jinja

    如果您使用外部文件渲染器,您也可以在将文件传递给 Goose 之前手动处理该文件:

    jinja2 .goosehints.template --format=json > .goosehints  
    goose --hints-file .goosehints
  • 编写 .goosehints 模板
  • 使用 Jinja 模板的 `.goosehints` 文件看起来与常规的 `.goosehints` 文件类似,但包含 Jinja 特定的语法。

    以下是一个例子:

  • 环境特定配置示例
  • # Project Information  
    Project Name: {{ project_name }}  
    Environment: {{ environment }}  
    
    # Framework  
    Framework: {{ framework }}  
    Version: {{ framework_version }}  
    
    # Key Modules  
    {% for module in modules %}  
    - {{ module.name }}: {{ module.description }}  
    {% endfor %}  
    
    # Preferences  
    - Code Formatting: {{ preferences.code_formatting }}  
    - Add Comments to Migrations: {{ preferences.comments }}

    **渲染文件**

    要呈现此文件,您需要提供所需的变量(例如,通过“ .json”或“ .yaml”文件)。

    jinja2 .goosehints.template -D project_name="Ticket Manager" -D environment="production" \
    -D framework="Django" -D framework_version="4.2" \
    -D modules='[{"name": "events", "description": "Handles events"}, {"name": "users", "description": "User management"}]' \
    -D preferences='{"code_formatting": "PEP8", "comments": "Yes"}' > .goosehints

    # Project Information  
    Project Name: Ticket Manager  
    Environment: production  
    
    # Framework  
    Framework: Django  
    Version: 4.2  
    
    # Key Modules  
    - events: Handles events  
    - users: User management  
    
    # Preferences  
    - Code Formatting: PEP8  
    - Add Comments to Migrations: Yes

    .goosehints 的高级 Jinja 功能

    Jinja 提供了强大的功能,让你的 `.goosehints` 更加动态:

  • 条件语句
  • # Deployment Settings  
    {% if environment == "production" %}  
    Use Optimized Database Connections: Yes  
    Enable Debug Mode: No  
    {% else %}  
    Use Optimized Database Connections: No  
    Enable Debug Mode: Yes  
    {% endif %}
  • 环形
  • # Active Features  
    {% for feature in active_features %}  
    - {{ feature }}  
    {% endfor %}
  • 使用宏重复使用逻辑块:

    {% macro module_info(module) %}  
    - {{ module.name }}: {{ module.description }}  
    {% endmacro %}  
    
    # Key Modules  
    {% for module in modules %}  
    {{ module_info(module) }}  
    {% endfor %}

    .goosehints 中 Jinja 的最佳实践

  • 保持模块化:使用宏将 .goosehints 文件分解为可重复使用的部分。
  • 验证输入数据:确保传递给 Jinja 模板的变量完整且正确。
  • 使用注释:在 .goosehints 文件中注释复杂的逻辑,以便于维护。
  • 避免过度工程:仅在必要时使用 Jinja。对于简单的项目,静态 .goosehints 文件就足够了。
  • 提交前测试:在本地呈现 .goosehints 文件,并在提交到存储库之前验证它是否按预期工作。
  • 示例用例:多环境 .goosehints

    下面是为暂存环境和生产环境设计的 `.goosehints` 文件的示例:

    # Project Info  
    Project: {{ project_name }}  
    Environment: {{ environment }}  
    
    # Database Settings  
    Database:  
    {% if environment == "production" %}  
      Host: {{ production_db.host }}  
      Port: {{ production_db.port }}  
    {% else %}  
      Host: {{ staging_db.host }}  
      Port: {{ staging_db.port }}  
    {% endif %}  
    
    # Logging Settings  
    Log Level: {{ "DEBUG" if environment == "staging" else "INFO" }}

    jinja2 .goosehints.template -D project_name="MyApp" -D environment="staging" \
    -D production_db='{"host": "prod-db.example.com", "port": 5432}' \
    -D staging_db='{"host": "staging-db.example.com", "port": 5432}'

    # Project Info  
    Project: MyApp  
    Environment: staging  
    
    # Database Settings  
    Database:  
      Host: staging-db.example.com  
      Port: 5432  
    
    # Logging Settings  
    Log Level: DEBUG

    项目级提示与用户特定提示

    了解项目级提示和用户特定提示之间的区别对于维护干净高效的开发环境至关重要。每种类型的提示都有不同的用途,并遵循不同的管理实践。

    项目级提示

    项目级提示在所有团队成员之间共享,并且通常致力于版本控制。

    位置和命名
    your-project/
    ├── .goosehints         # Main project hints
    ├── .goosehints.default # Template for user-specific hints
    ├── src/
    └── ...
    项目级提示的适当内容
    # .goosehints
    project:
      name: "example-project"
      repository: "https://github.com/org/example-project"
    
    standards:
      code_style:
        indent: 2
        max_line_length: 80
        quotes: "single"
    
    architecture:
      patterns:
        - "Repository pattern for data access"
        - "CQRS for complex operations"
    
    testing:
      framework: "jest"
      coverage:
        minimum: 80
        excludes:
          - "src/types/"
          - "src/migrations/"
    
    ci:
      required_checks:
        - "lint"
        - "test"
        - "build"

    用户特定提示

    用户特定提示包含个人偏好和本地环境配置。这些永远不应该提交到版本控制中。

    位置和命名
    ~/.goosehints                  # Global user hints
    your-project/.goosehints.local # Project-specific user hints
    适合用户特定提示的内容
    # .goosehints.local
    editor:
      path: "/usr/local/bin/vim"
      config:
        theme: "monokai"
        line_numbers: true
    
    local_env:
      database_url: "postgresql://localhost:5432/mydb"
      api_keys:
        service_a: "your-personal-key"
        service_b: "another-personal-key"
    
    debug:
      verbose: true
      log_level: "debug"
      breakpoints:
        - "src/critical/function.ts"
        - "src/auth/login.ts"
    
    shortcuts:
      build: "npm run build && npm run test"
      deploy: "kubectl apply -f ./my-local-config.yaml"

    管理两种类型

  • 新团队成员的设置模板创建 .goosehints.default 模板:
  • # .goosehints.default
    # Copy this file to .goosehints.local and customize
    local_env:
      database:
        host: "localhost"
        port: 5432
        name: "project_db"
        # Add your username and password here
        username: ""
        password: ""
    
      services:
        cache:
          port: 6379
        queue:
          port: 5672
    
    development:
      hot_reload: true
      mock_services: false  # Set to true if not running all services locally
  • Git 配置
  • 添加到 `.gitignore`:

    # .gitignore
    .goosehints.local
    */.goosehints.local
    ~/.goosehints
  • 优先规则
  • # Priority order (highest to lowest):
    # 1. .goosehints.local (user-specific, project directory)
    # 2. ~/.goosehints (user-specific, global)
    # 3. .goosehints (project-level)
    # 4. .goosehints.default (template)

    管理提示的最佳实践和技巧

  • 明确项目级别的关注点分离 (.goosehints)
  • # Things that should be consistent across all developers
    standards:
      formatting:
        indent_style: "space"
        indent_size: 2
    
    build:
      target_node: "16.x"
      optimization_level: "production"

    # Personal preferences and local setup
    workspace:
      editor_command: "code"
      terminal: "iterm2"
    
    debug:
      verbose_logging: true
      break_on_error: true

    文档

    在两个文件中添加注释,以帮助团队成员理解目的

    # .goosehints
    # Project-wide configuration that should be consistent across all developers.
    # Changes to this file should be reviewed by the team.
    
    # .goosehints.local
    # Local developer configurations. Customize as needed.
    # DO NOT commit this file to version control.

    迁移和更新

  • 更新项目级提示
  • # Create a pull request for project-level changes
    git checkout -b update-hints
    edit .goosehints
    git commit -m "Update project hints: Add new testing standards"
    git push origin update-hints
  • 更新用户特定提示
  • 创建脚本来帮助用户更新其本地提示

    #!/bin/bash
    # update-local-hints.sh
    
    if [ -f .goosehints.local ]; then
      echo "Backing up current local hints..."
      cp .goosehints.local .goosehints.local.backup
    fi
    
    echo "Updating from default template..."
    cp .goosehints.default .goosehints.local
    
    echo "Please update your personal settings in .goosehints.local"

    真实世界的例子

    为了更好地理解如何将 `.goosehints` 文件与 Goose 🦢 结合使用,让我们探索一些真实场景,在这些场景中,项目级和用户特定的提示可以发挥重要作用。这些示例展示了 `.goosehints` 如何帮助自动化工作流程、保持一致性和简化开发任务。

    Web 开发团队

    一个团队正在构建一个带有 **Node.js 后端** 和 **PostgreSQL 数据库** 的 **React 应用程序**。他们希望在整个项目中强制执行编码标准并自动执行开发任务。

  • 项目级 .goosehints
  • 团队在项目的根目录中创建一个“.goosehints”文件,以确保一致性。

    Framework: React  
    Backend: Node.js  
    Database: PostgreSQL  
    Linting: ESLint  
    Prettier: Enabled  
    Test Command: npm test  
    Build Command: npm run build  
    Deploy Command: ./scripts/deploy.sh  
    Directories:  
      - src/  
      - public/  
      - tests/  
      - scripts/

  • 在合并分支之前自动运行 npm test。
  • 使用 ESLint 和 Prettier 验证代码风格。
  • 使用 ./scripts/deploy.sh 脚本部署应用程序。
  • 用户特定的 .goosehints
  • 开发人员可以添加自己的“.goosehints”文件以满足本地偏好设置。

    Editor: VS Code  
    Node Path: /usr/local/bin/node  
    Custom Command: npm run dev  
    Personal Shortcut:  
      Start Server: npm start  
      Build App: npm run build

  • 当开发人员打开项目时自动启动 npm start。
  • 针对常用任务建议个性化的快捷方式。
  • 数据科学工作流程

    一组数据科学家正在使用 **Python**、**Jupyter Notebooks** 和 **TensorFlow** 合作开发 **机器学习管道**。他们希望 Goose 能够处理重复的设置和部署任务。

  • 项目级 .goosehints
  • 团队设置一个“.goosehints”文件来定义环境和项目结构。

    Framework: TensorFlow  
    Python Version: 3.10  
    Virtual Environment: .venv/  
    Install Command: pip install -r requirements.txt  
    Train Model Command: python train.py  
    Directories:  
      - notebooks/  
      - models/  
      - data/  
      - scripts/

  • 使用 pip install -r requirements.txt 安装依赖项。确保每个人都使用带有 .venv/ 环境的 Python 3.10。在模型训练期间自动运行 python train.py。
  • 用户特定的 .goosehints
  • 个人数据科学家使用个人“.goosehints”文件来定制他们的工作流程。

    Notebook Path: ~/projects/ml_project/notebooks  
    Data Path: /mnt/data/  
    Shortcut:  
      Open Notebook: jupyter notebook  
      Run All: python scripts/run_all.py

  • 在指定路径中打开 Jupyter Notebook。
  • 自动运行 run_all.py 来预处理数据和训练模型。
  • 开源项目贡献者

    一个使用 Django 构建的开源项目,贡献者来自世界各地。项目维护者提供 `.goosehints` 来快速吸纳新贡献者。

  • 项目级 .goosehints
  • 维护人员在存储库中定义了一个“.goosehints”文件来指导贡献者。

    Framework: Django  
    Database: SQLite (for development)  
    Python Version: 3.9  
    Virtual Environment: venv/  
    Runserver Command: python manage.py runserver  
    Test Command: python manage.py test  
    Linting: Enabled  
    Directories:  
      - app/  
      - templates/  
      - static/  
      - tests/

  • 指导贡献者使用 python -m venv venv 设置环境并安装依赖项。
  • 运行 python manage.py runserver 来启动本地服务器。
  • 强制执行 linting 和测试规则。
  • 用户特定的 .goosehints
  • 贡献者使用个人“.goosehints”文件定制他们的本地配置。