aboutsummaryrefslogtreecommitdiff
path: root/src/build.c
blob: 952389bd906b78f9e0486336617895f405b615d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <inttypes.h>

#include "config.h"
#include "fs.h"
#include "build.h"

static void write_build(char* project, char* build_id, struct build_t* build)
{
    char* build_path = build_dir(project, build_id);
    if (!build_path)
        return;

    if (!isDir(build_path))
        makeDir(build_path);

    size_t build_size = strlen(build_path);
    build_path = realloc(build_path, (build_size + 11 + 1) * sizeof(char));

    FILE* fd;

    strcat(build_path, "/timestamp");
    fd = fopen(build_path, "wb");
    if (fd)
    {
        fwrite(&build->timestamp, sizeof(time_t), 1, fd);
        fclose(fd);
    }
    build_path[build_size] = '\0';

    strcat(build_path, "/completion");
    fd = fopen(build_path, "wb");
    if (fd)
    {
        fwrite(&build->completion, sizeof(build->completion), 1, fd);
        fclose(fd);
    }
    build_path[build_size] = '\0';

    strcat(build_path, "/status");
    fd = fopen(build_path, "wb");
    if (fd)
    {
        fwrite(&build->status, sizeof(build->status), 1, fd);
        fclose(fd);
    }
    build_path[build_size] = '\0';

    free(build_path);

}

void create_build()
{
    if (!current_project)
        return;

    uintmax_t build_id = 0;

    if (current_project->build_count > 0)
        build_id = strtoumax(current_project->builds[0].name, NULL, 10)+1;

    if (build_id <= 0)
        build_id = 1;

    struct build_t build;
    build.name = NULL;
    build.timestamp = time(NULL);
    build.completion = 0;
    build.status = STATUS_INPROGRESS;

    size_t name_len = 1;
    uintmax_t temp = build_id;

    while (temp > 10)
    {
        temp /= 10;
        name_len++;
    }

    build.name = malloc((name_len + 1) * sizeof(char));
    sprintf(build.name, "%ld", build_id);

    write_build(current_project->name, build.name, &build);

    if (!fork())
    {
        char* build_path = build_dir(current_project->name, build.name);
        if (build_path)
        {
            makeDir(build_path);
            build_path = realloc(build_path, (strlen(build_path) + 4 + 1) * sizeof(char));
            strcat(build_path, "/log");

            freopen(build_path, "w", stdout);
            dup2(1, 2);

            free(build_path);
        }

        printf("Running '%s'\n", current_project->script_path);
        fflush(stdout);

        int status = system(current_project->script_path);
        if (status)
            build.status = STATUS_FAILURE;
        else
            build.status = STATUS_SUCCESS;

        build.completion = time(NULL);

        write_build(current_project->name, build.name, &build);
        exit(status);
    }
}