-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
There was a problem hiding this 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
#ifdef _WIN32 | ||
#include <direct.h> | ||
#define mkdir(path, mode) _mkdir(path) | ||
#endif | ||
#include <sys/stat.h> |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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{ |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); | ||
|
||
|
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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+"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again missing error handling.
There was a problem hiding this 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" |
There was a problem hiding this comment.
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+"); |
There was a problem hiding this comment.
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." |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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!"); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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")) { |
There was a problem hiding this comment.
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."); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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.
There was a problem hiding this 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" |
There was a problem hiding this comment.
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+"); |
There was a problem hiding this comment.
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.
closes #177