Files
yait/yait/main.c
2025-08-05 15:23:37 -04:00

411 lines
9.2 KiB
C

/* Copyright (C) vx_clutch
*
* This file is part of yait
*
* This project and file is licenced under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
// Usage: yait [OPTION]... PROJECT [NAME]
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../config.h"
#include "../core/file.h"
#include "../core/print.h"
#include "../core/standard.h"
#include "contents.h"
#include "debug.h"
#include "format.h"
#define DEFAULT_USER_NAME "unknown"
#define DEFAULT_PROJECT_NAME "Project"
#define DEFAULT_LICENCE BSD3
#define DEFAULT_GIT_INIT true
#define DEFAULT_CLANG_FORMAT true
/* This is to keep track of how deep we are within
the project tree. This is used in reset_path_ () */
int depth;
#define print_option(option, description) \
printf(" %-20s %-20s\n", option, description)
// clang-format off
static void
usage (int status)
{
if (status != 0)
{
fprintf (stderr, "Try 'yait --help' for more information.\n");
return;
}
printf ("Usage: yait [OPTION]... PROJECT [NAME]\n");
printf ("Creates a C project with opinionated defaults.\n");
printf ("When only given the first argument it will detect your name.\n\n");
printf ("Mandatory arguments to long options are mandatory for short options too\n");
print_option ("-l, --licence=NAME", "Set licence (gpl, mit, bsd) [default: bsd]");
print_option ("--lib=LIB", "Add a library to the project. You can list libraries with --lib=help.");
print_option ("--use-cpp", "Uses the CPP language instead of C");
print_option ("--git", "Initialize git repository");
print_option ("--GNU", "Adds standard GNU argument parsing to your project");
printf (" --help display this help text and exit\n");
printf (" --version output version information and exit\n");
}
// clang-format on
/* This macro exist purely because I like how it looks. This should be called
in every function that creates file to ensure they are being created in
right place. */
#define reset_path reset_path_()
static int reset_path_()
{
while (depth != 0) {
if (chdir("..") != 0)
return errno;
else
--depth;
}
return 0;
}
// TODO(vx-clutch): sanitize the alpha-numeric range
static int sanitize(manifest_t *m)
{
if (!m->project)
m->project = DEFAULT_PROJECT_NAME;
if (!m->name)
m->name = DEFAULT_USER_NAME;
if (!(m->licence == UNLICENCE))
m->licence = DEFAULT_LICENCE;
m->flag.git = m->flag.git ? true : DEFAULT_GIT_INIT;
m->flag.clang_format = m->flag.clang_format ? true :
DEFAULT_CLANG_FORMAT;
m->flag.GNU = m->flag.GNU ? true : DEFAULT_GNU;
if (!m->name) {
struct passwd *pw = getpwuid(getuid());
m->name = (pw && pw->pw_name) ? pw->pw_name : DEFAULT_USER_NAME;
}
return 0;
}
#define get(url) status = system("git submodule add -q " url)
static int create_libraries(manifest_t manifest)
{
int status = 0;
if (!manifest.libraries) {
return status;
}
reset_path;
for (int i = 0; i < LIB_COUNT_; ++i) {
if HAS_LIBRARY (manifest.libraries, LIB_RAYLIB) {
REMOVE_LIBRARY(manifest.libraries, LIB_RAYLIB);
get("https://github.com/raysan5/raylib");
} else if HAS_LIBRARY (manifest.libraries, LIB_NCURSES) {
REMOVE_LIBRARY(manifest.libraries, LIB_NCURSES);
get("https://github.com/mirror/ncurses");
} else if HAS_LIBRARY (manifest.libraries, LIB_CURL) {
REMOVE_LIBRARY(manifest.libraries, LIB_CURL);
get("https://github.com/raysan5/raylib");
}
}
return status;
}
static int create_licence(manifest_t manifest, char **licence_line_buffer)
{
if (manifest.licence == UNLICENCE)
return 0;
reset_path;
/* TODO(vx-clutch): Run better checks on licence_line_buffer to ensure we have enough
space. This could be done through a multitude of ways; that is for you to
figure out. */
assert(licence_line_buffer != NULL);
// TODO(vx-clutch): Remove this and actually implement the features.
#define TODO() \
printfn("Not impl"); \
assert(1 == 2)
switch (manifest.licence) {
case BSD3:
*licence_line_buffer = "Bsd";
TODO();
break;
case GPLv3:
TODO();
break;
case MIT:
TODO();
break;
case UNLICENCE:
default:
printfn("bad logic in create_licence ()");
return 1;
}
return 0;
}
static int maybe_create_clang_format(manifest_t manifest)
{
int status;
if (!manifest.flag.clang_format)
return 0;
reset_path;
status = create_file_with_content(".clang-format",
clang_format_template);
return status;
}
static int setup_git(manifest_t manifest)
{
if (!manifest.flag.git) {
return 0;
}
reset_path;
int status = system("git init --quiet");
if (status) {
printfn("failed on git initialize: %s", strerror(status));
}
return status;
}
static int create_makefile(manifest_t manifest)
{
char *makefile_name = strdup(manifest.project);
if (!makefile_name) {
printfn("fatal: out of memory");
return 1;
}
for (char *p = makefile_name; *p; ++p)
if (*p >= 'a' && *p <= 'z')
*p -= 32;
reset_path;
create_file_with_content("Makefile", makefile_template, makefile_name,
makefile_name, makefile_name, makefile_name,
makefile_name, makefile_name, manifest.project,
makefile_name, makefile_name);
free(makefile_name);
return 0;
}
static int create_configure(manifest_t manifest)
{
int status = 0;
reset_path;
char *cc;
if (manifest.flag.use_cpp) {
cc = "trycc g++\ntrycc CC\ntrycc clang++\n";
} else {
cc = "trycc gcc\ntrycc cc\ntrycc clang\n";
}
create_file_with_content("configure", configure_template, cc);
status = system("chmod +x configure");
if (status)
printfn("error: %s", strerror(status));
return status;
}
static int generate_source_code(manifest_t manifest)
{
int status;
debug("take %s/%s", manifest.project, manifest.project);
status = create_and_enter_directory(manifest.project);
on_error("failed to create or enter directory", status);
++depth;
if (manifest.flag.GNU) {
debug("GNU flag source branch");
create_file_with_content("main.c", main_c_gnu_template,
manifest.project, manifest.name);
goto atexit_clean;
}
debug("default sourcebranch");
create_file_with_content("main.c", main_c_template, manifest.project,
manifest.name);
atexit_clean:
reset_path;
return 0;
}
static int parse_arguments(manifest_t *conf, int argc, char **argv)
{
static struct option long_options[] = {
{ "GNU", no_argument, 0, 'g' },
{ "use-cpp", no_argument, 0, 'c' },
{ "git", no_argument, 0, 'i' },
{ "licence", required_argument, 0, 'l' },
{ "lib", required_argument, 0, 'L' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
int opt;
while ((opt = getopt_long(argc, argv, "gcil:L:h", long_options,
NULL)) != -1) {
switch (opt) {
case 'g':
conf->flag.GNU = true;
break;
case 'c':
conf->flag.use_cpp = true;
break;
case 'i':
conf->flag.git = true;
break;
case 'l':
if (strcmp(optarg, "bsd") == 0)
conf->licence = BSD3;
else if (strcmp(optarg, "gpl") == 0)
conf->licence = GPLv3;
else if (strcmp(optarg, "mit") == 0)
conf->licence = MIT;
break;
case 'L':
ADD_LIBRARY(conf->libraries, TOLibrary(optarg));
break;
case 'h':
usage(0);
exit(0);
default:
usage(1);
exit(1);
}
}
/* now handle positional args */
if (optind < argc)
conf->project = argv[optind++];
if (optind < argc)
conf->name = argv[optind++];
return 0;
}
static int create_project(manifest_t manifest)
{
int status;
debugc("sanitize");
status = sanitize(&manifest);
on_error("failed to sanitize format", status);
done;
debugc("take %s", manifest.project);
status = create_and_enter_directory(manifest.project);
depth = 0;
on_error("failed to create or enter directory", status);
done;
debugc("create makefile");
status = create_makefile(manifest);
on_error("failed to create Makefile", status);
done;
debug("setup git");
status = setup_git(manifest);
if (status) {
printfn("warning: git initialization failed: %s",
strerror(status));
}
debug("create .clang-format");
status = maybe_create_clang_format(manifest);
if (status) {
printfn("warning: clang-format setup failed: %s",
strerror(status));
}
debugc("generate source code");
status = generate_source_code(manifest);
on_error("failed to generate source code", status);
done;
debugc("get libraries");
status = create_libraries(manifest);
on_error("failed to get libraries", status);
done;
return 0;
}
int program_exists(const char *prog)
{
char *path = getenv("PATH");
if (!path)
return 1;
char *copy = strdup(path);
if (!copy)
return 1;
char *dir = strtok(copy, ":");
while (dir) {
char buf[4096];
snprintf(buf, sizeof(buf), "%s/%s", dir, prog);
if (access(buf, X_OK) == 0) {
free(copy);
return 0;
}
dir = strtok(NULL, ":");
}
free(copy);
return 1;
}
int main(int argc, char **argv)
{
int status;
manifest_t manifest = { 0 };
status = parse_standard_options(usage, argc, argv);
if (status && status != HELP_REQUESTED) {
printfn("error: %s", strerror(status));
return status;
}
status = program_exists("git");
on_error("git bin not present", status);
parse_arguments(&manifest, argc, argv);
status = create_project(manifest);
#ifdef DEBUG
if (!status)
debug("project made successfully");
else
debug("something when wrong");
#endif
return status;
}