コード例 #1
0
def sync_project(p, check_exists=False):
    if not can_run(): return

    if check_exists and ctx.js().locals.hasServer(p['id']):
        return

    print('Syncing project %s' % p['id'])

    config = p.get('config', {})
    # collect libraries for current project
    libs = copy(ternjs.DEFAULT_LIBS)
    for l in config.get('libs', []):
        if l not in libs:
            libs.append(l)

    # resolve all libraries
    resolved_libs = []
    project_dir = os.path.dirname(p['id'])
    for l in libs:
        if l in ctx.default_libs:
            resolved_libs.append(ctx.default_libs[l])
        else:
            # it's not a predefined library, try lo read it from disk
            lib_path = l
            if not os.path.isabs(lib_path):
                lib_path = os.path.normpath(os.path.join(
                    project_dir, lib_path))

            if os.path.isfile(lib_path):
                resolved_libs.append(_js_file_reader(lib_path))

    # pass data as JSON string to ensure that all
    # data types are valid
    ctx.js().locals.startServer(json.dumps(p, ensure_ascii=False),
                                resolved_libs)
コード例 #2
0
ファイル: tern.py プロジェクト: benmccormick/sublime-tern
def sync_project(p, check_exists=False):
	if not can_run(): return

	if check_exists and ctx.js().locals.hasServer(p['id']):
		return

	print('Syncing project %s' % p['id'])

	config = p.get('config', {})
	# collect libraries for current project
	libs = copy(ternjs.DEFAULT_LIBS);
	for l in config.get('libs', []):
		if l not in libs:
			libs.append(l)

	# resolve all libraries
	resolved_libs = []
	project_dir = os.path.dirname(p['id'])
	for l in libs:
		if l in ctx.default_libs:
			resolved_libs.append(ctx.default_libs[l])
		else:
			# it's not a predefined library, try lo read it from disk
			lib_path = l
			if not os.path.isabs(lib_path):
				lib_path = os.path.normpath(os.path.join(project_dir, lib_path))

			if os.path.isfile(lib_path):
				resolved_libs.append(_js_file_reader(lib_path))

	# pass data as JSON string to ensure that all
	# data types are valid
	ctx.js().locals.startServer(json.dumps(p, ensure_ascii=False), resolved_libs)
コード例 #3
0
def ternjs_file_reader(f, proj=None):
    # print('request file %s' % f)
    if f[0] == '{' and f[-1] == '}':
        # it's unsaved file, locate it
        buf_id = f[1:-1]
        view = view_for_buffer_id(buf_id)
        if view:
            return view.substr(sublime.Region(0, view.size()))
        else:
            return ''

    file_path = f
    if not os.path.isabs(file_path) and proj and proj['dir']:
        file_path = os.path.join(proj['dir'], file_path)

    if not os.path.exists(file_path) and proj and proj['config']:
        # are we using NodeJS plugin? If so, try to resolve it
        # with different extensions
        found = False
        for ext in ['.js', '.json']:
            if os.path.exists(file_path + ext):
                found = True
                file_path += ext
                break

        if not found:
            # Unable to find file, it might be a RequireJS module.
            # If project contains "path" option, iterate on it
            proj_path = os.path.dirname(proj['id'])
            if file_path[0] == '/':
                file_path = file_path[1:]

            lookup_paths = [proj_path]

            config = proj['config']
            if hasattr(config, 'paths'):
                for p in config['paths']:
                    if not os.path.isabs(p):
                        p = os.path.join(proj_path, p)
                    lookup_paths.append(p)

            for p in lookup_paths:
                target_path = os.path.join(p, file_path)
                if os.path.exists(target_path):
                    file_path = target_path
                    break

    try:
        return _js_file_reader(file_path, True)
    except Exception as e:
        print(e)
        return None
コード例 #4
0
ファイル: tern.py プロジェクト: emmetio/sublime-tern
def ternjs_file_reader(f, proj=None):
    # print('request file %s' % f)
    if f[0] == "{" and f[-1] == "}":
        # it's unsaved file, locate it
        buf_id = f[1:-1]
        view = view_for_buffer_id(buf_id)
        if view:
            return view.substr(sublime.Region(0, view.size()))
        else:
            return ""

    file_path = f
    if not os.path.isabs(file_path) and proj and proj["dir"]:
        file_path = os.path.join(proj["dir"], file_path)

    if not os.path.exists(file_path) and proj and proj["config"]:
        # are we using NodeJS plugin? If so, try to resolve it
        # with different extensions
        found = False
        for ext in [".js", ".json"]:
            if os.path.exists(file_path + ext):
                found = True
                file_path += ext
                break

        if not found:
            # Unable to find file, it might be a RequireJS module.
            # If project contains "path" option, iterate on it
            proj_path = os.path.dirname(proj["id"])
            if file_path[0] == "/":
                file_path = file_path[1:]

            lookup_paths = [proj_path]

            config = proj["config"]
            if hasattr(config, "paths"):
                for p in config["paths"]:
                    if not os.path.isabs(p):
                        p = os.path.join(proj_path, p)
                    lookup_paths.append(p)

            for p in lookup_paths:
                target_path = os.path.join(p, file_path)
                if os.path.exists(target_path):
                    file_path = target_path
                    break

    try:
        return _js_file_reader(file_path, True)
    except Exception as e:
        print(e)
        return None
コード例 #5
0
ファイル: tern.py プロジェクト: benmccormick/sublime-tern
def js_file_reader(file_path, use_unicode=True):
	if hasattr(sublime, 'load_resource'):
		rel_path = None
		for prefix in [sublime.packages_path(), sublime.installed_packages_path()]:
			if file_path.startswith(prefix):
				rel_path = os.path.join('Packages', file_path[len(prefix) + 1:])
				break

		if rel_path:
			rel_path = rel_path.replace('.sublime-package', '')
			# for Windows we have to replace slashes
			# print('Loading %s' % rel_path)
			rel_path = rel_path.replace('\\', '/')
			return sublime.load_resource(rel_path)

	return _js_file_reader(file_path, use_unicode)
コード例 #6
0
ファイル: tern.py プロジェクト: yougucaofan/sublime-tern
def ternjs_file_reader(f, proj=None):
	# print('request file %s' % f)
	if f[0] == '{' and f[-1] == '}':
		# it's unsaved file, locate it 
		buf_id = f[1:-1]
		view = view_for_buffer_id(buf_id)
		if view:
			return view.substr(sublime.Region(0, view.size()))
		else:
			return ''

	file_path = f
	if not os.path.isabs(file_path) and proj and proj['dir']:
		file_path = os.path.join(proj['dir'], file_path)

	if not os.path.exists(file_path) and proj and proj['config']:
		# Unable to find file, it might be a RequireJS module.
		# If project contains "path" option, iterate on it
		proj_path = os.path.dirname(proj['id'])
		if file_path[0] == '/':
			file_path = file_path[1:]


		lookup_paths = [proj_path]

		config = proj['config']
		if hasattr(config, 'paths'):
			for p in config['paths']:
				if not os.path.isabs(p):
					p = os.path.join(proj_path, p)
				lookup_paths.append(p)


		for p in lookup_paths:
			target_path = os.path.join(p, file_path)
			if os.path.exists(target_path):
				file_path = target_path
				break

	try:
		return _js_file_reader(file_path, True)
	except Exception as e:
		print(e)
		return None
コード例 #7
0
def js_file_reader(file_path, use_unicode=True):
    if hasattr(sublime, 'load_resource'):
        rel_path = None
        for prefix in [
                sublime.packages_path(),
                sublime.installed_packages_path()
        ]:
            if file_path.startswith(prefix):
                rel_path = os.path.join('Packages',
                                        file_path[len(prefix) + 1:])
                break

        if rel_path:
            rel_path = rel_path.replace('.sublime-package', '')
            # for Windows we have to replace slashes
            # print('Loading %s' % rel_path)
            rel_path = rel_path.replace('\\', '/')
            return sublime.load_resource(rel_path)

    return _js_file_reader(file_path, use_unicode)