Skip to content

Commit

Permalink
v1.1.0 - added supported for the project_name keyword in the file act…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
PedroHenriques committed Oct 9, 2016
1 parent 27e6dba commit 659b38d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ All keywords are in the format `|!keyword!|`. The supported keywords are:
Keyword | Replace Value | Supported Actions
--- | --- | ---
copyright | copyright text<br>see below for details on how to customize the text | project<br>file
project_name | the new project's name | project
project_name | the new project's name | project<br>file (1)
project_type | the new project's type | project
no_www_domain | the new project's name, striped of any starting "www." | project
file_name | the new file's name | file
file_type | the new file's type | file

(1) When creating a new file, the code will search the file's path for the first directory with a `.git` folder inside it. That directory will be treated as the file's project_name.

All static keywords and their replacement strings are defined in the `keywords.json` file.
Any keywords added to this file will become usable in file's content.
This is also where the copyright text is defined and can be customized.
Expand Down
36 changes: 34 additions & 2 deletions classes/Application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Python Project Manager v1.0.0 #
# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
Expand Down Expand Up @@ -148,6 +148,9 @@ def executeFile(self) :
# if any directory can't be created an error message will be given later when the file fails to be created
os.makedirs(file_path, exist_ok = True)

# this string will be inserted into any file's content where |!project_name!| is present
# since the project name wasn't provided in the cmd find it based on the file's destination
self.keywords["project_name"] = self.findProjectName(file_path)
# this string will be inserted into any file's content where |!file_name!| is present
self.keywords["file_name"] = file_name
# this string will be inserted into any file's content where |!file_type!| is present
Expand Down Expand Up @@ -239,7 +242,14 @@ def buildCopyrightString(self, file_extension) :
return("")

# build the dictionary with relevant keywords and replacement strings
replacements = self.keywords["copyright"]["replaces"][file_extension].copy()
# start by add all the keywords
replacements = self.keywords.copy()

# remove the copyright entries
del replacements["copyright"]

# add the copyright replaces relevant for this file extension
replacements.update(self.keywords["copyright"]["replaces"][file_extension])

# add the general replaces, if any
if ("general" in self.keywords["copyright"]["replaces"]) :
Expand Down Expand Up @@ -388,3 +398,25 @@ def replaceKeyWords(self, replacements, string) :

# return the final string
return(string)

# searches all the directories in the path provided for the project folder
# the project folder is the 1st folder found with a ".git" directory
# if none can be found, then an empty string will be returned
def findProjectName(self, destination_path) :
result = ""

# loop through the path provided untill a ".git" folder is found or the drive folder is reached
while (re.search("^[a-zA-Z]:\\\\$", destination_path) == None) :
# check if this directory has a ".git folder"
if (".git" in os.listdir(destination_path)) :
# it does
result = re.search("^.+\\\\([^\\\\]+)\\\\?$", destination_path).group(1)
break

# move to the parent directory
destination_path = os.path.dirname(destination_path)

# cosmetic changes to the result
result = result.title()

return(result)
2 changes: 1 addition & 1 deletion classes/CLI.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Python Project Manager v1.0.0 #
# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
Expand Down
2 changes: 1 addition & 1 deletion classes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Python Project Manager v1.0.0 #
# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
Expand Down
2 changes: 1 addition & 1 deletion data/keywords.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"copyright" : {
"text" : "|!start!||!char{59}!|\n|!char!||!tab{15}!||!char!|\n|!char!| PROJECT NAME v1.0.0|!tab{10}!||!char!|\n|!char!||!tab{15}!||!char!|\n|!char!| Copyright 2016, PedroHenriques|!tab{7}!||!char!|\n|!char!| http://www.pedrojhenriques.com|!tab{7}!||!char!|\n|!char!| https://github.com/PedroHenriques|!tab{7}!||!char!|\n|!char!||!tab{15}!||!char!|\n|!char!| Free to use under the MIT license.|!tab{6}!||!char!|\n|!char!| http://www.opensource.org/licenses/mit-license.php|!tab{2}!||!char!|\n|!char!||!tab{15}!||!char!|\n|!char{59}!||!end!|",
"text" : "|!start!||!char{59}!|\n|!char!||!tab{15}!||!char!|\n|!char!| |!project_name!| v1.0.0|!tab{10}!||!char!|\n|!char!||!tab{15}!||!char!|\n|!char!| Copyright 2016, PedroHenriques|!tab{7}!||!char!|\n|!char!| http://www.pedrojhenriques.com|!tab{7}!||!char!|\n|!char!| https://github.com/PedroHenriques|!tab{7}!||!char!|\n|!char!||!tab{15}!||!char!|\n|!char!| Free to use under the MIT license.|!tab{6}!||!char!|\n|!char!| http://www.opensource.org/licenses/mit-license.php|!tab{2}!||!char!|\n|!char!||!tab{15}!||!char!|\n|!char{59}!||!end!|",
"replaces" : {
"general" : {"tab" : "\t"},
"php" : {"start" : "/*", "end" : "*/", "char" : "*"},
Expand Down
3 changes: 2 additions & 1 deletion data/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"DB.php" : "<?php\n|!copyright!|\n\nclass DB {\n\t// DB connection information\n\tpublic static $db_name = \"\";\n\tpublic static $db_user = \"\";\n\tpublic static $db_pw = \"\";\n\n\t// stores the only instance of the DB class\n\tprivate static $instance = null;\n\n\t// stores the PDO instance\n\tprivate $pdo_obj = null;\n\n\tprivate function __construct() {\n\t\t// create a connection to the DB and store it\n\t\ttry {\n\t\t\t$this->pdo_obj = new PDO(\"mysql:host=localhost;dbname=\".self::$db_name, self::$db_user, self::$db_pw);\n\t\t}catch (PDOException $e) {\n\t\t\t// couldn't connect to DB\n\t\t\treturn;\n\t\t}\n\t}\n\n\t// returns the DB connection\n\t// if one doesn't exist, then a connection will be created\n\tpublic static function getInstance() {\n\t\t// if a DB connection hasn't been created\n\t\tif (self::$instance == null) {\n\t\t\t// create a DB instance and store it\n\t\t\t$new_instance = new self;\n\n\t\t\t// if an instance was successfuly created, store it\n\t\t\tif ($new_instance->pdo_obj !== null) {\n\t\t\t\tself::$instance = $new_instance;\n\t\t\t}\n\t\t}\n\n\t\treturn(self::$instance);\n\t}\n\n\t// begins a transaction\n\t// returns True if successful or False otherwise\n\tpublic function beginTransaction() {\n\t\ttry {\n\t\t\treturn((bool)$this->pdo_obj->beginTransaction());\n\t\t}catch (PDOException $e) {\n\t\t\treturn(false);\n\t\t}\n\t}\n\n\t// commits a transaction\n\t// returns True if successful or False otherwise\n\tpublic function commit() {\n\t\ttry {\n\t\t\t// check if there is an open transaction\n\t\t\tif (!(bool)$this->pdo_obj->inTransaction()) {\n\t\t\t\t// there isn't an open transition\n\t\t\t\treturn(true);\n\t\t\t}\n\n\t\t\treturn((bool)$this->pdo_obj->commit());\n\t\t}catch (PDOException $e) {\n\t\t\treturn(false);\n\t\t}\n\t}\n\n\t// rollback a transaction\n\t// returns True if successful or False otherwise\n\tpublic function rollBack() {\n\t\ttry {\n\t\t\t// check if there is an open transaction\n\t\t\tif (!(bool)$this->pdo_obj->inTransaction()) {\n\t\t\t\t// there isn't an open transition\n\t\t\t\treturn(true);\n\t\t\t}\n\n\t\t\treturn((bool)$this->pdo_obj->rollBack());\n\t\t}catch (PDOException $e) {\n\t\t\treturn(false);\n\t\t}\n\t}\n\n\t// prepares and runs a SELECT query\n\t// returns all query's results, as an assoc array\n\tpublic function querySelect($query, array $input_params) {\n\t\ttry {\n\t\t\t// prepare the query\n\t\t\t$pdo_statement = $this->pdo_obj->prepare($query);\n\n\t\t\tif ($pdo_statement === false) {\n\t\t\t\t// the query couldn't be prepared\n\t\t\t\treturn([]);\n\t\t\t}\n\n\t\t\t// execute the query\n\t\t\tif (!$pdo_statement->execute($input_params)) {\n\t\t\t\t// the query couldn't be executed\n\t\t\t\treturn([]);\n\t\t\t}\n\n\t\t\t// check if a game was found\n\t\t\tif ($pdo_statement->rowCount() === 0) {\n\t\t\t\t// it wasn't\n\t\t\t\treturn([]);\n\t\t\t}\n\n\t\t\t// fetch the results\n\t\t\t$pdo_results = $pdo_statement->fetchAll(PDO::FETCH_ASSOC);\n\n\t\t\tif ($pdo_results === false) {\n\t\t\t\t// failed to fetch results\n\t\t\t\treturn([]);\n\t\t\t}\n\n\t\t\t// at this point everything should have gone OK\n\t\t\treturn($pdo_results);\n\t\t}catch (PDOException $e) {\n\t\t\treturn([]);\n\t\t}\n\t}\n\n\t// prepares and runs an INSERT query\n\t// returns the IDs of the inserted rows\n\tpublic function queryInsert($query, array $input_params) {\n\t\ttry {\n\t\t\t// prepare the query\n\t\t\t$pdo_statement = $this->pdo_obj->prepare($query);\n\n\t\t\tif ($pdo_statement === false) {\n\t\t\t\t// the query couldn't be prepared\n\t\t\t\treturn([]);\n\t\t\t}\n\n\t\t\t// stores the inserted IDs\n\t\t\t$inserted_ids = [];\n\n\t\t\t// loop through each $input_params element\n\t\t\tforeach ($input_params as $params) {\n\t\t\t\t// execute the query\n\t\t\t\tif (!$pdo_statement->execute($params)) {\n\t\t\t\t\t// the query couldn't be executed\n\t\t\t\t\t$inserted_ids[] = null;\n\t\t\t\t}\n\n\t\t\t\t// store the inserted ID\n\t\t\t\t$inserted_ids[] = $this->pdo_obj->lastInsertId();\n\t\t\t}\n\n\t\t\t// return the inserted IDs\n\t\t\treturn($inserted_ids);\n\t\t}catch (PDOException $e) {\n\t\t\treturn([]);\n\t\t}\n\t}\n\n\t// prepares and runs an UPDATE query\n\t// returns the number of updated rows\n\tpublic function queryUpdate($query, array $input_params) {\n\t\ttry {\n\t\t\t// prepare the query\n\t\t\t$pdo_statement = $this->pdo_obj->prepare($query);\n\n\t\t\tif ($pdo_statement === false) {\n\t\t\t\t// the query couldn't be prepared\n\t\t\t\treturn([]);\n\t\t\t}\n\n\t\t\t// stores the number of updated rows\n\t\t\t$num_updated_rows = 0;\n\n\t\t\t// loop through each $input_params element\n\t\t\tforeach ($input_params as $params) {\n\t\t\t\t// execute the query\n\t\t\t\tif ($pdo_statement->execute($params)) {\n\t\t\t\t\t// the query was executed\n\t\t\t\t\t$num_updated_rows++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// return the number of updated rows\n\t\t\treturn($num_updated_rows);\n\t\t}catch (PDOException $e) {\n\t\t\treturn([]);\n\t\t}\n\t}\n}\n\n?>",
"General.php" : "<?php\n|!copyright!|\n\nclass General {\n\t// defines the PUBLIC_ROOT constant, containing the path to the project's public directory\n\t// i.e., the browser's root directory\n\tstatic function definePublicRoot($public_file_abs_path) {\n\t\t// make sure $public_file_abs_path ends with a backslash\n\t\tif (substr($public_file_abs_path, -1) !== \"\\\\\") {\n\t\t\t$public_file_abs_path .= \"\\\\\";\n\t\t}\n\n\t\t// work on building the path to the domain's public folder\n\t\t$public_file_abs_path = str_replace(\"/\", \"\\\\\", $public_file_abs_path);\n\n\t\t// find the name of the domain's public folder\n\t\t$public_folder_valid_names = [\"public_html\", \"www\"];\n\t\t$public_folder_positions = [];\n\t\tforeach ($public_folder_valid_names as $value) {\n\t\t\t$pos = strpos($public_file_abs_path, \"\\\\{$value}\\\\\");\n\n\t\t\tif ($pos === false) {\n\t\t\t\t// this folder name doesn't exist in the path\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t$public_folder_positions[$value] = $pos;\n\t\t}\n\n\t\t// make sure at least 1 of them was found\n\t\tif (empty(array_count_values($public_folder_positions))) {\n\t\t\t// couldn't find the domain's public folder name\n\t\t\texit;\n\t\t}\n\n\t\t// find the folder that comes first in the path\n\t\t$public_folder_pos = min($public_folder_positions);\n\t\t$public_folder_name = array_search($public_folder_pos, $public_folder_positions);\n\n\t\t// find and store the path to the domain's public folder\n\t\tdefine(\"PUBLIC_ROOT\", substr($public_file_abs_path, 0, $public_folder_pos + strlen($public_folder_name) + 1));\n\t}\n\n\t// returns the provided path in the form to be used by the client's machine browser\n\tstatic function getClientPath($abs_path) {\n\t\t// make sure the path is using backslashs as the folder divisor\n\t\t$abs_path = str_replace(\"/\", \"\\\\\", $abs_path);\n\n\t\treturn(str_replace(\"\\\\\", \"/\", str_replace(PUBLIC_ROOT, \"\", $abs_path)));\n\t}\n}\n\n?>"
},
"interfaces" : {},
"languages" : {},
"templates" : {
"init.php" : "<?php\n|!copyright!|\n\n// TODO: uncomment\n// error_reporting(0);"
Expand All @@ -39,7 +40,7 @@
},
"aux_pages" : {},
"templates" : {
"init.php" : "<?php\n|!copyright!|\n\n// define a constant with the path to the project's root directory\ndefine(\"PROJECT_ROOT\", dirname(dirname(dirname(__DIR__))));\n\n// autoload classes\nspl_autoload_register(function ($class_name) {\n\trequire_once(PROJECT_ROOT.\"\\\\release\\\\code\\\\classes\\\\{$class_name}.php\");\n});\n\n// define a constant with the path to the public directory\nGeneral::definePublicRoot(dirname(__FILE__));\n\nrequire_once(PROJECT_ROOT.\"\\\\release\\\\code\\\\templates\\\\init.php\");\n\n?>",
"init.php" : "<?php\n|!copyright!|\n\n// define a constant with the path to the project's root directory\ndefine(\"PROJECT_ROOT\", dirname(dirname(dirname(__DIR__))));\n\n// autoload classes\nspl_autoload_register(function ($class_name) {\n\t// build the partial file path\n\t$path = PROJECT_ROOT.\"\\\\release\\\\code\\\\\";\n\n\t// try loading as a class\n\t$folder = \"classes\";\n\tif (file_exists($path.\"{$folder}\\\\{$class_name}.php\")) {\n\t\trequire_once($path.\"{$folder}\\\\{$class_name}.php\");\n\t}else{\n\t\t// try loading as an interface\n\t\t$folder = \"interfaces\";\n\t\trequire_once($path.\"{$folder}\\\\{$class_name}.php\");\n\t}\n});\n\n// define a constant with the path to the public directory\nGeneral::definePublicRoot(dirname(__FILE__));\n\nrequire_once(PROJECT_ROOT.\"\\\\release\\\\code\\\\templates\\\\init.php\");\n\n?>",
"header.php" : "<?php\nsession_start();\n\n|!copyright!|\n\nrequire_once(dirname(__FILE__).\"\\\\init.php\");\n\n?>\n\n<!DOCTYPE html>\n<html>\n\n\t<head>\n\t\t<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n\t\t<meta name='description' content='' />\n\t\t<meta name='keywords' content='' />\n\t\t<meta name='author' content='http://www.pedrojhenriques.com' />\n\t\t<meta name='copyright' content='' />\n\n\t\t<title></title>\n\n\t\t<link rel='icon' type='image/png' sizes='16x16' href='<?php echo(General::getClientPath(PUBLIC_ROOT.\"\\\\assets\\\\images\\\\favicon_16x16.png\")); ?>'/>\n\t\t<link rel='icon' type='image/png' sizes='32x32' href='<?php echo(General::getClientPath(PUBLIC_ROOT.\"\\\\assets\\\\images\\\\favicon_32x32.png\")); ?>'/>\n\t\t<link rel='icon' type='image/png' sizes='96x96' href='<?php echo(General::getClientPath(PUBLIC_ROOT.\"\\\\assets\\\\images\\\\favicon_96x96.png\")); ?>'/>\n\n\t\t<link rel='stylesheet' type='text/css' href='<?php echo(General::getClientPath(PUBLIC_ROOT.\"\\\\assets\\\\js\\\\joined.min.css\")); ?>'/>\n\t</head>\n\n\t<body>",
"footer.php" : "<?php\n|!copyright!|\n\n?>\n\t\t<script src='<?php echo(General::getClientPath(PUBLIC_ROOT.\"\\\\assets\\\\js\\\\joined.min.js\")); ?>'></script>\n\t\t<script>init();</script>\n\t</body>\n\n</html>"
},
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Python Project Manager v1.0.0 #
# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
Expand Down

0 comments on commit 659b38d

Please sign in to comment.