forked from bumble-tech/appyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose_mpp_sample.py
66 lines (55 loc) · 2.27 KB
/
compose_mpp_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import subprocess
import shutil
import os
import sys
from urllib.parse import urljoin, urlparse
def compile_project(compile_task):
"""Compile a project using Gradle"""
process = subprocess.Popen(args=['./gradlew', compile_task],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
sys.stderr.write(stderr.decode('utf-8'))
sys.stderr.flush()
sys.exit(process.returncode)
def copy_files(source_directory, target_directory):
"""Copy files from one directory to another, preserving metadata"""
shutil.copytree(source_directory, target_directory, copy_function=shutil.copy2)
def generate_html(width, height, target_directory, html_file_name, classname):
"""Generate HTML code"""
return "<div class=\"{classname}\">" \
"<iframe " \
"width={width} " \
"height={height} " \
"frameBorder=0 " \
"scrolling=\"no\" " \
"src=\"{target_directory}/{html_file_name}\">" \
"</iframe>"\
"</div>".format(
width=width,
height=height,
target_directory=target_directory,
html_file_name=html_file_name,
classname=classname,
)
def define_env(env):
"""Compile, copy and generate HTML tag needed for embedding Compose Multiplatform content"""
@env.macro
def compose_mpp_sample(
project_output_directory,
compile_task,
width,
height,
target_directory,
html_file_name,
classname,
):
if not os.path.exists(project_output_directory):
compile_project(compile_task)
site_target_directory = os.path.join(env.variables.config.site_dir, target_directory)
if not os.path.exists(site_target_directory):
copy_files(project_output_directory, site_target_directory)
base_url = urlparse(env.variables.config.site_url).path
html_target_directory = urljoin(base_url, os.path.relpath(site_target_directory, env.variables.config.site_dir))
return generate_html(width, height, html_target_directory, html_file_name, classname)