Skip to content

Commit

Permalink
#409 Added corto publish command for managing version.txt
Browse files Browse the repository at this point in the history
TODO: integration with git
  • Loading branch information
SanderMertens committed Nov 30, 2015
1 parent 25a228b commit d7c5b82
Show file tree
Hide file tree
Showing 11 changed files with 489 additions and 278 deletions.
308 changes: 86 additions & 222 deletions packages/corto/lang/README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/corto/lang/src/corto.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct corto_exitHandler {

#define VERSION_MAJOR "0"
#define VERSION_MINOR "2"
#define VERSION_PATCH "7"
#define VERSION_PATCH "8"
#define VERSION_SUFFIX "alpha"

#ifdef VERSION_SUFFIX
Expand Down
8 changes: 7 additions & 1 deletion packages/corto/lang/src/corto_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ corto_argdata* corto_argparse(char *argv[], corto_argdata *data) {
/* Match AT LEAST once */
} else if ((pattern->pattern[1] == '+') || (pattern->pattern[1] == '|')) {
if (!fnmatch(pattern->pattern + 2, arg, 0)) {
if (count && tentative) {
if (count && tentative && !fnmatch(tentative->pattern + 2, arg, 0)) {
/* Push back one element to optional pattern */
if (matchCount && pattern->match && *pattern->match) {
corto_string arg = corto_llTakeFirst(*pattern->match);
Expand All @@ -88,6 +88,7 @@ corto_argdata* corto_argparse(char *argv[], corto_argdata *data) {
tentative = NULL;
match = TRUE;
} else {
tentative = NULL;
match = TRUE;
}
}
Expand All @@ -108,6 +109,11 @@ corto_argdata* corto_argparse(char *argv[], corto_argdata *data) {
p++;
}

if (tentative && !fnmatch(tentative->pattern + 2, arg, 0)) {
pattern = tentative;
match = TRUE;
}

if (match) {
if (pattern->match) {
if(!*(pattern->match)) {
Expand Down
64 changes: 64 additions & 0 deletions packages/corto/lang/test/src/test_Args.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,70 @@ corto_void _test_Args_tc_matchPattern(test_Args this) {
/* $end */
}

corto_void _test_Args_tc_matchPublish(test_Args this) {
/* $begin(test/Args/tc_matchPublish) */
char *argv[] = {"publish", "MyApp", "major", "--notag", NULL};
corto_ll notag, dirs, majorarg, minorarg, patcharg;

corto_argdata *data = corto_argparse(
argv,
(corto_argdata[]){
{"$0", NULL, NULL}, /* Ignore 'publish' argument */
{"--notag", &notag, NULL},
{"$?*", &dirs, NULL},
{"$+major", &majorarg, NULL},
{"$|minor", &minorarg, NULL},
{"$|patch", &patcharg, NULL},
{NULL}
}
);

test_assert(data != NULL);

test_assert(notag != NULL);
test_assert(majorarg != NULL);
test_assert(minorarg == NULL);
test_assert(patcharg == NULL);

test_assert(dirs != NULL);
test_assert(!strcmp(corto_llGet(dirs, 0), "MyApp"));

corto_argclean(data);

/* $end */
}

corto_void _test_Args_tc_matchPublishNoProject(test_Args this) {
/* $begin(test/Args/tc_matchPublishNoProject) */
char *argv[] = {"publish", "major", "--notag", NULL};
corto_ll notag, dirs, majorarg, minorarg, patcharg;

corto_argdata *data = corto_argparse(
argv,
(corto_argdata[]){
{"$0", NULL, NULL}, /* Ignore 'publish' argument */
{"--notag", &notag, NULL},
{"$?*", &dirs, NULL},
{"$+major", &majorarg, NULL},
{"$|minor", &minorarg, NULL},
{"$|patch", &patcharg, NULL},
{NULL}
}
);

test_assert(data != NULL);

test_assert(notag != NULL);
test_assert(majorarg != NULL);
test_assert(minorarg == NULL);
test_assert(patcharg == NULL);
test_assert(dirs == NULL);

corto_argclean(data);

/* $end */
}

corto_void _test_Args_tc_matchShell(test_Args this) {
/* $begin(test/Args/tc_matchShell) */
char *argv[] = {"shell", "-p", "corto/idl", "test.cx", "-d", "test2.cx", NULL};
Expand Down
112 changes: 112 additions & 0 deletions packages/corto/lang/test/src/test_Project.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,118 @@ corto_void _test_Project_tc_packageLocal(test_Project this) {
/* $end */
}

corto_void _test_Project_tc_publishNotag(test_Project this) {
/* $begin(test/Project/tc_publishNotag) */
corto_int8 ret;
corto_int16 waitResult;

corto_pid pid = corto_procrun(
"corto",
(char*[]){
"corto",
"create",
"package",
"Project",
"--silent",
"--local",
"--notest",
NULL
});

test_assert(pid != 0);

waitResult = corto_procwait(pid, &ret);
test_assert(waitResult == 0);
test_assert(ret == 0);

test_assert(corto_fileTest("Project"));
test_assert(corto_fileTest("Project/.corto/version.txt"));

corto_string v = corto_fileLoad("Project/.corto/version.txt");
test_assert(v != NULL);
test_assert(!strcmp(v, "0.0.0\n"));
corto_dealloc(v);

/* Test patch version increase */
pid = corto_procrun(
"corto",
(char*[]){
"corto",
"publish",
"Project",
"patch",
"--silent",
"--notag",
NULL
});

test_assert(pid != 0);

waitResult = corto_procwait(pid, &ret);
test_assert(waitResult == 0);
test_assert(ret == 0);

test_assert(corto_fileTest("Project/.corto/version.txt"));

v = corto_fileLoad("Project/.corto/version.txt");
test_assert(v != NULL);
test_assert(!strcmp(v, "0.0.1\n"));
corto_dealloc(v);

/* Test minor version increase */
pid = corto_procrun(
"corto",
(char*[]){
"corto",
"publish",
"Project",
"minor",
"--silent",
"--notag",
NULL
});

test_assert(pid != 0);

waitResult = corto_procwait(pid, &ret);
test_assert(waitResult == 0);
test_assert(ret == 0);

test_assert(corto_fileTest("Project/.corto/version.txt"));

v = corto_fileLoad("Project/.corto/version.txt");
test_assert(v != NULL);
test_assert(!strcmp(v, "0.1.1\n"));
corto_dealloc(v);

/* Test major version increase */
pid = corto_procrun(
"corto",
(char*[]){
"corto",
"publish",
"Project",
"major",
"--silent",
"--notag",
NULL
});

test_assert(pid != 0);

waitResult = corto_procwait(pid, &ret);
test_assert(waitResult == 0);
test_assert(ret == 0);

test_assert(corto_fileTest("Project/.corto/version.txt"));

v = corto_fileLoad("Project/.corto/version.txt");
test_assert(!strcmp(v, "1.1.1\n"));
corto_dealloc(v);

/* $end */
}

corto_void _test_Project_teardown(test_Project this) {
/* $begin(test/Project/teardown) */
char *env = corto_envparse(
Expand Down
4 changes: 3 additions & 1 deletion packages/corto/lang/test/test.cx
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,14 @@ test::Suite Env::
void tc_setenvVariableNotFound()
void tc_setenvNull()


test::Suite Project::
void setup() method
void teardown() method
void tc_app()
void tc_appNoTest()
void tc_package()
void tc_packageLocal()
void tc_publishNotag()

test::Suite Args::
void tc_matchSingle()
Expand All @@ -574,6 +574,8 @@ test::Suite Args::
void tc_matchOptionalOneArg()
void tc_matchOptionalWithOtherArgs()
void tc_matchShell()
void tc_matchPublish()
void tc_matchPublishNoProject()
void tc_matchCreate()
void tc_matchCreateMultiple()
void tc_matchCreateApp()
Expand Down
17 changes: 17 additions & 0 deletions tools/corto/include/cortotool_publish.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#ifndef CORTO_PUBLISH_H
#define CORTO_PUBLISH_H

#include "corto.h"

#ifdef __cplusplus
extern "C" {
#endif

corto_int16 cortotool_publish(int argc, char* argv[]);
void cortotool_publishHelp(void);

#ifdef __cplusplus
}
#endif
#endif
118 changes: 118 additions & 0 deletions tools/corto/src/corto_publish.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "cortotool_publish.h"

corto_int16 cortotool_publish(int argc, char *argv[]) {
corto_ll silent, mute, notag, dirs, majorarg, minorarg, patcharg;
corto_uint32 major = 0, minor = 0, patch = 0;

CORTO_UNUSED(argc);

corto_argdata *data = corto_argparse(
argv,
(corto_argdata[]){
{"$0", NULL, NULL}, /* Ignore 'publish' argument */
{"--silent", &silent, NULL},
{"--mute", &mute, NULL},
{"--notag", &notag, NULL},
{"$?*", &dirs, NULL},
{"$+major", &majorarg, NULL},
{"$|minor", &minorarg, NULL},
{"$|patch", &patcharg, NULL},
{NULL}
}
);

if (!data) {
corto_error("corto: %s", corto_lasterr());
goto error;
}

if (dirs) {
corto_string dir = corto_llGet(dirs, 0);
if (corto_chdir(dir)) {
corto_error("corto: %s", corto_lasterr());
goto error;
}
}


if (!corto_fileTest("./.corto")) {
corto_error("corto: invalid project directory");
goto error;
}

if (!notag) {
corto_error(
"corto: tagging of repository not yet supported "
"(use --notag to just increase version)");
goto error;
}

corto_string version = corto_fileLoad(".corto/version.txt");

/* Patch version */
if (version) {
char *v = version;

/* Parse major version */
char *ptr = strchr(version, '.');
if (ptr) {
*ptr = '\0';
major = atoi(v);
v = ptr + 1;
}

/* Parse minor version */
ptr = strchr(v, '.');
if (ptr) {
*ptr = '\0';
minor = atoi(v);
v = ptr + 1;
}

/* Parse patch version */
patch = atoi(v);
}

if (majorarg) {
major++;
}
if (minorarg) {
minor++;
}
if (patcharg) {
patch++;
}

FILE *f = fopen(".corto/version.txt", "w");
if (!f) {
corto_error("failed to open '.corto/version.txt' (check permissions)");
goto error;
}

fprintf(f, "%u.%u.%u\n", major, minor, patch);
fclose(f);

corto_argclean(data);
corto_dealloc(version);

if (!silent) {
corto_print("corto: version updated to %u.%u.%u", major, minor, patch);
}

return 0;
error:
return -1;
}

void cortotool_publishHelp(void) {
printf("Usage: corto publish [options]\n");
printf(" corto publish major|minor|patch [options]\n");
printf(" corto publish <project> [options]\n");
printf(" corto publish <project> major|minor|patch [options]\n");
printf("\n");
printf("Increase version number of project, and create git tag.\n");
printf("\n");
printf("Options:\n");
printf(" --notag: Just update version number, don't create git tag\n");
printf("\n");
}
Loading

0 comments on commit d7c5b82

Please sign in to comment.