Exemple #1
0
def output_file(file_path, fp_data, write_type='w'):
    if not os.path.exists(os.path.dirname(file_path)):
        os.makedirs(os.path.dirname(file_path))

    log.info('写入文件:%s' % file_path)
    with open(file_path, write_type) as fp:
        fp.write(fp_data)
Exemple #2
0
def build_html(data, offset_path='.', argv=None):
    """
    生成线上html
    :return:
    """
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            build_html(data, '/'.join([offset_path, fp]), argv)
    else:

        # 判断是不是.html文件
        if not now_path.endswith('.html'):
            return

        # 编译html文件
        print("编译html文件:%s" % now_path)
        log.info("编译html文件:%s" % now_path)

        with open(now_path, 'r') as fp:
            output_path = data['target_path'] + '/' + offset_path
            html = build_html_use_template(fp.read(), output_path,
                                           argv['project_path'], now_path,
                                           data)
            tools.output_file(output_path, html)
Exemple #3
0
def output_file(file_path, fp_data, write_type='w'):
    if not os.path.exists(os.path.dirname(file_path)):
        os.makedirs(os.path.dirname(file_path))

    log.info('写入文件:%s' % file_path)
    with open(file_path, write_type) as fp:
        fp.write(fp_data)
Exemple #4
0
def make_component(data):
    """
    制作组件
    :param data: 规则字典
    :return:
    """
    js, code = get_component_js_str(data['source_path'])

    if code == 0:
        tools.output_file(data['target_path'], js)
    elif code == -1:
        log.info('由于没有发现组件配置文件,所以忽略本次制作')
Exemple #5
0
def make_component(data):
    """
    制作组件
    :param data: 规则字典
    :return:
    """
    js, code = get_component_js_str(data['source_path'])

    if code == 0:
        tools.output_file(data['target_path'], js)
    elif code == -1:
        log.info('由于没有发现组件配置文件,所以忽略本次制作')
Exemple #6
0
def start_monitor(argv):
    """
    启动文件监视器
    :return:
    """
    log.info('启动前端模块化小工具')
    monitor = Observer()
    event_handler = MyHandle(argv)
    monitor.schedule(event_handler, path=argv['watch_path'], recursive=True)
    monitor.start()
    input("按回车自动退出\n")
    log.info('退出前端模块化小工具')
    exit(0)
Exemple #7
0
    def handle_all(self, event):

        # 忽略 .开头的隐藏文件
        if os.path.basename(event.src_path)[0:1] == '.':
            return
        # 忽略文件夹
        if not event.is_directory:
            log.info('监控到文件变化:%s' % event.src_path)
            argv = copy.deepcopy(self.argv)
            argv['change_file_path'] = event.src_path

            # 只处理已知的几种文件
            know_files = [
                'html', 'vue', 'txt', 'js', 'css',
                'jpg', 'png', 'less', 'scss', 'sass',
                'htm'
            ]
            if tools.get_file_type(event.src_path) not in know_files:
                return

            try:
                init(argv)
            except:
                traceback.print_exc()
Exemple #8
0
def build_html(data, offset_path='.', argv=None):
    """
    生成线上html
    :return:
    """
    now_path = data['source_path'] + '/' + offset_path
    if os.path.isdir(now_path):
        fps = os.listdir(now_path)
        for fp in fps:
            build_html(data, '/'.join([offset_path, fp]), argv)
    else:

        # 判断是不是.html文件
        if not now_path.endswith('.html'):
            return

        # 编译html文件
        print("编译html文件:%s" % now_path)
        log.info("编译html文件:%s" % now_path)

        with open(now_path, 'r') as fp:
            output_path = data['target_path'] + '/' + offset_path
            html = build_html_use_template(fp.read(), output_path, argv['project_path'], now_path, data)
            tools.output_file(output_path, html)
Exemple #9
0
def handle_rule(data, argv):
    """
    规则处理器
    :param data: 规则字典
    :param argv: 动态参数
    :return:
    """

    data = handle_rule_data(data, argv)

    # 判断规则是不是需要执行,(编译html除外)
    changle_file_path = argv['change_file_path']
    if data['rule'] != 'render_html' and not changle_file_path.startswith(os.path.realpath(data['source_path'])):
        return

    if data['rule'] == 'make_component':
        log.info('触发制作组件,文件源:%s' % changle_file_path)
        dao_lib.make_component(data)
        print('制作组件完成 ' + time.strftime('%H:%M:%S'))
    elif data['rule'] == 'merge':
        log.info('触发合并文件,文件源:%s' % changle_file_path)
        dao_lib.merge_file(data)
        print('合并文件完成 ' + time.strftime('%H:%M:%S'))
    elif data['rule'] == 'minify':
        log.info('触发压缩文件,文件源:%s' % changle_file_path)
        dao_lib.minify_file_in_script(data, argv=argv)
        print('压缩文件完成 ' + time.strftime('%H:%M:%S'))
    elif data['rule'] == 'css_precompiled':
        log.info('触发预编译css文件,文件源:%s' % changle_file_path)
        dao_lib.css_precompiled(data, argv=argv)
        print('预编译css文件完成 ' + time.strftime('%H:%M:%S'))
    elif data['rule'] == 'css_precompiled_and_merge':
        log.info('触发预编译css文件,文件源:%s' % changle_file_path)
        # 获取合并后的css
        if data['compiled_first']:
            # 先编译再合并
            d = dao_lib.css_precompiled_get_str(data)
        else:
            d = dao_lib.get_after_merge_file_str(data['source_path'], file_type='[scss|less]$')
            if data['compiled_type'] == 'scss':
                d = dao_lib.css_precompiled_lib.handle_scss(d)
            elif data['compiled_type'] == 'less':
                d = dao_lib.css_precompiled_lib.handle_less(d)

        tools.output_file(data['target_path'], d)
        print('预编译css文件(并且合并成一个css)完成 ' + time.strftime('%H:%M:%S'))
    elif data['rule'] == 'render_html':
        log.info('触发预编译html文件,文件源:%s' % changle_file_path)
        dao_lib.handle_render_html(data, argv=argv)
        print('预编译html文件完成 ' + time.strftime('%H:%M:%S'))