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

Conversation

GmosNM
Copy link
Contributor

@GmosNM GmosNM commented Nov 14, 2023

closes #177

Copy link
Owner

@travisdoor travisdoor left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, good job; however, there are some things to fix. Note that I made a new document for contributors here.

Please use clang-format before committing, the config can be found in the root of repo. Thanks.

src/main.c Outdated
Comment on lines 35 to 39
#ifdef _WIN32
#include <direct.h>
#define mkdir(path, mode) _mkdir(path)
#endif
#include <sys/stat.h>
Copy link
Owner

Choose a reason for hiding this comment

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

This can be removed; please see the following comments.

src/main.c Outdated
@@ -118,10 +123,17 @@ typedef struct ApplicationOptions {
bool do_cleanup_when_done;
} ApplicationOptions;


typedef struct ProjectOptions{
bool is_executable;
Copy link
Owner

Choose a reason for hiding this comment

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

This member is not used at all.

src/main.c Outdated
@@ -118,10 +123,17 @@ typedef struct ApplicationOptions {
bool do_cleanup_when_done;
} ApplicationOptions;


typedef struct ProjectOptions{
Copy link
Owner

Choose a reason for hiding this comment

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

I use typedefs only for commonly used helper types; please see https://github.com/travisdoor/bl/wiki/Contribution#code-guidelines

src/main.c Outdated
typedef struct Options {
ApplicationOptions app;
struct builder_options builder;
struct target *target;
ProjectOptions project;
Copy link
Owner

Choose a reason for hiding this comment

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

Since only project_name is used here I suggest to rewrite this to char *init_project_name.

Copy link
Owner

Choose a reason for hiding this comment

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

Or even better, you can remove it completely since you use this only as temporary later in case ID_INIT_PROJECT: branch. Project name can be local in the case branch.

src/main.c Outdated
@@ -245,6 +257,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.

We might make the --init option the first class, so -init should be used.

Copy link
Owner

Choose a reason for hiding this comment

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

Hey, you've changed the arg implementation, but not the help comment, it should be -init [project-name] here.

src/main.c Outdated

fprintf(build_file, "\n\n\n%s", build_function_code);

mkdir("src", 0777);
Copy link
Owner

Choose a reason for hiding this comment

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

Please use create_dir from common.h here.

src/main.c Outdated
"}\n";
fprintf(main_file, "%s", main_example);


Copy link
Owner

Choose a reason for hiding this comment

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

Both files stay open; you should call fclose when file modification is done.

src/main.c Outdated

builder_info("INFO: project was initialize successfully");
builder_info("INFO: try blc -build");
return state;
Copy link
Owner

Choose a reason for hiding this comment

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

Please use EXIT(EXIT_SUCCESS); instead to propper shutdown.

src/main.c Outdated
fprintf(build_file, "\n\n\n%s", build_function_code);

mkdir("src", 0777);
mkdir("bin", 0777);
Copy link
Owner

Choose a reason for hiding this comment

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

Same as the previous one...

src/main.c Outdated

mkdir("src", 0777);
mkdir("bin", 0777);
FILE *main_file = fopen("src/main.bl", "w+");
Copy link
Owner

Choose a reason for hiding this comment

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

Again missing error handling.

@travisdoor travisdoor added the enhancement New feature or request label Nov 14, 2023
Copy link
Owner

@travisdoor travisdoor left a comment

Choose a reason for hiding this comment

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

Thanks for the corrections, there is one important thing: We want to prevent users from accidentally override existing project. So before any modifications are done, we have to check whether there are src/main.bl or build.bl already present.

src/main.c Outdated
@@ -245,6 +257,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.

Hey, you've changed the arg implementation, but not the help comment, it should be -init [project-name] here.

src/main.c Outdated
project_name = argv[index + 1];
}

FILE *build_file = fopen("build.bl", "w+");
Copy link
Owner

Choose a reason for hiding this comment

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

This is not fixed, BUILD_SCRIPT_FILE should be used instead of hardcoded filename.

src/main.c Outdated

struct getarg_opt optlist[] = {
{
.name = "-init",
.help = "Creates a a project setup in your current folder."
Copy link
Owner

Choose a reason for hiding this comment

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

Creates a a project -> Creates a project + missing space after folder.

src/main.c Outdated

char *exe_name = project_name;

fprintf(build_file, "\n\n\n");
Copy link
Owner

Choose a reason for hiding this comment

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

No reason to have this IMHO.

fprintf(build_file, build_function_code_template, exe_name);

if (!create_dir("bin")) {
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.

src/main.c Outdated

FILE *main_file = fopen("./src/main.bl", "w+");
if (!main_file) {
builder_error("could not create build file!");
Copy link
Owner

Choose a reason for hiding this comment

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

This is IMHO copy-paste, but the message is wrong?

src/main.c Outdated
"}\n";
fprintf(main_file, "%s", main_example);

builder_info("INFO: project was initialize successfully");
Copy link
Owner

Choose a reason for hiding this comment

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

The INFO: prefix here might be redundant in the future, I generally don't do that. The builder_info can eventually do it automatically.

Copy link
Owner

Choose a reason for hiding this comment

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

Typo: initialize -> initialized.

src/main.c Outdated
" compile(exe);\n"
"}\n";

char *exe_name = project_name;
Copy link
Owner

Choose a reason for hiding this comment

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

What's the point of this variable? You can use project_name directly.

src/main.c Outdated
@@ -673,6 +683,57 @@ 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.bl")) {
Copy link
Owner

Choose a reason for hiding this comment

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

This is not fixed, BUILD_SCRIPT_FILE should be used instead of hardcoded filename.

src/main.c Outdated
@@ -673,6 +683,57 @@ 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.bl")) {
builder_error("File 'build.bl' exists in the current directory.");
Copy link
Owner

Choose a reason for hiding this comment

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

Similar: builder_error("File '%s' exists in the current directory.", BUILD_SCRIPT_FILE);

Copy link
Owner

Choose a reason for hiding this comment

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

It would be nice to have some descriptive message saying what is exactly wrong. Each error message should give the user some hint of how to fix it eventually or why it's reported as an error. What about: Current directory already contains initialized BL project. File '%s' exists.

Copy link
Owner

@travisdoor travisdoor left a comment

Choose a reason for hiding this comment

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

There are still some minor issues, but I guess I can merge it and do the fixes later.

@@ -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]

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.

@travisdoor travisdoor merged commit 5c5d630 into travisdoor:master Nov 16, 2023
6 checks passed
@GmosNM GmosNM deleted the init-command branch November 17, 2023 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add blc --init option
2 participants