Skip to content

Commit

Permalink
Implementation of init command (#94)
Browse files Browse the repository at this point in the history
* feat: implemented init command #31
* refactor: integrate init.lua in cli.lua streamlining the project init command
  • Loading branch information
lnpotter authored Oct 31, 2024
1 parent 426f37a commit 5d63ada
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/cli/commands/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ local function cli_dump(args)
return zeebo_bootstrap.dump(dist)
end

local function cli_init(args)
local project_name = args[1] or 'my_project'
init.init_project(project_name)
end

local P = {
['cli-build'] = cli_build,
['cli-test'] = cli_test,
['cli-dump'] = cli_dump
['cli-dump'] = cli_dump,
['cli-init'] = cli_init
}

return P
62 changes: 62 additions & 0 deletions src/cli/commands/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local os = require('os')

local function create_file(filepath, content)
local file = io.open(filepath, "w")
if file then
file:write(content)
file:close()
else
print("Error while creating file: " .. filepath)
end
end

local function create_directory(path)
local success = os.execute("mkdir " .. path)
if not success then
print("Error while creating directory: " .. path)
end
end

local function init_project(project_name)
create_directory(project_name)
create_file(project_name .. "/.gitignore", ".DS_Store\nThumbs.db\n")

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_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!")
end

return {
init_project = init_project
}

0 comments on commit 5d63ada

Please sign in to comment.