Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sub command init for generating new project #178

Merged
merged 10 commits into from
Nov 16, 2023
60 changes: 59 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "assembly.h"
#include "builder.h"
#include "common.h"
#include "conf.h"
#include "stb_ds.h"
#include <locale.h>
Expand All @@ -44,7 +45,7 @@ static char *get_exec_dir(void) {
return strdup(tmp);
}

static void get_config_file_location(str_buf_t* filepath) {
static void get_config_file_location(str_buf_t *filepath) {
str_buf_append_fmt(filepath, "{s}/../{s}", builder_get_exec_dir(), BL_CONFIG_FILE);
}

Expand Down Expand Up @@ -245,6 +246,7 @@ void print_help(FILE *stream, struct getarg_opt *opts) {
" blc [options] [source-files]\n\n"
"Alternative usage:\n"
" blc [options] <-build> [build-arguments]\n"
" blc [options] <-init> <project-name>\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be <-init> [project-name]

" blc [options] <-run> <source-file> [arguments] [forwarded-arguments]\n\n"
"Options:\n";
fprintf(stream, "%s", text);
Expand Down Expand Up @@ -354,14 +356,22 @@ int main(s32 argc, char *argv[]) {
#define ID_VMDBG_BREAK_ON 5
#define ID_RELEASE 6
#define ID_SILENT_RUN 7
#define ID_INIT_PROJECT 8

struct getarg_opt optlist[] = {
{
.name = "-init",
.help = "Creates a project setup in your current folder. "
"Use as '-init [project-name]",
.id = ID_INIT_PROJECT,
},
{
.name = "-build",
.help = "Invoke project build pipeline. All following arguments are forwarded into the "
"build script and ignored by compiler itself. Use as '-build [arguments]'.",
.id = ID_BUILD,
},

{
.name = "-run",
.help =
Expand Down Expand Up @@ -673,6 +683,54 @@ int main(s32 argc, char *argv[]) {
case ID_RELEASE:
opt.target->opt = ASSEMBLY_OPT_RELEASE_FAST;
break;
case ID_INIT_PROJECT:
if (file_exists(BUILD_SCRIPT_FILE) || file_exists("./src/main.bl")) {
builder_error("Project seems to be already initialized in this directory.");
EXIT(EXIT_FAILURE);
}
char *project_name = "out";
if (argv[index + 1]) project_name = argv[index + 1];

FILE *build_file = fopen(BUILD_SCRIPT_FILE, "w+");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"w+" is not necessary here, you're not going to read from the file, so "w" should be enough.

if (!build_file) {
builder_error("Could not create build file!");
EXIT(EXIT_FAILURE);
}
const char *build_function_code_template = "build :: fn () #build_entry {\n"
" exe := add_executable(\"%s\");\n"
" set_output_dir(exe,\"bin\");\n"
" add_unit(exe, \"src/main.bl\");\n"
" compile(exe);\n"
"}\n";

fprintf(build_file, build_function_code_template, project_name);

if (!create_dir("bin")) {
builder_error("Could not create bin directory!");
EXIT(EXIT_FAILURE);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an error report instead of a silent failure. Same for this src creation.

}
if (!create_dir("src")) {
builder_error("Could not create src directory!");
EXIT(EXIT_FAILURE);
}

FILE *main_file = fopen("./src/main.bl", "w+");
if (!main_file) {
builder_error("Could not create main file!");
EXIT(EXIT_FAILURE);
}
const char *main_example = "\n\nmain :: fn() s32 {\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous one, please do not add empty lines before/after code to generated files.

" print(\"Hello World!\\n\");\n"
" return 0;\n"
"}\n";
fprintf(main_file, "%s", main_example);

builder_info("Project was initialized successfully");
builder_info("Try blc -build");

fclose(main_file);
fclose(build_file);
EXIT(EXIT_SUCCESS);
default:
if (positional) {
target_add_file(opt.target, positional);
Expand Down
Loading