12
12
* @since 3.0.0
13
13
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14
14
*/
15
- namespace Cake \Shell \ Task ;
15
+ namespace Cake \Shell ;
16
16
17
17
use Cake \Console \Shell ;
18
- use Cake \Core \App ;
19
18
use Cake \Core \Plugin ;
20
19
use Cake \Filesystem \Folder ;
21
20
use Cake \Utility \Inflector ;
22
21
23
22
/**
24
- * Task for symlinking / copying plugin assets to app's webroot.
23
+ * Shell for symlinking / copying plugin assets to app's webroot.
25
24
*
26
25
*/
27
- class AssetsTask extends Shell {
26
+ class PluginAssetsShell extends Shell {
28
27
29
28
/**
30
- * Execution method always used for tasks
29
+ * Attempt to symlink plugin assets to app's webroot. If symlinking fails it
30
+ * fallback to copying the assets. For vendor namespaced plugin, parent folder
31
+ * for vendor name are created if required.
31
32
*
32
33
* @return void
33
34
*/
34
- public function main () {
35
- $ this ->_process ();
35
+ public function symlink () {
36
+ $ this ->_process ($ this -> _list () );
36
37
}
37
38
38
39
/**
39
- * Process plugins
40
+ * Get list of plugins to process. Plugins without a webroot directory are skipped.
40
41
*
41
- * @return void
42
+ * @return array
42
43
*/
43
- protected function _process () {
44
- $ plugins = Plugin:: loaded () ;
45
- foreach ($ plugins as $ plugin ) {
44
+ protected function _list () {
45
+ $ plugins = [] ;
46
+ foreach (Plugin:: loaded () as $ plugin ) {
46
47
$ path = Plugin::path ($ plugin ) . 'webroot ' ;
47
48
if (!is_dir ($ path )) {
48
- $ this ->out ();
49
+ $ this ->out ('' , 1 , Shell:: VERBOSE );
49
50
$ this ->out (
50
51
sprintf ('Skipping plugin %s. It does not have webroot folder. ' , $ plugin ),
51
52
2 ,
@@ -54,32 +55,67 @@ protected function _process() {
54
55
continue ;
55
56
}
56
57
57
- $ this ->out ();
58
- $ this ->out ('For plugin: ' . $ plugin );
59
- $ this ->hr ();
60
-
61
58
$ link = Inflector::underscore ($ plugin );
62
59
$ dir = WWW_ROOT ;
63
-
60
+ $ namespaced = false ;
64
61
if (strpos ('/ ' , $ link ) !== false ) {
62
+ $ namespaced = true ;
65
63
$ parts = explode ('/ ' , $ link );
66
64
$ link = array_pop ($ parts );
67
65
$ dir = WWW_ROOT . implode (DS , $ parts ) . DS ;
68
- if (!is_dir ($ dir ) && !$ this ->_createDirectory ($ dir )) {
69
- continue ;
70
- }
71
66
}
72
67
73
- if (file_exists ($ dir . $ link )) {
74
- $ this ->out ($ link . ' already exists ' , 1 , Shell::VERBOSE );
68
+ $ plugins [$ plugin ] = [
69
+ 'srcPath ' => Plugin::path ($ plugin ) . 'webroot ' ,
70
+ 'destDir ' => $ dir ,
71
+ 'link ' => $ link ,
72
+ 'namespaced ' => $ namespaced
73
+ ];
74
+ }
75
+ return $ plugins ;
76
+ }
77
+
78
+ /**
79
+ * Process plugins
80
+ *
81
+ * @return void
82
+ */
83
+ protected function _process ($ plugins ) {
84
+ foreach ($ plugins as $ plugin => $ config ) {
85
+ $ path = Plugin::path ($ plugin ) . 'webroot ' ;
86
+
87
+ $ this ->out ();
88
+ $ this ->out ('For plugin: ' . $ plugin );
89
+ $ this ->hr ();
90
+
91
+ if ($ config ['namespaced ' ] &&
92
+ !is_dir ($ config ['destDir ' ]) &&
93
+ !$ this ->_createDirectory ($ config ['destDir ' ])
94
+ ) {
95
+ continue ;
96
+ }
97
+
98
+ if (file_exists ($ config ['destDir ' ] . $ config ['link ' ])) {
99
+ $ this ->out (
100
+ $ config ['destDir ' ] . $ config ['link ' ] . ' already exists ' ,
101
+ 1 ,
102
+ Shell::VERBOSE
103
+ );
75
104
continue ;
76
105
}
77
106
78
- if ($ this ->_createSymlink ($ path , $ dir . $ link )) {
107
+ $ result = $ this ->_createSymlink (
108
+ $ config ['srcPath ' ],
109
+ $ config ['destDir ' ] . $ config ['link ' ]
110
+ );
111
+ if ($ result ) {
79
112
continue ;
80
113
}
81
114
82
- $ this ->_copyDirectory ($ path , $ dir . $ link );
115
+ $ this ->_copyDirectory (
116
+ $ config ['srcPath ' ],
117
+ $ config ['destDir ' ] . $ config ['link ' ]
118
+ );
83
119
}
84
120
85
121
$ this ->out ();
@@ -146,4 +182,19 @@ protected function _copyDirectory($source, $destination) {
146
182
return false ;
147
183
}
148
184
185
+ /**
186
+ * Gets the option parser instance and configures it.
187
+ *
188
+ * @return \Cake\Console\ConsoleOptionParser
189
+ */
190
+ public function getOptionParser () {
191
+ $ parser = parent ::getOptionParser ();
192
+
193
+ $ parser ->addSubcommand ('symlink ' , [
194
+ 'help ' => 'Symlink / copy assets to app \'s webroot '
195
+ ]);
196
+
197
+ return $ parser ;
198
+ }
199
+
149
200
}
0 commit comments