-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assets #597 #881
Merged
Merged
Assets #597 #881
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f82752c
update year
5007320
:camel: DRY, we use the same abstraction for renderer and dcc
ae13f90
:wrench: scripts change
99bba81
:hocho: remove the lock in main dash
0947c14
:memo: update the doc
8e2f639
:bug: unicode PEP263
9429598
rename ci to dev
ce2a99e
:truck: pull dcc away
01cb6d8
:bug: fix few bugs, and add extra bundles
b96c3b2
:bug: order
651e783
:ok_hand: rename to js
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
File renamed without changes.
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,179 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
import os | ||
import sys | ||
import json | ||
import string | ||
import shutil | ||
import logging | ||
import coloredlogs | ||
import fire | ||
|
||
from .._utils import run_command_with_process, compute_md5, job | ||
|
||
logger = logging.getLogger(__name__) | ||
coloredlogs.install( | ||
fmt="%(asctime)s,%(msecs)03d %(levelname)s - %(message)s", | ||
datefmt="%H:%M:%S", | ||
) | ||
|
||
|
||
class BuildProcess(object): | ||
def __init__(self, main, deps_info): | ||
self.logger = logger | ||
self.main = main | ||
self.deps_info = deps_info | ||
self.npm_modules = self._concat(self.main, "node_modules") | ||
self.package_lock = self._concat(self.main, "package-lock.json") | ||
self.package = self._concat(self.main, "package.json") | ||
self._parse_package(path=self.package) | ||
self.asset_paths = (self.build_folder, self.npm_modules) | ||
|
||
def _parse_package(self, path): | ||
with open(path, "r") as fp: | ||
package = json.load(fp) | ||
self.version = package["version"] | ||
self.name = package["name"] | ||
self.build_folder = self._concat( | ||
self.main, self.name.replace("-", "_") | ||
) | ||
self.deps = package["dependencies"] | ||
|
||
@staticmethod | ||
def _concat(*paths): | ||
return os.path.realpath( | ||
os.path.sep.join((path for path in paths if path)) | ||
) | ||
|
||
@staticmethod | ||
def _clean_path(path): | ||
if os.path.exists(path): | ||
logger.warning("🚨 %s already exists, remove it!", path) | ||
try: | ||
if os.path.isfile(path): | ||
os.remove(path) | ||
if os.path.isdir(path): | ||
shutil.rmtree(path) | ||
except OSError: | ||
sys.exit(1) | ||
else: | ||
logger.warning("🚨 %s doesn't exist, no action taken", path) | ||
|
||
@job("clean all the previous assets generated by build tool") | ||
def clean(self): | ||
for path in self.asset_paths: | ||
self._clean_path(path) | ||
|
||
@job("run `npm i --ignore-scripts`") | ||
def npm(self): | ||
"""job to install npm packages""" | ||
os.chdir(self.main) | ||
self._clean_path(self.package_lock) | ||
run_command_with_process("npm i --ignore-scripts") | ||
|
||
@job("build the renderer in dev mode") | ||
def watch(self): | ||
os.chdir(self.main) | ||
os.system("npm run build:dev") | ||
|
||
@job("run the whole building process in sequence") | ||
def build(self): | ||
self.clean() | ||
self.npm() | ||
self.bundles() | ||
self.digest() | ||
|
||
@job("compute the hash digest for assets") | ||
def digest(self): | ||
copies = tuple( | ||
_ | ||
for _ in os.listdir(self.build_folder) | ||
if os.path.splitext(_)[-1] in {".js", ".map"} | ||
) | ||
logger.info("bundles in %s %s", self.build_folder, copies) | ||
|
||
payload = {self.name: self.version} | ||
for copy in copies: | ||
payload["MD5 ({})".format(copy)] = compute_md5( | ||
self._concat(self.build_folder, copy) | ||
) | ||
|
||
with open(self._concat(self.main, "digest.json"), "w") as fp: | ||
json.dump( | ||
payload, fp, sort_keys=True, indent=4, separators=(",", ":") | ||
) | ||
logger.info( | ||
"bundle digest in digest.json:\n%s", | ||
json.dumps(payload, sort_keys=True, indent=4), | ||
) | ||
|
||
@job("copy and generate the bundles") | ||
def bundles(self): | ||
if not os.path.exists(self.build_folder): | ||
try: | ||
os.makedirs(self.build_folder) | ||
except OSError: | ||
logger.exception( | ||
"🚨 having issues manipulating %s", self.build_folder | ||
) | ||
sys.exit(1) | ||
|
||
self._parse_package(self.package_lock) | ||
|
||
getattr(self, "_bundles_extra", lambda: None)() | ||
|
||
versions = { | ||
"version": self.version, | ||
"package": self.name.replace(" ", "_").replace("-", "_"), | ||
} | ||
for name, subfolder, filename, target in self.deps_info: | ||
version = self.deps[name]["version"] | ||
versions[name.replace("-", "").replace(".", "")] = version | ||
|
||
logger.info("copy npm dependency => %s", filename) | ||
ext = "min.js" if "min" in filename.split(".") else "js" | ||
target = ( | ||
target.format(version) | ||
if target | ||
else "{}@{}.{}".format(name, version, ext) | ||
) | ||
shutil.copyfile( | ||
self._concat(self.npm_modules, name, subfolder, filename), | ||
self._concat(self.build_folder, target), | ||
) | ||
|
||
logger.info("run `npm run build:js`") | ||
os.chdir(self.main) | ||
run_command_with_process("npm run build:js") | ||
|
||
logger.info("generate the `__init__.py` from template and verisons") | ||
with open(self._concat(self.main, "init.template")) as fp: | ||
t = string.Template(fp.read()) | ||
|
||
with open(self._concat(self.build_folder, "__init__.py"), "w") as fp: | ||
fp.write(t.safe_substitute(versions)) | ||
|
||
|
||
class Renderer(BuildProcess): | ||
def __init__(self): | ||
# dash-renderer's path is binding with the dash folder hierarchy | ||
super(Renderer, self).__init__( | ||
self._concat( | ||
os.path.dirname(__file__), | ||
os.pardir, | ||
os.pardir, | ||
"dash-renderer", | ||
), | ||
( | ||
("react", "umd", "react.production.min.js", None), | ||
("react", "umd", "react.development.js", None), | ||
("react-dom", "umd", "react-dom.production.min.js", None), | ||
("react-dom", "umd", "react-dom.development.js", None), | ||
("prop-types", None, "prop-types.min.js", None), | ||
("prop-types", None, "prop-types.js", None), | ||
), | ||
) | ||
|
||
|
||
def renderer(): | ||
fire.Fire(Renderer) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not fully structured thoughts but I feel tying ourselves even more to Python for code generation is an anti-pattern for Dash.
While this clean up is advantageous in the short term, if we truly want to support multiple environments and not expect, say, an R or xyz developer in the future to have an environment setup for generating the other (n-1) languages / platforms supported, we should try and do our build with the only mandatory environment & language Dash requires: NodeJS
I realize I'm late in this but I don't feel fully comfortable with the direction we are taking generation towards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the concern here, but unless we can get rid of the entire
dash/development
in python code. the script is created for fixing the current problem, I had no intention to hijack the JS driven direction.