|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// package webui handles command line parameters and execution logic for build webui |
| 16 | +package webui |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "os/exec" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "google.golang.org/adk/cmd/adkgo/root/build" |
| 25 | + "google.golang.org/adk/internal/cli/util" |
| 26 | +) |
| 27 | + |
| 28 | +type buildFlags struct { |
| 29 | + targetDir string // command line param |
| 30 | +} |
| 31 | + |
| 32 | +type sourceFlags struct { |
| 33 | + webuiDir string // command line param |
| 34 | +} |
| 35 | + |
| 36 | +type runLocalFlags struct { |
| 37 | + build buildFlags |
| 38 | + source sourceFlags |
| 39 | +} |
| 40 | + |
| 41 | +var flags runLocalFlags |
| 42 | + |
| 43 | +// webuiCmd represents the build webui command |
| 44 | +var webuiCmd = &cobra.Command{ |
| 45 | + Use: "webui", |
| 46 | + Short: "Build static ADK Web UI from sources.", |
| 47 | + Long: ` |
| 48 | + Builds static ADK Web UI files from sources. |
| 49 | + WARNINIG: deletes the whole build directory and recreates it anew! |
| 50 | + You need: |
| 51 | + - a downloaded version of adk-web (available at https://github.com/google/adk-web) |
| 52 | + - an ability to build adk-web (prerequisites on https://github.com/google/adk-web): |
| 53 | + npm (node js: see https://nodejs.org/en/download) |
| 54 | + ng (angular cli: see https://angular.dev/tools/cli/setup-local) |
| 55 | + - go |
| 56 | +
|
| 57 | + Building the adk-web takes a while, and sometimes presents some warnings. |
| 58 | + `, |
| 59 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 60 | + err := flags.buildWebui() |
| 61 | + return err |
| 62 | + }, |
| 63 | +} |
| 64 | + |
| 65 | +func init() { |
| 66 | + build.BuildCmd.AddCommand(webuiCmd) |
| 67 | + |
| 68 | + webuiCmd.PersistentFlags().StringVarP(&flags.build.targetDir, "targetDir", "t", "", "Target directory for build output") |
| 69 | + webuiCmd.PersistentFlags().StringVarP(&flags.source.webuiDir, "sourceDir", "s", "", "Directory containing ADK Web UI (from https://github.com/google/adk-web)") |
| 70 | +} |
| 71 | + |
| 72 | +func (f *runLocalFlags) cleanTemp() error { |
| 73 | + return util.LogStartStop("Cleaning target directory", |
| 74 | + func(p util.Printer) error { |
| 75 | + p("Clean target directory starting with", f.build.targetDir) |
| 76 | + err := os.RemoveAll(f.build.targetDir) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to clean target directory %v: %v", f.build.targetDir, err) |
| 79 | + } |
| 80 | + err = os.MkdirAll(f.build.targetDir, os.ModeDir|0700) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to create the target directory %v: %v", f.build.targetDir, err) |
| 83 | + } |
| 84 | + return nil |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func (f *runLocalFlags) ngBuildADKWebUI() error { |
| 89 | + return util.LogStartStop("Building ADK Web UI", |
| 90 | + func(p util.Printer) error { |
| 91 | + cmd := exec.Command("ng", "build", "--output-path="+f.build.targetDir) |
| 92 | + cmd.Dir = f.source.webuiDir |
| 93 | + return util.LogCommand(cmd, p) |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func (f *runLocalFlags) buildWebui() error { |
| 98 | + err := f.cleanTemp() |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + return f.ngBuildADKWebUI() |
| 103 | +} |
0 commit comments