22Builds the application
33"""
44
5- import os
65import io
76import json
87import logging
8+ import os
99import pathlib
1010
1111import docker
12+ from aws_lambda_builders import RPC_PROTOCOL_VERSION as lambda_builders_protocol_version
1213from aws_lambda_builders .builder import LambdaBuilder
1314from 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 .utils . colors import Colored
18- from samcli .commands .build .exceptions import MissingBuildMethodException
17+ from samcli .lib .build . build_graph import FunctionBuildDefinition , LayerBuildDefinition , BuildGraph
18+ from samcli .lib .build .build_strategy import DefaultBuildStrategy , CachedBuildStrategy , ParallelBuildStrategy
1919from samcli .lib .providers .sam_base_provider import SamBaseProvider
20- from samcli .lib .build . build_graph import BuildDefinition , BuildGraph
20+ from samcli .lib .utils . colors import Colored
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,31 +54,37 @@ class ApplicationBuilder:
5454 converting source code into artifacts that can be run on AWS Lambda
5555 """
5656
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- ):
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 ):
6868 """
6969 Initialize the class
7070
7171 Parameters
7272 ----------
73- functions_to_build : Iterator
74- Iterator that can vend out functions available in the SAM template
73+ resources_to_build : Iterator
74+ Iterator that can vend out resources 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+
8288 is_building_specific_resource : boolean
8389 Whether customer requested to build a specific resource alone in isolation,
8490 by specifying function_identifier to the build command.
@@ -96,6 +102,8 @@ def __init__(
96102 self ._resources_to_build = resources_to_build
97103 self ._build_dir = build_dir
98104 self ._base_dir = base_dir
105+ self ._cache_dir = cache_dir
106+ self ._cached = cached
99107 self ._manifest_path_override = manifest_path_override
100108 self ._is_building_specific_resource = is_building_specific_resource
101109
@@ -115,72 +123,51 @@ def build(self):
115123 dict
116124 Returns the path to where each resource was built as a map of resource's LogicalId to the path string
117125 """
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."
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 )
126139 )
127- result [layer .name ] = self ._build_layer (
128- layer .name , layer .codeuri , layer .build_method , layer .compatible_runtimes
129- )
130-
131- return result
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 ()
132151
133152 def _get_build_graph (self ):
134153 """
135- Converts list of functions into a build graph, where we can iterate on each unique build and trigger build
154+ Converts list of functions and layers into a build graph, where we can iterate on each unique build and trigger
155+ build
136156 :return: BuildGraph, which represents list of unique build definitions
137157 """
138158 build_graph = BuildGraph (self ._build_dir )
139159 functions = self ._resources_to_build .functions
160+ layers = self ._resources_to_build .layers
140161 for function in functions :
141- build_details = BuildDefinition (function .runtime , function .codeuri , function .metadata )
142- build_graph .put_build_definition ( build_details , function )
162+ function_build_details = FunctionBuildDefinition (function .runtime , function .codeuri , function .metadata )
163+ build_graph .put_function_build_definition ( function_build_details , function )
143164
144- build_graph .clean_redundant_functions_and_update (not self ._is_building_specific_resource )
145- return build_graph
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 )
146168
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
169+ build_graph .clean_redundant_definitions_and_update (not self ._is_building_specific_resource )
170+ return build_graph
184171
185172 def update_template (self , template_dict , original_template_path , built_artifacts ):
186173 """
0 commit comments