将 .goosehints 文件与 Goose 结合使用
介绍
根据 goose 官方网站的介绍,Goose 🦢 是一个开发者代理,它通过直接在终端或 IDE 中自动执行一系列编码任务来增强你的软件开发能力。在你的指导下,它可以智能地评估你的项目需求,生成所需的代码或修改,并自行实施这些更改。Goose 可以通过外部 API 与多种工具进行交互,例如 Jira、GitHub、Slack、基础设施和数据管道等——如果你的任务使用 shell 命令或可以通过 Python 脚本执行,Goose 也可以为你完成!就像半自动驾驶一样,Goose 会处理繁重的工作,让你专注于其他优先事项。只需将它设置为一项任务,稍后返回即可发现它已完成,从而以更少的手动工作提高你的工作效率。在这篇文章中,我们将探讨什么是 `.goosehints` 文件。
了解更多
什么是 .goosehints 文件以及为什么要使用它们
理解 .goosehints
“提示”文件旨在提供有关您项目的更多背景信息。背景信息可以是特定于用户的,也可以是项目级别的,在这种情况下,您可以将其提交给 git。
`.goosehints` 文件是一个**纯文本配置文件**,它为 Goose 提供有关您项目的补充信息。它充当**指南**,帮助 Goose 更好地了解项目的结构、目的,甚至您使用的特定编码约定。通过为 Goose 提供额外的背景信息,您可以让它做出**更明智的决策**、定制建议并生成**更准确的代码**或**配置**。
为什么使用.goosehints?
这就是为什么 .goosehints 文件会改变游戏规则:
在你的项目中设置 .goosehints
# At your project root touch .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 中使用 Jinja?
以下是在 `.goosehints` 文件中使用 Jinja 模板的一些好处:
在 .goosehints 中设置 Jinja
goose --hints-file .goosehints --enable-jinja
如果您使用外部文件渲染器,您也可以在将文件传递给 Goose 之前手动处理该文件:
jinja2 .goosehints.template --format=json > .goosehints goose --hints-file .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
下面是为暂存环境和生产环境设计的 `.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 # 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
添加到 `.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)
管理提示的最佳实践和技巧
# 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”文件,以确保一致性。
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/
开发人员可以添加自己的“.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
数据科学工作流程
一组数据科学家正在使用 **Python**、**Jupyter Notebooks** 和 **TensorFlow** 合作开发 **机器学习管道**。他们希望 Goose 能够处理重复的设置和部署任务。
团队设置一个“.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/
个人数据科学家使用个人“.goosehints”文件来定制他们的工作流程。
Notebook Path: ~/projects/ml_project/notebooks Data Path: /mnt/data/ Shortcut: Open Notebook: jupyter notebook Run All: python scripts/run_all.py
开源项目贡献者
一个使用 Django 构建的开源项目,贡献者来自世界各地。项目维护者提供 `.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/
贡献者使用个人“.goosehints”文件定制他们的本地配置。