Skip to content

Commit

Permalink
feat: Make Custom{Build,Start}Command configurable in zbpack.toml
Browse files Browse the repository at this point in the history
Linear: ZEA-1924
  • Loading branch information
pan93412 committed Oct 3, 2023
1 parent 848ac8e commit 6289e7a
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/zeaburpack/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ func Plan(opt PlanOptions) (types.PlanType, types.PlanMeta) {
src = afero.NewBasePathFs(afero.NewOsFs(), *opt.Path)
}

config := plan.NewProjectConfigurationFromFs(src)

// You can specify customBuildCommand, customStartCommand, and
// outputDir in the project configuration file, with the following
// form:
//
// [project]
// build_command = "..."
// start_command = "..."
// output_dir = "..."
//
// [project.submodule]
// build_command = "..."
// start_command = "..."
// output_dir = "..."
//
// The submodule-specific configuration overrides the project
// configuration if defined.
if opt.CustomBuildCommand == nil {
opt.CustomBuildCommand = getProjectConfigValue(config, *opt.SubmoduleName, "build_command")
}
if opt.CustomStartCommand == nil {
opt.CustomStartCommand = getProjectConfigValue(config, *opt.SubmoduleName, "start_command")
}
if opt.OutputDir == nil {
opt.OutputDir = getProjectConfigValue(config, *opt.SubmoduleName, "output_dir")
}

planner := plan.NewPlanner(
&plan.NewPlannerOptions{
Source: src,
Expand All @@ -62,3 +90,31 @@ func Plan(opt PlanOptions) (types.PlanType, types.PlanMeta) {
t, m := planner.Plan()
return t, m
}

// getProjectConfigValue returns the project-specific configuration of the specified key.
//
// The format of project-specific configuration is like:
//
// [project] # global
// key = "value"
//
// [project.submodule] # submoduleName specific
// key = "value" # overrides global
//
// If no such key is found in config, it returns nil.
func getProjectConfigValue(config plan.ProjectConfiguration, submoduleName string, key string) *string {
submoduleKey := "project." + submoduleName + "."
globalKey := "project."

if config.IsSet(submoduleKey + key) {
value := config.GetString(submoduleKey + key)
return &value
}

if config.IsSet(globalKey + key) {
value := config.GetString(globalKey + key)
return &value
}

return nil
}

0 comments on commit 6289e7a

Please sign in to comment.