This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
generated from communitiesuk/funding-service-design-TEMPLATE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from communitiesuk/fmd-218-paketo-build
FMD-218: paketo build
- Loading branch information
Showing
17 changed files
with
191 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn wsgi:app -c run/gunicorn/run.py --workers=3 --timeout 180 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import glob | ||
import os | ||
import shutil | ||
import urllib.request | ||
import zipfile | ||
|
||
import static_assets | ||
|
||
|
||
def build_govuk_assets(static_dist_root="app/static/dist"): | ||
GOVUK_FRONTEND_VERSION = "4.6.0" | ||
DIST_ROOT = "./" + static_dist_root | ||
GOVUK_DIR = "/govuk-frontend" | ||
GOVUK_URL = ( | ||
"https://github.com/alphagov/govuk-frontend/releases/download/" | ||
f"v{GOVUK_FRONTEND_VERSION}/release-v{GOVUK_FRONTEND_VERSION}.zip" | ||
) | ||
ZIP_FILE = "./govuk_frontend.zip" | ||
DIST_PATH = DIST_ROOT + GOVUK_DIR | ||
ASSETS_DIR = "/assets" | ||
ASSETS_PATH = DIST_PATH + ASSETS_DIR | ||
|
||
# Checks if GovUK Frontend Assets already built | ||
print(DIST_PATH) | ||
if os.path.exists(DIST_PATH): | ||
print( | ||
"GovUK Frontend assets already built. If you require a rebuild manually run build.build_govuk_assets" | ||
) | ||
return True | ||
|
||
# Download zips from GOVUK_URL | ||
# There is a known problem on Mac where one must manually | ||
# run the script "Install Certificates.command" found | ||
# in the python application folder for this to work. | ||
|
||
print("Downloading static file zip.") | ||
urllib.request.urlretrieve(GOVUK_URL, ZIP_FILE) # nosec | ||
|
||
# Attempts to delete the old files, states if | ||
# one doesn't exist. | ||
|
||
print("Deleting old " + DIST_PATH) | ||
try: | ||
shutil.rmtree(DIST_PATH) | ||
except FileNotFoundError: | ||
print("No old " + DIST_PATH + " to remove.") | ||
|
||
# Extract the previously downloaded zip to DIST_PATH | ||
|
||
print("Unzipping file to " + DIST_PATH + "...") | ||
with zipfile.ZipFile(ZIP_FILE, "r") as zip_ref: | ||
zip_ref.extractall(DIST_PATH) | ||
|
||
# Move files from ASSETS_PATH to DIST_PATH | ||
|
||
print("Moving files from " + ASSETS_PATH + " to " + DIST_PATH) | ||
for file_to_move in os.listdir(ASSETS_PATH): | ||
shutil.move("/".join([ASSETS_PATH, file_to_move]), DIST_PATH) | ||
|
||
# Update relative paths | ||
|
||
print("Updating relative paths in css files to " + GOVUK_DIR) | ||
cwd = os.getcwd() | ||
os.chdir(DIST_PATH) | ||
for css_file in glob.glob("*.css"): | ||
# Read in the file | ||
with open(css_file, "r") as file: | ||
filedata = file.read() | ||
|
||
# Replace the target string | ||
filedata = filedata.replace(ASSETS_DIR, "/static/" + GOVUK_DIR) | ||
|
||
# Write the file out again | ||
with open(css_file, "w") as file: | ||
file.write(filedata) | ||
os.chdir(cwd) | ||
|
||
# Delete temp files | ||
print("Deleting " + ASSETS_PATH) | ||
shutil.rmtree(ASSETS_PATH) | ||
os.remove(ZIP_FILE) | ||
|
||
|
||
if __name__ == "__main__": | ||
build_govuk_assets() | ||
static_assets.build_bundles(static_folder="app/static/dist") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ class DevelopmentConfig(DefaultConfig): | |
"roles": [], | ||
"highest_role_map": {}, | ||
} | ||
AUTO_BUILD_ASSETS = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ _ ] | ||
schema-version = "0.2" | ||
|
||
[[ io.buildpacks.build.env ]] | ||
name = "BP_CPYTHON_VERSION" | ||
value = "3.11.*" # any valid semver constraints (e.g. 3.6.7, 3.*) are acceptable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Compile static assets.""" | ||
|
||
from os import path | ||
|
||
from flask import Flask | ||
from flask_assets import Bundle, Environment | ||
|
||
|
||
def init_assets(app=None, auto_build=False, static_folder="app/static/dist"): | ||
app = app or Flask(__name__) | ||
app.static_folder = static_folder | ||
with app.app_context(): | ||
env = Environment(app) | ||
env.load_path = [path.join(path.dirname(__file__), "app/static/src")] | ||
# Paketo doesn't support automatic rebuilding. | ||
env.auto_build = auto_build | ||
# This file needs to be shipped with your code. | ||
env.manifest = "file" | ||
|
||
js = Bundle("./js/*.js", filters="jsmin", output="js/custom-%(version)s.min.js") | ||
|
||
css = Bundle( | ||
Bundle( | ||
"./css/*.css", | ||
filters="cssmin", | ||
output="css/custom-%(version)s.min.css", | ||
) | ||
) | ||
|
||
env.register("js", js) | ||
env.register("css", css) | ||
|
||
bundles = [css, js] | ||
return bundles | ||
|
||
|
||
def build_bundles(static_folder="static/dist"): | ||
bundles = init_assets(static_folder=static_folder) | ||
for bundle in bundles: | ||
bundle.build() | ||
|
||
|
||
if __name__ == "__main__": | ||
build_bundles() |