Skip to content

Commit

Permalink
feat: handle default and supported values for building in source per …
Browse files Browse the repository at this point in the history
…workflow (#418)
  • Loading branch information
torresxb1 authored Jan 5, 2023
1 parent b48bc30 commit 75927e3
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 16 deletions.
42 changes: 42 additions & 0 deletions aws_lambda_builders/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging

from collections import namedtuple
from enum import Enum

from aws_lambda_builders.binary_path import BinaryPath
from aws_lambda_builders.path_resolver import PathResolver
Expand Down Expand Up @@ -37,6 +38,21 @@ class BuildMode(object):
RELEASE = "release"


class BuildInSourceSupport(Enum):
"""
Enum to define a workflow's support for building in source.
"""

# can't build in source directory (e.g. only able to build in temporary or artifacts directories)
NOT_SUPPORTED = [False]

# can build in source directory but not required to
OPTIONALLY_SUPPORTED = [False, True]

# only able to build in source directory and not somewhere else
EXCLUSIVELY_SUPPORTED = [True]


# TODO: Move sanitize out to its own class.
def sanitize(func): # pylint: disable=too-many-statements
"""
Expand Down Expand Up @@ -124,6 +140,15 @@ def __new__(mcs, name, bases, class_dict):
if not isinstance(cls.CAPABILITY, Capability):
raise ValueError("Workflow '{}' must register valid capabilities".format(cls.NAME))

# All workflows must define valid default and supported values for build in source
if (
not isinstance(cls.BUILD_IN_SOURCE_SUPPORT, BuildInSourceSupport)
or cls.BUILD_IN_SOURCE_BY_DEFAULT not in cls.BUILD_IN_SOURCE_SUPPORT.value
):
raise ValueError(
"Workflow '{}' must define valid default and supported values for build in source".format(cls.NAME)
)

LOG.debug("Registering workflow '%s' with capability '%s'", cls.NAME, cls.CAPABILITY)
DEFAULT_REGISTRY[cls.CAPABILITY] = cls

Expand All @@ -148,6 +173,12 @@ class BaseWorkflow(object, metaclass=_WorkflowMetaClass):
# Optional list of manifests file/folder names supported by this workflow.
SUPPORTED_MANIFESTS = []

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

def __init__(
self,
source_dir,
Expand Down Expand Up @@ -229,7 +260,18 @@ def __init__(
self.architecture = architecture
self.is_building_layer = is_building_layer
self.experimental_flags = experimental_flags if experimental_flags else []

self.build_in_source = build_in_source
if build_in_source not in self.BUILD_IN_SOURCE_SUPPORT.value:
# only show warning if an unsupported value was explicitly passed in
if build_in_source is not None:
LOG.warning(
'Workflow %s does not support value "%s" for building in source. Using default value "%s".',
self.NAME,
build_in_source,
self.BUILD_IN_SOURCE_BY_DEFAULT,
)
self.build_in_source = self.BUILD_IN_SOURCE_BY_DEFAULT

# Actions are registered by the subclasses as they seem fit
self.actions = []
Expand Down
9 changes: 4 additions & 5 deletions aws_lambda_builders/workflows/custom_make/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ProvidedMakeWorkflow
"""
from aws_lambda_builders.workflows.custom_make.validator import CustomMakeRuntimeValidator
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import CopySourceAction
from aws_lambda_builders.path_resolver import PathResolver
from .actions import CustomMakeAction
Expand All @@ -23,6 +23,9 @@ class CustomMakeWorkflow(BaseWorkflow):

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

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED

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

super(CustomMakeWorkflow, self).__init__(
Expand All @@ -44,10 +47,6 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim

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

# Don't build in source by default (backwards compatibility)
if build_in_source is None:
build_in_source = False

# an explicitly definied working directory should take precedence
working_directory = options.get("working_directory") or self._select_working_directory(
source_dir, scratch_dir, build_in_source
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/dotnet_clipackage/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
.NET Core CLI Package Workflow
"""
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport

from .actions import GlobalToolInstallAction, RunPackageAction
from .dotnetcli import SubprocessDotnetCLI
Expand All @@ -19,6 +19,9 @@ class DotnetCliPackageWorkflow(BaseWorkflow):

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

BUILD_IN_SOURCE_BY_DEFAULT = True
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.EXCLUSIVELY_SUPPORTED

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

super(DotnetCliPackageWorkflow, self).__init__(
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/go_modules/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Go Modules Workflow
"""
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport

from .actions import GoModulesBuildAction
from .builder import GoModulesBuilder
Expand All @@ -15,6 +15,9 @@ class GoModulesWorkflow(BaseWorkflow):

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

BUILD_IN_SOURCE_BY_DEFAULT = True
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.EXCLUSIVELY_SUPPORTED

def __init__(
self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, mode=None, **kwargs
):
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/java_gradle/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hashlib
import os
from aws_lambda_builders.actions import CleanUpAction
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
from aws_lambda_builders.workflows.java.utils import OSUtils

Expand All @@ -25,6 +25,9 @@ class JavaGradleWorkflow(BaseWorkflow):

INIT_FILE = "lambda-build-init.gradle"

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs):
super(JavaGradleWorkflow, self).__init__(source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs)

Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/java_maven/workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Java Maven Workflow
"""
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import CopySourceAction, CleanUpAction
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
from aws_lambda_builders.workflows.java.utils import OSUtils
Expand All @@ -28,6 +28,9 @@ class JavaMavenWorkflow(BaseWorkflow):

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

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs):
super(JavaMavenWorkflow, self).__init__(source_dir, artifacts_dir, scratch_dir, manifest_path, **kwargs)

Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/nodejs_npm/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging

from aws_lambda_builders.path_resolver import PathResolver
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import (
CopySourceAction,
CleanUpAction,
Expand Down Expand Up @@ -42,6 +42,9 @@ class NodejsNpmWorkflow(BaseWorkflow):

CONFIG_PROPERTY = "aws_sam"

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

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

super(NodejsNpmWorkflow, self).__init__(
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/nodejs_npm_esbuild/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing import List

from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, Capability, BuildInSourceSupport
from aws_lambda_builders.actions import (
CopySourceAction,
CleanUpAction,
Expand Down Expand Up @@ -44,6 +44,9 @@ class NodejsNpmEsbuildWorkflow(BaseWorkflow):

CONFIG_PROPERTY = "aws_sam"

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

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

super(NodejsNpmEsbuildWorkflow, self).__init__(
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/python_pip/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging

from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
from aws_lambda_builders.actions import CopySourceAction, CleanUpAction, LinkSourceAction
from aws_lambda_builders.workflows.python_pip.validator import PythonRuntimeValidator
from aws_lambda_builders.path_resolver import PathResolver
Expand Down Expand Up @@ -67,6 +67,9 @@ class PythonPipWorkflow(BaseWorkflow):

PYTHON_VERSION_THREE = "3"

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

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

super(PythonPipWorkflow, self).__init__(
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_builders/workflows/ruby_bundler/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging

from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
from aws_lambda_builders.actions import CopySourceAction, CopyDependenciesAction, CleanUpAction
from .actions import RubyBundlerInstallAction, RubyBundlerVendorAction
from .utils import OSUtils
Expand All @@ -25,6 +25,9 @@ class RubyBundlerWorkflow(BaseWorkflow):

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

BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.NOT_SUPPORTED

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

super(RubyBundlerWorkflow, self).__init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import os

from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.workflow import BaseWorkflow, BuildInSourceSupport, Capability
from aws_lambda_builders.actions import BaseAction, Purpose


Expand Down Expand Up @@ -34,6 +34,8 @@ class WriteHelloWorkflow(BaseWorkflow):

NAME = "WriteHelloWorkflow"
CAPABILITY = Capability(language="python", dependency_manager="test", application_framework="test")
BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED

def __init__(self, source_dir, artifacts_dir, *args, **kwargs):
super(WriteHelloWorkflow, self).__init__(source_dir, artifacts_dir, *args, **kwargs)
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from parameterized import parameterized, param

from aws_lambda_builders.builder import LambdaBuilder
from aws_lambda_builders.workflow import Capability, BaseWorkflow
from aws_lambda_builders.workflow import BuildInSourceSupport, Capability, BaseWorkflow
from aws_lambda_builders.registry import DEFAULT_REGISTRY


Expand Down Expand Up @@ -71,6 +71,8 @@ class MyWorkflow(BaseWorkflow):
CAPABILITY = Capability(
language=self.lang, dependency_manager=self.lang_framework, application_framework=self.app_framework
)
BUILD_IN_SOURCE_BY_DEFAULT = False
BUILD_IN_SOURCE_SUPPORT = BuildInSourceSupport.OPTIONALLY_SUPPORTED

def __init__(
self,
Expand Down
Loading

0 comments on commit 75927e3

Please sign in to comment.