From 6335b46ca4fc9cd3fa585b5bf6609274b1ae1efc Mon Sep 17 00:00:00 2001 From: RodrigoDornelles Date: Thu, 31 Oct 2024 10:36:36 -0300 Subject: [PATCH] chore: somes adjusts in #94 --- src/cli/commands/init.lua | 51 ++++++++++++++++----------------------- src/cli/main.lua | 7 +++--- src/lib/cli/argparse.lua | 2 +- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/cli/commands/init.lua b/src/cli/commands/init.lua index 558fd62..f9571fd 100644 --- a/src/cli/commands/init.lua +++ b/src/cli/commands/init.lua @@ -1,4 +1,5 @@ local os = require('os') +local ok = true local function create_file(filepath, content) local file = io.open(filepath, "w") @@ -7,6 +8,7 @@ local function create_file(filepath, content) file:close() else print("Error while creating file: " .. filepath) + ok = false end end @@ -14,49 +16,38 @@ local function create_directory(path) local success = os.execute("mkdir " .. path) if not success then print("Error while creating directory: " .. path) + ok = false end end -local function init_project(project_name) +local function init_project(args) + local project_name = args.project + local project_template = args.template + local project_gamefile, error_gamefile = io.open(project_template, 'r') + + ok = true + + if not project_gamefile then + return false, 'cannot open template: '..project_template + end + + local game_lua_content = project_gamefile:read('*a') + create_directory(project_name) - create_file(project_name .. "/.gitignore", ".DS_Store\nThumbs.db\n") + create_file(project_name .. "/.gitignore", ".DS_Store\nThumbs.db\nvendor\ndist\ncli.lua") create_directory(project_name .. "/dist") create_directory(project_name .. "/vendor") - create_file(project_name .. "/README.md", "# " .. project_name .. "\n\n * **use:** `./cli.sh run src/game.lua`\n") + create_file(project_name .. "/README.md", "# " .. project_name .. "\n\n * **use:** `lua cli.lua build src/game.lua`\n") create_directory(project_name .. "/src") - local game_lua_content = 'local function init(std, game)\n\nend\n\n' - ..'local function loop(std, game)\n\nend\n\n' - ..'local function draw(std, game)\n' - ..' std.draw.clear(std.color.black)\n' - ..' std.draw.color(std.color.white)\n' - ..' std.draw.text(8, 8, "hello world")\n' - ..'end\n\n' - ..'local function exit(std, game)\n\nend\n\n' - ..'local P = {\n' - ..' meta={\n' - ..' title="' .. project_name .. '",\n' - ..' author="YourName",\n' - ..' description="description about the game",\n' - ..' version="1.0.0"\n' - ..' },\n' - ..' callbacks={\n' - ..' init=init,\n' - ..' loop=loop,\n' - ..' draw=draw,\n' - ..' exit=exit\n' - ..' }\n' - ..'}\n\n' - ..'return P\n' - create_file(project_name .. "/src/game.lua", game_lua_content) - - print("Project " .. project_name .. " created with success!") + + return ok, ok and "Project " .. project_name .. " created with success!" end return { - init_project = init_project + init = init_project } diff --git a/src/cli/main.lua b/src/cli/main.lua index 94b3ee7..10c9a50 100644 --- a/src/cli/main.lua +++ b/src/cli/main.lua @@ -10,12 +10,13 @@ local commands_cli = require('src/cli/commands/cli') local commands_fs = require('src/cli/commands/fs') local commands_game = require('src/cli/commands/game') local commands_info = require('src/cli/commands/info') +local commands_init = require('src/cli/commands/init') local commands_tools = require('src/cli/commands/tools') local command = zeebo_argparse.from(arg) - .add_subcommand('init', commands_game) - .add_next_value('game', {require=true, alias='@examples/{{game}}/game.lua'}) - .add_option_get('dist', {default='{game}'}) + .add_subcommand('init', commands_init) + .add_next_value('project', {required=true}) + .add_option_get('template', {alias='@examples/{{template}}/game.lua', default='./examples/helloworld/game.lua'}) -- .add_subcommand('build', commands_build) .add_next_value('game', {alias='@examples/{{game}}/game.lua'}) diff --git a/src/lib/cli/argparse.lua b/src/lib/cli/argparse.lua index e604b19..79fc724 100644 --- a/src/lib/cli/argparse.lua +++ b/src/lib/cli/argparse.lua @@ -77,7 +77,7 @@ local function run(self, host_args) local required = self.param_dict_option_get_required[command][param] local default_value = self.param_dict_option_get_default[command][param] if alias and (value or ''):sub(1, 1) == alias:sub(1, 1) then - value = self.param_dict_value_alias[command][param]:sub(2):gsub('{{'..param..'}}', value:sub(2)) + value = alias:sub(2):gsub('{{'..param..'}}', value:sub(2)) end if required and not value then command = self.error_usage