/
appCreate
148 lines (132 loc) · 5.57 KB
/
appCreate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
if(array_key_exists(1, $argv)) {
$command = $argv[1];
if($command == 'help') {
echo "The following commands are available:\n\n".
"\ncreate:application AuthorName.PluginName ApplicationName\n".
" Attempt to create an application in the given\n".
" authorname/pluginname directory\n".
"\nremove:application AuthorName.PluginName ApplicationName\n".
" Attempt to remove an application in the given\n".
" authorname/pluginname directory\n".
"\nget:plugindir\n".
" Check if this script is able to correctly\n".
" determine your plugin directory\n".
"\nget:plugindir\n".
" Get the directory where this script will\n".
" try to make an applications directory in\n";
}
if($command == 'create:application') {
if(array_key_exists(2, $argv) && array_key_exists(3, $argv)) {
createApplication($argv[2],$argv[3]);
}
else {
echo "\n\ncreate:application expects two parameters. AuthorName.Pluginname as the first\n".
"parameter, and the name of the application as the second\n".
"A full command would look like:\n\n".
"php appCreate create:application Foo.Bar BazApplication\n\n";
}
}
if($command == 'get:plugindir') {
echo getPluginDir();
}
if($command == 'get:namespacedir') {
echo dirFromNameSpace($argv[2]);
}
if($command == 'remove:application') {
if(array_key_exists(2,$argv) && array_key_exists(3,$argv)) {
removeApplication($argv[2],$argv[3]);
}
else {
echo "\n\nremove:application expects two parameters. AuthorName.Pluginname as the first\n".
"parameter, and the name of the application as the second\n".
"A full command would look like:\n\n".
"php appCreate remove:application Foo.Bar BazApplication\n\n";
}
}
}
function findNestedPluginDir() {
$it = new RecursiveDirectoryIterator(__DIR__);
foreach(new RecursiveIteratorIterator($it) as $file)
{
if(is_dir($file)) {
$dirname = dirname($file);
if(strpos(strrev($dirname),strrev('plugins')) === 0) {
return $dirname;
}
}
}
throw new Exception("plugins dir not found!");
}
function getPluginDir() {
if(strpos(__DIR__,'plugins') !== false) {
return substr(__DIR__, 0, strpos(__DIR__,'plugins')+strlen('plugins'));
}
else {
return findNestedPluginDir();
}
}
function dirFromNameSpace($namespace) {
$split = explode('.',strtolower($namespace));
$author = $split[0];
$plugin = $split[1];
$plugindir = getPluginDir();
$basedir = implode(DIRECTORY_SEPARATOR,[$plugindir,$author,$plugin]);
return $basedir;
}
function removeApplication($appNamespace,$appname) {
$destinationdir = __DIR__.DIRECTORY_SEPARATOR.'applications'.DIRECTORY_SEPARATOR.strtolower($appname);
$destinationdir = dirFromNameSpace($appNamespace).DIRECTORY_SEPARATOR.'applications'.DIRECTORY_SEPARATOR;
$lowercase = strtolower($appname);
if(is_dir($destinationdir)) {
rrmdir($destinationdir);
}
}
function createApplication($appNamespace, $appname) {
$basedir = __DIR__.DIRECTORY_SEPARATOR.'base'.DIRECTORY_SEPARATOR.'baseApplication'.DIRECTORY_SEPARATOR;
$destinationdir = dirFromNameSpace($appNamespace).DIRECTORY_SEPARATOR.'applications';
if(!is_dir($destinationdir)) {
mkdir($destinationdir);
}
$destinationdir .= DIRECTORY_SEPARATOR;
$lowercase = strtolower($appname);
if(!file_exists($destinationdir.$appname.'.php') && !is_dir($destinationdir.$lowercase)) {
$contents = file_get_contents($basedir.'BaseApplication.php');
$contents = str_replace('BaseApplication',$appname,$contents);
$contents = str_replace('NamespaceLocation',str_replace('.','\\',$appNamespace),$contents);
mkdir($destinationdir.$lowercase);
$destinationdir .= $lowercase.DIRECTORY_SEPARATOR;
file_put_contents($destinationdir.$appname.'.php', $contents);
mkdir($destinationdir.'assets');
mkdir($destinationdir.'assets'.DIRECTORY_SEPARATOR.'css');
mkdir($destinationdir.'assets'.DIRECTORY_SEPARATOR.'js');
mkdir($destinationdir.'view');
$location = 'assets'.DIRECTORY_SEPARATOR.'css'.DIRECTORY_SEPARATOR.'style.css';
$contents = file_get_contents($basedir.$location);
$contents = str_replace('BaseApplication',$appname,$contents);
file_put_contents($destinationdir.$location, $contents);
$location = 'assets'.DIRECTORY_SEPARATOR.'js'.DIRECTORY_SEPARATOR.'script.js';
$contents = file_get_contents($basedir.$location);
$contents = str_replace('BaseApplication',$appname,$contents);
file_put_contents($destinationdir.$location, $contents);
$location = 'view'.DIRECTORY_SEPARATOR.'app.html';
$contents = file_get_contents($basedir.$location);
$contents = str_replace('BaseApplication',$appname,$contents);
file_put_contents($destinationdir.$location, $contents);
}
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
?>