mirror of
https://github.com/hanzoai/pack.git
synced 2026-09-30 23:43:13 +00:00
* refactor: update to go 1.21+ syntax and idioms
- Replace interface{} with any throughout the codebase.
- Use strings.SplitSeq and strings.CutPrefix for more efficient string processing.
- Utilize slices.Contains for cleaner slice membership checks.
- Use strings.Builder for efficient string concatenation.
- Adopt modern loop syntax (for range) for iteration.
- Remove redundant omitempty tags in struct definitions.
Generated-by: aiautocommit
* refactor: linting fix
* build: standardize commit messages for automation tasks
- Update commit messages in the upgrade task to follow conventional commit standards.
- Ensure consistency across automated fix, formatting, and snapshot update processes.
Generated-by: aiautocommit
* refactor: update commit messages in mise-upgrade task
Generated-by: aiautocommit
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/railwayapp/railpack/core"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
var PrepareCommand = &cli.Command{
|
|
Name: "prepare",
|
|
Aliases: []string{"p"},
|
|
Usage: "prepares all the files necessary for a platform to build an app with the BuildKit frontend",
|
|
ArgsUsage: "DIRECTORY",
|
|
EnableShellCompletion: true,
|
|
Flags: append([]cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "plan-out",
|
|
Usage: "output file for the JSON serialized build plan",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "info-out",
|
|
Usage: "output file for the JSON serialized build result info",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "show-plan",
|
|
Usage: "dump the build plan to stdout",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "hide-pretty-plan",
|
|
Usage: "hide the pretty-printed build result output",
|
|
},
|
|
}, commonPlanFlags()...),
|
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
|
buildResult, _, _, err := GenerateBuildResultForCommand(cmd)
|
|
if err != nil {
|
|
return cli.Exit(err, 1)
|
|
}
|
|
|
|
// Pretty print the result to stdout unless hidden
|
|
if !cmd.Bool("hide-pretty-plan") {
|
|
core.PrettyPrintBuildResult(buildResult, core.PrintOptions{Version: Version})
|
|
}
|
|
|
|
if !buildResult.Success {
|
|
os.Exit(1)
|
|
return nil
|
|
}
|
|
|
|
// Show plan to stdout if requested
|
|
if cmd.Bool("show-plan") {
|
|
planMap, err := addSchemaToPlanMap(buildResult.Plan)
|
|
if err != nil {
|
|
return cli.Exit(err, 1)
|
|
}
|
|
serialized, err := json.MarshalIndent(planMap, "", " ")
|
|
if err != nil {
|
|
return cli.Exit(err, 1)
|
|
}
|
|
_, _ = os.Stdout.Write(serialized)
|
|
}
|
|
|
|
// Save plan if requested
|
|
if planOut := cmd.String("plan-out"); planOut != "" {
|
|
// Include $schema in the plan JSON for editor support
|
|
planMap, err := addSchemaToPlanMap(buildResult.Plan)
|
|
if err != nil {
|
|
return cli.Exit(err, 1)
|
|
}
|
|
if err := writeJSONFile(planOut, planMap, "Build plan written to %s"); err != nil {
|
|
return cli.Exit(err, 1)
|
|
}
|
|
}
|
|
|
|
// Save info if requested
|
|
if infoOut := cmd.String("info-out"); infoOut != "" {
|
|
buildResult.Plan = nil
|
|
if err := writeJSONFile(infoOut, buildResult, "Build result info written to %s"); err != nil {
|
|
return cli.Exit(err, 1)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func writeJSONFile(path string, data any, logMessage string) error {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
serialized, err := json.MarshalIndent(data, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.WriteFile(path, serialized, 0644); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Debugf(logMessage, path)
|
|
return nil
|
|
}
|