Browse Source

CLI: adding option for global/local dir

pull/1005/head
jfrey 7 years ago
parent
commit
907ad50b5a
3 changed files with 68 additions and 41 deletions
  1. +1
    -1
      include/asset.hpp
  2. +53
    -38
      src/asset.cpp
  3. +14
    -2
      src/main.cpp

+ 1
- 1
include/asset.hpp View File

@@ -7,7 +7,7 @@
namespace rack {


void assetInit(bool devMode);
void assetInit(bool devMode, std::string customGlobalDir = std::string(), std::string customLocalDir = std::string());
/** Returns the path of a global resource. Should only read files from this location. */
std::string assetGlobal(std::string filename);
/** Returns the path of a local resource. Can read and write files to this location. */


+ 53
- 38
src/asset.cpp View File

@@ -27,7 +27,7 @@ static std::string globalDir;
static std::string localDir;


void assetInit(bool devMode) {
void assetInit(bool devMode, std::string customGlobalDir, std::string customLocalDir) {
if (devMode) {
// Use current working directory if running in development mode
globalDir = ".";
@@ -35,50 +35,65 @@ void assetInit(bool devMode) {
return;
}

if (customGlobalDir.empty()) {

#if ARCH_MAC
CFBundleRef bundle = CFBundleGetMainBundle();
assert(bundle);
CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
char resourcesBuf[PATH_MAX];
Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
assert(success);
CFRelease(resourcesUrl);
globalDir = resourcesBuf;

// Get home directory
struct passwd *pw = getpwuid(getuid());
assert(pw);
localDir = pw->pw_dir;
localDir += "/Documents/Rack";
CFBundleRef bundle = CFBundleGetMainBundle();
assert(bundle);
CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle);
char resourcesBuf[PATH_MAX];
Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf));
assert(success);
CFRelease(resourcesUrl);
globalDir = resourcesBuf;
#endif
#if ARCH_WIN
char moduleBuf[MAX_PATH];
DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
assert(length > 0);
PathRemoveFileSpec(moduleBuf);
globalDir = moduleBuf;

// Get "My Documents" folder
char documentsBuf[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
assert(result == S_OK);
localDir = documentsBuf;
localDir += "/Rack";
char moduleBuf[MAX_PATH];
DWORD length = GetModuleFileName(NULL, moduleBuf, sizeof(moduleBuf));
assert(length > 0);
PathRemoveFileSpec(moduleBuf);
globalDir = moduleBuf;
#endif
#if ARCH_LIN
// TODO For now, users should launch Rack from their terminal in the global directory
globalDir = ".";

// Get home directory
const char *homeBuf = getenv("HOME");
if (!homeBuf) {
struct passwd *pw = getpwuid(getuid());
assert(pw);
homeBuf = pw->pw_dir;
// TODO For now, users should launch Rack from their terminal in the global directory
globalDir = ".";
#endif
}
else {
globalDir = customGlobalDir;
}
localDir = homeBuf;
localDir += "/.Rack";

if (customGlobalDir.empty()) {
#if ARCH_MAC
// Get home directory
struct passwd *pw = getpwuid(getuid());
assert(pw);
localDir = pw->pw_dir;
localDir += "/Documents/Rack";
#endif
#if ARCH_WIN
// Get "My Documents" folder
char documentsBuf[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, documentsBuf);
assert(result == S_OK);
localDir = documentsBuf;
localDir += "/Rack";
#endif
#if ARCH_LIN
// Get home directory
const char *homeBuf = getenv("HOME");
if (!homeBuf) {
struct passwd *pw = getpwuid(getuid());
assert(pw);
homeBuf = pw->pw_dir;
}
localDir = homeBuf;
localDir += "/.Rack";
#endif
}
else {
localDir = customLocalDir;
}
}




+ 14
- 2
src/main.cpp View File

@@ -24,11 +24,15 @@ using namespace rack;
int main(int argc, char* argv[]) {
bool devMode = false;
std::string patchFile;
std::string customLocalDir;
std::string customGlobalDir;

// Parse command line arguments
argagg::parser argparser {{
{ "help", {"-h", "--help"}, "shows this help message", 0},
{ "devmod", {"-d", "--devmod"}, "enable dev mode", 0},
{ "devmod", {"-d", "--devmod"}, "enables dev mode (supersedes local/global folders)", 0},
{ "global", {"-g", "--globaldir"}, "set golbalDir", 1},
{ "local", {"-l", "--localdir"}, "set localDir", 1},
}};

argagg::parser_results args;
@@ -50,6 +54,14 @@ int main(int argc, char* argv[]) {
devMode = true;
}

if (args["global"]) {
customGlobalDir = args["global"].as<std::string>();
}

if (args["local"]) {
customLocalDir = args["local"].as<std::string>();
}

// Filename as first positional argument
if (args.pos.size() > 0) {
patchFile = args.as<std::string>(0);
@@ -57,7 +69,7 @@ int main(int argc, char* argv[]) {

// Initialize environment
randomInit();
assetInit(devMode);
assetInit(devMode, customGlobalDir, customLocalDir);
loggerInit(devMode);

// Log environment


Loading…
Cancel
Save