-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresget.php
More file actions
317 lines (293 loc) · 10.4 KB
/
resget.php
File metadata and controls
317 lines (293 loc) · 10.4 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
require base_path()."/app/storage/vendor/jsmin.php";
class resget extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'resget';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get essential javascripts and stylesheets for you';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$info = "ResGet is small tool for downloading js and css files to your project\nThis tool has beed developed by Farbod Nosrat Nezami\nThis tool is under MIT lisence feel free to use it or change it\nContact me on fn.nezami.info@gmail.com\nThank you for using ResGet";
$version= ' version 1.0.1 Beta ';
$help = '';
/* Getting all command arguments */
$arguments = $this->argument();
/* Checking all recived arguments run proper functionality and insure that only one argument at a time works */
/* Insure this by returning after each detected argument */
foreach ($arguments as $argument) {
/* Minify given javascripts and css */
if($argument == 'minify')
{
$this->info("ResGet ".$version);
$addresses = $this->option('path');
/* adding essential folders to the public directory*/
$path = base_path().str_replace('=', '/', $this->option('approot'));
if(!file_exists($path.'/js'))
{
$this->info('Creating JavaScript folder at '.$path.'/js');
mkdir($path.'/js');
}
if(!file_exists($path.'/css'))
{
$this->info('Creating StyleSheet folder at '.$path.'/css');
mkdir($path.'/css');
}
if(!file_exists($path.'/img'))
{
$this->info('Creating Image folder at '.$path.'/img');
mkdir($path.'/img');
}
if(!file_exists($path.'/fonts'))
{
$this->info('Creating JavaScript folder at '.$path.'/fonts');
mkdir($path.'/fonts');
}
/* reading file contents and merge them */
$this->info('Creating temporary file for merging ....');
$tmp = fopen('merger.tmp', "w");
foreach ($addresses as $address) {
$this->info('Reading following file content >> '.str_replace('=', '', $address));
fwrite($tmp,file_get_contents(str_replace('=', '', $address)));
}
$this->info('Minifying your files to '.str_replace('=', '', $this->option('name')).'....');
// minify package and write it to the given folder name
if($this->option('type') == '=js')
{
file_put_contents($path.'/js'.'/'.str_replace('=', '', $this->option('name')).'-min.js',JSMin::minify(file_get_contents('merger.tmp')));
$this->info('Removing temporary file ....');
unlink('merger.tmp');
}
$this->info('All done successfully ....');
return;
}
if($argument == 'get')
{
$this->info("ResGet ".$version);
/**
*
* This part of command tool is using GitHub search API you can read more about GitHub seach API from here:
* URL : http://developer.github.com/v3/search
* Note : By the time I was developing this command tool GitHub search API was in priview mode. note the CURLOPT_HTTPHEADER value
* Note : By default GitHub allows 5 queries per minute for non authunticated requests you can increase the limit to 20 by using authuntication
**/
/* building a url from user input to query github
/* starting a curl session, setting CURLOPT_URL and _HTTPHEADER you may need to change the header after search api exits preview mode */
$this->info('starting query session ....');
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, "https://api.github.com/search/repositories?q=jquery+language:javascript&sort=stars&order=desc");
curl_setopt($curlSession, CURLOPT_HTTPHEADER ,array('Accept: application/vnd.github.preview.text-match+json'));
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
/* execute curl session, recive data, check for errors, if any error occured return, else pars recived string to array with json decode method */
$this->info('Start searching GitHub for ....');
$curlData = curl_exec($curlSession);
if(!curl_errno($curlSession))
{
$info = curl_getinfo($curlSession);
$this->info('Took ' . $info['total_time'] . ' seconds to send a request to GitHub');
}
else
{
$this->error('Curl error: ' . curl_error($curlSession));
return;
}
$this->info('Searching in the result for ....');
$curlData = mb_convert_encoding($curlData, 'utf-8');
$jsonData = (array)json_decode($curlData);
$items = (array)$jsonData["items"];
dd($items);
foreach ($items as $item) {
if($item->name == 'jquery')
{
}
}
curl_close($curlSession);
return;
}
if($argument == 'info')
{
$this->info($info);
return;
}
if($argument == 'version')
{
$this->info('You are currently using ResGet'.$version);
return;
}
else if($argument != 'resget')
{
$this->info("ResGet ".$version);
$resources = $this->option('resname');
$path = base_path().str_replace('=', '/', $this->option('approot'));
if(!file_exists($path.'/js'))
{
$this->info('Creating JavaScript folder at '.$path.'/js');
mkdir($path.'/js');
}
if(!file_exists($path.'/css'))
{
$this->info('Creating StyleSheet folder at '.$path.'/css');
mkdir($path.'/css');
}
if(!file_exists($path.'/img'))
{
$this->info('Creating Image folder at '.$path.'/img');
mkdir($path.'/img');
}
if(!file_exists($path.'/fonts'))
{
$this->info('Creating JavaScript folder at '.$path.'/fonts');
mkdir($path.'/fonts');
}
foreach ($resources as $resource )
{
if($resource == "=jquery" || $resource == "=$")
{
if(!file_exists($path.'/js'.'/jquery-min.js'))
{
$this->info('Downloading jquery, please wait .....');
file_put_contents($path.'/js'.'/jquery-min.js', fopen("http://code.jquery.com/jquery-1.10.2.min.js", 'r'));
}
else
{
$this->error("Resource already exist!");
}
}
if($resource == "=underscore" || $resource == "=_")
{
if(!file_exists($path.'/js'.'/underscore-min.js'))
{
$this->info('Downloading underscorejs, please wait .....');
file_put_contents($path.'/js'.'/underscore-min.js', fopen("http://underscorejs.org/underscore-min.js", 'r'));
}
else
{
$this->error("Resource already exist!");
}
}
if($resource == "=angular" || $resource == "=ang")
{
if(!file_exists($path.'/js'.'/jquery-min.js'))
{
$this->info('Downloading angularjs, please wait .....');
file_put_contents($path.'/js'.'/angular-min.js', fopen("https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js", 'r'));
}
else
{
$this->error("Resource already exist!");
}
}
if($resource == "=backbone" || $resource == "=bb")
{
if(!file_exists($path.'/js'.'/jquery-min.js'))
{
$this->info('Downloading jquery, please wait .....');
file_put_contents($path.'/js'.'/jquery-min.js', fopen("http://code.jquery.com/jquery-1.10.2.min.js", 'r'));
}
if(!file_exists($path.'/js'.'/underscore-min.js'))
{
$this->info('Downloading underscore, please wait .....');
file_put_contents($path.'/js'.'/underscore-min.js', fopen("http://underscorejs.org/underscore-min.js", 'r'));
}
if(!file_exists($path.'/js'.'/backbone-min.js'))
{
$this->info('Downloading backbone, please wait .....');
file_put_contents($path.'/js'.'/backbone-min.js', fopen("http://backbonejs.org/backbone-min.js", 'r'));
}
else
{
$this->error("Resource already exist!");
}
}
if($resource == "=bootstrap3" || $resource == "=bt3")
{
if(!file_exists($path.'/css'.'/bootstrap-min.css'))
{
$this->info('Downloading Twitter Bootstrap 3 StyleSheet, please wait .....');
file_put_contents($path.'/css'.'/bootstrap-min.css', fopen("http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css", 'r'));
}
if(!file_exists($path.'/js'.'/bootstrap-min.js'))
{
$this->info('Downloading Twitter Bootstrap 3 JavaScript, please wait .....');
file_put_contents($path.'/js'.'/bootstrap-min.js', fopen("http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js", 'r'));
}
else
{
$this->error("Resource already exist!");
}
}
if($resource == "=fontawesome" || $resource == "=fa")
{
if(!file_exists($path.'/css'.'/font-awesome-min.css'))
{
$this->info('Downloading Font Awesome StyleSheet, please wait .....');
file_put_contents($path.'/css'.'/font-awesome-min.css', fopen("http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css", 'r'));
}
else
{
$this->error("Resource already exist!");
}
}
$this->info('Task done!');
}
$this->info('All done successfully!');
return;
}
}
/**/
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
//array('get', InputArgument::OPTIONAL, 'To add a custom resource',null),
array('info', InputArgument::OPTIONAL, 'Show developer information',null),
array('help', InputArgument::OPTIONAL, 'Show help information',null),
array('version', InputArgument::OPTIONAL, 'Show current resgen version',null),
array('minify', InputArgument::OPTIONAL, 'Show current resgen version',null),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('resname', 'r', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'specify resource name', null),
array('approot', 'a', InputOption::VALUE_OPTIONAL , 'specify you applicaion public folder. default is /public', '=public'),
array('path', 'p', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY , 'specify your file addresses', null),
array('name', null, InputOption::VALUE_OPTIONAL , 'specify your minified output name', null),
array('type', 't', InputOption::VALUE_OPTIONAL , 'specify your file type', 'js'),
);
}
}