对大型主机应用进行现代化改造

在本指南中,您将了解如何使用 Cloud Code for VS Code 扩展程序来对旧版大型主机应用进行现代化改造。

借助 Mainframe Modernization Code Rewrite,客户和合作伙伴可以采用迭代方法来重写代码。它通常在完成评估后使用 ,Mainframe Assessment Tool (MAT)。 此扩展程序集成了 Mainframe GenAI 功能,可用于代码分析、规范、代码生成和测试生成,从而提供交互式开发体验。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建账号,以评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 如果您尚未安装 Cloud Code 扩展程序,请进行安装。
  9. 如果您尚未安装 Google Cloud CLI,请进行安装。

设置 Mainframe Modernization Code Rewrite

以下部分介绍了如何设置 Mainframe Modernization Code Rewrite。 您首先需要在 IDE 中启用 Mainframe Modernization Code Rewrite,然后将其连接到 Vertex AI。

启用 Mainframe Modernization Code Rewrite

如需启用 Mainframe Modernization Code Rewrite,请按照以下步骤操作:

  1. 使用 Ctrl/Cmd+Shift+P 打开 Command Palette。

  2. 选择 Preferences: Open User Settings (JSON) 命令。

    如需启用 Mainframe Modernization Code Rewrite,请将 "cloudcode.beta.enableMainframeModernization": true 设置添加到您的 settings.json 文件:

    {
        "cloudcode.updateChannel": "Insiders",
        "cloudcode.beta.enableMainframeModernization": true
    }
    
  3. 再次打开 Command Palette(按 Ctrl/Cmd+Shift+P),然后选择 Developer: Reload Window 命令。

将 Mainframe Modernization Code Rewrite 连接到 Vertex AI

如需将 Mainframe Modernization Code Rewrite 连接到 Vertex AI,请按照以下步骤操作:

  1. 在终端窗口中,运行以下命令:

    gcloud auth application-default login
    
  2. 按照新打开的 Web 浏览器窗口中的屏幕说明操作。

  3. 按照提示登录 Google Cloud。

  4. 选择要使用的项目。

  5. 运行 Developer: Reload Window 命令。

设置完成后,您可以打开包含大型主机代码的任何工作区或文件夹,并对 COBOL、JCL、Easytrieve 和 HLASM 执行 Generate Specification SummaryGenerate Modernized Code 命令。

生成规范摘要

借助 Cloud Code: Generate Specification Summary for Active File 命令,您可以为旧版大型主机代码创建中性语言规范。 支持 COBOL、JCL、Easytrieve 和 HLASM。

您可以通过 Command Palette 或 Explorer 视图访问规范摘要:

Command Palette

  1. 在编辑器中,选择要使用的源文件,使其成为活动文件。

  2. 如需打开 Command Palette,请按 Ctrl/Cmd+Shift+P

  3. 搜索并选择 Cloud Code: Generate Specification Summary for Active File 命令。

规范摘要会显示在代码旁边。

Explorer

  1. 在活动栏中,点击 Explorer 或按 Ctrl/Cmd+Shift+E

  2. 右键点击首选源文件,然后选择 Generate Specification Summary

规范摘要会显示在代码旁边。

为大型主机旧版代码添加注解

为了指导规范摘要生成过程,您可以通过添加注释来为旧版代码添加注解。如需为 JCL 作业或 COBOL 程序添加注解,请 在 作业/程序 定义行旁边找到按钮:

JCL 作业

为 JCL 作业添加注解

COBOL 程序

为 COBOL 程序添加注释

  1. 如需打开内嵌 CMS 视图,以便为作业/程序添加注解,请点击 add Add。添加注释后,点击 Create Note 以保存注释。

  2. 保存注解后,使用 Generate Specification Summary 命令,根据您的注释为作业/程序生成规范。

    然后,您可以修改注解以完善提供的指导,或者删除注解(如果您不再希望模型考虑该注解)。

生成现代化代码

借助 Cloud Code: Generate Modernized Code for Active File 命令,您可以根据大型主机旧版代码创建现代化代码。

支持的源语言:COBOL、JCL、Easytrieve 和 HLASM。

支持的目标语言:Java、C#、Python、SQL。

您可以通过 Command Palette 或 Explorer 视图访问此命令:

Command Palette

  1. 在编辑器中,选择要使用的源文件,使其成为活动文件。

  2. Ctrl/Cmd+Shift+P 打开 Command Palette。

  3. 搜索并选择 Cloud Code: Generate Modernized Code for Active File 命令。

  4. 选择要生成现代化代码的目标语言。

    生成的现代化代码会以新的未命名文件形式显示在编辑器中。

Explorer

  1. 在活动栏中,点击 Explorer 或按 Ctrl/Cmd+Shift+E

  2. 右键点击要为其生成现代化代码的源文件,然后选择 Generate Modernized Code

  3. 选择要生成现代化代码的目标语言。

    生成的现代化代码会以新的未命名文件形式显示在编辑器中。

修改设置

Mainframe Modernization Code Rewrite 具有以下设置,您可以在工作区范围内或(全局)用户设置范围内进行配置。

如需在工作区级修改设置,请按 Ctrl/Cmd+Shift+P 以 打开 Command Palette,然后选择 Preferences: Open Workspace Settings (JSON) 命令。

如需在全局用户设置级修改设置,请按 Ctrl/Cmd+Shift+P 打开 Command Palette,然后选择 Preferences: Open User Settings (JSON) 命令。

以下是一个 settings.json 文件的示例,其中包含扩展程序的所有配置属性:

{
    "cloudcode.beta.enableMainframeModernization": true,
    "cloudcode.beta.mainframeModernization.enableGoogleAnalytics": true,
    "cloudcode.beta.mainframeModernization.enableCloudLogging": false,
    "cloudcode.beta.mainframeModernization.model": "gemini-2.5-pro",
    "cloudcode.beta.mainframeModernization.codeGenerationSettings.targetLanguage": "csharp",
    "cloudcode.beta.mainframeModernization.codeGenerationSettings.techStackHints": [
        "Do not print messages directly to the console; use a logging framework instead."
    ],
}

启用和停用 Google Analytics

此配置属性用于启用或停用扩展程序的使用情况分析数据的收集。

  • 配置属性名称cloudcode.beta.mainframeModernization.enableGoogleAnalytics

  • 配置值示例: false

  • 默认值true(已启用 Analytics)。

启用和停用 Cloud Logging

此配置属性用于控制是否启用 Logging。

  • 配置属性名称cloudcode.beta.mainframeModernization.enableCloudLogging

  • 配置值示例: true

  • 默认值false(已停用 Cloud Logging)。

模型

设置此配置属性后,Mainframe Modernization Code Rewrite 会使用配置中指定的 Gemini 模型。

  • 配置属性名称cloudcode.beta.mainframeModernization.model

  • 配置值示例"gemini-1.5-flash""gemini-1.5-pro""gemini-2.0-flash""gemini-2.0-flash-lite""gemini-2.5-pro"

  • 默认值"(default)"(每次都询问)。

目标语言

设置此配置属性后,Generate Modernized Code 命令在生成代码时不再要求您选择目标语言,而是使用该属性中设置的语言。

  • 配置属性名称cloudcode.beta.mainframeModernization.codeGenerationSettings.targetLanguage

  • 配置值示例"java""csharp""python""pgsql"

  • 默认值null(每次都询问)。

代码生成技术栈提示

设置此配置属性后,Generate Modernized Code 命令会在生成代码时将指定的技术栈提示列表作为额外的说明传递给 LLM。此配置可用作调整目标架构、框架和代码样式的指南。

  • 配置属性名称cloudcode.beta.mainframeModernization.codeGenerationSettings.techStackHints

  • 配置值示例

    [
      "Do not print messages directly to the console; use a logging framework instead.",
      "when generating java code - use Spring Boot version 3 as the framework"
    ]
    
  • 默认值[](空列表;无技术栈提示)。

删除 Mainframe Modernization Code Rewrite 数据

如需移除 Mainframe Modernization Code Rewrite 为当前工作区在本地存储的所有数据,请按照以下步骤操作:

  1. 从菜单栏中选择 View > Terminal ,或按 键盘快捷键,打开终端。

  2. 在终端中输入以下命令:

Linux(Bash 或 Zsh)

workspace_id=$(printf %s "$PWD$(stat -c '%i' .)" | md5sum | head -c 32)
workspace_storage_dir="$HOME/.config/Code/User/workspaceStorage/$workspace_id"
rm -r "$workspace_storage_dir/googlecloudtools.cloudcode/mainframe/"

Windows (PowerShell)

$workspacePath = (Get-Location).ToString()

$tempFile = (New-TemporaryFile).FullName
($workspacePath.Substring(0, 1).ToLower() + $workspacePath.Substring(1) +
    (([decimal](Get-Date (Get-ItemProperty . |
        Select-Object -ExpandProperty CreationTimeUtc) -UFormat %s) * 1000) -split '\.')[0]
) | Out-File -FilePath $tempFile -Encoding ascii -NoNewline
$workspaceId = (Get-FileHash -Algorithm MD5 -Path $tempFile).Hash.ToLower()
Remove-Item $tempFile

$workspaceStorageDir = "$env:APPDATA\Code\User\workspaceStorage\$workspaceId"
Remove-Item -Recurse "$workspaceStorageDir\googlecloudtools.cloudcode\mainframe"

问题排查

本部分简要介绍了 Mainframe Modernization Code Rewrite 的已知问题,并提供了问题排查步骤:

Vertex AI 在 Google Cloud 项目中不可用

如果 Generate Specification SummaryGenerate Modernized Code 命令执行时间过长,然后失败,则可能是 Vertex AI API 未启用,或者在所选项目中超出了配额。如需确认是否是此问题,请点击 Go to output 按钮。

如果 Go to output 按钮不可用,请按照 检查输出通道中的步骤操作。在输出通道中,查找包含“Vertex.GenerateContent failed”的错误消息。 例如,以下是因 Vertex AI API 未 在所选 Google Cloud 项目中启用而导致的错误消息:

"generic::unknown: retry budget exhausted (30 attempts): Vertex.GenerateContent failed: 403 Forbidden (403)"

如需解决此问题,请执行以下任一操作:

  • 确认 Vertex AI API 已在 Google Cloud 项目中启用。

  • 切换到已启用 Vertex AI API 的其他 Google Cloud 项目。

Mainframe Modernization Code Rewrite 命令不可用

如果 Command Palette 中没有 Generate Specification SummaryGenerate Modernized Code 等命令,则可能表示 Mainframe Modernization CLI 工具安装失败。如需确认是否是这种情况,请检查以下可执行文件是否缺失:

  • 在 Linux 上:~/.cache/cloud-code/mainframe/bin/codegen
  • 在 Windows 上:%LOCALAPPDATA%\cloud-code\mainframe\bin\codegen.exe
  • 在 macOS 上:$HOME/Library/Application Support/cloud-code/mainframe/bin/codegen_macos

如果是这种情况,请尝试执行以下任务来解决问题:

  1. 使用 Ctrl/Cmd+Shift+P 打开 Command Palette。
  2. 选择 Cloud Code: Install or Update Mainframe Modernization Tools 命令。
  3. 验证可执行文件是否不再缺失。
  4. 再次使用 Ctrl/Cmd+Shift+P 打开 Command Palette。
  5. 选择 Developer: Reload Window 命令。

检查输出通道

如需排查本指南其他位置未列出的其他错误,请尝试检查扩展程序的输出通道。如需打开输出通道,请执行以下任务:

  1. 使用 Ctrl/Cmd+Shift+P 打开 Command Palette。
  2. 选择 Output: Show Output Channels 命令。
  3. 选择 Cloud Code Mainframe Modernization 输出通道。