Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 1 | # Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame^] | 5 | """ |
| 6 | Helper script to update CodeMirror from upstream. |
| 7 | """ |
| 8 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 9 | import argparse |
| 10 | import glob |
| 11 | import os |
| 12 | import shutil |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 16 | def parse_options(cli_args): |
| 17 | parser = argparse.ArgumentParser(description='Roll CodeMirror') |
| 18 | parser.add_argument('cm_dir', help='CodeMirror directory') |
| 19 | parser.add_argument('devtools_dir', help='DevTools directory') |
| 20 | return parser.parse_args(cli_args) |
| 21 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 22 | def run_npm(options): |
| 23 | print 'Building CodeMirror in %s' % os.path.abspath(options.cm_dir) |
| 24 | subprocess.check_output(['npm', 'install'], cwd=options.cm_dir, stderr=subprocess.PIPE) |
| 25 | subprocess.check_output(['npm', 'run', 'build'], cwd=options.cm_dir, stderr=subprocess.PIPE) |
| 26 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 27 | def copy_lib_files(options): |
| 28 | print 'Copying codemirror.js and codemirror.css' |
| 29 | result = '' |
| 30 | target_dir = os.path.join(options.devtools_dir, 'front_end', 'cm') |
| 31 | |
| 32 | with open(os.path.join(options.cm_dir, 'lib', 'codemirror.js'), 'r') as read: |
| 33 | lines = read.readlines() |
| 34 | with open(os.path.join(target_dir, 'codemirror.js'), 'w') as write: |
| 35 | for line in lines: |
| 36 | if 'CodeMirror.version =' in line: |
| 37 | result = line.strip() |
| 38 | write.write(line) |
| 39 | |
| 40 | with open(os.path.join(options.cm_dir, 'lib', 'codemirror.css'), 'r') as read: |
| 41 | lines = read.readlines() |
| 42 | found_stop = False |
| 43 | with open(os.path.join(target_dir, 'codemirror.css'), 'w') as write: |
| 44 | for line in lines: |
| 45 | if found_stop: |
| 46 | write.write(line) |
| 47 | elif '/* STOP */' in line: |
| 48 | found_stop = True |
| 49 | assert found_stop |
| 50 | return result |
| 51 | |
| 52 | |
| 53 | def copy_headless_file(options): |
| 54 | print 'Copying runmode-standalone.js into headlesscodemirror.js' |
| 55 | source_file = os.path.join(options.cm_dir, 'addon', 'runmode', 'runmode-standalone.js') |
| 56 | target_file = os.path.join(options.devtools_dir, 'front_end', 'cm_headless', 'headlesscodemirror.js') |
| 57 | |
| 58 | with open(source_file, 'r') as read: |
| 59 | lines = read.readlines() |
| 60 | with open(target_file, 'w') as write: |
| 61 | write.write('// Content of the function is equal to runmode-standalone.js file\n') |
| 62 | write.write('// from CodeMirror distribution\n') |
| 63 | write.write('(function(window) {\n') |
| 64 | for line in lines: |
| 65 | write.write(line) |
| 66 | write.write('}(this))\n') |
| 67 | |
| 68 | |
| 69 | def find_and_copy_js_files(source_dir, target_dir, filter_fn): |
| 70 | for f in os.listdir(target_dir): |
| 71 | if not filter_fn(f): |
| 72 | continue |
| 73 | target_file = os.path.join(target_dir, f) |
| 74 | if not os.path.isfile(os.path.join(target_dir, f)): |
| 75 | continue |
| 76 | source = glob.glob(os.path.join(source_dir, '*', f)) |
| 77 | assert len(source) == 1 |
| 78 | source_file = source[0] |
| 79 | print 'Copying %s from %s' % (target_file, source_file) |
| 80 | shutil.copyfile(source_file, target_file) |
| 81 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 82 | def copy_cm_files(options): |
| 83 | source_dir = os.path.join(options.cm_dir, 'addon') |
| 84 | target_dir = os.path.join(options.devtools_dir, 'front_end', 'cm') |
| 85 | |
| 86 | def cm_filter(f): |
| 87 | return f.endswith('.js') and f != 'codemirror.js' |
| 88 | |
| 89 | find_and_copy_js_files(source_dir, target_dir, cm_filter) |
| 90 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 91 | def copy_cm_modes_files(options): |
| 92 | source_dir = os.path.join(options.cm_dir, 'mode') |
| 93 | target_dir = os.path.join(options.devtools_dir, 'front_end', 'cm_modes') |
| 94 | |
| 95 | def cm_modes_filter(f): |
| 96 | return f.endswith('.js') and f != 'DefaultCodeMirrorMimeMode.js' |
| 97 | |
| 98 | find_and_copy_js_files(source_dir, target_dir, cm_modes_filter) |
| 99 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 100 | def copy_cm_web_modes_files(options): |
| 101 | source_dir = os.path.join(options.cm_dir, 'mode') |
| 102 | target_dir = os.path.join(options.devtools_dir, 'front_end', 'cm_web_modes') |
| 103 | |
| 104 | def cm_web_modes_filter(f): |
| 105 | return f.endswith('.js') |
| 106 | |
| 107 | find_and_copy_js_files(source_dir, target_dir, cm_web_modes_filter) |
| 108 | |
Yang Guo | a999fec | 2019-07-25 07:54:38 +0000 | [diff] [blame] | 109 | if __name__ == '__main__': |
| 110 | OPTIONS = parse_options(sys.argv[1:]) |
| 111 | run_npm(OPTIONS) |
| 112 | copy_cm_files(OPTIONS) |
| 113 | copy_cm_modes_files(OPTIONS) |
| 114 | copy_cm_web_modes_files(OPTIONS) |
| 115 | copy_headless_file(OPTIONS) |
| 116 | VERSION = copy_lib_files(OPTIONS) |
| 117 | print VERSION |