Skip to content

Commit

Permalink
chore: somes adjusts in #94
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Oct 31, 2024
1 parent 5d63ada commit 6335b46
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
51 changes: 21 additions & 30 deletions src/cli/commands/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local os = require('os')
local ok = true

local function create_file(filepath, content)
local file = io.open(filepath, "w")
Expand All @@ -7,56 +8,46 @@ local function create_file(filepath, content)
file:close()
else
print("Error while creating file: " .. filepath)
ok = false
end
end

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
}
7 changes: 4 additions & 3 deletions src/cli/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cli/argparse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6335b46

Please sign in to comment.