Skip to content

tools/file_packager.py: Cleanup code and and its generated output. NFC #16034

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

Merged
merged 1 commit into from
Jan 18, 2022

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jan 15, 2022

  • Use enumerate() to avoid a counter
  • (mostly) Fix indentation of generated JS

@sbc100 sbc100 force-pushed the cleanup_file_packager branch from ab86c80 to d539534 Compare January 15, 2022 00:12
@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 15, 2022

View with "hide whitespace" since this diff has a bunch of white-space only changes to fix indentation.

@sbc100 sbc100 force-pushed the cleanup_file_packager branch from d539534 to 6c2ae7e Compare January 15, 2022 00:20
@sbc100 sbc100 requested a review from kripken January 15, 2022 00:39
@sbc100 sbc100 force-pushed the cleanup_file_packager branch from 6c2ae7e to 9017163 Compare January 17, 2022 23:06
@sbc100 sbc100 changed the title Cleanup tools/file_packager.py and its output. NFC tools/file_packager.py: Cleanup code and and its generated output. NFC Jan 17, 2022
@@ -282,8 +282,7 @@ def main():
# standalone calls
if not from_emcc:
ret = '''
var Module = typeof %(EXPORT_NAME)s !== 'undefined' ? %(EXPORT_NAME)s : {};
''' % {"EXPORT_NAME": export_name}
var Module = typeof %(EXPORT_NAME)s !== 'undefined' ? %(EXPORT_NAME)s : {};\n''' % {"EXPORT_NAME": export_name}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this line change. Before the change we have real newlines, while after this we still have one real newline (after ''') and one escaped one at the end of the string. What is the benefit to the refactor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with the old code is the three extra chars of whitespace at the end of the string. i.e. The new string ends in a newline.. the old string end in three space. The three spaces IIUC were there to avoid the """ being in column zero or the python code.

This change removes all of this pattern of "newline + some extra white space" in favor just a newline, and at the same time this reduces the overall number of lines in the file.

If you look at the resulting output you will see it is more readable with less random trailing whitespace.

An alternative to this approach would be make ret inton and array and use += in it and then use '\n'.join(ret) at the end of the file. This would avoid all the terminating newlines. Maybe I should give that a go?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three spaces IIUC were there to avoid the """ being in column zero or the python code.

Can we just remove the three spaces then? Python accepts that IIANM.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the problem is that we end up with lots of stuff in column 1 of the python code.. which IMHO makes it harder to read in general.. and add a lot of extra lines to the lines don't do anything.

I've updated the this PR to be even more consistent and added quick test for trailing/extra whitespace in the output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm, I'm mostly neutral here, so I don't want to block this. Good idea about the test.

@sbc100 sbc100 force-pushed the cleanup_file_packager branch from 9017163 to f4a5408 Compare January 18, 2022 18:20
- Use enumerate() to avoid a counter
- (mostly) Fix indentation of generated JS
@sbc100 sbc100 force-pushed the cleanup_file_packager branch from f4a5408 to 2923d7e Compare January 18, 2022 19:05
@@ -282,8 +282,7 @@ def main():
# standalone calls
if not from_emcc:
ret = '''
var Module = typeof %(EXPORT_NAME)s !== 'undefined' ? %(EXPORT_NAME)s : {};
''' % {"EXPORT_NAME": export_name}
var Module = typeof %(EXPORT_NAME)s !== 'undefined' ? %(EXPORT_NAME)s : {};\n''' % {"EXPORT_NAME": export_name}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm, I'm mostly neutral here, so I don't want to block this. Good idea about the test.

@sbc100
Copy link
Collaborator Author

sbc100 commented Jan 18, 2022

This is mostly just cleanup to make things more readable for me.. in preparation for actual file_packager changes to follow.

@sbc100 sbc100 enabled auto-merge (squash) January 18, 2022 20:26
@sbc100 sbc100 merged commit cbfb0af into main Jan 18, 2022
@sbc100 sbc100 deleted the cleanup_file_packager branch January 18, 2022 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants