Skip to content
Snippets Groups Projects
Commit 080cfa50 authored by Demian Katz's avatar Demian Katz
Browse files

More robust and consistent directory creation.

parent b9128361
No related merge requests found
...@@ -37,6 +37,12 @@ if (!isset($argv[1]) || !in_array('--use-defaults', $argv)) { ...@@ -37,6 +37,12 @@ if (!isset($argv[1]) || !in_array('--use-defaults', $argv)) {
$overrideDir = getOverrideDir($overrideDir); $overrideDir = getOverrideDir($overrideDir);
$module = getModule(); $module = getModule();
$basePath = getBasePath($basePath); $basePath = getBasePath($basePath);
} else {
// In interactive mode, we initialize the directory as part of the input
// process; in defaults mode, we need to do it here:
if (!initializeOverrideDir($overrideDir)) {
die("Cannot initialize local override directory: {$overrideDir}\n");
}
} }
// Build the Windows start file in case we need it: // Build the Windows start file in case we need it:
...@@ -104,6 +110,27 @@ function getBasePath($basePath) ...@@ -104,6 +110,27 @@ function getBasePath($basePath)
} }
} }
/**
* Initialize the override directory and report success or failure.
*
* @param string $dir Path to attempt to initialize
*
* @return void
*/
function initializeOverrideDir($dir)
{
$dirStatus = buildDirs(
array(
$dir,
$dir . '/cache',
$dir . '/config',
$dir . '/harvest',
$dir . '/import'
)
);
return $dirStatus === true;
}
/** /**
* Get an override directory from the user (or return a default). * Get an override directory from the user (or return a default).
* *
...@@ -119,8 +146,8 @@ function getOverrideDir($overrideDir) ...@@ -119,8 +146,8 @@ function getOverrideDir($overrideDir)
"Where would you like to store your local settings? [{$overrideDir}] " "Where would you like to store your local settings? [{$overrideDir}] "
); );
if (!empty($overrideDirInput)) { if (!empty($overrideDirInput)) {
if (!is_dir($overrideDirInput) && !@mkdir($overrideDirInput)) { if (!initializeOverrideDir($overrideDirInput)) {
echo "Error: Cannot create directory '$overrideDirInput'.\n\n"; echo "Error: Cannot initialize settings in '$overrideDirInput'.\n\n";
} else { } else {
return str_replace('\\', '/', realpath($overrideDirInput)); return str_replace('\\', '/', realpath($overrideDirInput));
} }
...@@ -256,16 +283,28 @@ function buildImportConfig($baseDir, $overrideDir, $filename) ...@@ -256,16 +283,28 @@ function buildImportConfig($baseDir, $overrideDir, $filename)
"/^\s*solrmarc.path\s*=.*$/m", "/^\s*solrmarc.path\s*=.*$/m",
"solrmarc.path = {$overrideDir}/import|{$baseDir}/import", $import "solrmarc.path = {$overrideDir}/import|{$baseDir}/import", $import
); );
if (!is_dir($overrideDir . '/import')) {
if (!@mkdir($overrideDir . '/import')) {
die("Problem creating {$overrideDir}/import directory.\n\n");
}
}
if (!@file_put_contents($overrideDir . '/import/' . $filename, $import)) { if (!@file_put_contents($overrideDir . '/import/' . $filename, $import)) {
die("Problem writing {$overrideDir}/import/{$filename}.\n\n"); die("Problem writing {$overrideDir}/import/{$filename}.\n\n");
} }
} }
/**
* Build a set of directories.
*
* @param array $dirs Directories to build
*
* @return bool|string True on success, name of problem directory on failure
*/
function buildDirs($dirs)
{
foreach ($dirs as $dir) {
if (!is_dir($dir) && !@mkdir($dir)) {
return $dir;
}
}
return true;
}
/** /**
* Build the module for storing local code changes. * Build the module for storing local code changes.
* *
...@@ -278,16 +317,16 @@ function buildModule($baseDir, $module) ...@@ -278,16 +317,16 @@ function buildModule($baseDir, $module)
{ {
// Create directories: // Create directories:
$moduleDir = $baseDir . '/module/' . $module; $moduleDir = $baseDir . '/module/' . $module;
$dirsToCreate = array( $dirStatus = buildDirs(
$moduleDir, array(
$moduleDir . '/config', $moduleDir,
$moduleDir . '/src', $moduleDir . '/config',
$moduleDir . '/src/' . $module $moduleDir . '/src',
$moduleDir . '/src/' . $module
)
); );
foreach ($dirsToCreate as $dir) { if ($dirStatus !== true) {
if (!is_dir($dir) && !@mkdir($dir)) { die("Problem creating {$dirStatus}.\n");
die("Problem creating {$dir}.\n");
}
} }
// Copy configuration: // Copy configuration:
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment