Joined: 01 Aug 2007 |
Posts: 14 |
Location: Elmshorn, Germany |
|
|
Posted: Mon Feb 23, 2009 6:16 am |
|
|
|
|
|
Hello,
i want to add a script that helps me a lot. I often create projects with the same structure (e.g. joomla-extensions). This script creates a new directory structure with file read from a template-directory-structure. Create a template directory structure you want to use. You can use placeholders (see the code) in your filenames, directory names and file content. They will be replaced by the script.
Just create a new folder in your explorer or project list, right-click and select your created menu-item. The script will create a whole new structure below this folder.
To make this work, create a new menu-item in Tools/Settings - Integration - Add Menu. Set "Execute with" to "Shell" and "Command line" to something like this: "@php5@" -n -f c:\entwicklung\createj15cmp.php -- "@Name@" "@FName@" |
where "c:\entwicklung\createj15cmp.php" should be of course the path to your script.
I have added two screenshots to show my settings:
Here is the code for the script. Please read the comments to see which settings you can / should edit:
<?php
/**
* @package phpEd Scripts
* @subpackage Create project structure from a template
* @version 1.0
* @author Thomas Kahl
* @copyright Copyright (C) 2005 - 2008 Backauf Consulting GmbH / Thomas Kahl. All rights reserved.
* @license GNU/GPL
* @todo ...
*/
define('DS','\\'); // For Windows
// Handle the parameters from phpEd
// Todo: why is there sometimes a " ?
$argv[1]=str_replace('"','',$argv[1]); // Directory Name (will be used as projectname)
$argv[2]=str_replace('"','',$argv[2]); // Full path to the directory
if (isset($argv[3])) {
$argv[3]=DS.str_replace('"','',$argv[3]); // directory-suffix for the template-path
} else {
$argv[3]='';
}
// Some basic settings
define('FILE_MODE','wb'); // For Windows
define('TEMPLATE_DIR', 'c:\entwicklung\j15\cmp_template'.$argv[3]); // Change to whatever your template-dir is
define('CMP_PATH',$argv[2]); // Automatic set to the directory-path of the new project
define('VERBOSE',0); // Turn status messages on (1) or off (0)
define('WRITESUMMARY',1); // Turn summary-file on (1) or off (0)
// Define a list of variables you want to use in your project-template (content and filenames)
// The array-key is the variable-name, the array-value the value that will replace the key
// Make sure that you use unique keys which you only use as placeholders in your template!
// Of course you could include a local file with the array-definition to use individual settings in a team
$template_vars=array();
$template_vars['_projectname_']=$argv[1];
$template_vars['_author_']='Thomas Kahl';
$template_vars['_created_']=date("F j, Y, g:i a");
$template_vars['_license_']='GNU/GPL';
$template_vars['_copyright_']='Thomas Kahl / Backauf Consulting GmbH';
$template_vars['_version_']='1.0';
$template_vars['_url_']='http://www.backauf-consulting.de';
$template_vars['filenameplaceholder']='_filename_'; // Don't change this key, replace the value with your placeholder for the filename
// Create array for the summary
$summary=array();
$summary['text']='';
$summary['dircnt']=0;
$summary['filecnt']=0;
$summary['summary_file']=$argv[1].'_projectinfo.txt';
// Normally there is no need to change the code from here...
// ----------------------------------------------------------------------------------------------------
// Check if we have a template-directory
if(!is_dir(TEMPLATE_DIR)) {
echo "Templatedir not found: ".TEMPLATE_DIR;
sleep(10);
exit();
}
// Send a message that we are on the way...
echo "Start to create Project ".$argv[1]." in ".CMP_PATH."\n";
// Here is the work called:
$summary=find_files(TEMPLATE_DIR, 'create_file', $summary);
// Shall we write a summary?
if(WRITESUMMARY) {
$mkname=makePath(CMP_PATH.DS).$summary['summary_file'];
if (!$handle = fopen($mkname, FILE_MODE)) {
echo "Could not open file $mkname\n";
} else {
// Header and content for the summary
$summary_content="PROJEKT: ".strtoupper($argv[1])."\n";
$summary_content.="---------------------------------------------------------------------------------------------------------\n";
$summary_content.="Projectdirectoy: ".$argv[2]."\n";
$summary_content.="Created: ".date("F j, Y, g:i a")."\n";
$summary_content.="Created by: ".$template_vars['_author_']."\n";
$summary_content.="---------------------------------------------------------------------------------------------------------\n";
$summary_content.="From Template: ".TEMPLATE_DIR."\n";
$summary_content.="\nDirectories created: ".$summary['dircnt']."\n";
$summary_content.="Files created: ".$summary['filecnt']."\n";
$summary_content.="---------------------------------------------------------------------------------------------------------\n";
$summary_content.=$summary['text'];
$summary_content.="---------------------------------------------------------------------------------------------------------\n";
// Try to write it
if (!fwrite($handle, $summary_content)) {
echo "Could not write file $mkname\n";
}
fclose($handle);
}
}
// Send a message that we are ready.
echo "Project created...\n";
//Uncomment if you don't use redirect to log:
// sleep(10);
exit();
function create_file($filename, &$summary) {
global $template_vars;
// Read the content of the template-file
$tmp_file=file_get_contents($filename);
// Create the full path to the output-file
$mkname=str_replace(makePath(TEMPLATE_DIR),makePath(CMP_PATH),$filename);
// Replace all placeholders in the template-file and filename(!)
foreach($template_vars as $key => $value) {
$mkname=str_replace($key,$value,$mkname);
$tmp_file=str_replace($key,$value,$tmp_file);
}
// Use the new filename to replace a placeholder
$tmp_file=str_replace($template_vars['filenameplaceholder'],basename($mkname),$tmp_file);
// Try to write the new file
if (!$handle = fopen($mkname, FILE_MODE)) {
$summary=makelog("Could not open file $mkname\n",$summary,3);
return;
}
if (!fwrite($handle, $tmp_file)) {
$summary=makeLog("Could not write file $mkname",$summary,3);
return;
}
$summary=makeLog("File $mkname created",$summary,2);
fclose($handle);
return $summary;
}
function find_files($path, $callback, $summary) {
global $template_vars;
// Make a correct path
$path = makePath($path);
// Read the directory
$entries = Array();
$dir = dir($path);
while (false !== ($entry = $dir->read())) {
$entries[] = $entry;
}
$dir->close();
// Step through directory content
foreach ($entries as $entry) {
// The full path of the entry
$fullname = $path . $entry;
// Check if this is a valid directory
if ($entry != '.' && $entry != '..' && is_dir($fullname)) {
// Create an outputpath from the entry
$mkname=str_replace(makePath(TEMPLATE_DIR),makePath(CMP_PATH),$fullname);
// Replace placeholders in the path
foreach($template_vars as $key => $value) {
$mkname=str_replace($key,$value,$mkname);
}
// Try to create the new directory
if (!is_dir($mkname)) {
if(!mkdir($mkname)) {
$summary=makeLog("Could not create directory $mkname",$summary,3);
} else {
$summary=makeLog("Directory $mkname created",$summary,1);
}
} else {
$summary=makeLog("Directory $mkname already exists",$summary,3);
}
// Because we have found a new directory, the function calls itself to check the subdirs
$summary=find_files($fullname , $callback, $summary);
// ... or do we have a valid file?
} else if (is_file($fullname) && $entry != '.' && $entry != '..') {
// then handle the new file
$summary=call_user_func($callback, $fullname, $summary);
}
}
return $summary;
}
function makePath($path) {
return rtrim(str_replace("\\", "/", $path), '/') . '/';
}
function makeLog($output, $summary, $type=0) {
// If verbose is on or type is ERROR (3)
if (VERBOSE || $type==3) echo $output."\n";
// Fill the summary-array
if(WRITESUMMARY) {
$prefix=($type==3) ? '-!- ' : '--- ';
$summary['text']=$summary['text'].$prefix.$output.".\n";
if($type==1) $summary['dircnt']=$summary['dircnt']+1;
if($type==2) $summary['filecnt']=$summary['filecnt']+1;
}
return $summary;
}
?>
|
Do you have better ideas / improvements / corrections? Please post!
Thomas
|