22Builds the application
33"""
44
5+ import os
56import io
67import json
78import logging
8- import os
99import pathlib
1010
1111import docker
12- from aws_lambda_builders import RPC_PROTOCOL_VERSION as lambda_builders_protocol_version
1312from aws_lambda_builders .builder import LambdaBuilder
1413from aws_lambda_builders .exceptions import LambdaBuilderError
14+ from aws_lambda_builders import RPC_PROTOCOL_VERSION as lambda_builders_protocol_version
1515
1616import samcli .lib .utils .osutils as osutils
17- from samcli .lib .build .build_graph import FunctionBuildDefinition , LayerBuildDefinition , BuildGraph
18- from samcli .lib .build .build_strategy import DefaultBuildStrategy , CachedBuildStrategy , ParallelBuildStrategy
19- from samcli .lib .providers .sam_base_provider import SamBaseProvider
2017from samcli .lib .utils .colors import Colored
18+ from samcli .commands .build .exceptions import MissingBuildMethodException
19+ from samcli .lib .providers .sam_base_provider import SamBaseProvider
20+ from samcli .lib .build .build_graph import BuildDefinition , BuildGraph
2121from samcli .local .docker .lambda_build_container import LambdaBuildContainer
2222from .workflow_config import get_workflow_config , get_layer_subfolder , supports_build_in_container
2323
@@ -54,37 +54,31 @@ class ApplicationBuilder:
5454 converting source code into artifacts that can be run on AWS Lambda
5555 """
5656
57- def __init__ (self ,
58- resources_to_build ,
59- build_dir ,
60- base_dir ,
61- cache_dir ,
62- cached = False ,
63- is_building_specific_resource = False ,
64- manifest_path_override = None ,
65- container_manager = None ,
66- parallel = False ,
67- mode = None ):
57+ def __init__ (
58+ self ,
59+ resources_to_build ,
60+ build_dir ,
61+ base_dir ,
62+ is_building_specific_resource = False ,
63+ manifest_path_override = None ,
64+ container_manager = None ,
65+ parallel = False ,
66+ mode = None ,
67+ ):
6868 """
6969 Initialize the class
7070
7171 Parameters
7272 ----------
73- resources_to_build : Iterator
74- Iterator that can vend out resources available in the SAM template
73+ functions_to_build : Iterator
74+ Iterator that can vend out functions available in the SAM template
7575
7676 build_dir : str
7777 Path to the directory where we will be storing built artifacts
7878
7979 base_dir : str
8080 Path to a folder. Use this folder as the root to resolve relative source code paths against
8181
82- cache_dir : str
83- Path to a the directory where we will be caching built artifacts
84-
85- cached:
86- Optional. Set to True to build each function with cache to improve performance
87-
8882 is_building_specific_resource : boolean
8983 Whether customer requested to build a specific resource alone in isolation,
9084 by specifying function_identifier to the build command.
@@ -102,8 +96,6 @@ def __init__(self,
10296 self ._resources_to_build = resources_to_build
10397 self ._build_dir = build_dir
10498 self ._base_dir = base_dir
105- self ._cache_dir = cache_dir
106- self ._cached = cached
10799 self ._manifest_path_override = manifest_path_override
108100 self ._is_building_specific_resource = is_building_specific_resource
109101
@@ -123,52 +115,73 @@ def build(self):
123115 dict
124116 Returns the path to where each resource was built as a map of resource's LogicalId to the path string
125117 """
126- build_graph = self ._get_build_graph ()
127- build_strategy = DefaultBuildStrategy (build_graph , self ._build_dir , self ._build_function , self ._build_layer )
128-
129- if self ._parallel :
130- if self ._cached :
131- build_strategy = ParallelBuildStrategy (
132- build_graph ,
133- CachedBuildStrategy (build_graph ,
134- build_strategy ,
135- self ._base_dir ,
136- self ._build_dir ,
137- self ._cache_dir ,
138- self ._is_building_specific_resource )
118+
119+ result = self ._build_functions ()
120+
121+ for layer in self ._resources_to_build .layers :
122+ LOG .info ("Building layer '%s'" , layer .name )
123+ if layer .build_method is None :
124+ raise MissingBuildMethodException (
125+ f"Layer { layer .name } cannot be build without BuildMethod. Please provide BuildMethod in Metadata."
139126 )
140- else :
141- build_strategy = ParallelBuildStrategy (build_graph , build_strategy )
142- elif self ._cached :
143- build_strategy = CachedBuildStrategy (build_graph ,
144- build_strategy ,
145- self ._base_dir ,
146- self ._build_dir ,
147- self ._cache_dir ,
148- self ._is_building_specific_resource )
149-
150- return build_strategy .build ()
127+ result [layer .name ] = self ._build_layer (
128+ layer .name , layer .codeuri , layer .build_method , layer .compatible_runtimes
129+ )
130+
131+ return result
151132
152133 def _get_build_graph (self ):
153134 """
154- Converts list of functions and layers into a build graph, where we can iterate on each unique build and trigger
155- build
135+ Converts list of functions into a build graph, where we can iterate on each unique build and trigger build
156136 :return: BuildGraph, which represents list of unique build definitions
157137 """
158138 build_graph = BuildGraph (self ._build_dir )
159139 functions = self ._resources_to_build .functions
160- layers = self ._resources_to_build .layers
161140 for function in functions :
162- function_build_details = FunctionBuildDefinition (function .runtime , function .codeuri , function .metadata )
163- build_graph .put_function_build_definition (function_build_details , function )
164-
165- for layer in layers :
166- layer_build_details = LayerBuildDefinition (layer .name , layer .codeuri , layer .build_method , layer .compatible_runtimes )
167- build_graph .put_layer_build_definition (layer_build_details , layer )
141+ build_details = BuildDefinition (function .runtime , function .codeuri , function .metadata )
142+ build_graph .put_build_definition (build_details , function )
168143
169- build_graph .clean_redundant_definitions_and_update (not self ._is_building_specific_resource )
144+ build_graph .clean_redundant_functions_and_update (not self ._is_building_specific_resource )
170145 return build_graph
171146
147+ def _build_functions (self ):
148+ """
149+ Iterates through build graph and runs each unique build and copies outcome to the corresponding function folder
150+ """
151+ build_graph = self ._get_build_graph ()
152+ function_build_results = {}
153+
154+ for build_definition in build_graph .get_build_definitions ():
155+ LOG .info ("Building codeuri: %s runtime: %s metadata: %s functions: %s" ,
156+ build_definition .codeuri ,
157+ build_definition .runtime ,
158+ build_definition .metadata ,
159+ [function .name for function in build_definition .functions ])
160+
161+ # build into one of the functions from this build definition
162+ single_function_name = build_definition .get_function_name ()
163+ single_build_dir = str (pathlib .Path (self ._build_dir , single_function_name ))
164+
165+ LOG .debug ("Building to following folder %s" , single_build_dir )
166+ self ._build_function (build_definition .get_function_name (),
167+ build_definition .codeuri ,
168+ build_definition .runtime ,
169+ build_definition .get_handler_name (),
170+ single_build_dir ,
171+ build_definition .metadata )
172+ function_build_results [single_function_name ] = single_build_dir
173+
174+ # copy results to other functions
175+ for function in build_definition .functions :
176+ if function .name is not single_function_name :
177+ # artifacts directory will be created by the builder
178+ artifacts_dir = str (pathlib .Path (self ._build_dir , function .name ))
179+ LOG .debug ("Copying artifacts from %s to %s" , single_build_dir , artifacts_dir )
180+ osutils .copytree (single_build_dir , artifacts_dir )
181+ function_build_results [function .name ] = artifacts_dir
182+
183+ return function_build_results
184+
172185 def update_template (self , template_dict , original_template_path , built_artifacts ):
173186 """
174187 Given the path to built artifacts, update the template to point appropriate resource CodeUris to the artifacts
0 commit comments