Skip to content

Commit

Permalink
Merge pull request cocos2d#183 from ricardoquesada/develop
Browse files Browse the repository at this point in the history
cocos2d.ini supports paths
  • Loading branch information
ricardoquesada committed Nov 19, 2014
2 parents 40e5835 + 01f81d5 commit f1a8f35
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 42 deletions.
133 changes: 91 additions & 42 deletions bin/cocos.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,65 @@
import cocos_project
import shutil
import string
import ConfigParser

COCOS2D_CONSOLE_VERSION = '1.3'


class Cocos2dIniParser:
def __init__(self):
self._cp = ConfigParser.ConfigParser(allow_no_value=True)
self._cp.optionxform = str

# read global config file
cocos2d_path = os.path.dirname(os.path.abspath(sys.argv[0]))
self._cp.read(os.path.join(cocos2d_path, "cocos2d.ini"))

# XXXX: override with local config ??? why ???
self._cp.read("~/.cocos2d-js/cocos2d.ini")

def parse_plugins(self):
classes = {}

for s in self._cp.sections():
if s == 'plugins':
for classname in self._cp.options(s):
plugin_class = get_class(classname)
category = plugin_class.plugin_category()
name = plugin_class.plugin_name()
if name is None:
print(
"Warning: plugin '%s' does not return a plugin name" % classname)
if len(category) == 0:
key = name
else:
# combine category & name as key
# eg. 'project_new'
key = category + '_' + name
classes[key] = plugin_class
_check_dependencies(classes)
return classes

def _sanitize_path(self, path):
if len(path) == 0:
return None
path = os.path.abspath(path)
if not os.path.isdir(path):
Logging.warning("Warning: Invalid directory defined in cocos2d.ini: %s" % path)
return None
return path

def parse_paths(self):
templates = self._cp.get('paths', 'templates')
cocos2d_x = self._cp.get('paths', 'cocos2d_x')

templates = self._sanitize_path(templates)
cocos2d_x = self._sanitize_path(cocos2d_x)

print templates, cocos2d_x
return [templates, cocos2d_x]


class Logging:
# TODO maybe the right way to do this is to use something like colorama?
RED = '\033[31m'
Expand Down Expand Up @@ -150,14 +205,30 @@ def _output_for(self, command):
def get_cocos2d_path(cls):
"""returns the path where cocos2d-x is installed"""

# variable setup by "cocos" and by cocos2d-x's setup.py
#
# 1: Check for config.ini
#
parser = Cocos2dIniParser()
templates_path, cocos2dx_path = parser.parse_paths()

if cocos2dx_path is not None:
return cocos2dx_path

#
# 2: variable setup by "cocos" and by cocos2d-x's setup.py
#
if "COCOS_X_ROOT" in os.environ:
return os.environ['COCOS_X_ROOT']

# old cocos2d-x variable
#
# 3: old cocos2d-x variable
#
if "COCOS2DX_ROOT" in os.environ:
return os.environ['COCOS2DX_ROOT']

#
# 4: old cocos2d-x variable
#
# possible path of console
# /Users/myself/cocos2d-x/tools/cocos2d-console/bin
# if so, we have to remove the last 3 segments
Expand Down Expand Up @@ -188,10 +259,21 @@ def get_console_path(cls):
def get_templates_paths(cls):
"""returns a set of paths where templates are installed"""

parser = Cocos2dIniParser()
templates_path, cocos2dx_path = parser.parse_paths()

paths = set()

#
# 1: Path defined in environemt variable
# 1: Check for config.ini
#
if templates_path is not None:
paths.add(templates_path)
else:
Logging.warning('Warning: config.ini has an invalid template path')

#
# 2: Path defined in environemt variable
#
if "COCOS_TEMPLATES_ROOT" in os.environ:
templates_path = os.path.abspath(os.environ['COCOS_TEMPLATES_ROOT'])
Expand All @@ -201,7 +283,7 @@ def get_templates_paths(cls):
Logging.warning('Warning: COCOS_TEMPLATE_ROOT points to an invalid directory')

#
# 2: Path defined by walking the cocos2d path
# 3: Path defined by walking the cocos2d path
#
path = cls.get_cocos2d_path()

Expand All @@ -215,7 +297,7 @@ def get_templates_paths(cls):
paths.add(os.path.abspath(template_path))

#
# 3: Templates can be in ~/.cocos2d/templates as well
# 4: Templates can be in ~/.cocos2d/templates as well
#
user_path = os.path.expanduser("~/.cocos/templates")
if os.path.isdir(user_path):
Expand Down Expand Up @@ -541,46 +623,12 @@ def pushd(newDir):
os.chdir(previousDir)


def parse_plugins():
import ConfigParser
classes = {}
cp = ConfigParser.ConfigParser(allow_no_value=True)
cp.optionxform = str

# read global config file
cocos2d_path = os.path.dirname(os.path.abspath(sys.argv[0]))
cp.read(os.path.join(cocos2d_path, "cocos2d.ini"))

# override it with local config
cp.read("~/.cocos2d-js/cocos2d.ini")

for s in cp.sections():
if s == 'plugins':
for classname in cp.options(s):
plugin_class = get_class(classname)
category = plugin_class.plugin_category()
name = plugin_class.plugin_name()
if name is None:
print(
"Warning: plugin '%s' does not return a plugin name" % classname)
if len(category) == 0:
key = name
else:
# combine category & name as key
# eg. 'project_new'
key = category + '_' + name
classes[key] = plugin_class

_check_dependencies(classes)

return classes


def help():
print("\n%s %s - cocos console: A command line tool for cocos2d-x" %
(sys.argv[0], COCOS2D_CONSOLE_VERSION))
print("\nAvailable commands:")
classes = parse_plugins()
parse = Cocos2dIniParser()
classes = parse.parse_plugins()
max_name = max(len(classes[key].plugin_name(
) + classes[key].plugin_category()) for key in classes.keys())
max_name += 4
Expand Down Expand Up @@ -651,7 +699,8 @@ def _check_python_version():
exit(0)

try:
plugins = parse_plugins()
parse = Cocos2dIniParser()
plugins = parse.parse_plugins()
command = sys.argv[1]
argv = sys.argv[2:]
# try to find plugin by name
Expand Down
15 changes: 15 additions & 0 deletions bin/cocos2d.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ plugin_luacompile.CCPluginLuaCompile
#plugin_dist.CCPluginDist
#plugin_test.CCPluginTest
# To add a new plugin add it's classname here


[paths]
# This section must be empty
# Only "installers" must edit it.

# where cocos2d-x is installed
# example: /usr/local/cocos2d-x
# eg: this file must exist: /usr/local/cocos2d-x/cocos/cocos2d.h
cocos2d_x=

# where are the cocos2d-x's templates installed
# example: /home/user/templates
# eg: this directory must exist: /home/user/templates/cpp-template-default
templates=

0 comments on commit f1a8f35

Please sign in to comment.