Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 176 lines (136 sloc) 4.72 KB
#!/usr/bin/env python3
"""
Copyright 2016-2018 Alex Margarit
This file is part of a2x-framework.
a2x-framework is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
a2x-framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with a2x-framework. If not, see <http://www.gnu.org/licenses/>.
"""
import os
from utils.output import Output
from utils.tool import Tool
class NewTool(Tool):
def main(self):
project_name = self.get_arg('ProjectName').replace(' ', '-')
if project_name != os.path.basename(project_name):
self.output.error('Project name cannot be a path'.format(project_name))
project_path = os.path.abspath(project_name)
if os.path.exists(project_path):
self.output.error('{} already exists'.format(project_path))
class Dir:
def __init__(self, Tool, Path):
self.tool = Tool
self.path = Path
self.name = os.path.basename(Path)
os.mkdir(Path)
def add_subdir(self, Name):
return Dir(self.tool, os.path.join(self.path, Name))
def write_file(self, Name, Content):
self.tool.writefile(os.path.join(self.path, Name), Content)
dir_root = Dir(self, project_path)
dir_assets = dir_root.add_subdir('assets')
dir_bin = dir_root.add_subdir('bin')
dir_make = dir_root.add_subdir('make')
dir_obj = dir_root.add_subdir('obj')
dir_src = dir_root.add_subdir('src')
os.symlink(os.path.join('..', dir_assets.name),
os.path.join(dir_bin.path, dir_assets.name))
author = '<your username>'
for key in ['USER', 'USERNAME', 'LOGNAME']:
if key in os.environ:
author = os.environ[key]
break
dir_make.write_file('Makefile',
"""\
A_CONFIG_APP_NAME := {0}
include $(A2X_PATH)/make/project/Makefile
""".format(project_name))
dir_src.write_file('main.c',
"""\
#include <a2x.h>
A_SETUP
{{
a_settings_stringSet(A_SETTING_APP_TITLE, "{0}");
a_settings_stringSet(A_SETTING_APP_VERSION, "0.1.0");
a_settings_stringSet(A_SETTING_APP_AUTHOR, "{1}");
a_settings_boolSet(A_SETTING_OUTPUT_ON, true);
}}
A_STATE(drawBox)
{{
static struct {{
int x, y;
AButton* up;
AButton* down;
AButton* left;
AButton* right;
}} context;
A_STATE_INIT
{{
context.x = a_screen_widthGet() / 2;
context.y = a_screen_heightGet() / 2;
context.up = a_button_new();
a_button_bind(context.up, A_KEY_UP);
a_button_bind(context.up, A_BUTTON_UP);
context.down = a_button_new();
a_button_bind(context.down, A_KEY_DOWN);
a_button_bind(context.down, A_BUTTON_DOWN);
context.left = a_button_new();
a_button_bind(context.left, A_KEY_LEFT);
a_button_bind(context.left, A_BUTTON_LEFT);
context.right = a_button_new();
a_button_bind(context.right, A_KEY_RIGHT);
a_button_bind(context.right, A_BUTTON_RIGHT);
}}
A_STATE_TICK
{{
if(a_button_pressGet(context.up)) {{
context.y--;
}} else if(a_button_pressGet(context.down)) {{
context.y++;
}}
if(a_button_pressGet(context.left)) {{
context.x--;
}} else if(a_button_pressGet(context.right)) {{
context.x++;
}}
}}
A_STATE_DRAW
{{
a_pixel_colorSetHex(0xaaff88);
a_draw_fill();
a_pixel_colorSetHex(0xffaa44);
a_draw_rectangle(context.x - 40, context.y - 40, 80, 80);
}}
A_STATE_FREE
{{
a_button_free(context.up);
a_button_free(context.down);
a_button_free(context.left);
a_button_free(context.right);
}}
}}
A_MAIN
{{
a_state_push(drawBox, "Draw Box");
}}
""".format(project_name, author))
dir_root.write_file('.gitignore',
"""\
/bin/
/obj/
""")
self.shell('cd {} && ' \
'git init && ' \
'git add .gitignore make/Makefile src/main.c'
.format(dir_root.path))
self.output.note('To build and run:')
self.output.note('cd {} && make run'.format(dir_make.path))
if __name__ == '__main__':
NewTool('ProjectName').run()