44package component
55
66import (
7- "context"
87 "errors"
98 "fmt"
109
@@ -14,11 +13,9 @@ import (
1413 "github.com/microsoft/azldev/internal/app/azldev/core/sources"
1514 "github.com/microsoft/azldev/internal/app/azldev/core/workdir"
1615 "github.com/microsoft/azldev/internal/buildenv"
17- "github.com/microsoft/azldev/internal/orchestration/artifacts"
18- "github.com/microsoft/azldev/internal/orchestration/orchestrator"
1916 "github.com/microsoft/azldev/internal/providers/sourceproviders"
2017 "github.com/microsoft/azldev/internal/utils/defers"
21- "github.com/samber/lo "
18+ "github.com/microsoft/azldev/internal/utils/fileutils "
2219 "github.com/spf13/cobra"
2320)
2421
@@ -81,6 +78,8 @@ func NewBuildCmd() *cobra.Command {
8178
8279func SelectAndBuildComponents (env * azldev.Env , options * ComponentBuildOptions ,
8380) ([]ComponentBuildResults , error ) {
81+ var comps * components.ComponentSet
82+
8483 resolver := components .NewResolver (env )
8584
8685 comps , err := resolver .FindComponents (& options .ComponentFilter )
@@ -95,10 +94,10 @@ func SelectAndBuildComponents(env *azldev.Env, options *ComponentBuildOptions,
9594 )
9695 }
9796
98- return buildComponents (env , comps , options )
97+ return BuildComponents (env , comps , options )
9998}
10099
101- func buildComponents (
100+ func BuildComponents (
102101 env * azldev.Env , components * components.ComponentSet , options * ComponentBuildOptions ,
103102) ([]ComponentBuildResults , error ) {
104103 if env .WorkDir () == "" {
@@ -114,18 +113,10 @@ func buildComponents(
114113 return nil , fmt .Errorf ("failed to create work dir factory:\n %w" , err )
115114 }
116115
117- orchestrator , err := orchestrator .NewOrchestrator (env .FS (),
118- orchestrator .WithBuildDir (env .WorkDir ()),
119- orchestrator .WithOutputDir (env .OutputDir ()),
120- )
121- if err != nil {
122- return nil , fmt .Errorf ("failed to create orchestrator:\n %w" , err )
123- }
124-
125116 results := make ([]ComponentBuildResults , 0 , components .Len ())
126117
127118 for _ , component := range components .Components () {
128- componentResults , buildErr := BuildComponent (env , orchestrator , component , workDirFactory , options )
119+ componentResults , buildErr := BuildComponent (env , component , workDirFactory , options )
129120 if buildErr != nil {
130121 buildErr = fmt .Errorf ("failed to build %q:\n %w" , component .GetName (), buildErr )
131122 }
@@ -143,7 +134,6 @@ func buildComponents(
143134
144135func BuildComponent (
145136 env * azldev.Env ,
146- orchestrator * orchestrator.Orchestrator ,
147137 component components.Component ,
148138 workDirFactory * workdir.Factory ,
149139 options * ComponentBuildOptions ,
@@ -179,119 +169,56 @@ func BuildComponent(
179169
180170 builder := componentbuilder .New (env , env .FS (), env , sourcePreparer , buildEnv , workDirFactory )
181171
182- return buildComponentUsingBuilder (env , orchestrator , component , builder ,
172+ return buildComponentUsingBuilder (env , component , builder ,
183173 options .SourcePackageOnly , options .RunChecks ,
184174 )
185175}
186176
187177func buildComponentUsingBuilder (
188- env * azldev.Env ,
189- orchestrator * orchestrator.Orchestrator ,
190- component components.Component ,
191- builder * componentbuilder.Builder ,
178+ env * azldev.Env , component components.Component , builder * componentbuilder.Builder ,
192179 sourcePackageOnly , runChecks bool ,
193- ) (ComponentBuildResults , error ) {
194- buildEvent := env .StartEvent ("Building packages with mock" , "component" , component .GetName ())
195- defer buildEvent .End ()
196-
197- results := ComponentBuildResults {
198- ComponentName : component .GetName (),
199- }
200-
201- srpmPath , err := buildSRPM (env .Context (), builder , component , orchestrator )
202- if err != nil {
203- return results , fmt .Errorf ("failed to build SRPM for %q:\n %w" , component .GetName (), err )
204- }
205-
206- results .SRPMPaths = []string {srpmPath }
207-
208- // Short circuit if we were asked only to build the SRPM.
209- if sourcePackageOnly {
210- return results , nil
211- }
212-
213- rpmPaths , err := buildRPMs (env .Context (), builder , component , srpmPath , runChecks , orchestrator )
214- if err != nil {
215- return results , fmt .Errorf ("failed to build RPMs for %q:\n %w" , component .GetName (), err )
216- }
217-
218- results .RPMPaths = rpmPaths
219-
220- return results , nil
221- }
222-
223- func buildRPMs (
224- ctx context.Context , builder componentbuilder.ComponentBuilder , component components.Component ,
225- srpmPath string , runChecks bool , orchestrator * orchestrator.Orchestrator ,
226- ) ([]string , error ) {
227- // Issue #273: replace external task creation for each component with a single goal task
228- // and move creation of subtasks into the orchestrator and task factories.
229- binaryTaskBuilder , err := NewBinaryPackageTaskBuilder (builder , component , srpmPath , runChecks )
230- if err != nil {
231- return nil , fmt .Errorf ("failed to create binary package task builder:\n %w" , err )
232- }
233-
234- err = orchestrator .AssignTaskServices (binaryTaskBuilder )
235- if err != nil {
236- return nil , fmt .Errorf ("failed to assign task services for binary package task:\n %w" , err )
237- }
180+ ) (results ComponentBuildResults , err error ) {
181+ // Compose the path to the output dir.
182+ outputDir := env .OutputDir ()
238183
239- binaryTask , err := binaryTaskBuilder .Build ()
184+ // Make sure we have a final output dir.
185+ err = fileutils .MkdirAll (env .FS (), outputDir )
240186 if err != nil {
241- return nil , fmt .Errorf ("failed to build binary package task: \n %w" , err )
187+ return results , fmt .Errorf ("failed to ensure dir %q exists: %w" , outputDir , err )
242188 }
243189
244- rawBinaryArtifacts , err := orchestrator .RunTask (ctx , binaryTask )
245- if err != nil {
246- return nil , fmt .Errorf ("failed to run binary package task:\n %w" , err )
247- }
190+ buildEvent := env .StartEvent ("Building packages with mock" , "component" , component .GetName ())
248191
249- typedBinaryArtifacts , err := artifacts .Cast [* FileArtifact ](rawBinaryArtifacts ... )
250- if err != nil {
251- return nil , fmt .Errorf ("failed to cast binary artifacts:\n %w" , err )
252- }
192+ defer buildEvent .End ()
253193
254- // Extract RPM paths from artifacts
255- return lo .Map (typedBinaryArtifacts , func (artifact * FileArtifact , _ int ) string {
256- return artifact .Path
257- }), nil
258- }
194+ //
195+ // Build the SRPM.
196+ //
259197
260- func buildSRPM (
261- ctx context.Context , builder componentbuilder.ComponentBuilder , component components.Component ,
262- orchestrator * orchestrator.Orchestrator ,
263- ) (string , error ) {
264- // Issue #273: replace external task creation for each component with a single goal task
265- // and move creation of subtasks into the orchestrator and task factories.
266- sourceTaskBuilder , err := NewSourcesTaskBuilder (builder , component )
267- if err != nil {
268- return "" , fmt .Errorf ("failed to create sources task builder:\n %w" , err )
269- }
198+ var outputSourcePackagePath string
270199
271- err = orchestrator . AssignTaskServices ( sourceTaskBuilder )
200+ outputSourcePackagePath , err = builder . BuildSourcePackage ( env , component , outputDir )
272201 if err != nil {
273- return "" , fmt .Errorf ("failed to assign task services for sources task :\n %w" , err )
202+ return results , fmt .Errorf ("failed to build SRPM for %q :\n %w" , component . GetName () , err )
274203 }
275204
276- sourcesTask , err := sourceTaskBuilder .Build ()
277- if err != nil {
278- return "" , fmt .Errorf ("failed to build sources task:\n %w" , err )
279- }
205+ // Start filling out results.
206+ results .ComponentName = component .GetName ()
207+ results .SRPMPaths = []string {outputSourcePackagePath }
280208
281- rawArtifacts , err := orchestrator . RunTask ( ctx , sourcesTask )
282- if err != nil {
283- return "" , fmt . Errorf ( "failed to run sources task: \n %w" , err )
209+ // Short circuit if we were asked only to build the SRPM.
210+ if sourcePackageOnly {
211+ return results , nil
284212 }
285213
286- if len (rawArtifacts ) != 1 {
287- return "" , fmt .Errorf ("expected exactly one source package artifact for component %#q, got %d" ,
288- component .GetName (), len (rawArtifacts ))
289- }
214+ //
215+ // Build the RPM.
216+ //
290217
291- typedArtifacts , err := artifacts . Cast [ * FileArtifact ]( rawArtifacts ... )
218+ results . RPMPaths , err = builder . BuildBinaryPackage ( env , component , outputSourcePackagePath , outputDir , runChecks )
292219 if err != nil {
293- return "" , fmt .Errorf ("failed to cast artifact: \n %w" , err )
220+ return results , fmt .Errorf ("failed to build RPM for %q: %w" , component . GetName () , err )
294221 }
295222
296- return typedArtifacts [ 0 ]. Path , nil
223+ return results , nil
297224}
0 commit comments