Skip to content

Commit 4008bdd

Browse files
authored
refactor: assign each workflow a default build directory (#428)
1 parent 40cbfa5 commit 4008bdd

File tree

13 files changed

+154
-82
lines changed

13 files changed

+154
-82
lines changed

aws_lambda_builders/workflow.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from collections import namedtuple
99
from enum import Enum
10+
from typing import Dict, Optional
1011

1112
from aws_lambda_builders.binary_path import BinaryPath
1213
from aws_lambda_builders.path_resolver import PathResolver
@@ -38,6 +39,12 @@ class BuildMode(object):
3839
RELEASE = "release"
3940

4041

42+
class BuildDirectory(Enum):
43+
SCRATCH = "scratch"
44+
ARTIFACTS = "artifacts"
45+
SOURCE = "source"
46+
47+
4148
class BuildInSourceSupport(Enum):
4249
"""
4350
Enum to define a workflow's support for building in source.
@@ -140,14 +147,13 @@ def __new__(mcs, name, bases, class_dict):
140147
if not isinstance(cls.CAPABILITY, Capability):
141148
raise ValueError("Workflow '{}' must register valid capabilities".format(cls.NAME))
142149

143-
# All workflows must define valid default and supported values for build in source
144-
if (
145-
not isinstance(cls.BUILD_IN_SOURCE_SUPPORT, BuildInSourceSupport)
146-
or cls.BUILD_IN_SOURCE_BY_DEFAULT not in cls.BUILD_IN_SOURCE_SUPPORT.value
147-
):
148-
raise ValueError(
149-
"Workflow '{}' must define valid default and supported values for build in source".format(cls.NAME)
150-
)
150+
# All workflows must define supported values for build in source
151+
if not isinstance(cls.BUILD_IN_SOURCE_SUPPORT, BuildInSourceSupport):
152+
raise ValueError("Workflow '{}' must define supported values for build in source".format(cls.NAME))
153+
154+
# All workflows must define default build directory
155+
if not isinstance(cls.DEFAULT_BUILD_DIR, BuildDirectory):
156+
raise ValueError("Workflow '{}' must define default build directory".format(cls.NAME))
151157

152158
LOG.debug("Registering workflow '%s' with capability '%s'", cls.NAME, cls.CAPABILITY)
153159
DEFAULT_REGISTRY[cls.CAPABILITY] = cls
@@ -173,12 +179,12 @@ class BaseWorkflow(object, metaclass=_WorkflowMetaClass):
173179
# Optional list of manifests file/folder names supported by this workflow.
174180
SUPPORTED_MANIFESTS = []
175181

176-
# Whether the workflow builds in source by default, each workflow should define this.
177-
# (some workflows build in temporary or artifact directories by default)
178-
BUILD_IN_SOURCE_BY_DEFAULT = None
179182
# Support for building in source, each workflow should define this.
180183
BUILD_IN_SOURCE_SUPPORT = None
181184

185+
# The directory where the workflow builds/installs by default, each workflow should define this.
186+
DEFAULT_BUILD_DIR = None
187+
182188
def __init__(
183189
self,
184190
source_dir,
@@ -261,21 +267,39 @@ def __init__(
261267
self.is_building_layer = is_building_layer
262268
self.experimental_flags = experimental_flags if experimental_flags else []
263269

264-
self.build_in_source = build_in_source
270+
# this represents where the build/install happens, not the final output directory (that's the artifacts_dir)
271+
self.build_dir = self._select_build_dir(build_in_source)
272+
273+
# Actions are registered by the subclasses as they seem fit
274+
self.actions = []
275+
self._binaries = {}
276+
277+
def _select_build_dir(self, build_in_source: Optional[bool]) -> str:
278+
"""
279+
Returns the build directory for the workflow.
280+
"""
281+
282+
should_build_in_source = build_in_source
265283
if build_in_source not in self.BUILD_IN_SOURCE_SUPPORT.value:
284+
# assign default value
285+
should_build_in_source = self.DEFAULT_BUILD_DIR == BuildDirectory.SOURCE
286+
266287
# only show warning if an unsupported value was explicitly passed in
267288
if build_in_source is not None:
268289
LOG.warning(
269290
'Workflow %s does not support value "%s" for building in source. Using default value "%s".',
270291
self.NAME,
271292
build_in_source,
272-
self.BUILD_IN_SOURCE_BY_DEFAULT,
293+
should_build_in_source,
273294
)
274-
self.build_in_source = self.BUILD_IN_SOURCE_BY_DEFAULT
275295

276-
# Actions are registered by the subclasses as they seem fit
277-
self.actions = []
278-
self._binaries = {}
296+
build_directory_mapping = {
297+
BuildDirectory.SCRATCH: self.scratch_dir,
298+
BuildDirectory.ARTIFACTS: self.artifacts_dir,
299+
BuildDirectory.SOURCE: self.source_dir,
300+
}
301+
302+
return self.source_dir if should_build_in_source else build_directory_mapping.get(self.DEFAULT_BUILD_DIR)
279303

280304
def is_supported(self):
281305
"""

aws_lambda_builders/workflows/custom_make/workflow.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ProvidedMakeWorkflow
33
"""
44
from aws_lambda_builders.workflows.custom_make.validator import CustomMakeRuntimeValidator
5-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
5+
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport, BuildDirectory
66
from aws_lambda_builders.actions import CopySourceAction
77
from aws_lambda_builders.path_resolver import PathResolver
88
from .actions import CustomMakeAction
@@ -23,7 +23,7 @@ class CustomMakeWorkflow(BaseWorkflow):
2323

2424
EXCLUDED_FILES = (".aws-sam", ".git")
2525

26-
BUILD_IN_SOURCE_BY_DEFAULT = False
26+
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
2727
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED
2828

2929
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
@@ -35,7 +35,6 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
3535
self.os_utils = OSUtils()
3636

3737
options = kwargs.get("options") or {}
38-
build_in_source = kwargs.get("build_in_source")
3938

4039
build_logical_id = options.get("build_logical_id", None)
4140
if not build_logical_id:
@@ -47,10 +46,8 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
4746

4847
subprocess_make = SubProcessMake(make_exe=self.binaries["make"].binary_path, osutils=self.os_utils)
4948

50-
# an explicitly definied working directory should take precedence
51-
working_directory = options.get("working_directory") or self._select_working_directory(
52-
source_dir, scratch_dir, build_in_source
53-
)
49+
# an explicitly defined working directory should take precedence
50+
working_directory = options.get("working_directory") or self.build_dir
5451

5552
make_action = CustomMakeAction(
5653
artifacts_dir,
@@ -63,18 +60,12 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
6360

6461
self.actions = []
6562

66-
if not self.build_in_source:
67-
# if we're building on scratch_dir, we have to first copy the source there
68-
self.actions.append(CopySourceAction(source_dir, scratch_dir, excludes=self.EXCLUDED_FILES))
63+
if self.build_dir != source_dir:
64+
# if we're not building in the source directory, we have to first copy the source
65+
self.actions.append(CopySourceAction(source_dir, self.build_dir, excludes=self.EXCLUDED_FILES))
6966

7067
self.actions.append(make_action)
7168

72-
def _select_working_directory(self, source_dir: str, scratch_dir: str, build_in_source: bool):
73-
"""
74-
Returns the directory where the make action should be executed
75-
"""
76-
return source_dir if build_in_source else scratch_dir
77-
7869
def get_resolvers(self):
7970
return [PathResolver(runtime="provided", binary="make", executable_search_paths=self.executable_search_paths)]
8071

aws_lambda_builders/workflows/dotnet_clipackage/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
.NET Core CLI Package Workflow
33
"""
4-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
4+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
55

66
from .actions import GlobalToolInstallAction, RunPackageAction
77
from .dotnetcli import SubprocessDotnetCLI
@@ -19,7 +19,7 @@ class DotnetCliPackageWorkflow(BaseWorkflow):
1919

2020
CAPABILITY = Capability(language="dotnet", dependency_manager="cli-package", application_framework=None)
2121

22-
BUILD_IN_SOURCE_BY_DEFAULT = True
22+
DEFAULT_BUILD_DIR = BuildDirectory.SOURCE
2323
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.EXCLUSIVELY_SUPPORTED
2424

2525
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, mode=None, **kwargs):

aws_lambda_builders/workflows/go_modules/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Go Modules Workflow
33
"""
4-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
4+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
55

66
from .actions import GoModulesBuildAction
77
from .builder import GoModulesBuilder
@@ -15,7 +15,7 @@ class GoModulesWorkflow(BaseWorkflow):
1515

1616
CAPABILITY = Capability(language="go", dependency_manager="modules", application_framework=None)
1717

18-
BUILD_IN_SOURCE_BY_DEFAULT = True
18+
DEFAULT_BUILD_DIR = BuildDirectory.SOURCE
1919
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.EXCLUSIVELY_SUPPORTED
2020

2121
def __init__(

aws_lambda_builders/workflows/java_gradle/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import hashlib
55
import os
66
from aws_lambda_builders.actions import CleanUpAction
7-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
7+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
88
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
99
from aws_lambda_builders.workflows.java.utils import OSUtils
1010

@@ -25,7 +25,7 @@ class JavaGradleWorkflow(BaseWorkflow):
2525

2626
INIT_FILE = "lambda-build-init.gradle"
2727

28-
BUILD_IN_SOURCE_BY_DEFAULT = False
28+
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
2929
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED
3030

3131
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs):

aws_lambda_builders/workflows/java_maven/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Java Maven Workflow
33
"""
4-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
4+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
55
from aws_lambda_builders.actions import CopySourceAction, CleanUpAction
66
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
77
from aws_lambda_builders.workflows.java.utils import OSUtils
@@ -28,7 +28,7 @@ class JavaMavenWorkflow(BaseWorkflow):
2828

2929
EXCLUDED_FILES = (".aws-sam", ".git")
3030

31-
BUILD_IN_SOURCE_BY_DEFAULT = False
31+
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
3232
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED
3333

3434
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs):

aws_lambda_builders/workflows/nodejs_npm/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66

77
from aws_lambda_builders.path_resolver import PathResolver
8-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
8+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
99
from aws_lambda_builders.actions import (
1010
CopySourceAction,
1111
CleanUpAction,
@@ -42,7 +42,7 @@ class NodejsNpmWorkflow(BaseWorkflow):
4242

4343
CONFIG_PROPERTY = "aws_sam"
4444

45-
BUILD_IN_SOURCE_BY_DEFAULT = False
45+
DEFAULT_BUILD_DIR = BuildDirectory.ARTIFACTS
4646
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED
4747

4848
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):

aws_lambda_builders/workflows/nodejs_npm_esbuild/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing import List
99

10-
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
10+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, Capability, BuildInSourceSupport
1111
from aws_lambda_builders.actions import (
1212
CopySourceAction,
1313
CleanUpAction,
@@ -44,7 +44,7 @@ class NodejsNpmEsbuildWorkflow(BaseWorkflow):
4444

4545
CONFIG_PROPERTY = "aws_sam"
4646

47-
BUILD_IN_SOURCE_BY_DEFAULT = False
47+
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
4848
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED
4949

5050
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):

aws_lambda_builders/workflows/python_pip/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import logging
55

6-
from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
6+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, BuildInSourceSupport, Capability
77
from aws_lambda_builders.actions import CopySourceAction, CleanUpAction, LinkSourceAction
88
from aws_lambda_builders.workflows.python_pip.validator import PythonRuntimeValidator
99
from aws_lambda_builders.path_resolver import PathResolver
@@ -67,7 +67,7 @@ class PythonPipWorkflow(BaseWorkflow):
6767

6868
PYTHON_VERSION_THREE = "3"
6969

70-
BUILD_IN_SOURCE_BY_DEFAULT = False
70+
DEFAULT_BUILD_DIR = BuildDirectory.SCRATCH
7171
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED
7272

7373
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):

aws_lambda_builders/workflows/ruby_bundler/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import logging
55

6-
from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
6+
from aws_lambda_builders.workflow import BaseWorkflow, BuildDirectory, BuildInSourceSupport, Capability
77
from aws_lambda_builders.actions import CopySourceAction, CopyDependenciesAction, CleanUpAction
88
from .actions import RubyBundlerInstallAction, RubyBundlerVendorAction
99
from .utils import OSUtils
@@ -25,7 +25,7 @@ class RubyBundlerWorkflow(BaseWorkflow):
2525

2626
EXCLUDED_FILES = (".aws-sam", ".git")
2727

28-
BUILD_IN_SOURCE_BY_DEFAULT = False
28+
DEFAULT_BUILD_DIR = BuildDirectory.ARTIFACTS
2929
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED
3030

3131
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):

0 commit comments

Comments
 (0)