-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew
executable file
·557 lines (444 loc) · 11.5 KB
/
new
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/env ruby
# frozen_string_literal: true
def sh(cmd)
puts "$ #{cmd}"
system cmd
end
def write(file, content)
puts ">>write to file: #{file}:\n#{content}"
puts ''
File.write file, content
end
def write_append(file, content)
open(file, 'a') { |f|
f.puts content
}
end
def new_phoenix_project
project_name = ARGV[1] || 'test_phoenix'
sh "mix phoenix.new #{project_name} --no-brunch --no-ecto"
# sh "mix phoenix.new #{project_name} --no-brunch --no-ecto --no-html"
end
$base_fabfile_content = <<~eos
# -*- coding: utf-8 -*
from fabric.api import *
def usage():
"""Usage"""
print('usage')
eos
$rakefile_content = <<~eos
task default: :usage
task :usage do
sh 'rake -T'
end
namespace :run do
desc 'run dev mode'
task :dev do
puts "run dev..."
end
end
eos
def new_rakefile
content = $rakefile_content
write 'Rakefile', content
end
def new_procfile
content = <<~eos
# sample
web: python serve.py
redis: redis-server
eos
puts 'create Procfile...'
File.write 'Procfile', content
end
def new_dockercompose
content = <<~eos
version: '2'
services:
seagull:
restart: always
image: tobegit3hub/seagull
ports:
- "10086:10086"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
eos
puts 'create docker-compose.yml'
File.write 'docker-compose.yml', content
end
def new_nginx_conf
content = <<~eos
worker_processes 4;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
proxy_pass http://localhost:3000;
#content_by_lua '
# ngx.say("<p>hello, world</p>")
#';
}
}
}
eos
puts 'create nginx config file...'
write 'nginx.conf', content
end
def new_openresty_project
project_name = ARGV[1] || 'test_openresty'
sh("mkdir -p #{project_name}/logs")
sh("mkdir -p #{project_name}/conf")
content = <<~eos
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 3000;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
}
eos
File.write "#{project_name}/conf/nginx.conf", content
content = <<~eos
def start():
"""nginx start"""
local('[ -d "./logs" ] || mkdir logs')
#local('nginx -p `pwd`/ -c conf/nginx.conf -g "daemon off;"')
local('nginx -p `pwd`/ -c conf/nginx.conf -g "daemon off;"')
def reload():
"""nginx reload"""
local('nginx -p `pwd`/ -s reload')
def stop():
"""nginx stop"""
with warn_only():
local('nginx -p `pwd`/ -s stop')
def restart():
"""restart"""
stop()
import time
time.sleep(0.5)
start()
def tip():
"""tip"""
print 'sudo bash -c "`which nginx` -p `pwd`/ -c conf/nginx.conf"'
eos
content = "#{$base_fabfile_content}#{content}"
File.write "#{project_name}/fabfile.py", content
content = <<~eos
*.pyc
logs/
tmp/
target/
.idea/
eos
File.write "#{project_name}/.gitignore", content
puts "cd #{project_name}"
end
def _parse(type)
_, tmp, *tail = ARGV
if !tmp || tmp.start_with?('-')
project_name = "test_#{type}"
args = tail.insert(0, tmp)
else
project_name = tmp || "test_#{type}"
args = tail
end
[project_name, args]
end
def new_kotlin_project
project_name, args = _parse('kotlin')
sh "ktnew #{project_name} #{args.join(' ')}"
end
def new_java_project
project_name, args = _parse('java')
sh "javanew #{project_name} #{args.join(' ')}"
end
def new_elixir_project
project_name, args = _parse('elixir')
if args.include?('--bin')
args.delete('--bin')
sh "mix new #{project_name} #{args.join(' ')}"
sh("cd #{project_name};new fabfile")
sh("echo /#{project_name} >> #{project_name}/.gitignore")
content = <<~eos
defmodule MyApp.CLI do
def main(_args) do
IO.puts("Hello from MyApp!")
end
end
eos
write "#{project_name}/lib/main.ex", content
module_name = File.read("#{project_name}/mix.exs").match(/defmodule (.+)\..+ do/)[1]
content = <<~eos
defmodule #{module_name}.Mixfile do
use Mix.Project
def project do
[app: :#{project_name},
version: "0.1.0",
elixir: "~> 1.7",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps(),
escript: escript()]
end
def application do
[applications: [:logger]]
end
defp deps do
[{:credo, "~> 1.0.0-rc1", only: [:dev, :test], runtime: false}]
end
def escript do
[main_module: MyApp.CLI]
end
end
eos
write "#{project_name}/mix.exs", content
content = <<~eos
#{$base_fabfile_content}
def dist():
"""dist"""
local('mix escript.build')
def run():
"""run"""
dist()
local('./#{project_name}')
def repl():
"""repl"""
local('iex -S mix')
eos
write "#{project_name}/fabfile.py", content
else
sh "mix new #{project_name} #{args.join(' ')}"
content = <<~eos
#{$base_fabfile_content}
def repl():
"""repl"""
local('iex -S mix')
eos
write "#{project_name}/fabfile.py", content
end
end
def new_rust_project
sh "new-rust #{ARGV[1..-1].join(' ')}"
end
def _new_g8_project(project, template_name)
project_name, _args = _parse(project)
sh "g8 #{template_name} --name=#{project_name}"
puts "cd #{project_name} && sbt tasks"
end
def new_scala3_project
_new_g8_project('scala3', 'scala/scala3.g8')
end
def new_scala2_project
_new_g8_project('scala2', 'scala/scala-seed.g8')
end
def new_scala_native_project
_new_g8_project('scala_native', 'scala-native/scala-native.g8')
end
def new_zio_project
_new_g8_project('zio', 'ScalaConsultants/zio-dotty-quickstart.g8')
end
def new_invokefile
content = <<~eos
from invoke import task
@task
def run(c):
'run'
c.run('echo hello')
eos
write('tasks.py', content)
end
def new_scalafmt_conf
content = <<~eos
version = "2.7.5"
eos
write('.scalafmt.conf', content)
end
def new_springboot
project_name, _args = _parse('springboot')
system("spring init --build=gradle -l kotlin #{project_name}")
system("cd #{project_name}; gradle wrapper --gradle-version=3.5")
end
private def new_from_github_template(project, new_project_name, user = 'itang')
sh "git clone [email protected]:#{user}/#{project}.git #{new_project_name}"
sh "cd #{new_project_name} && rm -rf .git"
puts "cd #{new_project_name}; inv"
end
def new_scalajs_nodejs_project
project_name, _args = _parse('scalajs-nodejs')
new_from_github_template('scalajs-nodejs-template', project_name)
end
def new_scalajs_deno_project
project_name, _args = _parse('scalajs-deno')
new_from_github_template('scalajs-deno-template', project_name)
end
def new_scalajs_project
new_scalajs_nodejs_project()
end
def new_scalajs_web_project
project_name, _args = _parse('scalajs-web')
new_from_github_template('scala-js-template', project_name)
end
def new_sbt_project
project_name, _args = _parse('sbt')
new_from_github_template('sbt-native-image-template', project_name)
end
def new_mill
project_name, _args = _parse('mill')
mill_version = '0.9.5'
begin
s = `mill --version`
mill_version = s.lines.first.split(/\s+/).last
puts "got mill version #{mill_version}"
rescue StandardError => e
puts e.message
end
puts "install example for #{mill_version}"
sh <<~EOS
if [ ! -f "$HOME/.mill/templates/example-3.zip" ]; then
mkdir -p $HOME/.mill/templates
curl -L https://github.com/lihaoyi/mill/releases/download/#{mill_version}/#{mill_version}-example-3.zip -o $HOME/.mill/templates/example-3.zip
fi
EOS
sh "rm -rf /tmp/example-3;unzip $HOME/.mill/templates/example-3.zip -d /tmp ; cp -r /tmp/example-3 #{project_name}"
rcontent = <<~eos
task default: :usage
task :usage do
sh 'rake -T'
end
namespace :gen do
desc 'gen idea project'
task :idea do
sh 'rake clean'
sh 'rake clean_idea'
sh 'mill mill.scalalib.GenIdea/idea'
end
desc 'gen IDE bsp project'
task :ide do
sh 'mill mill.bsp.BSP/install'
end
desc 'gen bloop'
task :bloop do
sh 'mill mill.contrib.Bloop/install'
end
end
desc 'run dev mode'
task :run do
sh 'tally mill foo.run'
end
desc 'dist'
task :dist do
sh 'mill foo.assembly'
sh 'cd out;$GRAALVM_HOME/bin/native-image --no-server -cp foo/assembly/dest/out.jar -H:Name=#{project_name} -H:+ReportUnsupportedElementsAtRuntime -H:+RemoveSaturatedTypeFlows foo.Example'
#sh 'cd out;upx #{project_name}'
end
desc 'clean'
task :clean do
sh 'rm -rf out/'
end
desc 'clean_idea'
task :clean_idea do
sh 'rm -rf .idea .idea_modules'
end
eos
write "#{project_name}/Rakefile", rcontent
bloopDepContent = 'import $ivy.`com.lihaoyi::mill-contrib-bloop:$MILL_VERSION`'
write_append("#{project_name}/build.sc", bloopDepContent)
#sh "cd #{project_name}; mill mill.contrib.Bloop/install"
puts "cd #{project_name};rake"
end
#############################################
tmp = ARGV[0]
case tmp
when 'phoenix'
new_phoenix_project
when 'rakefile'
new_rakefile
when 'procfile'
new_procfile
when 'invoke'
new_invokefile
when 'dockercompose'
new_dockercompose
when 'openresty'
new_openresty_project
when 'nginx_conf'
new_nginx_conf
when 'kotlin'
new_kotlin_project
when 'java'
new_java_project
when 'elixir'
new_elixir_project
when 'rust'
new_rust_project
when 'sbt'
new_sbt_project
when 'scala3'
new_scala3_project
when 'scala2'
new_scala2_project
when 'scala'
new_scala3_project
when 'scala-native'
new_scala_native_project
when 'zio'
new_zio_project
when 'scala-js'
new_scalajs_project
when 'scalajs'
new_scalajs_project
when 'scalajs-nodejs'
new_scalajs_nodejs_project
when 'scalajs-web'
new_scalajs_web_project
when 'scalajs-deno'
new_scalajs_deno_project
when 'scalafmt'
new_scalafmt_conf
when 'scalafmt.conf'
new_scalafmt_conf
when 'springboot'
new_springboot
when 'mill'
new_mill
else
puts 'Usage:
new kotlin project_name |
java project_name |
springboot project_name |
rust project_name |
sbt project_name |
scala3(scala) |
scala2 |
scala-native |
scalajs project_name |
scalajs-nodejs project_name |
scalajs-deno project_name |
scalajs-web project_name |
scalafmt.conf(alias scalafmt)|
zio project-name |
mill project_name |
rakefile |
invoke |
procfile |
dockercompose |
nginx_conf |
openresty project_name |
elixir project_name |
phoenix project_name |
'
end