|
| 1 | +import os, sh |
| 2 | +import uuid |
| 3 | + |
| 4 | +from PblCommand import PblCommand |
| 5 | + |
| 6 | +class PblProjectCreator(PblCommand): |
| 7 | + name = 'new-project' |
| 8 | + help = 'Create a new Pebble project' |
| 9 | + |
| 10 | + def configure_subparser(self, parser): |
| 11 | + parser.add_argument("name", help = "Name of the project you want to create") |
| 12 | + |
| 13 | + def run(self, args): |
| 14 | + print "Creating new project {}".format(args.name) |
| 15 | + |
| 16 | + # User can give a path to a new project dir |
| 17 | + project_path = args.name |
| 18 | + project_name = os.path.split(project_path)[1] |
| 19 | + project_root = os.path.join(os.getcwd(), project_path) |
| 20 | + |
| 21 | + project_resources_src = os.path.join(project_root, os.path.join("resources","src")) |
| 22 | + project_src = os.path.join(project_root, "src") |
| 23 | + |
| 24 | + # Create directories |
| 25 | + os.makedirs(project_root) |
| 26 | + os.makedirs(project_resources_src) |
| 27 | + os.makedirs(project_src) |
| 28 | + |
| 29 | + # Create main .c file |
| 30 | + self.generate_main_file(os.path.join(project_src, "%s.c" % (project_name))) |
| 31 | + |
| 32 | + # Add resource file |
| 33 | + open(os.path.join(project_resources_src, "resource_map.json"), "w").write(FILE_DUMMY_RESOURCE_MAP) |
| 34 | + |
| 35 | + # Add wscript file |
| 36 | + open(os.path.join(project_root, "wscript"), "w").write(FILE_WSCRIPT) |
| 37 | + |
| 38 | + # Add .gitignore file |
| 39 | + open(os.path.join(project_root, ".gitignore"), "w").write(FILE_GITIGNORE) |
| 40 | + |
| 41 | + def generate_uuid_as_array(self): |
| 42 | + """ |
| 43 | + Returns a freshly generated UUID value in string form formatted as |
| 44 | + a C array for inclusion in a template's "#define MY_UUID {...}" |
| 45 | + macro. |
| 46 | + """ |
| 47 | + return ", ".join(["0x%02X" % ord(b) for b in uuid.uuid4().bytes]) |
| 48 | + |
| 49 | + |
| 50 | + def generate_main_file(self, destination_filepath): |
| 51 | + """ |
| 52 | + Generates the main file *and* replaces a dummy UUID |
| 53 | + value in it with a freshly generated value. |
| 54 | + """ |
| 55 | + |
| 56 | + # This is the dummy UUID value in the template file. |
| 57 | + UUID_VALUE_TO_REPLACE="/* GENERATE YOURSELF USING `uuidgen` */ 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF" |
| 58 | + |
| 59 | + open(destination_filepath, "w").write(FILE_DUMMY_MAIN.replace(UUID_VALUE_TO_REPLACE, self.generate_uuid_as_array(), 1)) |
| 60 | + |
| 61 | + |
| 62 | +FILE_GITIGNORE = """ |
| 63 | +# Ignore build generated files |
| 64 | +build |
| 65 | +""" |
| 66 | + |
| 67 | +FILE_WSCRIPT = """ |
| 68 | +# |
| 69 | +# This file is the default set of rules to compile a Pebble project. |
| 70 | +# |
| 71 | +# Feel free to customize this to your needs. |
| 72 | +# |
| 73 | +
|
| 74 | +top = '.' |
| 75 | +out = 'build' |
| 76 | +
|
| 77 | +def options(ctx): |
| 78 | + ctx.load('pebble_sdk') |
| 79 | +
|
| 80 | +def configure(ctx): |
| 81 | + ctx.load('pebble_sdk') |
| 82 | +
|
| 83 | +def build(ctx): |
| 84 | + ctx.load('pebble_sdk') |
| 85 | +""" |
| 86 | + |
| 87 | +# When an empty resource map is required this can be used but... |
| 88 | +FILE_DEFAULT_RESOURCE_MAP = """ |
| 89 | +{"friendlyVersion": "VERSION", "versionDefName": "VERSION", "media": []} |
| 90 | +""" |
| 91 | + |
| 92 | +# ...for the moment we need to have one with a dummy entry due to |
| 93 | +# a bug that causes a hang when there's an empty resource map. |
| 94 | +FILE_DUMMY_RESOURCE_MAP = """ |
| 95 | +{"friendlyVersion": "VERSION", |
| 96 | + "versionDefName": "VERSION", |
| 97 | + "media": [ |
| 98 | + { |
| 99 | + "type":"raw", |
| 100 | + "defName":"DUMMY", |
| 101 | + "file":"resource_map.json" |
| 102 | + } |
| 103 | + ] |
| 104 | +} |
| 105 | +""" |
| 106 | + |
| 107 | +FILE_DUMMY_MAIN = """#include <pebble_os.h> |
| 108 | +#include <pebble_app.h> |
| 109 | +#include <pebble_fonts.h> |
| 110 | +
|
| 111 | +
|
| 112 | +#define MY_UUID { /* GENERATE YOURSELF USING `uuidgen` */ 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF, 0xBE, 0xEF } |
| 113 | +PBL_APP_INFO(MY_UUID, |
| 114 | + "Template App", "Your Company", |
| 115 | + 1, 0, /* App version */ |
| 116 | + DEFAULT_MENU_ICON, |
| 117 | + APP_INFO_STANDARD_APP); |
| 118 | +
|
| 119 | +Window *window; |
| 120 | +TextLayer *text_layer; |
| 121 | +
|
| 122 | +void select_click_handler(ClickRecognizerRef recognizer, void *context) { |
| 123 | + text_layer_set_text(text_layer, "Select"); |
| 124 | +} |
| 125 | +
|
| 126 | +void up_click_handler(ClickRecognizerRef recognizer, void *context) { |
| 127 | + text_layer_set_text(text_layer, "Up"); |
| 128 | +} |
| 129 | +
|
| 130 | +void down_click_handler(ClickRecognizerRef recognizer, void *context) { |
| 131 | + text_layer_set_text(text_layer, "Button"); |
| 132 | +} |
| 133 | +
|
| 134 | +void config_provider(ClickConfig **config, Window *window) { |
| 135 | + config[BUTTON_ID_SELECT]->click.handler = select_click_handler; |
| 136 | + config[BUTTON_ID_UP]->click.handler = up_click_handler; |
| 137 | + config[BUTTON_ID_DOWN]->click.handler = down_click_handler; |
| 138 | +} |
| 139 | +
|
| 140 | +void handle_init() { |
| 141 | + window = window_create(); |
| 142 | + window_stack_push(window, true /* Animated */); |
| 143 | +
|
| 144 | + window_set_click_config_provider(window, (ClickConfigProvider) config_provider); |
| 145 | +
|
| 146 | + text_layer = text_layer_create(GRect(/* x: */ 0, /* y: */ 74, |
| 147 | + /* width: */ 144, /* height: */ 20)); |
| 148 | + layer_add_child(window_get_root_layer(window), text_layer_get_layer(text_layer)); |
| 149 | +
|
| 150 | + text_layer_set_text(text_layer, "Press a button"); |
| 151 | +} |
| 152 | +
|
| 153 | +void handle_deinit() { |
| 154 | + // unsubscribe from services here |
| 155 | +} |
| 156 | +
|
| 157 | +void pbl_main(void *params) { |
| 158 | + register_init_handler(&handle_init); |
| 159 | + register_deinit_handler(&handle_deinit); |
| 160 | + app_event_loop(); |
| 161 | +} |
| 162 | +""" |
0 commit comments