[eluser]mtfd[/eluser]
I slapped together a quick script in Python to create files for my projects based on a simple template. Until recently, I was using Netbeans to do all my developing with CI so I had quite a few templates put together. When I switched to Sublime Text, I didn't really have a good way of duplicating that functionality.
I am not a python developer to please be gentle. I realize this code is pretty ugly and should be done properly but I just needed something quick.
The templates in the code are pretty straight forward. I omitted my controller template because it's about 175 lines.
I keep the file in htdocs root on my development machine so I can just pass the project (subfolder) name and run a cmd prompt from that folder.
Here's a screenshot in action:
http://gyazo.com/fd709c4aa8b57036306a06ec3a1bf5a0
Code:
import sys, os
def create_view(project, file_name):
template = "<?php \n\
\n\
/** \n\
* @package "+str(project)+" \n\
* @copyright (c) 2012 YOUR_COPYRIGHT_ETC \n\
* @author YOUR_NAME_ETC \n\
*/\n\
\n\
if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\
?>\n\
\n\
<!-- "+str(file_name)+" start -->\n\
\n\
<!-- "+str(file_name)+" end -->\n\
\n\
<?php\n\
\n\
/* End of file "+str(file_name)+" */\n\
/* Location: ./application/views/"+str(file_name)+" */\n\
"
FILE = open(project + "/application/views/" + file_name, "w")
FILE.writelines(template)
FILE.close()
def create_model(project, file_name):
file_name_exploded = file_name.split("/")
class_name, ext = os.path.splitext(file_name_exploded[-1])
template = "<?php\n\
\n\
/**\n\
* @package "+str(project)+" \n\
* @copyright (c) 2012 YOUR_COPYRIGHT_ETC \n\
* @author YOUR_NAME_ETC \n\
*/\n\
\n\
if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\
\n\
\n\
class "+str(class_name)+" extends CI_Model\n\
{\n\
\n\
function __construct()\n\
{\n\
parent::__construct();\n\
}\n\
\n\
}\n\
\n\
/* End of file "+str(file_name)+" */\n\
/* Location: ./application/models/"+str(file_name)+" */\n\
"
FILE = open(project + "/application/models/" + file_name, "w")
FILE.writelines(template)
FILE.close()
def create_controller(project, file_name):
print "Creating a controller!"
print ""
print "===================================="
print " CodeIgniter File Template Creator "
print "===================================="
print ""
if (len(sys.argv) >= 4):
project = sys.argv[1]
file_name = sys.argv[2]
file_type = sys.argv[3]
print "Trying to create file '" + str(file_name) + "' in project '" + str(project) + "' with type '" + file_type + "'...\n"
if file_type == "model":
create_model(project, file_name)
elif file_type == "view":
create_view(project, file_name)
elif file_type == "controller":
create_controller(project, file_name)
else:
print "ERROR: Unknown file type passed: '"+str(file_type)
print "Usage: controller|model|view"
exit()
else:
print "ERROR: Missing parameter(s)."
print "Usage: ci_new_file.py [project folder] [file name] [controller|model|view]"
print "Remember to append subfolder names to the beginning of the file name if applicable."
exit()
Enjoy.