Class: Dply::Cli::Setup

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dply/cli/setup.rb

Instance Method Summary collapse

Methods included from Logger

#debug?, #logger, stderr, #stderr

Constructor Details

#initialize(argv) ⇒ Setup

Returns a new instance of Setup.



12
13
14
15
# File 'lib/dply/cli/setup.rb', line 12

def initialize(argv)
  @argv = argv
  @options = {}
end

Instance Method Details

#lockObject



83
84
85
# File 'lib/dply/cli/setup.rb', line 83

def lock
  @lock ||= ::Dply::Lock.new
end

#optsObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dply/cli/setup.rb', line 87

def opts
  OptionParser.new do |opts|

    opts.banner = "Usage: drake setup [options] [namespace]"

    opts.on("-h", "--help", "Help") do
      puts opts
      exit
    end
  end
end

#proceed?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/dply/cli/setup.rb', line 31

def proceed?
  print "Are you sure?(y/n) "
  v = STDIN.gets.strip
  v == "y"
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dply/cli/setup.rb', line 17

def run
  opts.parse!(@argv)
  return if not proceed?
  lock.acquire
  setup_default
  namespace = @argv.shift
  if namespace
    setup_task(namespace)
  else
    setup_build_task
    setup_task(:production)
  end
end

#setup_build_taskObject



75
76
77
# File 'lib/dply/cli/setup.rb', line 75

def setup_build_task
  setup_task("build", template: "build.erb")
end

#setup_defaultObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dply/cli/setup.rb', line 37

def setup_default
  if not File.exist? "dply"
    FileUtils.mkdir_p "dply"
    logger.info "created dply/ dir"
  else
    logger.info "skipping dply/ dir"
  end

  rakefile = "dply/Rakefile"
  if not File.exist? rakefile
    FileUtils.touch rakefile
    logger.info "created #{rakefile}"
  else
    logger.info "skipping #{rakefile}"
  end
  
  pkgs_yml = "pkgs.yml"
  if not File.exist? pkgs_yml
    FileUtils.cp "#{templates_dir}/pkgs.erb", pkgs_yml
    logger.info "created #{pkgs_yml}"
  else
    logger.info "skipping #{pkgs_yml}"
  end
end

#setup_task(namespace, template: "deploy.erb") ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dply/cli/setup.rb', line 62

def setup_task(namespace, template: "deploy.erb")
  tasks_file = "dply/#{namespace}.rake"
  if File.exist? tasks_file
    logger.info "skipping #{tasks_file}"
    return
  end
  b = binding
  template_path = "#{templates_dir}/#{template}"
  erb = ERB.new(File.read(template_path), nil, '-')
  File.open(tasks_file, 'w') { |f| f.write erb.result(b) }
  logger.info "created #{tasks_file}"
end

#templates_dirObject



79
80
81
# File 'lib/dply/cli/setup.rb', line 79

def templates_dir
  @templates_dir ||= "#{__dir__}/../templates"
end