diff --git a/README.md b/README.md
index bb9d034..b3443e2 100644
--- a/README.md
+++ b/README.md
@@ -69,12 +69,14 @@ All keywords are in the format `|!keyword!|`. The supported keywords are:
Keyword | Replace Value | Supported Actions
--- | --- | ---
copyright | copyright text
see below for details on how to customize the text | project
file
-project_name | the new project's name | project
+project_name | the new project's name | project
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.
diff --git a/classes/Application.py b/classes/Application.py
index 261c7ac..975ff4b 100644
--- a/classes/Application.py
+++ b/classes/Application.py
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
-# Python Project Manager v1.0.0 #
+# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
@@ -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
@@ -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"]) :
@@ -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)
diff --git a/classes/CLI.py b/classes/CLI.py
index b4bac6a..1b991ad 100644
--- a/classes/CLI.py
+++ b/classes/CLI.py
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
-# Python Project Manager v1.0.0 #
+# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
diff --git a/classes/__init__.py b/classes/__init__.py
index be80fc6..e54e597 100644
--- a/classes/__init__.py
+++ b/classes/__init__.py
@@ -1,6 +1,6 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
-# Python Project Manager v1.0.0 #
+# Python Project Manager v1.1.0 #
# #
# Copyright 2016, PedroHenriques #
# http://www.pedrojhenriques.com #
diff --git a/data/keywords.json b/data/keywords.json
index 7ad0e9b..2b67738 100644
--- a/data/keywords.json
+++ b/data/keywords.json
@@ -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" : "*"},
diff --git a/data/project.json b/data/project.json
index 1fcde36..9cf0b76 100644
--- a/data/project.json
+++ b/data/project.json
@@ -23,6 +23,7 @@
"DB.php" : "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" : ""
},
+ "interfaces" : {},
"languages" : {},
"templates" : {
"init.php" : "",
+ "init.php" : "",
"header.php" : "\n\n\n\n\n\t