Last active
December 4, 2017 06:13
-
-
Save zshipko/180042d1081d6cf5dd34fb38bc2a1580 to your computer and use it in GitHub Desktop.
A script that simplifies the creation of Redis modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
redis_path=${REDIS_SRC=/tmp/redis-src} | |
base=`pwd` | |
generated_header="// DO NOT EDIT - This file was automatically generated by redismod" | |
fetch_or_update_redis () { | |
git clone https://github.com/antirez/redis $redis_path || \ | |
(cd $redis_path && git pull origin unstable && cd $base) | |
} | |
copy_files_from_redis () { | |
cp $redis_path/src/redismodule.h $1 | |
} | |
_create_header_file () { | |
cat > $1/redismod.h <<-END | |
$generated_header | |
#ifndef __REDISMOD_H | |
#define __REDISMOD_H | |
#include "redismodule.h" | |
#define RedisMod_AssertOk(x) if (x != REDISMODULE_OK) return REDISMODULE_ERR | |
#define RedisMod_AssertOkCleanup(x) if (x != REDISMODULE_OK) goto cleanup | |
#define RedisMod_Args(ctx, argv, argc) RedisModuleCtx *ctx, RedisModuleString **argv, int argc | |
#define RedisMod_Function(name, ctx, argv, argc) int name (RedisMod_Args(ctx, argv, argc)) | |
#endif // __REDISMOD_H | |
END | |
} | |
_create_default_makefile () { | |
tab=`printf "\t"` | |
cat > $1/Makefile <<-END | |
SRC=$1.c | |
OBJ=\$(SRC:.c=.o) | |
CFLAGS+= -fPIC | |
LDFLAGS?= | |
mod: \$(OBJ) | |
$tab\$(CC) -shared -o $1.so \$(OBJ) | |
clean: | |
${tab}rm -f *.o *.so | |
END | |
} | |
_create_module_c_file () { | |
cat > $1/$1.c <<-END | |
#include "redismod.h" | |
RedisMod_Function(exampleCommand, ctx, argv, argc){ | |
RedisModule_ReplyWithSimpleString(ctx, "OK"); | |
return REDISMODULE_OK; | |
} | |
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){ | |
RedisMod_AssertOk(RedisModule_Init(ctx, "$1", 1, REDISMODULE_APIVER_1)); | |
RedisMod_AssertOk(RedisModule_CreateCommand(ctx, "$1.example", exampleCommand, "readonly", 0, 0, 0)); | |
return REDISMODULE_OK; | |
} | |
END | |
} | |
create_default_files () { | |
_create_default_makefile $1 | |
_create_module_c_file $1 | |
_create_header_file $1 | |
} | |
create () { | |
name=`basename $1` | |
mkdir -p $1 | |
fetch_or_update_redis | |
copy_files_from_redis $name | |
create_default_files $name | |
echo Module created! | |
} | |
update () { | |
name=`basename $1` | |
fetch_or_update_redis | |
copy_files_from_redis $name | |
_create_header_file $1 | |
echo RedisMod files updated! | |
} | |
cmd=$1 | |
shift | |
$cmd $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment